From 624f7ef908b236a443b3f25bb36c658be16d23f3 Mon Sep 17 00:00:00 2001 From: Om Godse <64728411+OmGodse@users.noreply.github.com> Date: Wed, 5 Jul 2023 12:01:27 +0530 Subject: [PATCH] Added cache to reduce IO delay --- app/build.gradle | 4 ++-- app/src/main/java/com/omgodse/notally/Cache.kt | 8 ++++++++ .../com/omgodse/notally/activities/NotallyActivity.kt | 1 + .../main/java/com/omgodse/notally/room/dao/BaseNoteDao.kt | 3 +++ .../java/com/omgodse/notally/viewmodels/BaseNoteModel.kt | 8 ++++++++ .../java/com/omgodse/notally/viewmodels/NotallyModel.kt | 4 +++- app/src/main/res/layout/activity_notally.xml | 4 +++- 7 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/omgodse/notally/Cache.kt diff --git a/app/build.gradle b/app/build.gradle index f07f77f5..e831508b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { applicationId "com.omgodse.notally" minSdk 21 targetSdk 32 - versionCode 47 - versionName "5.2" + versionCode 48 + versionName "5.3" resConfigs "en", "ca", "cs", "da", "de", "el", "es", "fr", "hu", "in", "it", "ja", "nb", "nl", "nn", "pl", "pt-rBR", "pt-rPT", "ru", "sk", "sv", "tl", "tr", "uk", "vi", "zh-rCN" vectorDrawables.generatedDensities = [] } diff --git a/app/src/main/java/com/omgodse/notally/Cache.kt b/app/src/main/java/com/omgodse/notally/Cache.kt new file mode 100644 index 00000000..087a20d7 --- /dev/null +++ b/app/src/main/java/com/omgodse/notally/Cache.kt @@ -0,0 +1,8 @@ +package com.omgodse.notally + +import com.omgodse.notally.room.BaseNote + +object Cache { + + var list: List = ArrayList() +} \ No newline at end of file diff --git a/app/src/main/java/com/omgodse/notally/activities/NotallyActivity.kt b/app/src/main/java/com/omgodse/notally/activities/NotallyActivity.kt index 87dc45ca..235dd0bb 100644 --- a/app/src/main/java/com/omgodse/notally/activities/NotallyActivity.kt +++ b/app/src/main/java/com/omgodse/notally/activities/NotallyActivity.kt @@ -76,6 +76,7 @@ abstract class NotallyActivity(private val type: Type) : AppCompatActivity() { setStateFromModel() configureUI() + binding.ScrollView.visibility = View.VISIBLE } } diff --git a/app/src/main/java/com/omgodse/notally/room/dao/BaseNoteDao.kt b/app/src/main/java/com/omgodse/notally/room/dao/BaseNoteDao.kt index a5f41de4..1aff9a16 100644 --- a/app/src/main/java/com/omgodse/notally/room/dao/BaseNoteDao.kt +++ b/app/src/main/java/com/omgodse/notally/room/dao/BaseNoteDao.kt @@ -35,6 +35,9 @@ interface BaseNoteDao { @Query("SELECT * FROM BaseNote WHERE folder = :folder ORDER BY pinned DESC, timestamp DESC") fun getFrom(folder: Folder): LiveData> + @Query("SELECT * FROM BaseNote") + fun getAll(): LiveData> + @Query("SELECT * FROM BaseNote WHERE id = :id") suspend fun get(id: Long): BaseNote diff --git a/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt b/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt index b0a8ca47..c2b8cd94 100644 --- a/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt +++ b/app/src/main/java/com/omgodse/notally/viewmodels/BaseNoteModel.kt @@ -13,6 +13,7 @@ import androidx.core.text.toHtml import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import androidx.room.withTransaction +import com.omgodse.notally.Cache import com.omgodse.notally.R import com.omgodse.notally.legacy.Migrations import com.omgodse.notally.legacy.XMLUtils @@ -61,6 +62,7 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) { var currentFile: File? = null val labels = labelDao.getAll() + val allNotes = baseNoteDao.getAll() val baseNotes = Content(baseNoteDao.getFrom(Folder.NOTES), ::transform) val deletedNotes = Content(baseNoteDao.getFrom(Folder.DELETED), ::transform) val archivedNotes = Content(baseNoteDao.getFrom(Folder.ARCHIVED), ::transform) @@ -105,6 +107,9 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) { } } } + allNotes.observeForever { list -> + Cache.list = list + } } fun getNotesByLabel(label: String): Content { @@ -403,6 +408,7 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) { jsonObject.put("body", baseNote.body) jsonObject.put("spans", Converters.spansToJSONArray(baseNote.spans)) } + Type.LIST -> { jsonObject.put("items", Converters.itemsToJSONArray(baseNote.items)) } @@ -430,6 +436,7 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) { val body = baseNote.body.applySpans(baseNote.spans).toHtml() append(body) } + Type.LIST -> { append("
    ") baseNote.items.forEach { item -> @@ -455,6 +462,7 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) { val pattern = when (locale.language) { Locale.CHINESE.language, Locale.JAPANESE.language -> "yyyy年 MMM d日 (EEE)" + else -> "EEE d MMM yyyy" } return SimpleDateFormat(pattern, locale) diff --git a/app/src/main/java/com/omgodse/notally/viewmodels/NotallyModel.kt b/app/src/main/java/com/omgodse/notally/viewmodels/NotallyModel.kt index eeacaa1c..f908b942 100644 --- a/app/src/main/java/com/omgodse/notally/viewmodels/NotallyModel.kt +++ b/app/src/main/java/com/omgodse/notally/viewmodels/NotallyModel.kt @@ -13,6 +13,7 @@ import android.text.style.URLSpan import androidx.core.text.getSpans import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope +import com.omgodse.notally.Cache import com.omgodse.notally.miscellaneous.applySpans import com.omgodse.notally.preferences.Preferences import com.omgodse.notally.room.BaseNote @@ -61,7 +62,8 @@ class NotallyModel(app: Application) : AndroidViewModel(app) { suspend fun setState(id: Long) { - val baseNote = withContext(Dispatchers.IO) { baseNoteDao.get(id) } + val cachedNote = Cache.list.find { baseNote -> baseNote.id == id } + val baseNote = cachedNote ?: withContext(Dispatchers.IO) { baseNoteDao.get(id) } this.id = id folder = baseNote.folder diff --git a/app/src/main/res/layout/activity_notally.xml b/app/src/main/res/layout/activity_notally.xml index a4c5390a..71a44e45 100644 --- a/app/src/main/res/layout/activity_notally.xml +++ b/app/src/main/res/layout/activity_notally.xml @@ -11,11 +11,13 @@ app:navigationIcon="?attr/homeAsUpIndicator" /> + android:theme="@style/Colorless" + android:visibility="gone">