-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from willowtreeapps/EditCategoryPhrasesViewModel
Move functionality of EditCategoryPhrasesFragment from EditCategoriesViewModel into a new EditCategoryPhrasesViewModel
- Loading branch information
Showing
8 changed files
with
135 additions
and
48 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
app/src/androidTest/java/com/willowtree/vocable/settings/EditCategoryPhrasesViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.willowtree.vocable.settings | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import com.willowtree.vocable.MainDispatcherRule | ||
import com.willowtree.vocable.presets.Category | ||
import com.willowtree.vocable.presets.PresetCategories | ||
import com.willowtree.vocable.presets.PresetPhrase | ||
import com.willowtree.vocable.room.PresetPhrasesRepository | ||
import com.willowtree.vocable.utility.VocableKoinTestRule | ||
import junit.framework.TestCase.assertEquals | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.get | ||
|
||
class EditCategoryPhrasesViewModelTest: KoinComponent { | ||
|
||
@get:Rule | ||
val koinTestRule = VocableKoinTestRule() | ||
|
||
@get:Rule | ||
val mainDispatcherRule = MainDispatcherRule() | ||
|
||
@get:Rule | ||
val instantTaskExecutorRule = InstantTaskExecutorRule() | ||
|
||
private lateinit var viewModel: EditCategoryPhrasesViewModel | ||
|
||
@Before | ||
fun setUp() { | ||
val presetPhrasesRepository: PresetPhrasesRepository = get() | ||
runBlocking { | ||
presetPhrasesRepository.populateDatabase() | ||
} | ||
viewModel = get() | ||
} | ||
|
||
@Test | ||
fun getCategoryName_withRecents_returnsRecents() { | ||
val result = viewModel.getCategoryName(Category.Recents(false, 0)) | ||
assertEquals("Recents", result) | ||
} | ||
|
||
@Test | ||
fun getCategoryName_withPresetCategory_returnsCategoryName() { | ||
val result = viewModel.getCategoryName(Category.PresetCategory(PresetCategories.GENERAL.id, 0, false)) | ||
assertEquals("General", result) | ||
} | ||
|
||
@Test | ||
fun fetchCategoryPhrases_withGeneral_returnsCorrectPhraseList() { | ||
viewModel.fetchCategoryPhrases(Category.PresetCategory(PresetCategories.GENERAL.id, 0, false)) | ||
viewModel.categoryPhraseList.observeForever { | ||
assertEquals(9, it.size) | ||
assertEquals("preset_please", it[0].phraseId) | ||
} | ||
} | ||
|
||
@Test | ||
fun deletePhraseFromCategory_withPlease_deletesPleaseCorrectly() { | ||
viewModel.deletePhraseFromCategory( | ||
phrase = PresetPhrase( | ||
phraseId = "preset_please", | ||
sortOrder = 0, | ||
lastSpokenDate = 0, | ||
deleted = false, | ||
parentCategoryId = PresetCategories.GENERAL.id | ||
), | ||
category = Category.PresetCategory( | ||
categoryId = PresetCategories.GENERAL.id, | ||
sortOrder = 0, | ||
hidden = false | ||
) | ||
) | ||
|
||
viewModel.categoryPhraseList.observeForever { | ||
assertEquals(8, it.size) | ||
assertEquals("preset_thank_you", it[0].phraseId) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/willowtree/vocable/settings/EditCategoryPhrasesViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.willowtree.vocable.settings | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
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( | ||
private val phrasesUseCase: IPhrasesUseCase, | ||
private val localizedResourceUtility: ILocalizedResourceUtility | ||
): ViewModel() { | ||
private val liveCategoryPhraseList = MutableLiveData<List<Phrase>>() | ||
val categoryPhraseList: LiveData<List<Phrase>> = liveCategoryPhraseList | ||
|
||
fun getCategoryName(category: Category): String { | ||
return localizedResourceUtility.getTextFromCategory(category) | ||
} | ||
|
||
fun fetchCategoryPhrases(category: Category) { | ||
viewModelScope.launch { | ||
val phrasesForCategory = phrasesUseCase.getPhrasesForCategory(category.categoryId) | ||
.sortedBy { it.sortOrder } | ||
liveCategoryPhraseList.postValue(phrasesForCategory) | ||
} | ||
} | ||
|
||
fun deletePhraseFromCategory(phrase: Phrase, category: Category) { | ||
viewModelScope.launch { | ||
|
||
phrasesUseCase.deletePhrase(phrase.phraseId) | ||
|
||
// Refresh phrase list | ||
fetchCategoryPhrases(category) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters