Remove public MutableLiveData in steup UI

This commit is contained in:
iovxw 2026-07-29 17:40:58 +08:00
parent 048f581c65
commit 4cc1475784
No known key found for this signature in database
GPG Key ID: C84626C2D3D79995
3 changed files with 23 additions and 60 deletions

View File

@ -13,7 +13,6 @@ import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.core.app.NotificationCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
@ -31,8 +30,6 @@ class SetupActivity : FragmentActivity() {
private lateinit var viewPager: ViewPager2
private val viewModel: SetupViewModel by viewModels()
private lateinit var skipButton: Button
private lateinit var prevButton: Button
private lateinit var nextButton: Button
@ -65,39 +62,34 @@ class SetupActivity : FragmentActivity() {
viewPager = binding.viewpager.apply {
adapter = Adapter()
registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
// manually call following observer when page changed
// intentionally before changing the text of nextButton
viewModel.isAllDone.value = viewModel.isAllDone.value
// hide prev button for the first page
prevButton.visibility = if (position != 0) View.VISIBLE else View.GONE
nextButton.text =
getString(if (position.isLastPage()) R.string.done else R.string.next)
}
override fun onPageSelected(position: Int) = updateButtons()
})
}
viewModel.isAllDone.observe(this) { allDone ->
skipButton.apply {
visibility = if (allDone) View.GONE else View.VISIBLE
}
nextButton.apply {
// hide next button for the last page when allDone == false
(allDone || !viewPager.currentItem.isLastPage()).let {
visibility = if (it) View.VISIBLE else View.GONE
}
}
}
// skip to undone page
firstUndonePage()?.let { viewPager.currentItem = it.ordinal }
updateButtons()
shown = true
createNotificationChannel()
}
private fun updateButtons() {
val allDone = !SetupPage.hasUndonePage()
val isFirstPage = viewPager.currentItem == 0
val isLastPage = viewPager.currentItem.isLastPage()
prevButton.visibility = if (isFirstPage) View.GONE else View.VISIBLE
skipButton.visibility = if (allDone) View.GONE else View.VISIBLE
nextButton.text = getString(if (isLastPage) R.string.done else R.string.next)
nextButton.visibility = if (isLastPage && !allDone) View.GONE else View.VISIBLE
}
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (!hasFocus) return
supportFragmentManager.fragments.forEach {
if (it.isVisible) (it as SetupFragment).sync()
}
updateButtons()
}
private fun createNotificationChannel() {

View File

@ -9,34 +9,15 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import org.fcitx.fcitx5.android.databinding.FragmentSetupBinding
import org.fcitx.fcitx5.android.ui.setup.SetupPage.Companion.isLastPage
import org.fcitx.fcitx5.android.utils.serializable
class SetupFragment : Fragment() {
private val viewModel: SetupViewModel by activityViewModels()
private lateinit var binding: FragmentSetupBinding
private val page: SetupPage by lazy { requireArguments().serializable(PAGE)!! }
private var isDone: Boolean = false
set(value) {
if (value && page.isLastPage()) {
viewModel.isAllDone.value = true
}
with(binding) {
hintText.text = page.getHintText(requireContext())
actionButton.visibility = if (value) View.GONE else View.VISIBLE
actionButton.text = page.getButtonText(requireContext())
actionButton.setOnClickListener { page.getButtonAction(requireContext()) }
doneText.visibility = if (value) View.VISIBLE else View.GONE
}
field = value
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@ -49,12 +30,14 @@ class SetupFragment : Fragment() {
// called on window focus changed
fun sync() {
isDone = page.isDone()
}
override fun onResume() {
super.onResume()
sync()
val done = page.isDone()
with(binding) {
hintText.text = page.getHintText(requireContext())
actionButton.visibility = if (done) View.GONE else View.VISIBLE
actionButton.text = page.getButtonText(requireContext())
actionButton.setOnClickListener { page.getButtonAction(requireContext()) }
doneText.visibility = if (done) View.VISIBLE else View.GONE
}
}
companion object {

View File

@ -1,12 +0,0 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* SPDX-FileCopyrightText: Copyright 2021-2023 Fcitx5 for Android Contributors
*/
package org.fcitx.fcitx5.android.ui.setup
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class SetupViewModel : ViewModel() {
val isAllDone = MutableLiveData(false)
}