Use kotlin flow (welcome activity)
This commit is contained in:
parent
007668fa97
commit
7974a92c69
6 changed files with 70 additions and 51 deletions
|
|
@ -53,7 +53,8 @@ class ToDoViewModel : ViewModel() {
|
|||
fun updateTask(position: Int, todo: ToDoRoom) {
|
||||
todoList.removeAt(position)
|
||||
todoList.add(
|
||||
position, ToDo(
|
||||
position,
|
||||
ToDo(
|
||||
todo.uuid,
|
||||
todo.state,
|
||||
todo.content,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package cn.super12138.todo.views.welcome
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
|
|
@ -8,7 +7,9 @@ import androidx.activity.viewModels
|
|||
import androidx.appcompat.content.res.AppCompatResources
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.commit
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import cn.super12138.todo.R
|
||||
import cn.super12138.todo.constant.GlobalValues
|
||||
import cn.super12138.todo.databinding.ActivityWelcomeBinding
|
||||
|
|
@ -20,11 +21,11 @@ import cn.super12138.todo.views.welcome.pages.IntroPage
|
|||
import cn.super12138.todo.views.welcome.pages.ProgressPage
|
||||
import cn.super12138.todo.views.welcome.pages.ToDoBtnPage
|
||||
import cn.super12138.todo.views.welcome.pages.ToDoItemPage
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
|
||||
private val viewModel by viewModels<WelcomeViewModel>()
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
|
@ -47,7 +48,7 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
|
|||
val intent = Intent(this, MainActivity::class.java)
|
||||
startActivity(intent)
|
||||
} else {
|
||||
currentPage.value = 1
|
||||
viewModel.setCurrentPage(1)
|
||||
nextPage(1)
|
||||
}
|
||||
}
|
||||
|
|
@ -57,58 +58,62 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
|
|||
|
||||
supportFragmentManager.popBackStack()
|
||||
|
||||
currentPage.value = currentPage.value?.minus(1)
|
||||
viewModel.decreasePage()
|
||||
}
|
||||
|
||||
nextBtn.setOnIntervalClickListener {
|
||||
VibrationUtils.performHapticFeedback(it)
|
||||
|
||||
nextPage(currentPage.value!!.plus(1))
|
||||
nextPage(currentPage.value + 1)
|
||||
|
||||
currentPage.value = currentPage.value?.plus(1)
|
||||
viewModel.increasePage()
|
||||
}
|
||||
|
||||
currentPage.observe(this, Observer { page ->
|
||||
when (page) {
|
||||
0 -> {
|
||||
centerBtn.apply {
|
||||
text = getString(R.string.start)
|
||||
icon = AppCompatResources.getDrawable(
|
||||
this@WelcomeActivity,
|
||||
R.drawable.ic_arrow_forward
|
||||
)
|
||||
show()
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.currentPage.collect { page ->
|
||||
when (page) {
|
||||
0 -> {
|
||||
centerBtn.apply {
|
||||
text = getString(R.string.start)
|
||||
icon = AppCompatResources.getDrawable(
|
||||
this@WelcomeActivity,
|
||||
R.drawable.ic_arrow_forward
|
||||
)
|
||||
show()
|
||||
}
|
||||
nextBtn.hide()
|
||||
previousBtn.hide()
|
||||
}
|
||||
|
||||
in 1..2 -> {
|
||||
centerBtn.hide()
|
||||
previousBtn.show()
|
||||
nextBtn.show()
|
||||
}
|
||||
|
||||
3 -> {
|
||||
centerBtn.apply {
|
||||
text = getString(R.string.enter_app)
|
||||
icon = AppCompatResources.getDrawable(
|
||||
this@WelcomeActivity,
|
||||
R.drawable.ic_focus
|
||||
)
|
||||
show()
|
||||
}
|
||||
previousBtn.show()
|
||||
nextBtn.hide()
|
||||
}
|
||||
|
||||
else -> {
|
||||
if (page > 3) viewModel.setCurrentPage(3)
|
||||
|
||||
if (page < 0) viewModel.setCurrentPage(0)
|
||||
}
|
||||
}
|
||||
nextBtn.hide()
|
||||
previousBtn.hide()
|
||||
}
|
||||
|
||||
in 1..2 -> {
|
||||
centerBtn.hide()
|
||||
previousBtn.show()
|
||||
nextBtn.show()
|
||||
}
|
||||
|
||||
3 -> {
|
||||
centerBtn.apply {
|
||||
text = getString(R.string.enter_app)
|
||||
icon = AppCompatResources.getDrawable(
|
||||
this@WelcomeActivity,
|
||||
R.drawable.ic_focus
|
||||
)
|
||||
show()
|
||||
}
|
||||
previousBtn.show()
|
||||
nextBtn.hide()
|
||||
}
|
||||
|
||||
else -> {
|
||||
if (page > 3) currentPage.value = 3
|
||||
|
||||
if (page < 0) currentPage.value = 0
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
|
||||
override fun handleOnBackPressed() {
|
||||
|
|
@ -116,7 +121,7 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
|
|||
finish()
|
||||
} else {
|
||||
supportFragmentManager.popBackStack()
|
||||
currentPage.value = currentPage.value?.minus(1)
|
||||
viewModel.decreasePage()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,8 +1,22 @@
|
|||
package cn.super12138.todo.views.welcome
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
||||
class WelcomeViewModel : ViewModel() {
|
||||
val currentPage = MutableLiveData<Int>(0)
|
||||
private val _currentPage = MutableStateFlow(0)
|
||||
val currentPage = _currentPage.asStateFlow()
|
||||
|
||||
fun setCurrentPage(page: Int) {
|
||||
_currentPage.value = page
|
||||
}
|
||||
|
||||
fun increasePage() {
|
||||
_currentPage.value += 1
|
||||
}
|
||||
|
||||
fun decreasePage() {
|
||||
_currentPage.value -= 1
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="70dp"
|
||||
android:height="70dp"
|
||||
android:tint="?android:attr/textColorPrimary"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@
|
|||
<item name="colorSurfaceContainerHigh">@color/md_theme_surfaceContainerHigh</item>
|
||||
<item name="colorSurfaceContainerHighest">@color/md_theme_surfaceContainerHighest</item>
|
||||
|
||||
|
||||
<item name="android:textColorHighlight">@color/md_theme_secondary</item>
|
||||
<item name="preferenceTheme">@style/AppPreferenceThemeOverlay</item>
|
||||
<item name="alertDialogTheme">@style/ToDo.M3AlertDialog</item>
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@
|
|||
</style>
|
||||
|
||||
<style name="Theme.ToDo" parent="Base.Theme.ToDo">
|
||||
<!-- 透明状态栏 -->
|
||||
<item name="android:navigationBarColor">@android:color/transparent</item>
|
||||
<item name="android:windowTranslucentNavigation">true</item>
|
||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||
|
|
|
|||
Loading…
Reference in a new issue