Skip to content

Commit

Permalink
Populate database some example data
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Jul 14, 2021
1 parent 1bd59dc commit 8a831e2
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 16 deletions.
4 changes: 4 additions & 0 deletions app/bibleview-js/src/composables/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ export function useBookmarks(documentId,
const lastOrdinal = (endOff === null ? endOrdinal : endOrdinal - 1)
for(let ord = startOrdinal; ord <= lastOrdinal; ord ++) {
const elem = document.querySelector(`#doc-${documentId} #o-${ord}`);
if(elem == null) {
console.error("Element is not found!", documentId, ord);
return;
}
lastElement = elem;
const oldStyle = elem.style;
elem.style = style;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ open class CurrentPageManager @Inject constructor(
Verse(versification, BibleBook.GEN, 1, 1),
Verse(versification, BibleBook.PS, 1, 1))
val bibles = swordDocumentFacade.bibles
if (bibles.size == 1) {
if (bibles.isNotEmpty()) {
val bible = bibles[0]
for (verse in defaultVerses) {
if (bible.contains(verse)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ open class WindowRepository @Inject constructor(

fun initialize() {
if(initialized) return
var firstTime = false
if(id == 0L) {
id = settings.getLong("current_workspace_id", 0)
if (id == 0L || dao.workspace(id) == null) {
id = dao.insertWorkspace(WorkspaceEntities.Workspace(getResourceString(R.string.workspace_number, 1)))
settings.setLong("current_workspace_id", id)
firstTime = true
}
}
loadFromDb(id)
val firstTime = settings.getBoolean("first-time", true)
if(firstTime) {
activeWindow.pageManager.setFirstUseDefaultVerse()
settings.setBoolean("first-time", false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class SpeakSettingsChangedEvent(val speakSettings: SpeakSettings, val updat

fun SpeakSettings.save(updateBookmark: Boolean = false) {
if(SpeakSettings.Companion.currentSettings?.equals(this) != true) {
CommonUtils.settings.setString(PERSIST_SETTINGS, toJson())
CommonUtils.realSharedPreferences.edit().putString(PERSIST_SETTINGS, toJson()).apply()
Log.d(TAG, "SpeakSettings saved! $this")
val oldSettings = SpeakSettings.Companion.currentSettings
SpeakSettings.Companion.currentSettings = this.makeCopy()
Expand All @@ -41,7 +41,10 @@ fun SpeakSettings.save(updateBookmark: Boolean = false) {

fun SpeakSettings.Companion.load(): SpeakSettings {
val rv = currentSettings?.makeCopy()?: {
val sharedPreferences = CommonUtils.settings
// Excuse of using realSharedPreferences here is that this is loaded early because of widgets. But in practice,
// these are persisted to workspace in WindowRepository and loaded from there.
// This code could be therefore cleaned up.
val sharedPreferences = CommonUtils.realSharedPreferences
val settings = fromJson(sharedPreferences.getString(PERSIST_SETTINGS, "")!!)
settings }()
Log.d(TAG, "SpeakSettings loaded! $rv")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ open class StartupActivity : CustomTitlebarActivityBase() {
Log.i(TAG, "Going to main bible view")

// When I mess up database, I can re-create database like this.
// BackupControl.resetDatabase()
//BackupControl.resetDatabase()
initializeDatabase()

CommonUtils.initializeApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,11 @@ class BibleView(val mainBibleActivity: MainBibleActivity,
anchorOrdinal: Int? = null)
{
val currentPage = window.pageManager.currentPage
bookmarkLabels = bookmarkControl.assignableLabels

// make sure this has been created
bookmarkControl.labelUnlabelled

bookmarkLabels = bookmarkControl.assignableLabels.toMutableList()
initialVerse = verse

initialAnchorOrdinal = anchorOrdinal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ class PreferenceStore: PreferenceDataStore() {

override fun putBoolean(key: String, value: Boolean) = prefs.setBoolean(key, value)

private fun useRealShared(key: String): Boolean = key == "locale_pref" || key.startsWith("night_mode")

override fun putString(key: String, value: String?) =
if(key == "locale_pref") CommonUtils.realSharedPreferences.edit().putString("locale_pref", value).apply()
if(useRealShared(key)) CommonUtils.realSharedPreferences.edit().putString(key, value).apply()
else prefs.setString(key, value)

override fun getString(key: String, defValue: String?): String? =
if (key == "locale_pref") CommonUtils.realSharedPreferences.getString("locale_pref", null)
if (useRealShared(key)) CommonUtils.realSharedPreferences.getString(key, defValue)
else prefs.getString(key, defValue)

override fun getLong(key: String, defValue: Long): Long = prefs.getLong(key, defValue)
Expand Down
73 changes: 72 additions & 1 deletion app/src/main/java/net/bible/service/common/CommonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import android.content.pm.PackageInfo
import android.content.pm.PackageManager.NameNotFoundException
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Build
Expand Down Expand Up @@ -59,6 +60,9 @@ import net.bible.android.activity.BuildConfig.GitHash
import net.bible.android.activity.R
import net.bible.android.database.WorkspaceEntities
import net.bible.android.database.bookmarks.BookmarkEntities
import net.bible.android.database.bookmarks.BookmarkType
import net.bible.android.database.bookmarks.KJVA
import net.bible.android.database.bookmarks.LabelType
import net.bible.android.database.json
import net.bible.android.view.activity.ActivityComponent
import net.bible.android.view.activity.DaggerActivityComponent
Expand All @@ -79,6 +83,7 @@ import org.crosswire.jsword.book.sword.SwordBookMetaData
import org.crosswire.jsword.passage.Key
import org.crosswire.jsword.passage.Verse
import org.crosswire.jsword.passage.VerseRange
import org.crosswire.jsword.passage.VerseRangeFactory
import org.spongycastle.util.io.pem.PemReader
import java.io.File
import java.io.FileInputStream
Expand Down Expand Up @@ -110,6 +115,7 @@ const val beta34introVideo = "https://www.youtube.com/watch?v=ZpZ25uqR_BY" // Fo
const val speakHelpVideo = "https://youtu.be/_wWnS-pjv2A"
const val automaticSpeakBookmarkingVideo = "https://www.youtube.com/watch?v=1HFXLeTERcs"

class DataBaseNotReady: Exception()

val BookmarkEntities.Label.displayName get() =
when {
Expand Down Expand Up @@ -242,7 +248,16 @@ object CommonUtils {
fun removeBoolean(key: String) = setBoolean(key, null)
}

val settings = AndBibleSettings()
private var _settings: AndBibleSettings? = null
val settings: AndBibleSettings get() {
val s = _settings
if(s != null) return s
if(DatabaseContainer.initialized) {
return AndBibleSettings().apply { _settings = this }
} else {
throw DataBaseNotReady()
}
}

val localePref: String?
get() = realSharedPreferences.getString("locale_pref", null)
Expand Down Expand Up @@ -812,6 +827,8 @@ object CommonUtils {

fun initializeApp() {
if(!initialized) {
DatabaseContainer.db

docDao.getUnlocked().forEach {
val book = Books.installed().getBook(it.initials)
book.unlock(it.cipherKey)
Expand All @@ -823,10 +840,64 @@ object CommonUtils {
// Books.installed().getBook(it.initials)?.putProperty(REPOSITORY_KEY, it.repository)
//}

prepareData()

initialized = true
}
}

private fun prepareData() {
val dataVersionNow = 1L
val dataVersion = settings.getLong("data-version", 0)

if(dataVersion < 1) {
val workspaceDao = DatabaseContainer.db.workspaceDao()
val bookmarkDao = DatabaseContainer.db.bookmarkDao()

val highlightLabels = listOf(
BookmarkEntities.Label(name = application.getString(R.string.label_red), type = LabelType.HIGHLIGHT, color = Color.argb(255, 255, 0, 0), underlineStyleWholeVerse = false),
BookmarkEntities.Label(name = application.getString(R.string.label_green), type = LabelType.HIGHLIGHT, color = Color.argb(255, 0, 255, 0), underlineStyleWholeVerse = false),
BookmarkEntities.Label(name = application.getString(R.string.label_blue), type = LabelType.HIGHLIGHT, color = Color.argb(255, 0, 0, 255), underlineStyleWholeVerse = false),
BookmarkEntities.Label(name = application.getString(R.string.label_underline), type = LabelType.HIGHLIGHT, color = Color.argb(255, 255, 255, 0), underlineStyle = true, underlineStyleWholeVerse = true),
)
val salvationLabel = BookmarkEntities.Label(name = application.getString(R.string.label_salvation), type = LabelType.EXAMPLE, color = Color.argb(255, 255, 0, 255))
val highlightIds = bookmarkDao.insertLabels(highlightLabels)
val salvationId = bookmarkDao.insert(salvationLabel)

listOf("Gen.1.1", "Joh.3.16", "Joh.3.3", "Tit.3.3-Tit.3.7", "Rom.3.23-Rom.3.24", "Rom.4.3", "1Tim.1.15", "Eph.2.8-Eph.2.9", "Isa.6.3", "Rev.4.8", "Exo.20.2-Exo.2.17")
.map { VerseRangeFactory.fromString(KJVA, it) }
.map {
BookmarkEntities.Bookmark(it, textRange = null, wholeVerse = true, book = null).apply { type = BookmarkType.EXAMPLE }
}.forEach {
val bid = bookmarkDao.insert(it)
bookmarkDao.insert(BookmarkEntities.BookmarkToLabel(bid, salvationId))
}

val ws = workspaceDao.allWorkspaces()
if(ws.isNotEmpty()) {
ws.forEach {
it.workspaceSettings?.favouriteLabels?.addAll(highlightIds)
}
workspaceDao.updateWorkspaces(ws)
settings.setBoolean("first-time", false)

} else {
val workspaceSettings = WorkspaceEntities.WorkspaceSettings(favouriteLabels = highlightIds.toMutableSet())
val workspaceIds = listOf(
WorkspaceEntities.Workspace(name = application.getString(R.string.workspace_number, 1), workspaceSettings = workspaceSettings),
WorkspaceEntities.Workspace(name = application.getString(R.string.workspace_number, 2), workspaceSettings = workspaceSettings),
).map {
workspaceDao.insertWorkspace(it)
}
settings.setLong("current_workspace_id", workspaceIds[0])
}
}

if(dataVersion != dataVersionNow) {
settings.setLong("data-version", dataVersionNow)
}
}

fun iconWithSync(icon: Int, syncOn: Boolean, sizeMultiplier: Float? = null): Drawable {
val syncIcon = if (syncOn) {
R.drawable.ic_sync_white_24dp
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/net/bible/service/db/DatabaseContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ private val MIGRATION_53_54_booleanSettings = object : Migration(53, 54) {
private val MIGRATION_54_55_bookmarkType = object : Migration(54, 55) {
override fun doMigrate(db: SupportSQLiteDatabase) {
db.apply {
execSQL("ALTER TABLE Label ADD COLUMN type TEXT DEFAULT NULL")
execSQL("ALTER TABLE Bookmark ADD COLUMN type TEXT DEFAULT NULL")
}
}
Expand All @@ -952,6 +953,8 @@ private val MIGRATION_54_55_bookmarkType = object : Migration(54, 55) {
object DatabaseContainer {
private var instance: AppDatabase? = null

val initialized get() = instance != null

val db: AppDatabase
get () {
return instance ?: synchronized(this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object ScreenSettings {
private const val DARK_READING_THRESHOLD = 15
private const val MIN_LIGHT_READING = 50

val preferences get() = CommonUtils.settings
val preferences get() = CommonUtils.realSharedPreferences

val isScreenOn: Boolean
get() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
buildscript {
ext.kotlin_version = '1.4.32'
ext.room_version = '2.3.0'
ext.jsword_version = '2.3.31'
ext.jsword_version = '2.3.32'
ext.kotlinx_serialization_version = '1.1.0'

repositories {
Expand Down
13 changes: 10 additions & 3 deletions db/schemas/net.bible.android.database.AppDatabase/55.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 55,
"identityHash": "15ef9582bebdb37cf3ac2c55c858e9c8",
"identityHash": "78b2d7a505d763da97ddbafda4a4302c",
"entities": [
{
"tableName": "Bookmark",
Expand Down Expand Up @@ -158,7 +158,7 @@
},
{
"tableName": "Label",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `bookmarkStyle` TEXT, `color` INTEGER NOT NULL DEFAULT 0, `underlineStyle` INTEGER NOT NULL DEFAULT 0, `underlineStyleWholeVerse` INTEGER NOT NULL DEFAULT 0)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `bookmarkStyle` TEXT, `color` INTEGER NOT NULL DEFAULT 0, `underlineStyle` INTEGER NOT NULL DEFAULT 0, `underlineStyleWholeVerse` INTEGER NOT NULL DEFAULT 0, `type` TEXT DEFAULT NULL)",
"fields": [
{
"fieldPath": "id",
Expand Down Expand Up @@ -198,6 +198,13 @@
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "TEXT",
"notNull": false,
"defaultValue": "NULL"
}
],
"primaryKey": {
Expand Down Expand Up @@ -1489,7 +1496,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '15ef9582bebdb37cf3ac2c55c858e9c8')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '78b2d7a505d763da97ddbafda4a4302c')"
]
}
}
7 changes: 7 additions & 0 deletions db/src/main/java/net/bible/android/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import net.bible.android.database.bookmarks.BookmarkDao
import net.bible.android.database.bookmarks.BookmarkEntities
import net.bible.android.database.bookmarks.BookmarkStyle
import net.bible.android.database.bookmarks.BookmarkType
import net.bible.android.database.bookmarks.LabelType
import net.bible.android.database.bookmarks.PlaybackSettings
import net.bible.android.database.bookmarks.SpeakSettings
import net.bible.android.database.readingplan.ReadingPlanDao
Expand All @@ -48,6 +49,12 @@ import java.util.*
const val DATABASE_VERSION = 55

class Converters {
@TypeConverter
fun toLabelType(value: String?) = if(value==null) null else LabelType.valueOf(value)

@TypeConverter
fun fromLabelType(value: LabelType?) = value?.name

@TypeConverter
fun toBookmarkType(value: String?) = if(value==null) null else BookmarkType.valueOf(value)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ interface VerseRangeUser {
val verseRange: VerseRange
}

enum class BookmarkType {HIGHLIGHT}
enum class LabelType {HIGHLIGHT, EXAMPLE}
enum class BookmarkType {EXAMPLE}

class BookmarkEntities {
@Serializable
Expand Down Expand Up @@ -270,6 +271,7 @@ class BookmarkEntities {
@ColumnInfo(defaultValue = "0") var color: Int = defaultLabelColor,
@ColumnInfo(defaultValue = "0") var underlineStyle: Boolean = false,
@ColumnInfo(defaultValue = "0") var underlineStyleWholeVerse: Boolean = true,
@ColumnInfo(defaultValue = "NULL") var type: LabelType? = null,
) {
override fun toString() = name
val isSpeakLabel get() = name == SPEAK_LABEL_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ interface BookmarkDao {
@Update fun update(entity: BookmarkEntities.StudyPadTextEntry)

@Insert fun insert(entity: Label): Long
@Insert fun insertLabels(entity: List<Label>): List<Long>

@Update fun update(entity: Label)

Expand Down

0 comments on commit 8a831e2

Please sign in to comment.