From 8f85aed7e8b29002fd5e37838f2aabf1f502aba3 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Fri, 10 Nov 2023 12:25:22 -0700 Subject: [PATCH] Some fixes --- .../cloudstream3/ui/account/AccountAdapter.kt | 66 +++++++++---------- .../cloudstream3/ui/account/AccountHelper.kt | 28 ++------ .../ui/account/AccountSelectActivity.kt | 9 +-- .../ui/account/AccountViewModel.kt | 49 ++++++++++---- .../ui/settings/SettingsGeneral.kt | 9 --- .../main/res/layout/account_select_linear.xml | 6 +- app/src/main/res/xml/settings_account.xml | 6 ++ app/src/main/res/xml/settins_general.xml | 6 -- 8 files changed, 91 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountAdapter.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountAdapter.kt index 644018ac..60260edf 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountAdapter.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/account/AccountAdapter.kt @@ -1,8 +1,5 @@ package com.lagradost.cloudstream3.ui.account -import android.graphics.RenderEffect -import android.graphics.Shader -import android.os.Build import android.view.LayoutInflater import android.view.ViewGroup import androidx.core.content.ContextCompat @@ -17,13 +14,14 @@ import com.lagradost.cloudstream3.ui.account.AccountHelper.showAccountEditDialog import com.lagradost.cloudstream3.ui.result.setImage import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.utils.DataStoreHelper +import com.lagradost.cloudstream3.utils.UIHelper.setImage class AccountAdapter( private val accounts: List, private val accountSelectCallback: (DataStoreHelper.Account) -> Unit, private val accountCreateCallback: (DataStoreHelper.Account) -> Unit, private val accountEditCallback: (DataStoreHelper.Account) -> Unit, - private val accountDeleteCallback: () -> Unit + private val accountDeleteCallback: (DataStoreHelper.Account) -> Unit ) : RecyclerView.Adapter() { companion object { @@ -62,11 +60,13 @@ class AccountAdapter( ) } else { root.setOnLongClickListener { - showAccountEditDialog(root.context, account, isNewAccount = false) { - if (it != null) { - accountEditCallback.invoke(it) - } else accountDeleteCallback.invoke() - } + showAccountEditDialog( + context = root.context, + account = account, + isNewAccount = false, + accountEditCallback = { account -> accountEditCallback.invoke(account) }, + accountDeleteCallback = { account -> accountDeleteCallback.invoke(account) } + ) true } @@ -85,18 +85,14 @@ class AccountAdapter( val isLastUsedAccount = account.keyIndex == DataStoreHelper.selectedKeyIndex accountName.text = account.name - accountImage.setImage(account.image) + accountImage.setImage( + account.image, + fadeIn = false, + radius = 10 + ) lockIcon.isVisible = account.lockPin != null outline.isVisible = !isTv && isLastUsedAccount - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - accountImage.setRenderEffect( - RenderEffect.createBlurEffect( - 10f, 10f, Shader.TileMode.CLAMP - ) - ) - } - if (isTv) { // For emulator but this is fine on TV also root.isFocusableInTouchMode = true @@ -111,11 +107,13 @@ class AccountAdapter( } root.setOnClickListener { - showAccountEditDialog(root.context, account, isNewAccount = false) { - if (it != null) { - accountEditCallback.invoke(it) - } else accountDeleteCallback.invoke() - } + showAccountEditDialog( + context = root.context, + account = account, + isNewAccount = false, + accountEditCallback = { account -> accountEditCallback.invoke(account) }, + accountDeleteCallback = { account -> accountDeleteCallback.invoke(account) } + ) } } @@ -131,16 +129,18 @@ class AccountAdapter( val accountName = root.context.getString(R.string.account) - showAccountEditDialog(root.context, DataStoreHelper.Account( - keyIndex = keyIndex, - name = "$accountName $keyIndex", - customImage = null, - defaultImageIndex = image - ), isNewAccount = true) { - if (it != null) { - accountCreateCallback.invoke(it) - } - } + showAccountEditDialog( + root.context, + DataStoreHelper.Account( + keyIndex = keyIndex, + name = "$accountName $keyIndex", + customImage = null, + defaultImageIndex = image + ), + isNewAccount = true, + accountEditCallback = { account -> accountCreateCallback.invoke(account) }, + accountDeleteCallback = {} + ) } } } 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 18fb0265..dc49a0a6 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 @@ -18,7 +18,6 @@ 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 import com.lagradost.cloudstream3.databinding.AccountEditDialogBinding @@ -31,17 +30,16 @@ import com.lagradost.cloudstream3.ui.result.setLinearListLayout import com.lagradost.cloudstream3.utils.AppUtils.setDefaultFocus import com.lagradost.cloudstream3.utils.DataStoreHelper 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( context: Context, account: DataStoreHelper.Account, isNewAccount: Boolean, - callback: (DataStoreHelper.Account?) -> Unit + accountEditCallback: (DataStoreHelper.Account) -> Unit, + accountDeleteCallback: (DataStoreHelper.Account) -> Unit ) { val binding = AccountEditDialogBinding.inflate(LayoutInflater.from(context), null, false) val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom) @@ -63,17 +61,7 @@ object AccountHelper { val dialogClickListener = DialogInterface.OnClickListener { _, which -> when (which) { DialogInterface.BUTTON_POSITIVE -> { - // Remove the account - removeKeys(account.keyIndex.toString()) - val currentAccounts = DataStoreHelper.accounts.toMutableList() - currentAccounts.removeIf { it.keyIndex == account.keyIndex } - DataStoreHelper.accounts = currentAccounts.toTypedArray() - - if (account.keyIndex == DataStoreHelper.selectedKeyIndex) { - setAccount(getDefaultAccount(context)) - } - - callback.invoke(null) + accountDeleteCallback.invoke(account) dialog?.dismissSafe() } @@ -117,12 +105,12 @@ object AccountHelper { showPinInputDialog(context, currentEditAccount.lockPin, false) { pin -> if (pin == null) return@showPinInputDialog // PIN is correct, proceed to update the account - callback.invoke(currentEditAccount) + accountEditCallback.invoke(currentEditAccount) dialog.dismissSafe() } } else { // No lock PIN set, proceed to update the account - callback.invoke(currentEditAccount) + accountEditCallback.invoke(currentEditAccount) dialog.dismissSafe() } } @@ -220,7 +208,7 @@ object AccountHelper { builder.setTitle(context.getString(R.string.enter_pin_with_name, currentAccount?.name)) builder.setOnDismissListener { if (!isPinValid) { - exitProcess(0) + context.getActivity()?.finish() } } // So that if they don't know the PIN for the current account, @@ -335,8 +323,6 @@ object AccountHelper { recyclerView.setLinearListLayout(isHorizontal = true) - viewModel.updateAccounts(activity) - activity.observe(viewModel.accounts) { liveAccounts -> recyclerView.adapter = AccountAdapter( liveAccounts, @@ -346,7 +332,7 @@ object AccountHelper { }, accountCreateCallback = { viewModel.handleAccountUpdate(it, activity) }, accountEditCallback = { viewModel.handleAccountUpdate(it, activity) }, - accountDeleteCallback = { viewModel.updateAccounts(activity) } + accountDeleteCallback = { viewModel.handleAccountDelete(it, activity) } ) activity.observe(viewModel.selectedKeyIndex) { selectedKeyIndex -> 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 c0ec65a2..23071f59 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 @@ -5,8 +5,8 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.ViewModelProvider +import androidx.preference.PreferenceManager 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 @@ -40,10 +40,11 @@ class AccountSelectActivity : AppCompatActivity() { false ) - val skipStartup = getKey( + val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) + val skipStartup = settingsManager.getBoolean( getString(R.string.skip_startup_account_select_key), false - ) ?: false || accounts.count() <= 1 + ) || accounts.count() <= 1 viewModel = ViewModelProvider(this)[AccountViewModel::class.java] @@ -105,7 +106,7 @@ class AccountSelectActivity : AppCompatActivity() { navigateToMainActivity() } }, - accountDeleteCallback = { viewModel.updateAccounts(this) } + accountDeleteCallback = { viewModel.handleAccountDelete(it,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 f237d785..cd2b7183 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 @@ -5,15 +5,19 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.lagradost.cloudstream3.AcraApplication.Companion.context +import com.lagradost.cloudstream3.AcraApplication.Companion.removeKeys import com.lagradost.cloudstream3.ui.account.AccountHelper.showPinInputDialog import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper.getAccounts +import com.lagradost.cloudstream3.utils.DataStoreHelper.getDefaultAccount import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount class AccountViewModel : ViewModel() { - private val _accounts: MutableLiveData> = MutableLiveData( - context?.let { getAccounts(it) } ?: DataStoreHelper.accounts.toList() - ) + private fun getAllAccounts(): List { + return context?.let { getAccounts(it) } ?: DataStoreHelper.accounts.toList() + } + + private val _accounts: MutableLiveData> = MutableLiveData(getAllAccounts()) val accounts: LiveData> = _accounts private val _isEditing = MutableLiveData(false) @@ -22,7 +26,11 @@ class AccountViewModel : ViewModel() { private val _isAllowedLogin = MutableLiveData(false) val isAllowedLogin: LiveData = _isAllowedLogin - private val _selectedKeyIndex = MutableLiveData(DataStoreHelper.selectedKeyIndex) + private val _selectedKeyIndex = MutableLiveData( + getAllAccounts().indexOfFirst { + it.keyIndex == DataStoreHelper.selectedKeyIndex + } + ) val selectedKeyIndex: LiveData = _selectedKeyIndex fun setIsEditing(value: Boolean) { @@ -33,11 +41,6 @@ class AccountViewModel : ViewModel() { _isEditing.postValue(!(_isEditing.value ?: false)) } - fun updateAccounts(context: Context) { - _accounts.postValue(getAccounts(context)) - _selectedKeyIndex.postValue(DataStoreHelper.selectedKeyIndex) - } - fun handleAccountUpdate( account: DataStoreHelper.Account, context: Context @@ -58,7 +61,29 @@ class AccountViewModel : ViewModel() { DataStoreHelper.accounts = currentAccounts.toTypedArray() _accounts.postValue(getAccounts(context)) - _selectedKeyIndex.postValue(account.keyIndex) + _selectedKeyIndex.postValue(getAccounts(context).indexOf(account)) + } + + fun handleAccountDelete( + account: DataStoreHelper.Account, + context: Context + ) { + removeKeys(account.keyIndex.toString()) + + val currentAccounts = getAccounts(context).toMutableList() + + currentAccounts.removeIf { it.keyIndex == account.keyIndex } + + DataStoreHelper.accounts = currentAccounts.toTypedArray() + + if (account.keyIndex == DataStoreHelper.selectedKeyIndex) { + setAccount(getDefaultAccount(context)) + } + + _accounts.postValue(getAccounts(context)) + _selectedKeyIndex.postValue(getAllAccounts().indexOfFirst { + it.keyIndex == DataStoreHelper.selectedKeyIndex + }) } fun handleAccountSelect( @@ -78,13 +103,13 @@ class AccountViewModel : ViewModel() { if (pin == null) return@showPinInputDialog // Pin is correct, proceed _isAllowedLogin.postValue(true) - _selectedKeyIndex.postValue(account.keyIndex) + _selectedKeyIndex.postValue(getAccounts(context).indexOf(account)) setAccount(account) } } else { // No PIN set for the selected account, proceed _isAllowedLogin.postValue(true) - _selectedKeyIndex.postValue(account.keyIndex) + _selectedKeyIndex.postValue(getAccounts(context).indexOf(account)) setAccount(account) } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsGeneral.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsGeneral.kt index 5d873391..224ca74a 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsGeneral.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsGeneral.kt @@ -335,15 +335,6 @@ class SettingsGeneral : PreferenceFragmentCompat() { return@setOnPreferenceChangeListener true } - settingsManager.edit().putBoolean( - getString(R.string.skip_startup_account_select_key), - getKey(getString(R.string.skip_startup_account_select_key), false) ?: false - ).apply() - getPref(R.string.skip_startup_account_select_key)?.setOnPreferenceChangeListener { _, newValue -> - setKey(getString(R.string.skip_startup_account_select_key), newValue) - return@setOnPreferenceChangeListener true - } - getPref(R.string.download_path_key)?.setOnPreferenceClickListener { val dirs = getDownloadDirs() diff --git a/app/src/main/res/layout/account_select_linear.xml b/app/src/main/res/layout/account_select_linear.xml index 47300273..b78c0d44 100644 --- a/app/src/main/res/layout/account_select_linear.xml +++ b/app/src/main/res/layout/account_select_linear.xml @@ -32,13 +32,13 @@ + style="@style/BlackButton" /> \ No newline at end of file diff --git a/app/src/main/res/xml/settings_account.xml b/app/src/main/res/xml/settings_account.xml index d3dbcb31..ec882088 100644 --- a/app/src/main/res/xml/settings_account.xml +++ b/app/src/main/res/xml/settings_account.xml @@ -1,6 +1,12 @@ + + diff --git a/app/src/main/res/xml/settins_general.xml b/app/src/main/res/xml/settins_general.xml index 7d6660ec..c4900bca 100644 --- a/app/src/main/res/xml/settins_general.xml +++ b/app/src/main/res/xml/settins_general.xml @@ -11,12 +11,6 @@ android:title="@string/download_path_pref" android:icon="@drawable/netflix_download" /> - -