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.syncproviders.AccountManager.Companion.inAppAuths
import com.lagradost.cloudstream3.ui.APIRepository import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.WatchType 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.download.DOWNLOAD_NAVIGATE_TO
import com.lagradost.cloudstream3.ui.home.HomeViewModel import com.lagradost.cloudstream3.ui.home.HomeViewModel
import com.lagradost.cloudstream3.ui.player.BasicLink import com.lagradost.cloudstream3.ui.player.BasicLink
@ -1612,9 +1610,4 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
false 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 package com.lagradost.cloudstream3.ui.account
import android.app.Activity
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
@ -12,10 +13,11 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialog
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKeys import com.lagradost.cloudstream3.AcraApplication.Companion.removeKeys
import com.lagradost.cloudstream3.MainActivity
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.AccountEditDialogBinding import com.lagradost.cloudstream3.databinding.AccountEditDialogBinding
import com.lagradost.cloudstream3.databinding.AccountSelectLinearBinding 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.ui.result.setLinearListLayout
import com.lagradost.cloudstream3.utils.AppUtils.setDefaultFocus import com.lagradost.cloudstream3.utils.AppUtils.setDefaultFocus
import com.lagradost.cloudstream3.utils.DataStoreHelper 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.getDefaultAccount
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
@ -277,85 +278,45 @@ object AccountHelper {
}, 200) }, 200)
} }
fun showAccountSelectLinear( fun Activity?.showAccountSelectLinear() {
context: Context, val activity = this as? MainActivity ?: return
viewModel: AccountViewModel, val viewModel = ViewModelProvider(activity)[AccountViewModel::class.java]
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()
}
val binding: AccountSelectLinearBinding = AccountSelectLinearBinding.inflate( val binding: AccountSelectLinearBinding = AccountSelectLinearBinding.inflate(
LayoutInflater.from(context) LayoutInflater.from(activity)
) )
val builder = BottomSheetDialog(context) val builder = BottomSheetDialog(activity)
builder.setContentView(binding.root) builder.setContentView(binding.root)
builder.show() builder.show()
binding.manageAccountsButton.setOnClickListener { binding.manageAccountsButton.setOnClickListener {
val accountSelectIntent = Intent(context, AccountSelectActivity::class.java) val accountSelectIntent = Intent(activity, AccountSelectActivity::class.java)
accountSelectIntent.putExtra("isEditingFromMainActivity", true) accountSelectIntent.putExtra("isEditingFromMainActivity", true)
context.startActivity(accountSelectIntent) activity.startActivity(accountSelectIntent)
builder.dismissSafe() builder.dismissSafe()
} }
val recyclerView: RecyclerView = binding.accountRecyclerView val recyclerView: RecyclerView = binding.accountRecyclerView
val itemWidth = recyclerView.resources.getDimensionPixelSize( val itemSize = recyclerView.resources.getDimensionPixelSize(
R.dimen.account_select_linear_item_size R.dimen.account_select_linear_item_size
) )
val itemHeight = recyclerView.resources.getDimensionPixelSize( recyclerView.addItemDecoration(AccountSelectLinearItemDecoration(itemSize))
R.dimen.account_select_linear_item_size
)
recyclerView.addItemDecoration(AccountSelectLinearItemDecoration(itemWidth, itemHeight))
recyclerView.setLinearListLayout(isHorizontal = true) recyclerView.setLinearListLayout(isHorizontal = true)
lifecycleOwner.observe(viewModel.accountsLiveData) { liveAccounts -> activity.observe(viewModel.accounts) { liveAccounts ->
recyclerView.adapter = AccountAdapter( recyclerView.adapter = AccountAdapter(
liveAccounts, liveAccounts,
accountSelectCallback = { account -> accountSelectCallback = { account ->
// Check if the selected account has a lock PIN set viewModel.handleAccountSelect(account, activity)
if (account.lockPin != null) { builder.dismissSafe()
// 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()
}
}, },
accountCreateCallback = { accountCreateCallback = { viewModel.handleAccountUpdate(it, activity) },
onAccountUpdated(it) accountEditCallback = { viewModel.handleAccountUpdate(it, activity) },
viewModel.updateAccounts(getAccounts(context)) accountDeleteCallback = { viewModel.handleAccountUpdate(activity) }
},
accountEditCallback = {
onAccountUpdated(it)
viewModel.updateAccounts(getAccounts(context))
},
accountDeleteCallback = {
viewModel.updateAccounts(getAccounts(context))
}
) )
} }
} }

