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

Only scrolls to a newly added Category after immediately adding it (in Settings) #564

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
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import com.willowtree.vocable.room.RoomStoredPhrasesRepository
import com.willowtree.vocable.room.VocableDatabase
import com.willowtree.vocable.utility.FakeDateProvider
import com.willowtree.vocable.utility.StubLegacyCategoriesAndPhrasesRepository
import com.willowtree.vocable.utility.VocableKoinTestRule
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

class EditCategoriesViewModelTest {

@get:Rule
val koinTestRule = VocableKoinTestRule()

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

Expand Down Expand Up @@ -70,6 +74,70 @@ class EditCategoriesViewModelTest {
)
}

// TODO: CC - the following 4 tests are flaky due to the way refreshCategories() is implemented
@Test
fun refreshing_categories_without_adding_new_category_remains_at_first_index() = runTest {
val vm = createViewModel()
vm.refreshCategories()

vm.liveLastViewedIndex.test {
assertEquals(0, awaitItem())
}
}

@Test
fun adding_new_category_and_refreshing_once_flips_to_new_categorys_index() = runTest {

val vm = createViewModel()
vm.refreshCategories()

categoriesUseCase.addCategory("new category")
vm.refreshCategories()

vm.liveLastViewedIndex.test {
assertEquals(0, awaitItem())
assertEquals(7, awaitItem())
}
}

@Test
fun adding_new_category_and_refreshing_twice_flips_to_first_index() = runTest {
val vm = createViewModel()
vm.refreshCategories()

categoriesUseCase.addCategory("new category")

vm.refreshCategories()
vm.liveLastViewedIndex.test {
assertEquals(0, awaitItem())
assertEquals(7, awaitItem())
}

vm.refreshCategories()
vm.liveLastViewedIndex.test {
assertEquals(7, awaitItem())
assertEquals(0, awaitItem())
}
}

@Test
fun adding_new_category_amongst_hidden_categories_and_refreshing_once_flips_to_last_non_hidden_index() = runTest {
val vm = createViewModel()
categoriesUseCase.updateCategoryHidden("preset_general", hidden = true)
categoriesUseCase.updateCategoryHidden("preset_basic_needs", hidden = true)

vm.refreshCategories()

categoriesUseCase.addCategory("new category")

vm.refreshCategories()
vm.liveLastViewedIndex.test {
assertEquals(0, awaitItem())
assertEquals(5, awaitItem())
}

}

@Test
fun categories_are_populated() = runTest {
val vm = createViewModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.willowtree.vocable.BaseFragment
Expand All @@ -16,6 +17,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.flow.first
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ViewModelOwner
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down Expand Up @@ -121,15 +123,12 @@ class EditCategoriesFragment : BaseFragment<FragmentEditCategoriesBinding>() {
}
}

editCategoriesViewModel.lastViewedIndex.observe(viewLifecycleOwner) {
it?.let { index ->
val pageNum = index / maxEditCategories
// Wait until view pager has finished its layout
binding.editCategoriesViewPager.post {
editCategoriesViewModel.viewModelScope.launch {
editCategoriesViewModel.liveLastViewedIndex.collect {
val pageNum = it/maxEditCategories
if (isAdded && binding.editCategoriesViewPager.currentItem != pageNum) {
binding.editCategoriesViewPager.setCurrentItem(pageNum, false)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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.ICategoriesUseCase
import com.willowtree.vocable.presets.Category
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class EditCategoriesViewModel(
Expand All @@ -15,8 +15,7 @@ class EditCategoriesViewModel(

val categoryList = categoriesUseCase.categories()

private val liveLastViewedIndex = MutableLiveData<Int>()
val lastViewedIndex: LiveData<Int> = liveLastViewedIndex
val liveLastViewedIndex = MutableStateFlow(0)

private var overallCategories = listOf<Category>()

Expand All @@ -27,22 +26,25 @@ class EditCategoriesViewModel(

overallCategories = categoriesUseCase.categories().first()

// Check if a new category was added and scroll to it
// Check if a new category was added and scroll to it only immediately after added
if (oldCategories.isNotEmpty() && oldCategories.size < overallCategories.size) {
when (val firstHiddenIndex = overallCategories.indexOfFirst { it.hidden }) {
-1 -> {
liveLastViewedIndex.postValue(overallCategories.size - 1)
liveLastViewedIndex.update { overallCategories.size - 1 }
}

0 -> {
liveLastViewedIndex.postValue(0)
liveLastViewedIndex.update { 0 }
}

else -> {
liveLastViewedIndex.postValue(firstHiddenIndex - 1)
liveLastViewedIndex.update { firstHiddenIndex - 1 }
}
}
}
else {
liveLastViewedIndex.update { 0 }
}
}
}

Expand Down
Loading