Skip to content

Commit

Permalink
Merge pull request #558 from willowtreeapps/chore/508-abstract-sort-h…
Browse files Browse the repository at this point in the history
…andling-3

Chore/508 abstract sort handling 3
  • Loading branch information
PaulKlauser authored Jun 4, 2024
2 parents a3810dd + d17dbf8 commit badf604
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 107 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
testImplementation "androidx.arch.core:core-testing:2.2.0"
testImplementation(libs.turbine)
androidTestImplementation "androidx.room:room-testing:2.2.5"
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand All @@ -123,6 +124,7 @@ dependencies {
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation "androidx.arch.core:core-testing:2.2.0"
androidTestImplementation(libs.turbine)

// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:2.3.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.willowtree.vocable.BaseFragment
Expand All @@ -13,6 +16,7 @@ import com.willowtree.vocable.R
import com.willowtree.vocable.databinding.FragmentEditCategoriesBinding
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.utils.VocableFragmentStateAdapter
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ViewModelOwner
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.math.min
Expand Down Expand Up @@ -107,9 +111,13 @@ class EditCategoriesFragment : BaseFragment<FragmentEditCategoriesBinding>() {
}

private fun subscribeToViewModel() {
editCategoriesViewModel.categoryList.observe(viewLifecycleOwner) {
it?.let { categories ->
categoriesAdapter.setItems(categories)
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
editCategoriesViewModel.categoryList.collect {
it.let { categories ->
categoriesAdapter.setItems(categories)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.willowtree.vocable.BaseFragment
import com.willowtree.vocable.BindingInflater
Expand All @@ -12,6 +15,7 @@ import com.willowtree.vocable.databinding.CategoryEditButtonBinding
import com.willowtree.vocable.databinding.FragmentEditCategoriesListBinding
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.utils.locale.LocalizedResourceUtility
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ViewModelOwner
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down Expand Up @@ -101,22 +105,26 @@ class EditCategoriesListFragment : BaseFragment<FragmentEditCategoriesListBindin
}

private fun subscribeToViewModel() {
editCategoriesViewModel.categoryList.observe(viewLifecycleOwner) { list ->
list?.let { overallList ->
val hiddenCategories = overallList.filter { it.hidden }
if (endPosition > overallList.size) {
endPosition = overallList.size - 1
}
if (startPosition <= endPosition) {
overallList.subList(startPosition, endPosition)
.forEachIndexed { index, category ->
bindCategoryEditButton(
editButtonList[index],
category,
startPosition + index,
overallList.size - hiddenCategories.size
)
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
editCategoriesViewModel.categoryList.collect { list ->
list.let { overallList ->
val hiddenCategories = overallList.filter { it.hidden }
if (endPosition > overallList.size) {
endPosition = overallList.size - 1
}
if (startPosition <= endPosition) {
overallList.subList(startPosition, endPosition)
.forEachIndexed { index, category ->
bindCategoryEditButton(
editButtonList[index],
category,
startPosition + index,
overallList.size - hiddenCategories.size
)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@ package com.willowtree.vocable.settings
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
import com.willowtree.vocable.ICategoriesUseCase
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.room.CategorySortOrder
import com.willowtree.vocable.settings.editcategories.EditCategoriesPage
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

class EditCategoriesViewModel(
private val categoriesUseCase: ICategoriesUseCase
) : ViewModel() {

val categoryList: LiveData<List<Category>> = categoriesUseCase.categories().asLiveData()

val categoryPages = categoriesUseCase.categories().map { categories ->
val pageSize = 8
categories.chunked(pageSize).map { EditCategoriesPage(it) }
}
val categoryList = categoriesUseCase.categories()

private val liveLastViewedIndex = MutableLiveData<Int>()
val lastViewedIndex: LiveData<Int> = liveLastViewedIndex
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.willowtree.vocable.settings

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import app.cash.turbine.test
import com.willowtree.vocable.FakeCategoriesUseCase
import com.willowtree.vocable.MainDispatcherRule
import com.willowtree.vocable.getOrAwaitValue
import com.willowtree.vocable.presets.createStoredCategory
import com.willowtree.vocable.settings.editcategories.EditCategoriesPage
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
Expand All @@ -18,9 +15,6 @@ class EditCategoriesViewModelTest {
@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

private val categoriesUseCase = FakeCategoriesUseCase()

private fun createViewModel(): EditCategoriesViewModel {
Expand All @@ -30,7 +24,7 @@ class EditCategoriesViewModelTest {
}

@Test
fun `categories are populated`() {
fun `categories are populated`() = runTest {
categoriesUseCase._categories.update {
listOf(
createStoredCategory(categoryId = "1")
Expand All @@ -39,16 +33,18 @@ class EditCategoriesViewModelTest {
val vm = createViewModel()
vm.refreshCategories()

assertEquals(
listOf(
createStoredCategory(categoryId = "1")
),
vm.categoryList.getOrAwaitValue()
)
vm.categoryList.test {
assertEquals(
listOf(
createStoredCategory(categoryId = "1")
),
awaitItem()
)
}
}

@Test
fun `move category up`() {
fun `move category up`() = runTest {
categoriesUseCase._categories.update {
listOf(
createStoredCategory(
Expand All @@ -65,23 +61,25 @@ class EditCategoriesViewModelTest {
vm.refreshCategories()
vm.moveCategoryUp("2")

assertEquals(
listOf(
createStoredCategory(
categoryId = "2",
sortOrder = 0
vm.categoryList.test {
assertEquals(
listOf(
createStoredCategory(
categoryId = "2",
sortOrder = 0
),
createStoredCategory(
categoryId = "1",
sortOrder = 1
)
),
createStoredCategory(
categoryId = "1",
sortOrder = 1
)
),
vm.categoryList.getOrAwaitValue()
)
awaitItem()
)
}
}

@Test
fun `move category down`() {
fun `move category down`() = runTest {
categoriesUseCase._categories.update {
listOf(
createStoredCategory(
Expand All @@ -98,61 +96,21 @@ class EditCategoriesViewModelTest {
vm.refreshCategories()
vm.moveCategoryDown("1")

assertEquals(
listOf(
createStoredCategory(
categoryId = "2",
sortOrder = 0
vm.categoryList.test {
assertEquals(
listOf(
createStoredCategory(
categoryId = "2",
sortOrder = 0
),
createStoredCategory(
categoryId = "1",
sortOrder = 1
)
),
createStoredCategory(
categoryId = "1",
sortOrder = 1
)
),
vm.categoryList.getOrAwaitValue()
)
}

@Test
fun `category pages are populated`() = runTest {
categoriesUseCase._categories.update {
listOf(
createStoredCategory(categoryId = "1"),
createStoredCategory(categoryId = "2"),
createStoredCategory(categoryId = "3"),
createStoredCategory(categoryId = "4"),
createStoredCategory(categoryId = "5"),
createStoredCategory(categoryId = "6"),
createStoredCategory(categoryId = "7"),
createStoredCategory(categoryId = "8"),
createStoredCategory(categoryId = "9"),
awaitItem()
)
}
val vm = createViewModel()
vm.refreshCategories()

assertEquals(
listOf(
EditCategoriesPage(
listOf(
createStoredCategory(categoryId = "1"),
createStoredCategory(categoryId = "2"),
createStoredCategory(categoryId = "3"),
createStoredCategory(categoryId = "4"),
createStoredCategory(categoryId = "5"),
createStoredCategory(categoryId = "6"),
createStoredCategory(categoryId = "7"),
createStoredCategory(categoryId = "8"),
)
),
EditCategoriesPage(
listOf(
createStoredCategory(categoryId = "9"),
)
)
),
vm.categoryPages.first()
)
}

}
5 changes: 5 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[versions]
turbine = "1.1.0"

[libraries]
turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" }

0 comments on commit badf604

Please sign in to comment.