Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many UI fixes #606

Merged
merged 10 commits into from
Sep 15, 2023
25 changes: 20 additions & 5 deletions app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey
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.Event
import com.lagradost.cloudstream3.utils.IOnBackPressed
Expand Down Expand Up @@ -305,6 +306,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {

// kinda shitty solution, but cant com main->home otherwise for popups
val bookmarksUpdatedEvent = Event<Boolean>()
/**
* Used by data store helper to fully reload home when switching accounts
*/
val reloadHomeEvent = Event<Boolean>()


/**
Expand Down Expand Up @@ -539,6 +544,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
val isTrueTv = isTrueTvSettings()
navView.menu.findItem(R.id.navigation_library)?.isVisible = !isTrueTv
navRailView.menu.findItem(R.id.navigation_library)?.isVisible = !isTrueTv

// Hide downloads on TV
navView.menu.findItem(R.id.navigation_downloads)?.isVisible = !isTrueTv
navRailView.menu.findItem(R.id.navigation_downloads)?.isVisible = !isTrueTv
}
}

Expand Down Expand Up @@ -1112,16 +1121,17 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
newLocalBinding.root.viewTreeObserver.addOnGlobalFocusChangeListener { _, newFocus ->
// println("refocus $oldFocus -> $newFocus")
try {
val r = Rect(0,0,0,0)
val r = Rect(0, 0, 0, 0)
newFocus.getDrawingRect(r)
val x = r.centerX()
val y = r.centerY()
val dx = 0 //screenWidth / 2
val dy = screenHeight / 2
val r2 = Rect(x-dx,y-dy,x+dx,y+dy)
val r2 = Rect(x - dx, y - dy, x + dx, y + dy)
newFocus.requestRectangleOnScreen(r2, false)
// TvFocus.current =TvFocus.current.copy(y=y.toFloat())
} catch (_ : Throwable) { }
// TvFocus.current =TvFocus.current.copy(y=y.toFloat())
} catch (_: Throwable) {
}
TvFocus.updateFocusView(newFocus)
/*var focus = newFocus

Expand Down Expand Up @@ -1182,7 +1192,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
}
} else if (lastError == null) {
ioSafe {
getKey<String>(USER_SELECTED_HOMEPAGE_API)?.let { homeApi ->
DataStoreHelper.currentHomePage?.let { homeApi ->
mainPluginsLoadedEvent.invoke(loadSinglePlugin(this@MainActivity, homeApi))
} ?: run {
mainPluginsLoadedEvent.invoke(false)
Expand Down Expand Up @@ -1543,6 +1553,11 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
migrateResumeWatching()
}

getKey<String>(USER_SELECTED_HOMEPAGE_API)?.let { homepage ->
DataStoreHelper.currentHomePage = homepage
removeKey(USER_SELECTED_HOMEPAGE_API)
}

try {
if (getKey(HAS_DONE_SETUP_KEY, false) != true) {
navController.navigate(R.id.navigation_setup_language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.ui
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.core.view.children
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs
Expand Down Expand Up @@ -70,8 +71,8 @@ class GrdLayoutManager(val context: Context, _spanCount: Int) :
val orientation = this.orientation

// fixes arabic by inverting left and right layout focus
val correctDirection = if(this.isLayoutRTL) {
when(direction) {
val correctDirection = if (this.isLayoutRTL) {
when (direction) {
View.FOCUS_RIGHT -> View.FOCUS_LEFT
View.FOCUS_LEFT -> View.FOCUS_RIGHT
else -> direction
Expand All @@ -83,12 +84,15 @@ class GrdLayoutManager(val context: Context, _spanCount: Int) :
View.FOCUS_DOWN -> {
return spanCount
}

View.FOCUS_UP -> {
return -spanCount
}

View.FOCUS_RIGHT -> {
return 1
}

View.FOCUS_LEFT -> {
return -1
}
Expand All @@ -98,12 +102,15 @@ class GrdLayoutManager(val context: Context, _spanCount: Int) :
View.FOCUS_DOWN -> {
return 1
}

View.FOCUS_UP -> {
return -1
}

View.FOCUS_RIGHT -> {
return spanCount
}

View.FOCUS_LEFT -> {
return -spanCount
}
Expand Down Expand Up @@ -155,4 +162,32 @@ class AutofitRecyclerView @JvmOverloads constructor(context: Context, attrs: Att

layoutManager = manager
}
}

/**
* Recyclerview wherein the max item width or height is set by the biggest view to prevent inconsistent view sizes.
*/
class MaxRecyclerView(ctx: Context, attrs: AttributeSet) : RecyclerView(ctx, attrs) {
private var biggestObserved: Int = 0
private val orientation = LayoutManager.getProperties(context, attrs, 0, 0).orientation
private val isHorizontal = orientation == HORIZONTAL
private fun View.updateMaxSize() {
if (isHorizontal) {
this.minimumHeight = biggestObserved
} else {
this.minimumWidth = biggestObserved
}
}

override fun onChildAttachedToWindow(child: View) {
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
val observed = if (isHorizontal) child.measuredHeight else child.measuredWidth
if (observed > biggestObserved) {
biggestObserved = observed
children.forEach { it.updateMaxSize() }
} else {
child.updateMaxSize()
}
super.onChildAttachedToWindow(child)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.UIHelper.getSpanCount
import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIconsAndNoStringRes
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API

import java.util.*

Expand Down Expand Up @@ -669,7 +668,7 @@ class HomeFragment : Fragment() {
}

homeViewModel.reloadStored()
homeViewModel.loadAndCancel(getKey(USER_SELECTED_HOMEPAGE_API), false)
homeViewModel.loadAndCancel(DataStoreHelper.currentHomePage, false)
//loadHomePage(false)

// nice profile pic on homepage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.getBookmarkedData
import com.lagradost.cloudstream3.utils.DataStoreHelper.getLastWatched
import com.lagradost.cloudstream3.utils.DataStoreHelper.getResultWatchState
import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.VideoDownloadHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -426,23 +425,29 @@ class HomeViewModel : ViewModel() {
}

private fun afterPluginsLoaded(forceReload: Boolean) {
loadAndCancel(getKey(USER_SELECTED_HOMEPAGE_API), forceReload)
loadAndCancel(DataStoreHelper.currentHomePage, forceReload)
}

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 {
MainActivity.bookmarksUpdatedEvent += ::bookmarksUpdated
MainActivity.afterPluginsLoadedEvent += ::afterPluginsLoaded
MainActivity.mainPluginsLoadedEvent += ::afterMainPluginsLoaded
MainActivity.reloadHomeEvent += ::reloadHome
}

override fun onCleared() {
MainActivity.bookmarksUpdatedEvent -= ::bookmarksUpdated
MainActivity.afterPluginsLoadedEvent -= ::afterPluginsLoaded
MainActivity.mainPluginsLoadedEvent -= ::afterMainPluginsLoaded
MainActivity.reloadHomeEvent -= ::reloadHome
super.onCleared()
}

Expand Down Expand Up @@ -495,7 +500,7 @@ class HomeViewModel : ViewModel() {
val api = getApiFromNameNull(preferredApiName)
if (preferredApiName == noneApi.name) {
// just set to random
if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name)
if (fromUI) DataStoreHelper.currentHomePage = noneApi.name
loadAndCancel(noneApi)
} else if (preferredApiName == randomApi.name) {
// randomize the api, if none exist like if not loaded or not installed
Expand All @@ -506,7 +511,7 @@ class HomeViewModel : ViewModel() {
} else {
val apiRandom = validAPIs.random()
loadAndCancel(apiRandom)
if (fromUI) setKey(USER_SELECTED_HOMEPAGE_API, apiRandom.name)
if (fromUI) DataStoreHelper.currentHomePage = apiRandom.name
}
} else if (api == null) {
// API is not found aka not loaded or removed, post the loading
Expand All @@ -520,7 +525,7 @@ class HomeViewModel : ViewModel() {
}
} else {
// 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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class ResultFragmentTv : Fragment() {
isVisible = true
}

this.animate().alpha(if (turnVisible) 1.0f else 0.0f).apply {
this.animate().alpha(if (turnVisible) 0.97f else 0.0f).apply {
duration = 200
interpolator = DecelerateInterpolator()
setListener(object : Animator.AnimatorListener {
Expand Down Expand Up @@ -294,9 +294,9 @@ class ResultFragmentTv : Fragment() {
toggleEpisodes(true)
binding?.apply {
val views = listOf(
resultDubSelection,
resultSeasonSelection,
resultRangeSelection,
resultDubSelection,
resultEpisodes,
resultPlayTrailer,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,8 @@ class ResultViewModel2 : ViewModel() {
val episodeNumber = episodes[currentIndex].episode
if (episodeNumber < currentMin) {
currentMin = episodeNumber
} else if (episodeNumber > currentMax) {
}
if (episodeNumber > currentMax) {
currentMax = episodeNumber
}
++currentIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.setPaddingBottom
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.SubtitleHelper
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
Expand Down Expand Up @@ -96,7 +96,7 @@ class SettingsProviders : PreferenceFragmentCompat() {
this.getString(R.string.prefer_media_type_key),
selectedList.map { it.toString() }.toMutableSet()
).apply()
removeKey(USER_SELECTED_HOMEPAGE_API)
DataStoreHelper.currentHomePage = null
//(context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.TvType
import com.lagradost.cloudstream3.databinding.FragmentSetupMediaBinding
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API


class SetupFragmentMedia : Fragment() {
Expand Down Expand Up @@ -77,7 +77,7 @@ class SetupFragmentMedia : Fragment() {
.apply()

// Regenerate set homepage
removeKey(USER_SELECTED_HOMEPAGE_API)
DataStoreHelper.currentHomePage = null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,28 @@ object DataStoreHelper {
var selectedKeyIndex by PreferenceDelegate("$TAG/account_key_index", 0)
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
showToast(account.name)
MainActivity.bookmarksUpdatedEvent(true)
if (refreshHomePage) {
MainActivity.reloadHomeEvent(true)
}
}

private fun editAccount(context: Context, account: Account, isNewAccount: Boolean) {
Expand Down Expand Up @@ -112,7 +130,7 @@ object DataStoreHelper {
accounts = currentAccounts.toTypedArray()

// update UI
setAccount(getDefaultAccount(context))
setAccount(getDefaultAccount(context), true)
MainActivity.bookmarksUpdatedEvent(true)
dialog?.dismissSafe()
}
Expand Down Expand Up @@ -161,8 +179,13 @@ object DataStoreHelper {
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
setAccount(currentEditAccount)
setAccount(currentEditAccount, false)
DataStoreHelper.currentHomePage = currentHomePage

accounts = currentAccounts.toTypedArray()

dialog.dismissSafe()
Expand Down Expand Up @@ -204,7 +227,7 @@ object DataStoreHelper {
)
binding.profilesRecyclerview.adapter = WhoIsWatchingAdapter(
selectCallBack = { account ->
setAccount(account)
setAccount(account, true)
builder.dismissSafe()
},
addAccountCallback = {
Expand Down Expand Up @@ -353,7 +376,7 @@ object DataStoreHelper {
removeKeys(folder2)
}

fun deleteBookmarkedData(id : Int?) {
fun deleteBookmarkedData(id: Int?) {
if (id == null) return
removeKey("$currentAccount/$RESULT_WATCH_STATE", id.toString())
removeKey("$currentAccount/$RESULT_WATCH_STATE_DATA", id.toString())
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/drawable/episodes_shadow.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="@color/transparent"
android:endColor="?attr/primaryBlackBackground"/>
android:centerColor="?attr/primaryBlackBackground"
android:centerX="0.2"
android:endColor="?attr/primaryBlackBackground"
android:startColor="@color/transparent" />
</shape>
Loading
Loading