Skip to content

Commit

Permalink
Fix #2657 (Choose document search)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Sep 5, 2023
1 parent 68def6f commit 1db398d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ import net.bible.android.control.download.repo
import net.bible.android.control.event.ABEventBus
import net.bible.android.control.event.ToastEvent
import net.bible.android.database.DocumentSearch
import net.bible.android.database.DocumentSearchDao
import net.bible.android.view.activity.base.ListActionModeHelper.ActionModeActivity
import net.bible.android.view.activity.download.BadDocumentAction
import net.bible.android.view.activity.download.isBadDocument
import net.bible.android.view.activity.download.isRecommended
import net.bible.android.view.activity.navigation.ChooseDocument
import net.bible.android.view.activity.page.MainBibleActivity
import net.bible.service.common.CommonUtils
import net.bible.service.common.Ref
Expand Down Expand Up @@ -127,7 +129,8 @@ abstract class DocumentSelectionBase(
private lateinit var langArrayAdapter: ArrayAdapter<Language>

private var isPopulated = false
private val dao get() = DatabaseContainer.instance.temporaryDb.documentSearchDao()

abstract val dao: DocumentSearchDao

val pseudoBooks = Ref<List<PseudoBook>>()
val defaultDocuments = Ref<DocumentConfiguration>()
Expand Down Expand Up @@ -384,7 +387,7 @@ abstract class DocumentSelectionBase(
allDocuments.clear()
allDocuments.addAll(newDocs)
}
if(refresh) {
if(refresh || this@DocumentSelectionBase is ChooseDocument) {

This comment has been minimized.

Copy link
@tuomas2

tuomas2 Sep 5, 2023

Author Contributor

Could have more clever way of keeping this up to date, but this is fast operation if there's not much to do. But making ticket: #2813

dao.clear()
dao.insertDocuments(allDocuments.map {
DocumentSearch(it.osisID, it.abbreviation, if (it.isPseudoBook) "" else it.name, it.language.name, it.getProperty(DownloadManager.REPOSITORY_KEY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.serialization.serializer
import net.bible.android.control.document.canDelete
import net.bible.android.control.event.ABEventBus
import net.bible.android.database.DocumentSearchDao
import net.bible.android.database.SwordDocumentInfo
import net.bible.android.view.activity.base.Dialogs
import net.bible.android.view.activity.base.installedDocument
Expand Down Expand Up @@ -355,6 +356,8 @@ open class DownloadActivity : DocumentSelectionBase(
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
}

override val dao: DocumentSearchDao get() = DatabaseContainer.instance.downloadDocumentsDb.documentSearchDao()

override suspend fun getDocumentsFromSource(refresh: Boolean): List<Book> {
downloadManager.refreshInstallManager()
val docs = downloadControl.getDownloadableDocuments(repoFactory, refresh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import kotlinx.coroutines.launch
import net.bible.android.activity.R
import net.bible.android.control.backup.BackupControl
import net.bible.android.control.document.canDelete
import net.bible.android.database.DocumentSearchDao
import net.bible.android.view.activity.base.Dialogs
import net.bible.android.view.activity.base.DocumentSelectionBase
import net.bible.android.view.activity.base.IntentHelper
import net.bible.android.view.activity.base.installedDocument
import net.bible.android.view.activity.download.DownloadActivity
import net.bible.android.view.activity.navigation.genbookmap.ChooseGeneralBookKey
import net.bible.service.common.CommonUtils
import net.bible.service.db.DatabaseContainer
import net.bible.service.download.FakeBookFactory
import net.bible.service.sword.SwordDocumentFacade
import org.crosswire.common.util.Language
Expand Down Expand Up @@ -73,6 +75,8 @@ class ChooseDocument : DocumentSelectionBase(R.menu.choose_document_menu, R.menu
selectedLanguageNo = -1
}

override val dao: DocumentSearchDao get() = DatabaseContainer.instance.chooseDocumentsDb.documentSearchDao()

/** load list of docs to display
*
*/
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/net/bible/service/db/DatabaseContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,19 @@ class DatabaseContainer {
}
}

val temporaryDb: TemporaryDatabase =
val downloadDocumentsDb: TemporaryDatabase =
Room.databaseBuilder(
application, TemporaryDatabase::class.java, TemporaryDatabase.dbFileName
application, TemporaryDatabase::class.java, "temporary.sqlite3"
)
.allowMainThreadQueries()
.addMigrations(*temporaryMigrations)
.openHelperFactory(dbFactory)
.setJournalMode(RoomDatabase.JournalMode.TRUNCATE)
.build()

val chooseDocumentsDb: TemporaryDatabase =
Room.databaseBuilder(
application, TemporaryDatabase::class.java, "choose-document.sqlite3"
)
.allowMainThreadQueries()
.addMigrations(*temporaryMigrations)
Expand Down Expand Up @@ -241,7 +251,7 @@ class DatabaseContainer {
}

private val backedUpDatabases = arrayOf(bookmarkDb, readingPlanDb, workspaceDb, repoDb, settingsDb)
private val allDatabases = arrayOf(*backedUpDatabases, temporaryDb)
private val allDatabases = arrayOf(*backedUpDatabases, downloadDocumentsDb, chooseDocumentsDb)

internal fun sync() = allDatabases.forEach {
it.openHelper.writableDatabase
Expand Down
4 changes: 0 additions & 4 deletions db/src/main/java/net/bible/android/database/Databases.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ const val TEMPORARY_DATABASE_VERSION = 1
@TypeConverters(Converters::class)
abstract class TemporaryDatabase: RoomDatabase() {
abstract fun documentSearchDao(): DocumentSearchDao

companion object {
const val dbFileName = "temporary.sqlite3"
}
}

const val REPO_DATABASE_VERSION = 1
Expand Down
3 changes: 3 additions & 0 deletions db/src/main/java/net/bible/android/database/DocumentSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ interface DocumentSearchDao {
@Insert
fun insertDocuments(documentSearches: List<DocumentSearch>)

@Query("""SELECT count(*) from Document""")
fun count(): Long

@Query("""SELECT osisId from Document WHERE Document MATCH :search""")
fun search(search: String): List<String>

Expand Down

0 comments on commit 1db398d

Please sign in to comment.