Make account homepage persistent

This commit is contained in:
CranberrySoup 2023-09-15 17:51:15 +02:00
parent 71ae9a59a1
commit 65c927496d
6 changed files with 55 additions and 18 deletions

View file

@ -128,6 +128,7 @@ import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey import com.lagradost.cloudstream3.utils.DataStore.setKey
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.migrateResumeWatching import com.lagradost.cloudstream3.utils.DataStoreHelper.migrateResumeWatching
import com.lagradost.cloudstream3.utils.Event import com.lagradost.cloudstream3.utils.Event
import com.lagradost.cloudstream3.utils.IOnBackPressed import com.lagradost.cloudstream3.utils.IOnBackPressed
@ -305,6 +306,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
// kinda shitty solution, but cant com main->home otherwise for popups // kinda shitty solution, but cant com main->home otherwise for popups
val bookmarksUpdatedEvent = Event<Boolean>() val bookmarksUpdatedEvent = Event<Boolean>()
/**
* Used by data store helper to fully reload home when switching accounts
*/
val reloadHomeEvent = Event<Boolean>()
/** /**
@ -1187,7 +1192,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
} }
} else if (lastError == null) { } else if (lastError == null) {
ioSafe { ioSafe {
getKey<String>(USER_SELECTED_HOMEPAGE_API)?.let { homeApi -> DataStoreHelper.currentHomePage?.let { homeApi ->
mainPluginsLoadedEvent.invoke(loadSinglePlugin(this@MainActivity, homeApi)) mainPluginsLoadedEvent.invoke(loadSinglePlugin(this@MainActivity, homeApi))
} ?: run { } ?: run {
mainPluginsLoadedEvent.invoke(false) mainPluginsLoadedEvent.invoke(false)
@ -1548,6 +1553,11 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
migrateResumeWatching() migrateResumeWatching()
} }
getKey<String>(USER_SELECTED_HOMEPAGE_API)?.let { homepage ->
DataStoreHelper.currentHomePage = homepage
removeKey(USER_SELECTED_HOMEPAGE_API)
}
try { try {
if (getKey(HAS_DONE_SETUP_KEY, false) != true) { if (getKey(HAS_DONE_SETUP_KEY, false) != true) {
navController.navigate(R.id.navigation_setup_language) navController.navigate(R.id.navigation_setup_language)

View file

@ -69,7 +69,6 @@ import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount
import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import java.util.* import java.util.*
@ -669,7 +668,7 @@ class HomeFragment : Fragment() {
} }
homeViewModel.reloadStored() homeViewModel.reloadStored()
homeViewModel.loadAndCancel(getKey(USER_SELECTED_HOMEPAGE_API), false) homeViewModel.loadAndCancel(DataStoreHelper.currentHomePage, false)
//loadHomePage(false) //loadHomePage(false)
// nice profile pic on homepage // nice profile pic on homepage

View file

@ -49,7 +49,6 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getLastWatched import com.lagradost.cloudstream3.utils.DataStoreHelper.getLastWatched
import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultWatchState import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultWatchState
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.VideoDownloadHelper import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -426,23 +425,29 @@ class HomeViewModel : ViewModel() {
} }
private fun afterPluginsLoaded(forceReload: Boolean) { private fun afterPluginsLoaded(forceReload: Boolean) {
loadAndCancel(getKey(USER_SELECTED_HOMEPAGE_API), forceReload) loadAndCancel(DataStoreHelper.currentHomePage, forceReload)
} }
private fun afterMainPluginsLoaded(unused: Boolean = false) { private fun afterMainPluginsLoaded(unused: Boolean = false) {
loadAndCancel(getKey(USER_SELECTED_HOMEPAGE_API), false) loadAndCancel(DataStoreHelper.currentHomePage, false)
}
private fun reloadHome(unused: Boolean = false) {
loadAndCancel(DataStoreHelper.currentHomePage, true)
} }
init { init {
MainActivity.bookmarksUpdatedEvent += ::bookmarksUpdated MainActivity.bookmarksUpdatedEvent += ::bookmarksUpdated
MainActivity.afterPluginsLoadedEvent += ::afterPluginsLoaded MainActivity.afterPluginsLoadedEvent += ::afterPluginsLoaded
MainActivity.mainPluginsLoadedEvent += ::afterMainPluginsLoaded MainActivity.mainPluginsLoadedEvent += ::afterMainPluginsLoaded
MainActivity.reloadHomeEvent += ::reloadHome
} }
override fun onCleared() { override fun onCleared() {
MainActivity.bookmarksUpdatedEvent -= ::bookmarksUpdated MainActivity.bookmarksUpdatedEvent -= ::bookmarksUpdated
MainActivity.afterPluginsLoadedEvent -= ::afterPluginsLoaded MainActivity.afterPluginsLoadedEvent -= ::afterPluginsLoaded
MainActivity.mainPluginsLoadedEvent -= ::afterMainPluginsLoaded MainActivity.mainPluginsLoadedEvent -= ::afterMainPluginsLoaded
MainActivity.reloadHomeEvent -= ::reloadHome
super.onCleared() super.onCleared()
} }
@ -495,7 +500,7 @@ class HomeViewModel : ViewModel() {
val api = getApiFromNameNull(preferredApiName) val api = getApiFromNameNull(preferredApiName)
if (preferredApiName == noneApi.name) { if (preferredApiName == noneApi.name) {
// just set to random // just set to random
if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name) if (fromUI) DataStoreHelper.currentHomePage = noneApi.name
loadAndCancel(noneApi) loadAndCancel(noneApi)
} else if (preferredApiName == randomApi.name) { } else if (preferredApiName == randomApi.name) {
// randomize the api, if none exist like if not loaded or not installed // randomize the api, if none exist like if not loaded or not installed
@ -506,7 +511,7 @@ class HomeViewModel : ViewModel() {
} else { } else {
val apiRandom = validAPIs.random() val apiRandom = validAPIs.random()
loadAndCancel(apiRandom) loadAndCancel(apiRandom)
if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, apiRandom.name) if (fromUI) DataStoreHelper.currentHomePage = apiRandom.name
} }
} else if (api == null) { } else if (api == null) {
// API is not found aka not loaded or removed, post the loading // API is not found aka not loaded or removed, post the loading
@ -520,7 +525,7 @@ class HomeViewModel : ViewModel() {
} }
} else { } else {
// if the api is found, then set it to it and save key // if the api is found, then set it to it and save key
if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, api.name) if (fromUI) DataStoreHelper.currentHomePage = api.name
loadAndCancel(api) loadAndCancel(api)
} }
} }

View file

@ -14,7 +14,7 @@ import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
import com.lagradost.cloudstream3.utils.SubtitleHelper import com.lagradost.cloudstream3.utils.SubtitleHelper
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
@ -96,7 +96,7 @@ class SettingsProviders : PreferenceFragmentCompat() {
this.getString(R.string.prefer_media_type_key), this.getString(R.string.prefer_media_type_key),
selectedList.map { it.toString() }.toMutableSet() selectedList.map { it.toString() }.toMutableSet()
).apply() ).apply()
removeKey(USER_SELECTED_HOMEPAGE_API) DataStoreHelper.currentHomePage = null
//(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) } //(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
} }

View file

@ -15,8 +15,8 @@ import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.TvType import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.databinding.FragmentSetupMediaBinding import com.lagradost.cloudstream3.databinding.FragmentSetupMediaBinding
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
class SetupFragmentMedia : Fragment() { class SetupFragmentMedia : Fragment() {
@ -77,7 +77,7 @@ class SetupFragmentMedia : Fragment() {
.apply() .apply()
// Regenerate set homepage // Regenerate set homepage
removeKey(USER_SELECTED_HOMEPAGE_API) DataStoreHelper.currentHomePage = null
} }
} }

View file

@ -77,10 +77,28 @@ object DataStoreHelper {
var selectedKeyIndex by PreferenceDelegate("$TAG/account_key_index", 0) var selectedKeyIndex by PreferenceDelegate("$TAG/account_key_index", 0)
val currentAccount: String get() = selectedKeyIndex.toString() val currentAccount: String get() = selectedKeyIndex.toString()
private fun setAccount(account: Account) { /**
* Get or set the current account homepage.
* Setting this does not automatically reload the homepage.
*/
var currentHomePage: String?
get() = getKey("$currentAccount/$USER_SELECTED_HOMEPAGE_API")
set(value) {
val key = "$currentAccount/$USER_SELECTED_HOMEPAGE_API"
if (value == null) {
removeKey(key)
} else {
setKey(key, value)
}
}
private fun setAccount(account: Account, refreshHomePage: Boolean) {
selectedKeyIndex = account.keyIndex selectedKeyIndex = account.keyIndex
showToast(account.name) showToast(account.name)
MainActivity.bookmarksUpdatedEvent(true) MainActivity.bookmarksUpdatedEvent(true)
if (refreshHomePage) {
MainActivity.reloadHomeEvent(true)
}
} }
private fun editAccount(context: Context, account: Account, isNewAccount: Boolean) { private fun editAccount(context: Context, account: Account, isNewAccount: Boolean) {
@ -112,7 +130,7 @@ object DataStoreHelper {
accounts = currentAccounts.toTypedArray() accounts = currentAccounts.toTypedArray()
// update UI // update UI
setAccount(getDefaultAccount(context)) setAccount(getDefaultAccount(context), true)
MainActivity.bookmarksUpdatedEvent(true) MainActivity.bookmarksUpdatedEvent(true)
dialog?.dismissSafe() dialog?.dismissSafe()
} }
@ -161,8 +179,13 @@ object DataStoreHelper {
currentAccounts.add(currentEditAccount) currentAccounts.add(currentEditAccount)
} }
// Save the current homepage for new accounts
val currentHomePage = DataStoreHelper.currentHomePage
// set the new default account as well as add the key for the new account // set the new default account as well as add the key for the new account
setAccount(currentEditAccount) setAccount(currentEditAccount, false)
DataStoreHelper.currentHomePage = currentHomePage
accounts = currentAccounts.toTypedArray() accounts = currentAccounts.toTypedArray()
dialog.dismissSafe() dialog.dismissSafe()
@ -204,7 +227,7 @@ object DataStoreHelper {
) )
binding.profilesRecyclerview.adapter = WhoIsWatchingAdapter( binding.profilesRecyclerview.adapter = WhoIsWatchingAdapter(
selectCallBack = { account -> selectCallBack = { account ->
setAccount(account) setAccount(account, true)
builder.dismissSafe() builder.dismissSafe()
}, },
addAccountCallback = { addAccountCallback = {
@ -353,7 +376,7 @@ object DataStoreHelper {
removeKeys(folder2) removeKeys(folder2)
} }
fun deleteBookmarkedData(id : Int?) { fun deleteBookmarkedData(id: Int?) {
if (id == null) return if (id == null) return
removeKey("$currentAccount/$RESULT_WATCH_STATE", id.toString()) removeKey("$currentAccount/$RESULT_WATCH_STATE", id.toString())
removeKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString()) removeKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())