Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CategorySelectionViewModel to remove Redundant code #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ captures/
.idea/sonarlint
.idea/misc.xml
.idea/deploymentTargetDropDown.xml
.idea/deploymentTargetSelector.xml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,27 @@ class CategorySelectionViewModel @Inject constructor(
private val _categories = MutableStateFlow<List<Category>>(emptyList())
val categories = _categories.asStateFlow()

private val _selectedCategory = MutableStateFlow<List<Category>>(emptyList())
val selectedCategories = _selectedCategory.asStateFlow()
private val _selectedCategories = MutableStateFlow<List<Category>>(emptyList())
val selectedCategories = _selectedCategories.asStateFlow()

init {
getCategoriesUseCase.invoke().map { categories ->
_categories.value = categories
if (_selectedCategory.value.isEmpty()) {
_selectedCategory.value = categories
}
}.launchIn(viewModelScope)
getCategoriesUseCase.invoke()
.map { categories ->
_categories.value = categories
if (_selectedCategories.value.isEmpty()) {
_selectedCategories.value = categories
}
}
.launchIn(viewModelScope)
}

fun clearChanges() {
_selectedCategory.value = emptyList()
_selectedCategories.value = emptyList()
}

fun selectThisCategory(category: Category, selected: Boolean) {
viewModelScope.launch {
val selectedCategories = _selectedCategory.value.toMutableList()
val selectedCategories = _selectedCategories.value.toMutableList()

val selectedCategory = selectedCategories.firstOrNull {
category.id == it.id
Expand All @@ -54,33 +56,17 @@ class CategorySelectionViewModel @Inject constructor(
}
}

_selectedCategory.value = selectedCategories
_selectedCategories.value = selectedCategories
}
}

fun selectAllThisCategory(categories: List<Category>) {
if (categories.isEmpty()) {
return
}
fun selectAllTheseCategories(categories: List<Category>) {
categories.ifEmpty { return }

viewModelScope.launch {
clearChanges()

val selectedCategories = _selectedCategory.value.toMutableList()

repeat(categories.size) {
val category = categories[it]

val selectedCategory = selectedCategories.firstOrNull {
category.id == it.id
}

if (selectedCategory == null) {
selectedCategories.add(category)
}
}

_selectedCategory.value = selectedCategories
_selectedCategories.value = categories
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fun MultipleCategoriesSelectionScreen(
selectedCategories: List<Category> = emptyList(),
onItemSelection: ((List<Category>, Boolean) -> Unit)? = null,
) {
viewModel.selectAllThisCategory(selectedCategories)
viewModel.selectAllTheseCategories(selectedCategories)

CategorySelectionView(viewModel, onItemSelection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CategorySelectionViewModelTest : BaseCoroutineTest() {
Truth.assertThat(firstItem).isNotEmpty()
Truth.assertThat(firstItem).hasSize(5)

categorySelectionViewModel.selectAllThisCategory(getRandomCategoryData(3))
categorySelectionViewModel.selectAllTheseCategories(getRandomCategoryData(3))

val secondItem = awaitItem()
Truth.assertThat(secondItem).isEmpty()
Expand All @@ -117,7 +117,7 @@ class CategorySelectionViewModelTest : BaseCoroutineTest() {
Truth.assertThat(firstItem).isNotEmpty()
Truth.assertThat(firstItem).hasSize(5)

categorySelectionViewModel.selectAllThisCategory(emptyList())
categorySelectionViewModel.selectAllTheseCategories(emptyList())
}
}
}
Loading