Add popup after login for the tutorial

This commit is contained in:
Matthieu 2024-08-30 16:03:23 +02:00
parent 9ebeda23a3
commit 212ae778fb
4 changed files with 56 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
@ -20,6 +21,8 @@ import org.pixeldroid.app.BuildConfig
import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.ActivityLoginBinding
import org.pixeldroid.app.main.MainActivity
import org.pixeldroid.app.settings.SettingsActivity
import org.pixeldroid.app.settings.TutorialSettingsDialog.Companion.START_TUTORIAL
import org.pixeldroid.app.utils.BaseActivity
import org.pixeldroid.app.utils.api.PixelfedAPI
import org.pixeldroid.app.utils.openUrl
@ -89,12 +92,26 @@ class LoginActivity : BaseActivity() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
model.finishedLogin.collectLatest {
if (it) {
when (it) {
LoginActivityViewModel.FinishedLogin.Finished -> {
val intent = Intent(this@LoginActivity, MainActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
}
LoginActivityViewModel.FinishedLogin.FinishedFirstTime -> MaterialAlertDialogBuilder(binding.root.context)
.setMessage(R.string.first_time_question)
.setPositiveButton(android.R.string.ok) { _, _ ->
val intent = Intent(this@LoginActivity, SettingsActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.putExtra(START_TUTORIAL, true)
startActivity(intent)
}
.setNegativeButton(R.string.skip_tutorial) { _, _ -> model.finishLogin()}
.show()
LoginActivityViewModel.FinishedLogin.NotFinished -> {}
}
}
}
}

View File

@ -50,7 +50,10 @@ class LoginActivityViewModel @Inject constructor(
private val _loadingState: MutableStateFlow<LoginState> = MutableStateFlow(LoginState(LoginState.LoadingState.Resting))
val loadingState = _loadingState.asStateFlow()
private val _finishedLogin = MutableStateFlow(false)
enum class FinishedLogin {
NotFinished, Finished, FinishedFirstTime
}
private val _finishedLogin = MutableStateFlow(FinishedLogin.NotFinished)
val finishedLogin = _finishedLogin.asStateFlow()
private val _promptOauth: MutableStateFlow<PromptOAuth?> = MutableStateFlow(null)
@ -207,6 +210,7 @@ class LoginActivityViewModel @Inject constructor(
private suspend fun storeUser(accessToken: String, refreshToken: String?, clientId: String, clientSecret: String, instance: String) {
try {
val firstTime = db.userDao().getActiveUser() == null
val user = pixelfedAPI.verifyCredentials("Bearer $accessToken")
db.userDao().deActivateActiveUsers()
addUser(
@ -220,12 +224,14 @@ class LoginActivityViewModel @Inject constructor(
clientSecret = clientSecret
)
apiHolder.setToCurrentUser()
fetchNotifications()
_finishedLogin.value = if(firstTime) FinishedLogin.FinishedFirstTime else FinishedLogin.Finished
} catch (exception: Exception) {
return failedRegistration(R.string.verify_credentials)
}
fetchNotifications()
_finishedLogin.value = true
}
// Fetch the latest notifications of this account, to avoid launching old notifications
@ -280,4 +286,8 @@ class LoginActivityViewModel @Inject constructor(
_loadingState.value = LoginState(LoginState.LoadingState.Resting)
}
fun finishLogin() {
_finishedLogin.value = FinishedLogin.Finished
}
}

View File

@ -22,6 +22,7 @@ import kotlinx.coroutines.launch
import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.SettingsBinding
import org.pixeldroid.app.main.MainActivity
import org.pixeldroid.app.settings.TutorialSettingsDialog.Companion.START_TUTORIAL
import org.pixeldroid.app.utils.setThemeFromPreferences
import org.pixeldroid.common.ThemedActivity
@ -46,6 +47,20 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
val showTutorial = intent.getBooleanExtra(START_TUTORIAL, false)
if(showTutorial){
lifecycleScope.launch {
var target =
(supportFragmentManager.findFragmentByTag("topsettingsfragment") as? SettingsFragment)?.scrollToArrangeTabs(10)
while (target == null) {
target = (supportFragmentManager.findFragmentByTag("topsettingsfragment") as? SettingsFragment)?.scrollToArrangeTabs(10)
delay(100)
}
target.performClick()
}
}
onBackPressedDispatcher.addCallback(this /* lifecycle owner */) {
// Handle the back button event
// If a setting (for example language or theme) was changed, the main activity should be
@ -111,9 +126,9 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC
fun customTabsTutorial(){
lifecycleScope.launch {
var target =
(supportFragmentManager.findFragmentByTag("topsettingsfragment") as? SettingsFragment)?.scrollToArrangeTabs()
(supportFragmentManager.findFragmentByTag("topsettingsfragment") as? SettingsFragment)?.scrollToArrangeTabs(5)
while (target == null) {
target = (supportFragmentManager.findFragmentByTag("topsettingsfragment") as? SettingsFragment)?.scrollToArrangeTabs()
target = (supportFragmentManager.findFragmentByTag("topsettingsfragment") as? SettingsFragment)?.scrollToArrangeTabs(5)
delay(100)
}
TapTargetView.showFor(
@ -157,9 +172,8 @@ class SettingsActivity : ThemedActivity(), SharedPreferences.OnSharedPreferenceC
super.onDisplayPreferenceDialog(preference)
}
}
fun scrollToArrangeTabs(): View? {
//Hardcoded because it's too annoying to find!
val position = 5
fun scrollToArrangeTabs(position: Int): View? {
//Hardcoded positions because it's too annoying to find!
if (listView != null && position != -1) {
listView.post {

View File

@ -368,4 +368,6 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="tutorial_choice">What could you use some help with?</string>
<string name="dm_tutorial">Direct Messages: keep in touch!</string>
<string name="custom_tabs_tutorial">Customize what tabs show up on the main PixelDroid screen!</string>
<string name="first_time_question">It seems like it might be your first time using PixelDroid. Do you want to open a tutorial? You can always find the tutorials in the settings.</string>
<string name="skip_tutorial">No, continue</string>
</resources>