Skip to content

Commit

Permalink
Added cache to reduce IO delay
Browse files Browse the repository at this point in the history
  • Loading branch information
OmGodse committed Jul 5, 2023
1 parent 26f28a8 commit 624f7ef
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/omgodse/notally/Cache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.omgodse.notally

import com.omgodse.notally.room.BaseNote

object Cache {

var list: List<BaseNote> = ArrayList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ abstract class NotallyActivity(private val type: Type) : AppCompatActivity() {
setStateFromModel()

configureUI()
binding.ScrollView.visibility = View.VISIBLE
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/omgodse/notally/room/dao/BaseNoteDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ interface BaseNoteDao {
@Query("SELECT * FROM BaseNote WHERE folder = :folder ORDER BY pinned DESC, timestamp DESC")
fun getFrom(folder: Folder): LiveData<List<BaseNote>>

@Query("SELECT * FROM BaseNote")
fun getAll(): LiveData<List<BaseNote>>

@Query("SELECT * FROM BaseNote WHERE id = :id")
suspend fun get(id: Long): BaseNote

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -105,6 +107,9 @@ class BaseNoteModel(private val app: Application) : AndroidViewModel(app) {
}
}
}
allNotes.observeForever { list ->
Cache.list = list
}
}

fun getNotesByLabel(label: String): Content {
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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("<ol style=\"list-style: none; padding: 0;\">")
baseNote.items.forEach { item ->
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_notally.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
app:navigationIcon="?attr/homeAsUpIndicator" />

<androidx.core.widget.NestedScrollView
android:id="@+id/ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/Toolbar"
android:fillViewport="true"
android:theme="@style/Colorless">
android:theme="@style/Colorless"
android:visibility="gone">

<LinearLayout
android:layout_width="match_parent"
Expand Down

1 comment on commit 624f7ef

@OmGodse
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not accepting feature requests right now.

Please sign in to comment.