Skip to content

Commit

Permalink
Upgrade Material and Lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
amberin committed Sep 8, 2024
1 parent 7a1f7a6 commit 469d2ff
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/orgzly/android/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 15 in app/src/main/java/com/orgzly/android/data/DataRepository.kt

View workflow job for this annotation

GitHub Actions / localUnitTests

'LocalBroadcastManager' is deprecated. Deprecated in Java

Check warning on line 15 in app/src/main/java/com/orgzly/android/data/DataRepository.kt

View workflow job for this annotation

GitHub Actions / localUnitTests

'LocalBroadcastManager' is deprecated. Deprecated in Java
import androidx.sqlite.db.SupportSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQueryBuilder
Expand Down Expand Up @@ -2139,7 +2139,7 @@ class DataRepository @Inject constructor(
* Return all known tags
*/
fun selectAllTagsLiveData(): LiveData<List<String>> {
return Transformations.map(db.note().getDistinctTagsLiveData()) { tagsList ->
return db.note().getDistinctTagsLiveData().map { tagsList ->
tagsList.flatMap { Note.dbDeSerializeTags(it) }.distinct().sorted()
}
}
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/orgzly/android/sync/SyncRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,8 +88,8 @@ object SyncRunner {
}

@JvmStatic
fun onStateChange(tag: String): LiveData<SyncState> {
return Transformations.map(onAllWorkInfo()) { workInfoList ->
fun onStateChange(tag: String): LiveData<SyncState?> {
return onAllWorkInfo().map { workInfoList ->
syncStateFromWorkInfoList(workInfoList).also { state ->
logStateChange(tag, state, workInfoList)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,8 +49,8 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod

val viewState = MutableLiveData<ViewState>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>()

private val booksSubject: LiveData<List<BookView>> = Transformations.switchMap(booksParams) {
private val booksSubject: LiveData<List<BookView>> = booksParams.switchMap {
dataRepository.getBooksLiveData()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -34,7 +34,7 @@ class BookViewModel(private val dataRepository: DataRepository, val bookId: Long

data class Data(val book: Book?, val notes: List<NoteView>?)

val data = Transformations.switchMap(params) { _ ->
val data = params.switchMap { _ ->
MediatorLiveData<Data>().apply {
addSource(dataRepository.getBookLiveData(bookId)) {
value = Data(it, value?.notes)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,9 +24,9 @@ class QueryViewModel(private val dataRepository: DataRepository) : CommonViewMod

private val notesParams = MutableLiveData<Params>()

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 {
Expand All @@ -37,7 +36,7 @@ class QueryViewModel(private val dataRepository: DataRepository) : CommonViewMod
it
}
} else {
MutableLiveData<List<NoteView>>()
MutableLiveData()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -15,10 +15,10 @@ class SavedSearchesViewModel(dataRepository: DataRepository) : CommonViewModel()
EMPTY
}

val viewState = MutableLiveData<ViewState>(ViewState.LOADING)
val viewState = MutableLiveData(ViewState.LOADING)

val data: LiveData<List<SavedSearch>> by lazy {
Transformations.map(dataRepository.getSavedSearchesLiveData()) { searches ->
dataRepository.getSavedSearchesLiveData().map { searches ->
viewState.value = if (searches.isNotEmpty()) {
ViewState.LOADED
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.orgzly.android.sync.SyncState
import com.orgzly.android.util.LogUtils

class SyncViewModel : ViewModel() {
val state: LiveData<SyncState> = SyncRunner.onStateChange("sync-view-model")
val state: LiveData<SyncState?> = SyncRunner.onStateChange("sync-view-model")

fun isSyncRunning(): Boolean {
val currentState = state.value
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit 469d2ff

Please sign in to comment.