From 675771d4bfeae91141a32bca45c7594ce3701bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolo=CC=80=20Tosi?= Date: Fri, 2 Feb 2024 10:10:31 +0100 Subject: [PATCH] Improved Welcome screen lifecycle --- .../java/it/bz/noi/community/MainActivity.kt | 27 +++++++++++-------- .../bz/noi/community/SplashScreenActivity.kt | 5 +++- .../ui/onboarding/OnboardingActivity.kt | 6 +++-- .../ui/onboarding/OnboardingViewModel.kt | 3 --- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/it/bz/noi/community/MainActivity.kt b/app/src/main/java/it/bz/noi/community/MainActivity.kt index b632fa0..b64fe09 100644 --- a/app/src/main/java/it/bz/noi/community/MainActivity.kt +++ b/app/src/main/java/it/bz/noi/community/MainActivity.kt @@ -6,6 +6,7 @@ package it.bz.noi.community import android.content.Intent import android.os.Bundle +import android.os.PersistableBundle import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @@ -40,10 +41,7 @@ class MainActivity : AppCompatActivity() { private val navController: NavController get() = findNavController(R.id.nav_host_fragment) - - private val showWelcome by lazy { - intent.getBooleanExtra(EXTRA_SHOW_WELCOME, false) - } + private var showWelcome: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -52,13 +50,14 @@ class MainActivity : AppCompatActivity() { binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - if (true || showWelcome) { - if (!runBlocking { getWelcomeUnderstood() }) { - val inflater = navController.navInflater - val graph = inflater.inflate(R.navigation.mobile_navigation) - graph.setStartDestination(R.id.welcome) - navController.graph = graph - } + showWelcome = savedInstanceState?.getBoolean(STATE_SHOW_WELCOME) ?: intent.getBooleanExtra(EXTRA_SHOW_WELCOME, false) + + if (showWelcome) { + showWelcome = false + val inflater = navController.navInflater + val graph = inflater.inflate(R.navigation.mobile_navigation) + graph.setStartDestination(R.id.welcome) + navController.graph = graph } val toolbar = binding.toolbar @@ -187,8 +186,14 @@ class MainActivity : AppCompatActivity() { } } + override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) { + super.onSaveInstanceState(outState, outPersistentState) + outState.putBoolean(STATE_SHOW_WELCOME, showWelcome) + } + companion object { internal const val EXTRA_SHOW_WELCOME: String = "show_welcome" + private const val STATE_SHOW_WELCOME: String = "show_welcome" private const val TAG = "MainActivity" } } diff --git a/app/src/main/java/it/bz/noi/community/SplashScreenActivity.kt b/app/src/main/java/it/bz/noi/community/SplashScreenActivity.kt index dafe34a..6488fb4 100644 --- a/app/src/main/java/it/bz/noi/community/SplashScreenActivity.kt +++ b/app/src/main/java/it/bz/noi/community/SplashScreenActivity.kt @@ -17,6 +17,7 @@ import it.bz.noi.community.databinding.ActivitySplashBinding import it.bz.noi.community.oauth.AuthManager import it.bz.noi.community.oauth.AuthStateStatus import it.bz.noi.community.storage.getSkipParam +import it.bz.noi.community.storage.getWelcomeUnderstood import it.bz.noi.community.storage.setSkipParam import it.bz.noi.community.ui.onboarding.OnboardingActivity import kotlinx.coroutines.Dispatchers @@ -82,7 +83,9 @@ class SplashScreenActivity : AppCompatActivity() { private fun goToMainActivity() { if (isFinishing) return - startActivity(Intent(this, MainActivity::class.java)) + startActivity(Intent(this, MainActivity::class.java).apply { + putExtra(MainActivity.EXTRA_SHOW_WELCOME, runBlocking { !getWelcomeUnderstood() }) + }) finish() } diff --git a/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingActivity.kt b/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingActivity.kt index 573ed35..07d1772 100644 --- a/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingActivity.kt +++ b/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingActivity.kt @@ -27,6 +27,7 @@ import it.bz.noi.community.R import it.bz.noi.community.databinding.ActivityOnboardingBinding import it.bz.noi.community.oauth.AuthManager import it.bz.noi.community.oauth.AuthStateStatus +import it.bz.noi.community.storage.getWelcomeUnderstood import it.bz.noi.community.ui.WebViewFragment import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -112,9 +113,10 @@ class OnboardingActivity : AppCompatActivity() { } } - internal fun goToMainActivity() { + internal suspend fun goToMainActivity() { + val showWelcome = !getWelcomeUnderstood() startActivity(Intent(this, MainActivity::class.java).apply { - putExtra(MainActivity.EXTRA_SHOW_WELCOME, true) + putExtra(MainActivity.EXTRA_SHOW_WELCOME, showWelcome) }) } diff --git a/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingViewModel.kt b/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingViewModel.kt index 1de8c6b..3ba5125 100644 --- a/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingViewModel.kt +++ b/app/src/main/java/it/bz/noi/community/ui/onboarding/OnboardingViewModel.kt @@ -10,15 +10,12 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.viewModelScope import it.bz.noi.community.oauth.AuthManager import it.bz.noi.community.oauth.AuthStateStatus -import it.bz.noi.community.storage.getPrivacyAccepted import it.bz.noi.community.storage.getPrivacyAcceptedFlow import it.bz.noi.community.storage.setPrivacyAccepted import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.debounce -import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking class OnboardingViewModel( app: Application,