Skip to content

Commit

Permalink
Fix race condition in updated phrase not immediately showing in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlauser committed Jun 12, 2024
1 parent 88119ad commit 1012824
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/willowtree/vocable/AppKoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.willowtree.vocable.settings.AddUpdateCategoryViewModel
import com.willowtree.vocable.settings.EditCategoriesViewModel
import com.willowtree.vocable.settings.EditCategoryMenuViewModel
import com.willowtree.vocable.settings.EditCategoryPhrasesViewModel
import com.willowtree.vocable.settings.customcategories.CustomCategoryPhraseViewModel
import com.willowtree.vocable.settings.selectionmode.SelectionModeViewModel
import com.willowtree.vocable.splash.SplashActivity
import com.willowtree.vocable.splash.SplashViewModel
Expand Down Expand Up @@ -106,4 +107,5 @@ val vocableKoinModule = module {
viewModel { EditCategoryPhrasesViewModel(get(), get(), get()) }
viewModel { AddUpdateCategoryViewModel(get(), get(), get()) }
viewModel { EditCategoryMenuViewModel(get()) }
viewModel { CustomCategoryPhraseViewModel(get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.willowtree.vocable.IPhrasesUseCase
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.Phrase
import com.willowtree.vocable.utils.ILocalizedResourceUtility
import kotlinx.coroutines.launch

class EditCategoryPhrasesViewModel(
savedStateHandle: SavedStateHandle,
private val phrasesUseCase: IPhrasesUseCase,
phrasesUseCase: IPhrasesUseCase,
private val localizedResourceUtility: ILocalizedResourceUtility
) : ViewModel() {

Expand All @@ -23,10 +21,4 @@ class EditCategoryPhrasesViewModel(
fun getCategoryName(category: Category): String {
return localizedResourceUtility.getTextFromCategory(category)
}

fun deletePhraseFromCategory(phrase: Phrase) {
viewModelScope.launch {
phrasesUseCase.deletePhrase(phrase.phraseId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.willowtree.vocable.databinding.FragmentCustomCategoryPhraseListBindin
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.Phrase
import com.willowtree.vocable.settings.EditCategoryPhrasesFragmentDirections
import com.willowtree.vocable.settings.EditCategoryPhrasesViewModel
import com.willowtree.vocable.settings.customcategories.adapter.CustomCategoryPhraseAdapter
import com.willowtree.vocable.utils.ItemOffsetDecoration
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand All @@ -34,7 +33,7 @@ class CustomCategoryPhraseListFragment : BaseFragment<FragmentCustomCategoryPhra
}
}

private val editCategoriesViewModel: EditCategoryPhrasesViewModel by viewModel()
private val viewModel: CustomCategoryPhraseViewModel by viewModel()
private lateinit var category: Category

private val onPhraseEdit = { phrase: Phrase ->
Expand Down Expand Up @@ -88,7 +87,7 @@ class CustomCategoryPhraseListFragment : BaseFragment<FragmentCustomCategoryPhra
with(dialogPositiveButton) {
setText(R.string.delete)
action = {
editCategoriesViewModel.deletePhraseFromCategory(phrase)
viewModel.deletePhraseFromCategory(phrase)
toggleDialogVisibility(false)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.willowtree.vocable.settings.customcategories

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.willowtree.vocable.IPhrasesUseCase
import com.willowtree.vocable.presets.Phrase
import kotlinx.coroutines.launch

class CustomCategoryPhraseViewModel(
private val phrasesUseCase: IPhrasesUseCase
) : ViewModel() {
fun deletePhraseFromCategory(phrase: Phrase) {
viewModelScope.launch {
phrasesUseCase.deletePhrase(phrase.phraseId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ abstract class VocableFragmentStateAdapter<T>(fm: FragmentManager, lifecycle: Li
with(this.items) {
clear()
addAll(items)
notifyDataSetChanged()
}

val oldNumPages = numPages
Expand Down

0 comments on commit 1012824

Please sign in to comment.