From 469d2ff9e71a113874884e4c4aade147e4f65095 Mon Sep 17 00:00:00 2001 From: Victor Andreasson Date: Sun, 8 Sep 2024 21:41:09 +0200 Subject: [PATCH] Upgrade Material and Lifecycle --- .../java/com/orgzly/android/data/DataRepository.kt | 4 ++-- .../main/java/com/orgzly/android/sync/SyncRunner.kt | 13 +++++++++---- .../com/orgzly/android/ui/SyncProgressViewModel.kt | 4 +++- .../com/orgzly/android/ui/books/BooksViewModel.kt | 7 ++++--- .../orgzly/android/ui/main/MainActivityViewModel.kt | 13 ++++++++++--- .../orgzly/android/ui/notes/book/BookViewModel.kt | 6 +++--- .../orgzly/android/ui/notes/query/QueryViewModel.kt | 11 +++++------ .../ui/savedsearches/SavedSearchesViewModel.kt | 12 ++++++------ .../com/orgzly/android/ui/sync/SyncViewModel.kt | 2 +- build.gradle | 4 ++-- 10 files changed, 45 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/orgzly/android/data/DataRepository.kt b/app/src/main/java/com/orgzly/android/data/DataRepository.kt index bff22b141..15add0b85 100644 --- a/app/src/main/java/com/orgzly/android/data/DataRepository.kt +++ b/app/src/main/java/com/orgzly/android/data/DataRepository.kt @@ -11,7 +11,7 @@ import android.os.Handler import android.text.TextUtils import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Transformations +import androidx.lifecycle.map import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.sqlite.db.SupportSQLiteQuery import androidx.sqlite.db.SupportSQLiteQueryBuilder @@ -2139,7 +2139,7 @@ class DataRepository @Inject constructor( * Return all known tags */ fun selectAllTagsLiveData(): LiveData> { - return Transformations.map(db.note().getDistinctTagsLiveData()) { tagsList -> + return db.note().getDistinctTagsLiveData().map { tagsList -> tagsList.flatMap { Note.dbDeSerializeTags(it) }.distinct().sorted() } } diff --git a/app/src/main/java/com/orgzly/android/sync/SyncRunner.kt b/app/src/main/java/com/orgzly/android/sync/SyncRunner.kt index ef4b98e61..11e5a64c9 100644 --- a/app/src/main/java/com/orgzly/android/sync/SyncRunner.kt +++ b/app/src/main/java/com/orgzly/android/sync/SyncRunner.kt @@ -4,8 +4,13 @@ import android.content.Intent import androidx.core.content.ContextCompat.startActivity import androidx.fragment.app.FragmentActivity import androidx.lifecycle.LiveData -import androidx.lifecycle.Transformations -import androidx.work.* +import androidx.lifecycle.map +import androidx.work.ExistingWorkPolicy +import androidx.work.OneTimeWorkRequestBuilder +import androidx.work.OutOfQuotaPolicy +import androidx.work.WorkInfo +import androidx.work.WorkManager +import androidx.work.workDataOf import com.orgzly.BuildConfig import com.orgzly.R import com.orgzly.android.App @@ -83,8 +88,8 @@ object SyncRunner { } @JvmStatic - fun onStateChange(tag: String): LiveData { - return Transformations.map(onAllWorkInfo()) { workInfoList -> + fun onStateChange(tag: String): LiveData { + return onAllWorkInfo().map { workInfoList -> syncStateFromWorkInfoList(workInfoList).also { state -> logStateChange(tag, state, workInfoList) } diff --git a/app/src/main/java/com/orgzly/android/ui/SyncProgressViewModel.kt b/app/src/main/java/com/orgzly/android/ui/SyncProgressViewModel.kt index 4d9058a75..0fd784e28 100644 --- a/app/src/main/java/com/orgzly/android/ui/SyncProgressViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/SyncProgressViewModel.kt @@ -25,7 +25,9 @@ class SyncProgressViewModel : ViewModel() { init { viewModelScope.launch { SyncRunner.onStateChange("sync-view-model").asFlow().collect { - _syncState.tryEmit(it) + if (it != null) { + _syncState.tryEmit(it) + } } } } diff --git a/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt b/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt index 8b922a84c..dd756a181 100644 --- a/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt @@ -2,7 +2,8 @@ package com.orgzly.android.ui.books import android.net.Uri import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Transformations +import androidx.lifecycle.map +import androidx.lifecycle.switchMap import com.orgzly.BuildConfig import com.orgzly.android.App import com.orgzly.android.BookFormat @@ -48,8 +49,8 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod val viewState = MutableLiveData(ViewState.LOADING) - val data = Transformations.switchMap(booksParams) { - Transformations.map(dataRepository.getBooksLiveData()) { books -> + val data = booksParams.switchMap { + dataRepository.getBooksLiveData().map { books -> viewState.value = if (books.isNotEmpty()) { ViewState.LOADED } else { diff --git a/app/src/main/java/com/orgzly/android/ui/main/MainActivityViewModel.kt b/app/src/main/java/com/orgzly/android/ui/main/MainActivityViewModel.kt index 4ae86cc08..cc18737f0 100644 --- a/app/src/main/java/com/orgzly/android/ui/main/MainActivityViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/main/MainActivityViewModel.kt @@ -3,7 +3,7 @@ package com.orgzly.android.ui.main import android.net.Uri import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Transformations +import androidx.lifecycle.switchMap import com.orgzly.BuildConfig import com.orgzly.R import com.orgzly.android.App @@ -15,14 +15,21 @@ import com.orgzly.android.db.entity.SavedSearch import com.orgzly.android.prefs.AppPreferences import com.orgzly.android.ui.CommonViewModel import com.orgzly.android.ui.SingleLiveEvent -import com.orgzly.android.usecase.* +import com.orgzly.android.usecase.BookScrollToNote +import com.orgzly.android.usecase.BookSparseTreeForNote +import com.orgzly.android.usecase.LinkFindTarget +import com.orgzly.android.usecase.NoteFindWithProperty +import com.orgzly.android.usecase.NoteUpdateClockingState +import com.orgzly.android.usecase.SavedSearchExport +import com.orgzly.android.usecase.SavedSearchImport +import com.orgzly.android.usecase.UseCaseRunner import com.orgzly.android.util.LogUtils import java.io.File class MainActivityViewModel(private val dataRepository: DataRepository) : CommonViewModel() { private val booksParams = MutableLiveData() - private val booksSubject: LiveData> = Transformations.switchMap(booksParams) { + private val booksSubject: LiveData> = booksParams.switchMap { dataRepository.getBooksLiveData() } diff --git a/app/src/main/java/com/orgzly/android/ui/notes/book/BookViewModel.kt b/app/src/main/java/com/orgzly/android/ui/notes/book/BookViewModel.kt index 02944cda8..dbf4e4795 100644 --- a/app/src/main/java/com/orgzly/android/ui/notes/book/BookViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/notes/book/BookViewModel.kt @@ -2,14 +2,14 @@ package com.orgzly.android.ui.notes.book import androidx.lifecycle.MediatorLiveData import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Transformations +import androidx.lifecycle.switchMap import com.orgzly.android.App import com.orgzly.android.data.DataRepository import com.orgzly.android.db.entity.Book import com.orgzly.android.db.entity.NoteView +import com.orgzly.android.ui.AppBar import com.orgzly.android.ui.CommonViewModel import com.orgzly.android.ui.SingleLiveEvent -import com.orgzly.android.ui.AppBar import com.orgzly.android.usecase.BookCycleVisibility import com.orgzly.android.usecase.UseCaseRunner @@ -34,7 +34,7 @@ class BookViewModel(private val dataRepository: DataRepository, val bookId: Long data class Data(val book: Book?, val notes: List?) - val data = Transformations.switchMap(params) { _ -> + val data = params.switchMap { _ -> MediatorLiveData().apply { addSource(dataRepository.getBookLiveData(bookId)) { value = Data(it, value?.notes) diff --git a/app/src/main/java/com/orgzly/android/ui/notes/query/QueryViewModel.kt b/app/src/main/java/com/orgzly/android/ui/notes/query/QueryViewModel.kt index 6e8b1d6ce..7f5628be4 100644 --- a/app/src/main/java/com/orgzly/android/ui/notes/query/QueryViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/notes/query/QueryViewModel.kt @@ -1,11 +1,10 @@ package com.orgzly.android.ui.notes.query -import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Transformations +import androidx.lifecycle.map +import androidx.lifecycle.switchMap import com.orgzly.BuildConfig import com.orgzly.android.data.DataRepository -import com.orgzly.android.db.entity.NoteView import com.orgzly.android.ui.AppBar import com.orgzly.android.ui.CommonViewModel import com.orgzly.android.util.LogUtils @@ -25,9 +24,9 @@ class QueryViewModel(private val dataRepository: DataRepository) : CommonViewMod private val notesParams = MutableLiveData() - val data = Transformations.switchMap(notesParams) { params -> + val data = notesParams.switchMap { params -> if (params.query != null) { - Transformations.map(dataRepository.selectNotesFromQueryLiveData(params.query)) { + dataRepository.selectNotesFromQueryLiveData(params.query).map { viewState.value = if (it.isNotEmpty()) { ViewState.LOADED } else { @@ -37,7 +36,7 @@ class QueryViewModel(private val dataRepository: DataRepository) : CommonViewMod it } } else { - MutableLiveData>() + MutableLiveData() } } diff --git a/app/src/main/java/com/orgzly/android/ui/savedsearches/SavedSearchesViewModel.kt b/app/src/main/java/com/orgzly/android/ui/savedsearches/SavedSearchesViewModel.kt index b337f3372..0cce24839 100644 --- a/app/src/main/java/com/orgzly/android/ui/savedsearches/SavedSearchesViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/savedsearches/SavedSearchesViewModel.kt @@ -1,12 +1,12 @@ package com.orgzly.android.ui.savedsearches -import com.orgzly.android.data.DataRepository -import com.orgzly.android.db.entity.SavedSearch -import com.orgzly.android.ui.CommonViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.Transformations +import androidx.lifecycle.map +import com.orgzly.android.data.DataRepository +import com.orgzly.android.db.entity.SavedSearch import com.orgzly.android.ui.AppBar +import com.orgzly.android.ui.CommonViewModel class SavedSearchesViewModel(dataRepository: DataRepository) : CommonViewModel() { enum class ViewState { @@ -15,10 +15,10 @@ class SavedSearchesViewModel(dataRepository: DataRepository) : CommonViewModel() EMPTY } - val viewState = MutableLiveData(ViewState.LOADING) + val viewState = MutableLiveData(ViewState.LOADING) val data: LiveData> by lazy { - Transformations.map(dataRepository.getSavedSearchesLiveData()) { searches -> + dataRepository.getSavedSearchesLiveData().map { searches -> viewState.value = if (searches.isNotEmpty()) { ViewState.LOADED } else { diff --git a/app/src/main/java/com/orgzly/android/ui/sync/SyncViewModel.kt b/app/src/main/java/com/orgzly/android/ui/sync/SyncViewModel.kt index 4f224ebc7..e057d9e10 100644 --- a/app/src/main/java/com/orgzly/android/ui/sync/SyncViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/sync/SyncViewModel.kt @@ -8,7 +8,7 @@ import com.orgzly.android.sync.SyncState import com.orgzly.android.util.LogUtils class SyncViewModel : ViewModel() { - val state: LiveData = SyncRunner.onStateChange("sync-view-model") + val state: LiveData = SyncRunner.onStateChange("sync-view-model") fun isSyncRunning(): Boolean { val currentState = state.value diff --git a/build.gradle b/build.gradle index eebe63608..d4ee0ec6a 100644 --- a/build.gradle +++ b/build.gradle @@ -24,12 +24,12 @@ buildscript { versions.android_viewpager = '1.0.0' versions.android_constraint_layout = '2.1.4' versions.android_preference = '1.2.0' - versions.android_material = '1.7.0' + versions.android_material = '1.12.0' versions.android_swiperefreshlayout = '1.1.0' versions.android_room = '2.4.3' - versions.android_lifecycle = '2.5.1' + versions.android_lifecycle = '2.8.5' versions.android_annotation = '1.3.0'