Skip to content

Commit

Permalink
Add welcome page
Browse files Browse the repository at this point in the history
  • Loading branch information
Super12138 committed Aug 15, 2024
1 parent 231e687 commit 7b05b2e
Show file tree
Hide file tree
Showing 34 changed files with 718 additions and 33 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
android:exported="false"
android:label="@string/about_label"
android:launchMode="singleTask" />
<activity
android:name=".views.welcome.WelcomeActivity"
android:exported="false"
android:label="@string/welcome_label"
android:launchMode="singleTask" />
<activity
android:name=".views.crash.CrashActivity"
android:exported="false" />
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/ToDoApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package cn.super12138.todo
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.Intent
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.logic.dao.ToDoRoomDB
import cn.super12138.todo.views.crash.CrashHandler
import cn.super12138.todo.views.welcome.WelcomeActivity
import com.google.android.material.color.DynamicColors

class ToDoApp : Application() {
Expand All @@ -25,5 +28,12 @@ class ToDoApp : Application() {
Thread.setDefaultUncaughtExceptionHandler(crashHandler)

db = database

if (!GlobalValues.welcomePage) {
val intent = Intent(this, WelcomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
startActivity(intent)
}
}
}
6 changes: 5 additions & 1 deletion app/src/main/kotlin/cn/super12138/todo/constant/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.super12138.todo.constant

object Constants {
const val WELCOME_PAGE= "welcome_page"

const val AUTHOR_GITHUB_URL = "https://github.com/Super12138/"
const val REPO_GITHUB_URL = "https://github.com/Super12138/ToDo"
const val UPDATE_URL = "https://github.com/Super12138/ToDo/releases"
Expand All @@ -11,12 +13,14 @@ object Constants {
const val PREF_SECURE_MODE = "secure_mode"
const val PREF_HAPTIC_FEEDBACK = "haptic_feedback"
const val PREF_ALL_TASKS = "all_tasks"
const val PREF_REENTER_WELCOME_ACTIVITY = "reenter_welcome_activity"
const val PREF_ABOUT = "about"
const val PREF_DEV_MODE = "dev_mode"
const val PREF_SPRING_FESTIVAL_THEME = "spring_festival_theme"
// const val PREF_SPRING_FESTIVAL_THEME = "spring_festival_theme"
const val PREF_BACKUP_DB = "backup_db"
const val PREF_RESTORE_DB = "restore_db"


const val STRING_DEV_MODE = "/DEV_MODE"

const val TAG_INFO_BOTTOM_SHEET = "InfoBottomSheet"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cn.super12138.todo.constant
import cn.super12138.todo.utils.sp.SPDelegates

object GlobalValues {
var welcomePage: Boolean by SPDelegates(Constants.WELCOME_PAGE, false)
var darkMode: String by SPDelegates(Constants.PREF_DARK_MODE, "0")
var devMode: Boolean by SPDelegates(Constants.PREF_DEV_MODE, false)
var springFestivalTheme: Boolean by SPDelegates(Constants.PREF_SPRING_FESTIVAL_THEME, false)
// var springFestivalTheme: Boolean by SPDelegates(Constants.PREF_SPRING_FESTIVAL_THEME, false)
var secureMode: Boolean by SPDelegates(Constants.PREF_SECURE_MODE, false)
var hapticFeedback: Boolean by SPDelegates(Constants.PREF_HAPTIC_FEEDBACK, true)
}
16 changes: 0 additions & 16 deletions app/src/main/kotlin/cn/super12138/todo/utils/Toast.kt

This file was deleted.

36 changes: 36 additions & 0 deletions app/src/main/kotlin/cn/super12138/todo/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.super12138.todo.utils

import android.content.Context
import android.os.SystemClock
import android.view.View
import android.widget.Toast
import cn.super12138.todo.ToDoApp

private var clickInterval = 400L
private var lastTime = 0L

/**
* 延迟点击
*/
fun View.setOnIntervalClickListener(onIntervalClickListener: (View) -> Unit) {
this.setOnClickListener {
if (SystemClock.elapsedRealtime() - lastTime > clickInterval) {
lastTime = SystemClock.elapsedRealtime()
onIntervalClickListener.invoke(it)
}
}
}

/**
* Toast
*/
fun String.showToast(
context: Context = ToDoApp.context,
duration: Int = Toast.LENGTH_SHORT
) {
Toast.makeText(context, this, duration).show()
}

fun Int.showToast(context: Context = ToDoApp.context, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(context, this, duration).show()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.super12138.todo.views.all

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.lifecycle.Observer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import android.content.Intent
import android.os.Bundle
import android.view.WindowManager
import cn.super12138.todo.R
import cn.super12138.todo.ToDoApp
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.ActivityMainBinding
import cn.super12138.todo.utils.VibrationUtils
import cn.super12138.todo.views.BaseActivity
import cn.super12138.todo.views.settings.SettingsActivity
import cn.super12138.todo.views.welcome.WelcomeActivity

class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if(!GlobalValues.welcomePage) finish()

binding.toolBar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.item_settings -> {
Expand All @@ -38,6 +41,10 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {

false -> window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
if (!GlobalValues.welcomePage) {
val intent = Intent(this, WelcomeActivity::class.java)
startActivity(intent)
}
}

override fun getViewBinding(): ActivityMainBinding {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ProgressFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.progress.observe(viewLifecycleOwner, Observer { value ->
binding.progressBar.setProgressCompat(value, true)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.HapticFeedbackConstants
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.lifecycleScope
Expand All @@ -25,6 +24,7 @@ import cn.super12138.todo.utils.VibrationUtils
import cn.super12138.todo.views.about.AboutActivity
import cn.super12138.todo.views.all.AllTasksActivity
import cn.super12138.todo.views.main.MainActivity
import cn.super12138.todo.views.welcome.WelcomeActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.gson.Gson
Expand Down Expand Up @@ -177,7 +177,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
setOnPreferenceClickListener {
startActivity(Intent(context, AllTasksActivity::class.java))
VibrationUtils.performHapticFeedback(view)

true
}
}
Expand All @@ -186,7 +185,17 @@ class SettingsFragment : PreferenceFragmentCompat() {
setOnPreferenceClickListener {
startActivity(Intent(context, AboutActivity::class.java))
VibrationUtils.performHapticFeedback(view)
true
}
}

findPreference<Preference>(Constants.PREF_REENTER_WELCOME_ACTIVITY)?.apply {
setOnPreferenceClickListener {
val intent = Intent(context, WelcomeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
startActivity(intent)
VibrationUtils.performHapticFeedback(view)
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import cn.super12138.todo.ToDoApp
import cn.super12138.todo.databinding.FragmentTodoBinding
import cn.super12138.todo.logic.Repository
import cn.super12138.todo.utils.VibrationUtils
import cn.super12138.todo.utils.showToast
import cn.super12138.todo.views.bottomsheet.ToDoBottomSheet
import cn.super12138.todo.views.progress.ProgressViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package cn.super12138.todo.views.welcome

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.activity.OnBackPressedCallback
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 cn.super12138.todo.R
import cn.super12138.todo.constant.GlobalValues
import cn.super12138.todo.databinding.ActivityWelcomeBinding
import cn.super12138.todo.utils.VibrationUtils
import cn.super12138.todo.utils.setOnIntervalClickListener
import cn.super12138.todo.views.BaseActivity
import cn.super12138.todo.views.main.MainActivity
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

class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
private val viewModel by viewModels<WelcomeViewModel>()
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val previousBtn = binding.previousBtn
val nextBtn = binding.nextBtn
val centerBtn = binding.centerBtn

val currentPage = viewModel.currentPage // 0: Intro 1: Progress 2: ToDo Btn 3: ToDo Item

centerBtn.setOnIntervalClickListener {
VibrationUtils.performHapticFeedback(it)

centerBtn.hide()
nextBtn.show()
previousBtn.show()


if (currentPage.value == 3) {
GlobalValues.welcomePage = true
finish()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
} else {
currentPage.value = 1
}
}

previousBtn.setOnIntervalClickListener {
VibrationUtils.performHapticFeedback(it)

currentPage.value = currentPage.value?.minus(1)
}

nextBtn.setOnIntervalClickListener {
VibrationUtils.performHapticFeedback(it)

currentPage.value = currentPage.value?.plus(1)
}

currentPage.observe(this, Observer { page ->
supportFragmentManager.commit {
addToBackStack(System.currentTimeMillis().toString())
hide(supportFragmentManager.fragments.last())
add(R.id.welcome_page_container, getCurrentPage(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) currentPage.value = 3

if (page < 0) currentPage.value = 0
}
}
})

onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (currentPage.value == 0) {
finish()
} else {
currentPage.value = currentPage.value?.minus(1)
}
}
})
}

override fun getViewBinding(): ActivityWelcomeBinding {
return ActivityWelcomeBinding.inflate(layoutInflater)
}

private fun getCurrentPage(pageIndex: Int): Fragment {
return when (pageIndex) {
0 -> IntroPage()
1 -> ProgressPage()
2 -> ToDoBtnPage()
3 -> ToDoItemPage()
else -> IntroPage()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cn.super12138.todo.views.welcome

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class WelcomeViewModel : ViewModel() {
val currentPage = MutableLiveData<Int>(0)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.super12138.todo.views.welcome.pages

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.android.material.transition.MaterialSharedAxis

open class BasePage : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enterTransition = MaterialSharedAxis(MaterialSharedAxis.X, /* forward= */ true)
returnTransition = MaterialSharedAxis(MaterialSharedAxis.X, /* forward= */ false)
exitTransition = MaterialSharedAxis(MaterialSharedAxis.X, /* forward= */ true)
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.X, /* forward= */ false)
}
}
Loading

0 comments on commit 7b05b2e

Please sign in to comment.