From 352f90a08b7ae40883ed233c928cc345f81f38d1 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Sun, 5 Nov 2023 14:14:12 -0700 Subject: [PATCH] If we are skipping account select on startup, still ask for PIN if needed with an option to use default account --- .../cloudstream3/ui/account/AccountHelper.kt | 25 +++++++++++ .../ui/account/AccountSelectActivity.kt | 44 ++++++++++++++----- .../ui/account/AccountViewModel.kt | 6 ++- app/src/main/res/values/strings.xml | 2 + 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountHelper.kt index 8fb3f065..8fb9be2e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountHelper.kt @@ -17,6 +17,7 @@ import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.material.bottomsheet.BottomSheetDialog +import com.lagradost.cloudstream3.AcraApplication.Companion.getActivity import com.lagradost.cloudstream3.AcraApplication.Companion.removeKeys import com.lagradost.cloudstream3.MainActivity import com.lagradost.cloudstream3.R @@ -33,6 +34,7 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.getDefaultAccount import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.showInputMethod +import kotlin.system.exitProcess object AccountHelper { fun showAccountEditDialog( @@ -169,6 +171,7 @@ object AccountHelper { context: Context, currentPin: String?, editAccount: Boolean, + forStartup: Boolean = false, errorText: String? = null, callback: (String?) -> Unit ) { @@ -207,6 +210,28 @@ object AccountHelper { } } + if (forStartup) { + val currentAccount = DataStoreHelper.accounts.firstOrNull { + it.keyIndex == DataStoreHelper.selectedKeyIndex + } + + builder.setTitle(context.getString(R.string.enter_pin_with_name, currentAccount?.name)) + builder.setOnDismissListener { + if (!isPinValid) { + exitProcess(0) + } + } + // So that if they don't know the PIN for the current account, + // they don't get completely locked out + builder.setNeutralButton(R.string.use_default_account) { _, _ -> + val activity = context.getActivity() + if (activity is AccountSelectActivity) { + isPinValid = true + activity.viewModel.handleAccountSelect(getDefaultAccount(context), activity) + } + } + } + if (isNewPin) { if (errorText != null) binding.pinEditTextError.visibleWithText(errorText) builder.setPositiveButton(R.string.setup_done) { _, _ -> diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt index 3cf9c04b..c0ec65a2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountSelectActivity.kt @@ -9,6 +9,7 @@ import androidx.recyclerview.widget.GridLayoutManager import com.lagradost.cloudstream3.AcraApplication.Companion.getKey import com.lagradost.cloudstream3.CommonActivity import com.lagradost.cloudstream3.CommonActivity.loadThemes +import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.MainActivity import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.databinding.ActivityAccountSelectBinding @@ -18,14 +19,20 @@ import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_ import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_SELECT_ACCOUNT import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.utils.DataStoreHelper.accounts +import com.lagradost.cloudstream3.utils.DataStoreHelper.selectedKeyIndex import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute class AccountSelectActivity : AppCompatActivity() { + lateinit var viewModel: AccountViewModel + @SuppressLint("NotifyDataSetChanged") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + loadThemes(this) + + window.navigationBarColor = colorFromAttribute(R.attr.primaryBlackBackground) // Are we editing and coming from MainActivity? val isEditingFromMainActivity = intent.getBooleanExtra( @@ -38,31 +45,48 @@ class AccountSelectActivity : AppCompatActivity() { false ) ?: false || accounts.count() <= 1 + viewModel = ViewModelProvider(this)[AccountViewModel::class.java] + // Don't show account selection if there is only // one account that exists if (!isEditingFromMainActivity && skipStartup) { - navigateToMainActivity() + val currentAccount = accounts.firstOrNull { it.keyIndex == selectedKeyIndex } + if (currentAccount?.lockPin != null) { + CommonActivity.init(this) + viewModel.handleAccountSelect(currentAccount, this, true) + observe(viewModel.isAllowedLogin) { isAllowedLogin -> + if (isAllowedLogin) { + // We are allowed to continue to MainActivity + navigateToMainActivity() + } + } + } else { + if (accounts.count() > 1) { + showToast(this, getString( + R.string.logged_account, + currentAccount?.name + )) + } + + navigateToMainActivity() + } + return } CommonActivity.init(this) - loadThemes(this) - - window.navigationBarColor = colorFromAttribute(R.attr.primaryBlackBackground) val binding = ActivityAccountSelectBinding.inflate(layoutInflater) setContentView(binding.root) val recyclerView: AutofitRecyclerView = binding.accountRecyclerView - val viewModel = ViewModelProvider(this)[AccountViewModel::class.java] - observe(viewModel.accounts) { liveAccounts -> val adapter = AccountAdapter( liveAccounts, // Handle the selected account accountSelectCallback = { - viewModel.handleAccountSelect(it,this@AccountSelectActivity) + viewModel.handleAccountSelect(it, this) observe(viewModel.isAllowedLogin) { isAllowedLogin -> if (isAllowedLogin) { // We are allowed to continue to MainActivity @@ -70,9 +94,9 @@ class AccountSelectActivity : AppCompatActivity() { } } }, - accountCreateCallback = { viewModel.handleAccountUpdate(it, this@AccountSelectActivity) }, + accountCreateCallback = { viewModel.handleAccountUpdate(it, this) }, accountEditCallback = { - viewModel.handleAccountUpdate(it, this@AccountSelectActivity) + viewModel.handleAccountUpdate(it, this) // We came from MainActivity, return there // and switch to the edited account @@ -81,7 +105,7 @@ class AccountSelectActivity : AppCompatActivity() { navigateToMainActivity() } }, - accountDeleteCallback = { viewModel.updateAccounts(this@AccountSelectActivity) } + accountDeleteCallback = { viewModel.updateAccounts(this) } ) recyclerView.adapter = adapter diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountViewModel.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountViewModel.kt index 4eaad559..f237d785 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountViewModel.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountViewModel.kt @@ -63,7 +63,8 @@ class AccountViewModel : ViewModel() { fun handleAccountSelect( account: DataStoreHelper.Account, - context: Context + context: Context, + forStartup: Boolean = false ) { // Check if the selected account has a lock PIN set if (account.lockPin != null) { @@ -71,7 +72,8 @@ class AccountViewModel : ViewModel() { showPinInputDialog( context, account.lockPin, - false + false, + forStartup ) { pin -> if (pin == null) return@showPinInputDialog // Pin is correct, proceed diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ad741626..ce660a67 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -721,6 +721,7 @@ Enter PIN + Enter PIN for %s Enter Current PIN Lock Profile PIN @@ -731,4 +732,5 @@ Edit account Logged in as %s Skip account selection at startup + Use Default Account