Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…roject-team-6 into feat/logout
  • Loading branch information
yjeong-k committed Nov 12, 2023
2 parents 0bafd4c + 50d4fb0 commit cef1ff2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ResetPasswordViewModel @Inject internal constructor(
isValidPassword = false,
error = ResetPasswordError(
type = ResetPasswordErrorType.PASSWORD,
messageId = R.string.false_new_password
messageId = R.string.password_qualification
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SignupViewModel @Inject internal constructor(
isValidPassword = false,
error = SignupError(
type = SignupErrorType.PASSWORD,
messageId = R.string.false_new_password
messageId = R.string.password_qualification
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,10 +42,10 @@ class SymbolSelectionViewModel @Inject internal constructor(
private val _entries = MutableLiveData<List<Entry>>()
val entries: LiveData<List<Entry>> get() = _entries

private var getEntriesJob: Job? = null

init {
viewModelScope.launch {
getEntries()
}
getEntries()
}

fun expandMenu() {
Expand All @@ -69,16 +70,16 @@ class SymbolSelectionViewModel @Inject internal constructor(
isMenuExpanded = false, displayMode = displayMode
)
}
viewModelScope.launch {
getEntries()
}
getEntries()
}

fun setQuery(input: String) {
queryInput = input
viewModelScope.launch {
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) {
Expand All @@ -90,6 +91,7 @@ class SymbolSelectionViewModel @Inject internal constructor(
}

fun selectSymbol(symbol: Symbol) {
queryInput = ""
selectedSymbols =
selectedSymbols.plus(SymbolItem(id = selectedSymbols.size, symbol = symbol))
}
Expand All @@ -103,42 +105,59 @@ 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(query: String? = null) {
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)
/**
* In case of DisplayMode.SYMBOL and DisplayMode.CATEGORY,
* if getEntries() is called by setQuery(),
* retrieve both symbols and categories from the repository
*/
DisplayMode.SYMBOL -> {
if (query != null) // called from setQuery()
repository.getEntries(query).collect { entries ->
_entries.postValue(entries)
}
else // called from somewhere else
repository.getSymbols(queryInput).collect { symbols ->
_entries.postValue(symbols)
}
}
}

DisplayMode.CATEGORY -> {
repository.getCategories(queryInput).collect { categories ->
_entries.postValue(categories)
DisplayMode.CATEGORY -> {
if (query != null)
repository.getEntries(query).collect { entries ->
_entries.postValue(entries)
}
else
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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ class SignupViewModelTest {
assertEquals(false, viewModel.uiState.value.isValidPassword)
assertEquals(false, viewModel.uiState.value.isValidEmail)
assertEquals(SignupErrorType.PASSWORD, viewModel.uiState.value.error?.type)
assertEquals(R.string.false_new_password, viewModel.uiState.value.error?.messageId)
assertEquals(R.string.password_qualification, viewModel.uiState.value.error?.messageId)
}

@Test
Expand All @@ -448,7 +448,7 @@ class SignupViewModelTest {
assertEquals(false, viewModel.uiState.value.isValidPassword)
assertEquals(false, viewModel.uiState.value.isValidEmail)
assertEquals(SignupErrorType.PASSWORD, viewModel.uiState.value.error?.type)
assertEquals(R.string.false_new_password, viewModel.uiState.value.error?.messageId)
assertEquals(R.string.password_qualification, viewModel.uiState.value.error?.messageId)
}

@Test
Expand Down

0 comments on commit cef1ff2

Please sign in to comment.