View file

@ -1,8 +1,8 @@
package com.lagradost.cloudstream3.ui.account package com.lagradost.cloudstream3.ui.account
import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager 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.mvvm.observe
import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_EDIT_ACCOUNT 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.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.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAccounts import com.lagradost.cloudstream3.utils.DataStoreHelper.getAccounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
class AccountSelectActivity : AppCompatActivity() { class AccountSelectActivity : AppCompatActivity() {
@SuppressLint("NotifyDataSetChanged")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -53,75 +54,58 @@ class AccountSelectActivity : AppCompatActivity() {
val viewModel = ViewModelProvider(this)[AccountViewModel::class.java] val viewModel = ViewModelProvider(this)[AccountViewModel::class.java]
observe(viewModel.accountsLiveData) { liveAccounts -> observe(viewModel.accounts) { liveAccounts ->
val adapter = AccountAdapter( val adapter = AccountAdapter(
liveAccounts, liveAccounts,
// Handle the selected account // Handle the selected account
accountSelectCallback = { onAccountSelected(it) }, accountSelectCallback = {
accountCreateCallback = { viewModel.handleAccountSelect(it,this@AccountSelectActivity)
onAccountUpdated(it) navigateToMainActivity()
viewModel.updateAccounts(
getAccounts(binding.root.context)
)
}, },
accountCreateCallback = { viewModel.handleAccountUpdate(it, this@AccountSelectActivity) },
accountEditCallback = { accountEditCallback = {
onAccountUpdated(it) viewModel.handleAccountUpdate(it, this@AccountSelectActivity)
viewModel.updateAccounts(
getAccounts(binding.root.context)
)
// We came from MainActivity, return there // We came from MainActivity, return there
// and switch to the edited account // and switch to the edited account
if (isEditingFromMainActivity) { if (isEditingFromMainActivity) {
DataStoreHelper.setAccount( setAccount(it, it.keyIndex != DataStoreHelper.selectedKeyIndex)
it,
it.keyIndex != DataStoreHelper.selectedKeyIndex
)
navigateToMainActivity() navigateToMainActivity()
} }
}, },
accountDeleteCallback = { accountDeleteCallback = { viewModel.handleAccountUpdate(this@AccountSelectActivity) }
viewModel.updateAccounts(
getAccounts(binding.root.context)
)
}
) )
recyclerView.adapter = adapter recyclerView.adapter = adapter
var isEditing = false observe(viewModel.isEditing) { isEditing ->
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
if (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) binding.title.setText(R.string.manage_accounts)
adapter.viewType = VIEW_TYPE_EDIT_ACCOUNT adapter.viewType = VIEW_TYPE_EDIT_ACCOUNT
} else { } else {
// We came from MainActivity, return there binding.editAccountButton.setImageResource(R.drawable.ic_baseline_edit_24)
// and resume it's state
if (isEditingFromMainActivity) {
navigateToMainActivity()
}
(it as ImageView).setImageResource(R.drawable.ic_baseline_edit_24)
binding.title.setText(R.string.select_an_account) binding.title.setText(R.string.select_an_account)
adapter.viewType = VIEW_TYPE_SELECT_ACCOUNT adapter.viewType = VIEW_TYPE_SELECT_ACCOUNT
} }
adapter.notifyDataSetChanged() 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()) { 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() { private fun navigateToMainActivity() {
val mainIntent = Intent(this, MainActivity::class.java) val mainIntent = Intent(this, MainActivity::class.java)
startActivity(mainIntent) startActivity(mainIntent)

View file

@ -4,11 +4,11 @@ import android.graphics.Rect
import android.view.View import android.view.View
import androidx.recyclerview.widget.RecyclerView 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) { override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
val layoutParams = view.layoutParams as RecyclerView.LayoutParams val layoutParams = view.layoutParams as RecyclerView.LayoutParams
layoutParams.width = width layoutParams.width = size
layoutParams.height = height layoutParams.height = size
view.layoutParams = layoutParams view.layoutParams = layoutParams
} }
} }

View file

@ -1,21 +1,74 @@
package com.lagradost.cloudstream3.ui.account package com.lagradost.cloudstream3.ui.account
import android.content.Context
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.lagradost.cloudstream3.ui.account.AccountHelper.showPinInputDialog
import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.getAccounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
class AccountViewModel : ViewModel() { class AccountViewModel : ViewModel() {
private val _accountsLiveData = MutableLiveData<List<DataStoreHelper.Account>>() private val _accounts: MutableLiveData<List<DataStoreHelper.Account>> = MutableLiveData(DataStoreHelper.accounts.toList())
val accountsLiveData: LiveData<List<DataStoreHelper.Account>> get() = _accountsLiveData val accounts: LiveData<List<DataStoreHelper.Account>> = _accounts
init { private val _isEditing = MutableLiveData(false)
// Default to using the getAccounts function to retrieve initial data val isEditing: LiveData<Boolean> = _isEditing
val initialAccounts = DataStoreHelper.accounts.toList()
_accountsLiveData.value = initialAccounts fun handleAccountUpdate(context: Context) {
_accounts.postValue(getAccounts(context))
} }
fun updateAccounts(newAccounts: List<DataStoreHelper.Account>) { fun setIsEditing(value: Boolean) {
_accountsLiveData.value = newAccounts _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.mvvm.observeNullable
import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi import com.lagradost.cloudstream3.ui.APIRepository.Companion.noneApi
import com.lagradost.cloudstream3.ui.APIRepository.Companion.randomApi 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.result.txt
import com.lagradost.cloudstream3.ui.search.* import com.lagradost.cloudstream3.ui.search.*
import com.lagradost.cloudstream3.ui.search.SearchHelper.handleSearchClickCallback import com.lagradost.cloudstream3.ui.search.SearchHelper.handleSearchClickCallback
@ -495,8 +496,8 @@ class HomeFragment : Fragment() {
//homeChangeApiLoading.setOnClickListener(apiChangeClickListener) //homeChangeApiLoading.setOnClickListener(apiChangeClickListener)
homeApiFab.setOnClickListener(apiChangeClickListener) homeApiFab.setOnClickListener(apiChangeClickListener)
homeChangeApi.setOnClickListener(apiChangeClickListener) homeChangeApi.setOnClickListener(apiChangeClickListener)
homeSwitchAccount.setOnClickListener { v -> homeSwitchAccount.setOnClickListener {
(CommonActivity.activity as MainActivity).showAccountSelectLinear(v?.context ?: return@setOnClickListener) activity?.showAccountSelectLinear()
} }
homeRandom.setOnClickListener { 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.debugException
import com.lagradost.cloudstream3.mvvm.observe import com.lagradost.cloudstream3.mvvm.observe
import com.lagradost.cloudstream3.ui.WatchType 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.home.HomeFragment.Companion.selectHomepage
import com.lagradost.cloudstream3.ui.result.FOCUS_SELF import com.lagradost.cloudstream3.ui.result.FOCUS_SELF
import com.lagradost.cloudstream3.ui.result.ResultViewModel2 import com.lagradost.cloudstream3.ui.result.ResultViewModel2
@ -477,8 +478,8 @@ class HomeParentItemAdapterPreview(
} }
} }
homeAccount?.setOnClickListener { v -> homeAccount?.setOnClickListener {
(activity as MainActivity).showAccountSelectLinear(v?.context ?: return@setOnClickListener) activity?.showAccountSelectLinear()
} }
(binding as? FragmentHomeHeadTvBinding)?.apply { (binding as? FragmentHomeHeadTvBinding)?.apply {