If we are skipping account select on startup, still ask for PIN if needed with an option to use default account

This commit is contained in:
Luna712 2023-11-05 14:14:12 -07:00
parent 9b3d13a7d0
commit 352f90a08b
4 changed files with 65 additions and 12 deletions

View file

@ -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) { _, _ ->

View file

@ -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

View file

@ -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

View file

@ -721,6 +721,7 @@
<string name="enter_pin">Enter PIN</string>
<string name="enter_pin_with_name" formatted="true">Enter PIN for %s</string>
<string name="enter_current_pin">Enter Current PIN</string>
<string name="lock_profile">Lock Profile</string>
<string name="pin">PIN</string>
@ -731,4 +732,5 @@
<string name="edit_account">Edit account</string>
<string name="logged_account" formatted="true">Logged in as %s</string>
<string name="skip_startup_account_select_pref">Skip account selection at startup</string>
<string name="use_default_account">Use Default Account</string>
</resources>