From 432d08e6773dd03dd6cfc36f0dde59fa7d3b3a98 Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Sat, 11 Nov 2023 14:39:23 +0900 Subject: [PATCH 1/5] :bug: Fix screen error when updating favorites --- .../viewmodel/SymbolSelectionViewModel.kt | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt index 558abdb5..fff878f7 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt @@ -15,6 +15,7 @@ import com.example.speechbuddy.ui.models.DisplayMode import com.example.speechbuddy.ui.models.SymbolItem import com.example.speechbuddy.ui.models.SymbolSelectionUiState import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow @@ -41,10 +42,10 @@ class SymbolSelectionViewModel @Inject internal constructor( private val _entries = MutableLiveData>() val entries: LiveData> get() = _entries + private var getEntriesJob: Job? = null + init { - viewModelScope.launch { - getEntries() - } + getEntries() } fun expandMenu() { @@ -69,16 +70,12 @@ class SymbolSelectionViewModel @Inject internal constructor( isMenuExpanded = false, displayMode = displayMode ) } - viewModelScope.launch { - getEntries() - } + getEntries() } fun setQuery(input: String) { queryInput = input - viewModelScope.launch { - getEntries() - } + getEntries() } fun clear(symbolItem: SymbolItem) { @@ -103,42 +100,44 @@ class SymbolSelectionViewModel @Inject internal constructor( fun selectCategory(category: Category) { if (category != selectedCategory) { selectedCategory = category - viewModelScope.launch { + getEntriesJob?.cancel() + getEntriesJob = viewModelScope.launch { repository.getSymbolsByCategory(category).collect { symbols -> _entries.postValue(listOf(category) + symbols) } } } else { selectedCategory = null - viewModelScope.launch { - getEntries() - } + getEntries() } } - private suspend fun getEntries() { - when (_uiState.value.displayMode) { - DisplayMode.ALL -> { - repository.getEntries(queryInput).collect { entries -> - _entries.postValue(entries) + private fun getEntries() { + getEntriesJob?.cancel() + getEntriesJob = viewModelScope.launch { + when (_uiState.value.displayMode) { + DisplayMode.ALL -> { + repository.getEntries(queryInput).collect { entries -> + _entries.postValue(entries) + } } - } - DisplayMode.SYMBOL -> { - repository.getSymbols(queryInput).collect { symbols -> - _entries.postValue(symbols) + DisplayMode.SYMBOL -> { + repository.getSymbols(queryInput).collect { symbols -> + _entries.postValue(symbols) + } } - } - DisplayMode.CATEGORY -> { - repository.getCategories(queryInput).collect { categories -> - _entries.postValue(categories) + DisplayMode.CATEGORY -> { + repository.getCategories(queryInput).collect { categories -> + _entries.postValue(categories) + } } - } - DisplayMode.FAVORITE -> { - repository.getFavoriteSymbols(queryInput).collect { symbols -> - _entries.postValue(symbols) + DisplayMode.FAVORITE -> { + repository.getFavoriteSymbols(queryInput).collect { symbols -> + _entries.postValue(symbols) + } } } } From 7749414b27b8dc5268db6b69a1226b62b6bc631e Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Sat, 11 Nov 2023 14:51:35 +0900 Subject: [PATCH 2/5] :children_crossing: Improve search --- .../viewmodel/SymbolSelectionViewModel.kt | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt index fff878f7..b306b2f8 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt @@ -75,7 +75,11 @@ class SymbolSelectionViewModel @Inject internal constructor( fun setQuery(input: String) { queryInput = input - getEntries() + /** + * Passes a new queryInput to getEntries() to ensure that + * getEntries() is called precisely because of a change in query + */ + getEntries(input) } fun clear(symbolItem: SymbolItem) { @@ -87,6 +91,7 @@ class SymbolSelectionViewModel @Inject internal constructor( } fun selectSymbol(symbol: Symbol) { + queryInput = "" selectedSymbols = selectedSymbols.plus(SymbolItem(id = selectedSymbols.size, symbol = symbol)) } @@ -112,7 +117,7 @@ class SymbolSelectionViewModel @Inject internal constructor( } } - private fun getEntries() { + private fun getEntries(query: String? = null) { getEntriesJob?.cancel() getEntriesJob = viewModelScope.launch { when (_uiState.value.displayMode) { @@ -122,16 +127,31 @@ class SymbolSelectionViewModel @Inject internal constructor( } } + /** + * In case of DisplayMode.SYMBOL and DisplayMode.CATEGORY, + * if getEntries() is called by setQuery(), + * retrieve both symbols and categories from the repository + */ DisplayMode.SYMBOL -> { - repository.getSymbols(queryInput).collect { symbols -> - _entries.postValue(symbols) - } + if (query != null) // called from setQuery() + repository.getEntries(query).collect { symbols -> + _entries.postValue(symbols) + } + else // called from somewhere else + repository.getSymbols(queryInput).collect { entries -> + _entries.postValue(entries) + } } DisplayMode.CATEGORY -> { - repository.getCategories(queryInput).collect { categories -> - _entries.postValue(categories) - } + if (query != null) + repository.getEntries(query).collect { symbols -> + _entries.postValue(symbols) + } + else + repository.getCategories(queryInput).collect { categories -> + _entries.postValue(categories) + } } DisplayMode.FAVORITE -> { From 91f285d3207150e05ecf968122c4d06e6fecb0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9D=EC=B0=AC=20/=20Lee=20Sukchan?= Date: Sun, 12 Nov 2023 13:24:19 +0900 Subject: [PATCH 3/5] :art: Update frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt Co-authored-by: YeonJeong Kim <65206075+yjeong-k@users.noreply.github.com> --- .../example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt index b306b2f8..efe0b105 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt @@ -134,8 +134,8 @@ class SymbolSelectionViewModel @Inject internal constructor( */ DisplayMode.SYMBOL -> { if (query != null) // called from setQuery() - repository.getEntries(query).collect { symbols -> - _entries.postValue(symbols) + repository.getEntries(query).collect { entries -> + _entries.postValue(entries) } else // called from somewhere else repository.getSymbols(queryInput).collect { entries -> From c3f73e42818800eb88511a803d9f189c79a849c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9D=EC=B0=AC=20/=20Lee=20Sukchan?= Date: Sun, 12 Nov 2023 13:24:30 +0900 Subject: [PATCH 4/5] :art: Update frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt Co-authored-by: YeonJeong Kim <65206075+yjeong-k@users.noreply.github.com> --- .../example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt index efe0b105..2c6b76b3 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt @@ -138,8 +138,8 @@ class SymbolSelectionViewModel @Inject internal constructor( _entries.postValue(entries) } else // called from somewhere else - repository.getSymbols(queryInput).collect { entries -> - _entries.postValue(entries) + repository.getSymbols(queryInput).collect { symbols -> + _entries.postValue(symbols) } } From a88bca3264e5e27524fce8d7681d1e6d3831966c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=84=9D=EC=B0=AC=20/=20Lee=20Sukchan?= Date: Sun, 12 Nov 2023 13:24:41 +0900 Subject: [PATCH 5/5] :art: Update frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt Co-authored-by: YeonJeong Kim <65206075+yjeong-k@users.noreply.github.com> --- .../example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt index 2c6b76b3..7e634d8b 100644 --- a/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt +++ b/frontend/app/src/main/java/com/example/speechbuddy/viewmodel/SymbolSelectionViewModel.kt @@ -145,8 +145,8 @@ class SymbolSelectionViewModel @Inject internal constructor( DisplayMode.CATEGORY -> { if (query != null) - repository.getEntries(query).collect { symbols -> - _entries.postValue(symbols) + repository.getEntries(query).collect { entries -> + _entries.postValue(entries) } else repository.getCategories(queryInput).collect { categories ->