Move more stuff to ViewModel

This commit is contained in:
Luna712 2023-11-04 12:22:59 -06:00
parent 00f2714b2c
commit 3536da6166
7 changed files with 118 additions and 168 deletions

View file

@ -92,8 +92,6 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStri
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.inAppAuths
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.ui.account.AccountHelper
import com.lagradost.cloudstream3.ui.account.AccountViewModel
import com.lagradost.cloudstream3.ui.download.DOWNLOAD_NAVIGATE_TO
import com.lagradost.cloudstream3.ui.home.HomeViewModel
import com.lagradost.cloudstream3.ui.player.BasicLink
@ -1612,9 +1610,4 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
false
}
}
fun showAccountSelectLinear(context: Context) {
val viewModel = ViewModelProvider(this)[AccountViewModel::class.java]
AccountHelper.showAccountSelectLinear(context, viewModel, this)
}
}

View file

@ -1,5 +1,6 @@
package com.lagradost.cloudstream3.ui.account
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
@ -12,10 +13,11 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKeys
import com.lagradost.cloudstream3.MainActivity
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.AccountEditDialogBinding
import com.lagradost.cloudstream3.databinding.AccountSelectLinearBinding
@ -26,7 +28,6 @@ import com.lagradost.cloudstream3.ui.result.setImage
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.getAccounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.getDefaultAccount
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
@ -277,85 +278,45 @@ object AccountHelper {
}, 200)
}
fun showAccountSelectLinear(
context: Context,
viewModel: AccountViewModel,
lifecycleOwner: LifecycleOwner
) {
fun onAccountUpdated(account: DataStoreHelper.Account) {
val currentAccounts = DataStoreHelper.accounts.toMutableList()
val overrideIndex = currentAccounts.indexOfFirst { it.keyIndex == account.keyIndex }
if (overrideIndex != -1) {
currentAccounts[overrideIndex] = account
} else currentAccounts.add(account)
val currentHomePage = DataStoreHelper.currentHomePage
setAccount(account, false)
DataStoreHelper.currentHomePage = currentHomePage
DataStoreHelper.accounts = currentAccounts.toTypedArray()
}
fun Activity?.showAccountSelectLinear() {
val activity = this as? MainActivity ?: return
val viewModel = ViewModelProvider(activity)[AccountViewModel::class.java]
val binding: AccountSelectLinearBinding = AccountSelectLinearBinding.inflate(
LayoutInflater.from(context)
LayoutInflater.from(activity)
)
val builder = BottomSheetDialog(context)
val builder = BottomSheetDialog(activity)
builder.setContentView(binding.root)
builder.show()
binding.manageAccountsButton.setOnClickListener {
val accountSelectIntent = Intent(context, AccountSelectActivity::class.java)
val accountSelectIntent = Intent(activity, AccountSelectActivity::class.java)
accountSelectIntent.putExtra("isEditingFromMainActivity", true)
context.startActivity(accountSelectIntent)
activity.startActivity(accountSelectIntent)
builder.dismissSafe()
}
val recyclerView: RecyclerView = binding.accountRecyclerView
val itemWidth = recyclerView.resources.getDimensionPixelSize(
val itemSize = recyclerView.resources.getDimensionPixelSize(
R.dimen.account_select_linear_item_size
)
val itemHeight = recyclerView.resources.getDimensionPixelSize(
R.dimen.account_select_linear_item_size
)
recyclerView.addItemDecoration(AccountSelectLinearItemDecoration(itemWidth, itemHeight))
recyclerView.addItemDecoration(AccountSelectLinearItemDecoration(itemSize))
recyclerView.setLinearListLayout(isHorizontal = true)
lifecycleOwner.observe(viewModel.accountsLiveData) { liveAccounts ->
activity.observe(viewModel.accounts) { liveAccounts ->
recyclerView.adapter = AccountAdapter(
liveAccounts,
accountSelectCallback = { account ->
// Check if the selected account has a lock PIN set
if (account.lockPin != null) {
// Prompt for the lock pin
showPinInputDialog(context, account.lockPin, false) { pin ->
if (pin == null) return@showPinInputDialog
// Pin is correct, unlock the profile
setAccount(account, true)
builder.dismissSafe()
}
} else {
// No lock PIN set, directly set the account
setAccount(account, true)
builder.dismissSafe()
}
viewModel.handleAccountSelect(account, activity)
builder.dismissSafe()
},
accountCreateCallback = {
onAccountUpdated(it)
viewModel.updateAccounts(getAccounts(context))
},
accountEditCallback = {
onAccountUpdated(it)
viewModel.updateAccounts(getAccounts(context))
},
accountDeleteCallback = {
viewModel.updateAccounts(getAccounts(context))
}
accountCreateCallback = { viewModel.handleAccountUpdate(it, activity) },
accountEditCallback = { viewModel.handleAccountUpdate(it, activity) },
accountDeleteCallback = { viewModel.handleAccountUpdate(activity) }
)
}
}

View file

@ -1,8 +1,8 @@
package com.lagradost.cloudstream3.ui.account
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
@ -15,14 +15,15 @@ import com.lagradost.cloudstream3.databinding.ActivityAccountSelectBinding
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_EDIT_ACCOUNT
import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_SELECT_ACCOUNT
import com.lagradost.cloudstream3.ui.account.AccountHelper.showPinInputDialog
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAccounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
class AccountSelectActivity : AppCompatActivity() {
@SuppressLint("NotifyDataSetChanged")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -53,75 +54,58 @@ class AccountSelectActivity : AppCompatActivity() {
val viewModel = ViewModelProvider(this)[AccountViewModel::class.java]
observe(viewModel.accountsLiveData) { liveAccounts ->
observe(viewModel.accounts) { liveAccounts ->
val adapter = AccountAdapter(
liveAccounts,
// Handle the selected account
accountSelectCallback = { onAccountSelected(it) },
accountCreateCallback = {
onAccountUpdated(it)
viewModel.updateAccounts(
getAccounts(binding.root.context)
)
accountSelectCallback = {
viewModel.handleAccountSelect(it,this@AccountSelectActivity)
navigateToMainActivity()
},
accountCreateCallback = { viewModel.handleAccountUpdate(it, this@AccountSelectActivity) },
accountEditCallback = {
onAccountUpdated(it)
viewModel.updateAccounts(
getAccounts(binding.root.context)
)
viewModel.handleAccountUpdate(it, this@AccountSelectActivity)
// We came from MainActivity, return there
// and switch to the edited account
if (isEditingFromMainActivity) {
DataStoreHelper.setAccount(
it,
it.keyIndex != DataStoreHelper.selectedKeyIndex
)
setAccount(it, it.keyIndex != DataStoreHelper.selectedKeyIndex)
navigateToMainActivity()
}
},
accountDeleteCallback = {
viewModel.updateAccounts(
getAccounts(binding.root.context)
)
}
accountDeleteCallback = { viewModel.handleAccountUpdate(this@AccountSelectActivity) }
)
recyclerView.adapter = adapter
var isEditing = false
if (isEditingFromMainActivity) {
binding.editAccountButton.setImageResource(R.drawable.ic_baseline_close_24)
binding.title.setText(R.string.manage_accounts)
adapter.viewType = VIEW_TYPE_EDIT_ACCOUNT
isEditing = true
adapter.notifyDataSetChanged()
}
binding.editAccountButton.setOnClickListener {
isEditing = !isEditing
observe(viewModel.isEditing) { isEditing ->
if (isEditing) {
(it as ImageView).setImageResource(R.drawable.ic_baseline_close_24)
binding.editAccountButton.setImageResource(R.drawable.ic_baseline_close_24)
binding.title.setText(R.string.manage_accounts)
adapter.viewType = VIEW_TYPE_EDIT_ACCOUNT
} else {
// We came from MainActivity, return there
// and resume it's state
if (isEditingFromMainActivity) {
navigateToMainActivity()
}
(it as ImageView).setImageResource(R.drawable.ic_baseline_edit_24)
binding.editAccountButton.setImageResource(R.drawable.ic_baseline_edit_24)
binding.title.setText(R.string.select_an_account)
adapter.viewType = VIEW_TYPE_SELECT_ACCOUNT
}
adapter.notifyDataSetChanged()
}
if (isEditingFromMainActivity) {
viewModel.setIsEditing(true)
}
binding.editAccountButton.setOnClickListener {
// We came from MainActivity, return there
// and resume its state
if (isEditingFromMainActivity) {
navigateToMainActivity()
return@setOnClickListener
}
viewModel.toggleIsEditing()
}
}
if (isTvSettings()) {
@ -133,49 +117,6 @@ class AccountSelectActivity : AppCompatActivity() {
}
}
private fun onAccountSelected(selectedAccount: DataStoreHelper.Account) {
if (selectedAccount.lockPin != null) {
// The selected account has a PIN set, prompt the user to enter the PIN
showPinInputDialog(this@AccountSelectActivity, selectedAccount.lockPin, false) { pin ->
if (pin == null) return@showPinInputDialog
// Pin is correct, proceed to main activity
setAccount(selectedAccount)
navigateToMainActivity()
}
} else {
// No PIN set for the selected account, proceed to main activity
setAccount(selectedAccount)
navigateToMainActivity()
}
}
private fun onAccountUpdated(account: DataStoreHelper.Account) {
val currentAccounts = DataStoreHelper.accounts.toMutableList()
val overrideIndex = currentAccounts.indexOfFirst { it.keyIndex == account.keyIndex }
if (overrideIndex != -1) {
currentAccounts[overrideIndex] = account
} else currentAccounts.add(account)
val currentHomePage = DataStoreHelper.currentHomePage
setAccount(account)
DataStoreHelper.accounts = currentAccounts.toTypedArray()
DataStoreHelper.currentHomePage = currentHomePage
}
private fun setAccount(account: DataStoreHelper.Account) {
// Don't reload if it is the same account
if (DataStoreHelper.selectedKeyIndex == account.keyIndex) {
return
}
DataStoreHelper.selectedKeyIndex = account.keyIndex
MainActivity.bookmarksUpdatedEvent(true)
MainActivity.reloadHomeEvent(true)
}
private fun navigateToMainActivity() {
val mainIntent = Intent(this, MainActivity::class.java)
startActivity(mainIntent)

View file

@ -4,11 +4,11 @@ import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
class AccountSelectLinearItemDecoration(private val width: Int, private val height: Int) : RecyclerView.ItemDecoration() {
class AccountSelectLinearItemDecoration(private val size: Int) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val layoutParams = view.layoutParams as RecyclerView.LayoutParams
layoutParams.width = width
layoutParams.height = height
layoutParams.width = size
layoutParams.height = size
view.layoutParams = layoutParams
}
}

View file

@ -1,21 +1,74 @@
package com.lagradost.cloudstream3.ui.account
import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
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.setAccount
class AccountViewModel : ViewModel() {
private val _accountsLiveData = MutableLiveData<List<DataStoreHelper.Account>>()
val accountsLiveData: LiveData<List<DataStoreHelper.Account>> get() = _accountsLiveData
private val _accounts: MutableLiveData<List<DataStoreHelper.Account>> = MutableLiveData(DataStoreHelper.accounts.toList())
val accounts: LiveData<List<DataStoreHelper.Account>> = _accounts
init {
// Default to using the getAccounts function to retrieve initial data
val initialAccounts = DataStoreHelper.accounts.toList()
_accountsLiveData.value = initialAccounts
private val _isEditing = MutableLiveData(false)
val isEditing: LiveData<Boolean> = _isEditing
fun handleAccountUpdate(context: Context) {
_accounts.postValue(getAccounts(context))
}
fun updateAccounts(newAccounts: List<DataStoreHelper.Account>) {
_accountsLiveData.value = newAccounts
fun setIsEditing(value: Boolean) {
_isEditing.postValue(value)
}
}
fun toggleIsEditing() {
_isEditing.postValue(!(_isEditing.value ?: false))
}
fun handleAccountUpdate(
account: DataStoreHelper.Account,
context: Context
) {
val currentAccounts = getAccounts(context).toMutableList()
val overrideIndex = currentAccounts.indexOfFirst { it.keyIndex == account.keyIndex }
if (overrideIndex != -1) {
currentAccounts[overrideIndex] = account
} else currentAccounts.add(account)
val currentHomePage = DataStoreHelper.currentHomePage
setAccount(account, false)
DataStoreHelper.currentHomePage = currentHomePage
DataStoreHelper.accounts = currentAccounts.toTypedArray()
_accounts.postValue(getAccounts(context))
}
fun handleAccountSelect(
account: DataStoreHelper.Account,
context: Context
) {
// Check if the selected account has a lock PIN set
if (account.lockPin != null) {
// The selected account has a PIN set, prompt the user to enter the PIN
showPinInputDialog(
context,
account.lockPin,
false
) { pin ->
if (pin == null) return@showPinInputDialog
// Pin is correct, proceed to main activity
setAccount(account, true)
}
} else {
// No PIN set for the selected account, proceed to main activity
setAccount(account, true)
}
}
}

View file

@ -38,6 +38,7 @@ import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.mvvm.observeNullable
import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi
import com.lagradost.cloudstream3.ui.APIRepository.Companion.randomApi
import com.lagradost.cloudstream3.ui.account.AccountHelper.showAccountSelectLinear
import com.lagradost.cloudstream3.ui.result.txt
import com.lagradost.cloudstream3.ui.search.*
import com.lagradost.cloudstream3.ui.search.SearchHelper.handleSearchClickCallback
@ -495,8 +496,8 @@ class HomeFragment : Fragment() {
//homeChangeApiLoading.setOnClickListener(apiChangeClickListener)
homeApiFab.setOnClickListener(apiChangeClickListener)
homeChangeApi.setOnClickListener(apiChangeClickListener)
homeSwitchAccount.setOnClickListener { v ->
(CommonActivity.activity as MainActivity).showAccountSelectLinear(v?.context ?: return@setOnClickListener)
homeSwitchAccount.setOnClickListener {
activity?.showAccountSelectLinear()
}
homeRandom.setOnClickListener {

View file

@ -27,6 +27,7 @@ import com.lagradost.cloudstream3.mvvm.Resource
import com.lagradost.cloudstream3.mvvm.debugException
import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.ui.WatchType
import com.lagradost.cloudstream3.ui.account.AccountHelper.showAccountSelectLinear
import com.lagradost.cloudstream3.ui.home.HomeFragment.Companion.selectHomepage
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.ResultViewModel2
@ -477,8 +478,8 @@ class HomeParentItemAdapterPreview(
}
}
homeAccount?.setOnClickListener { v ->
(activity as MainActivity).showAccountSelectLinear(v?.context ?: return@setOnClickListener)
homeAccount?.setOnClickListener {
activity?.showAccountSelectLinear()
}
(binding as? FragmentHomeHeadTvBinding)?.apply {