cloudstream/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt

224 lines
8.6 KiB
Kotlin
Raw Normal View History

2021-06-10 23:00:22 +00:00
package com.lagradost.cloudstream3.ui.settings
import android.os.Bundle
import android.util.Log
2022-05-15 18:38:32 +00:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
2022-06-04 00:11:32 +00:00
import androidx.annotation.StringRes
import androidx.core.view.children
2023-11-02 19:50:49 +00:00
import androidx.core.view.updateLayoutParams
2022-05-15 18:38:32 +00:00
import androidx.fragment.app.Fragment
2021-08-07 01:53:45 +00:00
import androidx.preference.Preference
2021-06-10 23:00:22 +00:00
import androidx.preference.PreferenceFragmentCompat
2023-11-02 19:50:49 +00:00
import com.google.android.material.appbar.AppBarLayout
2023-07-14 00:28:49 +00:00
import com.google.android.material.appbar.MaterialToolbar
2024-03-20 07:33:50 +00:00
import com.lagradost.cloudstream3.BuildConfig
2021-06-10 23:00:22 +00:00
import com.lagradost.cloudstream3.R
2023-07-14 00:28:49 +00:00
import com.lagradost.cloudstream3.databinding.MainSettingsBinding
2021-09-02 14:06:43 +00:00
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.accountManagers
2022-06-03 17:18:13 +00:00
import com.lagradost.cloudstream3.ui.home.HomeFragment
import com.lagradost.cloudstream3.ui.result.txt
import com.lagradost.cloudstream3.ui.settings.Globals.EMULATOR
import com.lagradost.cloudstream3.ui.settings.Globals.TV
import com.lagradost.cloudstream3.ui.settings.Globals.isLayout
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.UIHelper
import com.lagradost.cloudstream3.utils.UIHelper.clipboardHelper
2022-05-15 18:38:32 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.navigate
2022-06-03 17:18:13 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.setImage
2022-08-21 20:13:53 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.toPx
2021-11-01 15:33:46 +00:00
import java.io.File
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone
2021-09-02 14:06:43 +00:00
2022-05-15 18:38:32 +00:00
class SettingsFragment : Fragment() {
2021-10-30 18:14:12 +00:00
companion object {
2022-08-28 23:52:15 +00:00
2022-05-15 18:38:32 +00:00
fun PreferenceFragmentCompat?.getPref(id: Int): Preference? {
if (this == null) return null
return try {
findPreference(getString(id))
} catch (e: Exception) {
logError(e)
null
}
}
2022-08-21 20:13:53 +00:00
/**
* On TV you cannot properly scroll to the bottom of settings, this fixes that.
* */
fun PreferenceFragmentCompat.setPaddingBottom() {
if (isLayout(TV or EMULATOR)) {
2022-08-21 20:13:53 +00:00
listView?.setPadding(0, 0, 0, 100.toPx)
}
}
2023-11-02 19:50:49 +00:00
fun PreferenceFragmentCompat.setToolBarScrollFlags() {
if (isLayout(TV or EMULATOR)) {
2023-11-02 19:50:49 +00:00
val settingsAppbar = view?.findViewById<MaterialToolbar>(R.id.settings_toolbar)
2022-08-21 20:13:53 +00:00
2023-11-02 19:50:49 +00:00
settingsAppbar?.updateLayoutParams<AppBarLayout.LayoutParams> {
scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL
}
}
}
fun Fragment?.setToolBarScrollFlags() {
if (isLayout(TV or EMULATOR)) {
val settingsAppbar = this?.view?.findViewById<MaterialToolbar>(R.id.settings_toolbar)
settingsAppbar?.updateLayoutParams<AppBarLayout.LayoutParams> {
scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL
}
}
}
2022-08-07 21:11:13 +00:00
fun Fragment?.setUpToolbar(title: String) {
if (this == null) return
2023-07-14 00:28:49 +00:00
val settingsToolbar = view?.findViewById<MaterialToolbar>(R.id.settings_toolbar) ?: return
settingsToolbar.apply {
2022-08-07 21:11:13 +00:00
setTitle(title)
setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
setNavigationOnClickListener {
activity?.onBackPressedDispatcher?.onBackPressed()
2022-08-07 21:11:13 +00:00
}
}
UIHelper.fixPaddingStatusbar(settingsToolbar)
2022-08-07 21:11:13 +00:00
}
fun Fragment?.setUpToolbar(@StringRes title: Int) {
2022-06-04 00:11:32 +00:00
if (this == null) return
2023-07-14 00:28:49 +00:00
val settingsToolbar = view?.findViewById<MaterialToolbar>(R.id.settings_toolbar) ?: return
settingsToolbar.apply {
2022-06-04 00:11:32 +00:00
setTitle(title)
setNavigationIcon(R.drawable.ic_baseline_arrow_back_24)
children.firstOrNull { it is ImageView }?.tag = getString(R.string.tv_no_focus_tag)
2022-06-04 00:11:32 +00:00
setNavigationOnClickListener {
activity?.onBackPressedDispatcher?.onBackPressed()
2022-06-04 00:11:32 +00:00
}
}
UIHelper.fixPaddingStatusbar(settingsToolbar)
2022-06-04 00:11:32 +00:00
}
2022-05-15 18:38:32 +00:00
fun getFolderSize(dir: File): Long {
var size: Long = 0
dir.listFiles()?.let {
for (file in it) {
size += if (file.isFile) {
// System.out.println(file.getName() + " " + file.length());
file.length()
} else getFolderSize(file)
}
}
return size
}
2021-10-30 18:14:12 +00:00
}
2023-07-14 00:28:49 +00:00
override fun onDestroyView() {
binding = null
super.onDestroyView()
}
var binding: MainSettingsBinding? = null
2022-05-15 18:38:32 +00:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
2023-07-14 00:28:49 +00:00
): View {
val localBinding = MainSettingsBinding.inflate(inflater, container, false)
binding = localBinding
return localBinding.root
2021-11-08 18:13:39 +00:00
}
2022-05-15 18:38:32 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
fun navigate(id: Int) {
activity?.navigate(id, Bundle())
2021-11-07 22:10:19 +00:00
}
/** used to debug leaks
showToast(activity,"${VideoDownloadManager.downloadStatusEvent.size} :
${VideoDownloadManager.downloadProgressEvent.size}") **/
fun hasProfilePictureFromAccountManagers(accountManagers: List<AccountManager>): Boolean {
for (syncApi in accountManagers) {
val login = syncApi.loginInfo()
val pic = login?.profilePicture ?: continue
if (binding?.settingsProfilePic?.setImage(
pic,
errorImageDrawable = HomeFragment.errorProfilePic
) == true
) {
binding?.settingsProfileText?.text = login.name
return true // sync profile exists
}
}
return false // not syncing
}
// display local account information if not syncing
if (!hasProfilePictureFromAccountManagers(accountManagers)) {
val activity = activity ?: return
val currentAccount = try {
DataStoreHelper.accounts.firstOrNull {
it.keyIndex == DataStoreHelper.selectedKeyIndex
} ?: activity.let { DataStoreHelper.getDefaultAccount(activity) }
} catch (t: IllegalStateException) {
Log.e("AccountManager", "Activity not found", t)
null
2022-06-03 17:18:13 +00:00
}
binding?.settingsProfilePic?.setImage(currentAccount?.image)
binding?.settingsProfileText?.text = currentAccount?.name
2022-06-03 17:18:13 +00:00
}
2023-07-14 00:28:49 +00:00
binding?.apply {
listOf(
settingsGeneral to R.id.action_navigation_global_to_navigation_settings_general,
settingsPlayer to R.id.action_navigation_global_to_navigation_settings_player,
settingsCredits to R.id.action_navigation_global_to_navigation_settings_account,
settingsUi to R.id.action_navigation_global_to_navigation_settings_ui,
settingsProviders to R.id.action_navigation_global_to_navigation_settings_providers,
settingsUpdates to R.id.action_navigation_global_to_navigation_settings_updates,
settingsExtensions to R.id.action_navigation_global_to_navigation_settings_extensions,
2023-07-14 00:28:49 +00:00
).forEach { (view, navigationId) ->
view.apply {
setOnClickListener {
navigate(navigationId)
}
if (isLayout(TV)) {
2023-07-14 00:28:49 +00:00
isFocusable = true
isFocusableInTouchMode = true
}
2022-05-27 16:39:00 +00:00
}
}
2023-09-18 21:22:39 +00:00
// Default focus on TV
if (isLayout(TV)) {
2023-09-18 21:22:39 +00:00
settingsGeneral.requestFocus()
}
}
val appVersion = getString(R.string.app_version)
val commitInfo = getString(R.string.commit_hash)
val buildTimestamp = SimpleDateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
Locale.getDefault()
).apply { timeZone = TimeZone.getTimeZone("UTC")
}.format(Date(BuildConfig.BUILD_DATE)).replace("UTC", "")
binding?.buildDate?.text = buildTimestamp
binding?.appVersionInfo?.setOnLongClickListener {
clipboardHelper(txt(R.string.extension_version), "$appVersion $commitInfo $buildTimestamp")
true
}
2021-11-04 10:42:51 +00:00
}
}