Skip to content

Commit

Permalink
Merge pull request #525 from willowtreeapps/refactor/edit-category-ph…
Browse files Browse the repository at this point in the history
…rases

Issue 416: Migrate EditCategoryPhrasesFragment to use VocableFragmentStateAdapter
  • Loading branch information
josmith42 authored Apr 12, 2024
2 parents a57ac68 + ee18c86 commit a428744
Showing 1 changed file with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.Phrase
import com.willowtree.vocable.presets.PresetCategories
import com.willowtree.vocable.settings.customcategories.CustomCategoryPhraseListFragment
import com.willowtree.vocable.utils.LegacyFragmentStateAdapter
import com.willowtree.vocable.utils.VocableFragmentStateAdapter
import org.koin.androidx.viewmodel.ViewModelOwner
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand Down Expand Up @@ -104,17 +104,16 @@ class EditCategoryPhrasesFragment : BaseFragment<FragmentEditCategoryPhrasesBind
}
}

binding.editCategoryPhraseHolder.registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
val pageNum = position % phrasesAdapter.numPages + 1
binding.editCategoryPageNumber.text = getString(
R.string.phrases_page_number,
pageNum,
phrasesAdapter.numPages
)
}
})
binding.editCategoryPhraseHolder.apply {
isSaveEnabled = false
adapter = phrasesAdapter
registerOnPageChangeCallback(object :
ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
post { updatePageNumber() }
}
})
}

subscribeToViewModel()

Expand Down Expand Up @@ -144,34 +143,27 @@ class EditCategoryPhrasesFragment : BaseFragment<FragmentEditCategoryPhrasesBind
binding.editCategoryPageNumber.isVisible = phrases.isNotEmpty()

if (phrases.isNotEmpty()) {
with(binding.editCategoryPhraseHolder) {
isSaveEnabled = false

adapter = phrasesAdapter

phrasesAdapter.setItems(phrases)

// Move adapter to middle so user can scroll both directions
val middle = phrasesAdapter.itemCount / 2
if (middle % phrasesAdapter.numPages == 0) {
setCurrentItem(middle, false)
} else {
val mod = middle % phrasesAdapter.numPages
setCurrentItem(
middle + (phrasesAdapter.numPages - mod),
false
)
}
}
phrasesAdapter.setItems(phrases)
}
}

private fun updatePageNumber() {
val currentPage = binding.editCategoryPhraseHolder.currentItem
val pageNum = currentPage % phrasesAdapter.numPages + 1
binding.editCategoryPageNumber.text = getString(
R.string.phrases_page_number,
pageNum,
phrasesAdapter.numPages
)
}

inner class PhrasesPagerAdapter(fm: FragmentManager) :
LegacyFragmentStateAdapter<Phrase>(fm, viewLifecycleOwner.lifecycle) {
VocableFragmentStateAdapter<Phrase>(fm, viewLifecycleOwner.lifecycle) {

override fun setItems(items: List<Phrase>) {
super.setItems(items)
setPagingButtonsEnabled(numPages > 1)
updatePageNumber()
}

private fun setPagingButtonsEnabled(enable: Boolean) {
Expand Down

0 comments on commit a428744

Please sign in to comment.