Fix homepage and fix some tv issues

This commit is contained in:
Blatzar 2022-08-16 21:07:49 +02:00
parent ef165ad4cb
commit 318ee08366
9 changed files with 90 additions and 32 deletions

View file

@ -11,6 +11,9 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.WindowManager import android.view.WindowManager
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.IdRes import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible import androidx.core.view.isVisible
@ -33,6 +36,7 @@ import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.initAll import com.lagradost.cloudstream3.APIHolder.initAll
import com.lagradost.cloudstream3.APIHolder.updateHasTrailers import com.lagradost.cloudstream3.APIHolder.updateHasTrailers
import com.lagradost.cloudstream3.CommonActivity.currentToast
import com.lagradost.cloudstream3.CommonActivity.loadThemes import com.lagradost.cloudstream3.CommonActivity.loadThemes
import com.lagradost.cloudstream3.CommonActivity.onColorSelectedEvent import com.lagradost.cloudstream3.CommonActivity.onColorSelectedEvent
import com.lagradost.cloudstream3.CommonActivity.onDialogDismissedEvent import com.lagradost.cloudstream3.CommonActivity.onDialogDismissedEvent
@ -85,7 +89,10 @@ import com.lagradost.cloudstream3.plugins.PluginManager
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringRepo import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringRepo
import com.lagradost.cloudstream3.ui.setup.SetupFragmentExtensions import com.lagradost.cloudstream3.ui.setup.SetupFragmentExtensions
import com.lagradost.cloudstream3.utils.AppUtils.loadRepository import com.lagradost.cloudstream3.utils.AppUtils.loadRepository
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.Event import com.lagradost.cloudstream3.utils.Event
import kotlinx.coroutines.delay
import java.lang.ref.WeakReference
const val VLC_PACKAGE = "org.videolan.vlc" const val VLC_PACKAGE = "org.videolan.vlc"
@ -293,7 +300,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (VLC_REQUEST_CODE == requestCode) { if (requestCode == VLC_REQUEST_CODE) {
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null) {
val pos: Long = val pos: Long =
data.getLongExtra( data.getLongExtra(
@ -677,13 +684,25 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
&& PluginManager.getPluginsLocal().isEmpty() && PluginManager.getPluginsLocal().isEmpty()
// && PREBUILT_REPOSITORIES.isNotEmpty() // && PREBUILT_REPOSITORIES.isNotEmpty()
) { ) {
navController.navigate(R.id.navigation_setup_extensions, SetupFragmentExtensions.newInstance(false)) navController.navigate(
R.id.navigation_setup_extensions,
SetupFragmentExtensions.newInstance(false)
)
} }
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
} finally { } finally {
setKey(HAS_DONE_SETUP_KEY, true) setKey(HAS_DONE_SETUP_KEY, true)
} }
// Used to check current focus for TV
// main {
// while (true) {
// delay(1000)
// println("Current focus: $currentFocus")
// }
// }
/* /*
val relativePath = (Environment.DIRECTORY_DOWNLOADS) + File.separatorChar val relativePath = (Environment.DIRECTORY_DOWNLOADS) + File.separatorChar

View file

@ -60,8 +60,7 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper.deleteAllResumeStateIds
import com.lagradost.cloudstream3.utils.DataStoreHelper.removeLastWatched import com.lagradost.cloudstream3.utils.DataStoreHelper.removeLastWatched
import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultWatchState import com.lagradost.cloudstream3.utils.DataStoreHelper.setResultWatchState
import com.lagradost.cloudstream3.utils.Event import com.lagradost.cloudstream3.utils.Event
import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.HOMEPAGE_API
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showOptionSelectStringRes import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showOptionSelectStringRes
import com.lagradost.cloudstream3.utils.SubtitleHelper.getFlagFromIso import com.lagradost.cloudstream3.utils.SubtitleHelper.getFlagFromIso
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
@ -455,8 +454,10 @@ class HomeFragment : Fragment() {
homeViewModel.loadStoredData(list) homeViewModel.loadStoredData(list)
} }
private fun loadHomePage(successful: Boolean = true) { private fun loadHomePage(successful: Boolean = false) {
val apiName = context?.getKey<String>(HOMEPAGE_API) val apiName = context?.getKey<String>(USER_SELECTED_HOMEPAGE_API)
println("LOAD HOMEPAGE $successful $apiName ${homeViewModel.apiName.value}")
if (homeViewModel.apiName.value != apiName || apiName == null) { if (homeViewModel.apiName.value != apiName || apiName == null) {
//println("Caught home: " + homeViewModel.apiName.value + " at " + apiName) //println("Caught home: " + homeViewModel.apiName.value + " at " + apiName)
homeViewModel.loadAndCancel(apiName) homeViewModel.loadAndCancel(apiName)
@ -512,7 +513,7 @@ class HomeFragment : Fragment() {
observe(homeViewModel.apiName) { apiName -> observe(homeViewModel.apiName) { apiName ->
currentApiName = apiName currentApiName = apiName
setKey(HOMEPAGE_API, apiName) // setKey(USER_SELECTED_HOMEPAGE_API, apiName)
home_api_fab?.text = apiName home_api_fab?.text = apiName
home_provider_name?.text = apiName home_provider_name?.text = apiName
try { try {

View file

@ -31,7 +31,7 @@ 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.HOMEPAGE_API 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
@ -227,7 +227,8 @@ class HomeViewModel : ViewModel() {
expandable.clear() expandable.clear()
data.value.forEach { home -> data.value.forEach { home ->
home?.items?.forEach { list -> home?.items?.forEach { list ->
val filteredList = context?.filterHomePageListByFilmQuality(list) ?: list val filteredList =
context?.filterHomePageListByFilmQuality(list) ?: list
expandable[list.name] = expandable[list.name] =
ExpandableHomepageList(filteredList, 1, home.hasNext) ExpandableHomepageList(filteredList, 1, home.hasNext)
} }
@ -244,7 +245,9 @@ class HomeViewModel : ViewModel() {
.toList() .toList()
if (currentList.isNotEmpty()) { if (currentList.isNotEmpty()) {
val randomItems = context?.filterSearchResultByFilmQuality(currentList.shuffled()) ?: currentList.shuffled() val randomItems =
context?.filterSearchResultByFilmQuality(currentList.shuffled())
?: currentList.shuffled()
_randomItems.postValue(randomItems) _randomItems.postValue(randomItems)
} }
@ -266,18 +269,22 @@ class HomeViewModel : ViewModel() {
fun loadAndCancel(preferredApiName: String?) = viewModelScope.launch { fun loadAndCancel(preferredApiName: String?) = viewModelScope.launch {
val api = getApiFromNameNull(preferredApiName) val api = getApiFromNameNull(preferredApiName)
if (preferredApiName == noneApi.name) if (preferredApiName == noneApi.name){
setKey(USER_SELECTED_HOMEPAGE_API, noneApi.name)
loadAndCancel(noneApi) loadAndCancel(noneApi)
}
else if (preferredApiName == randomApi.name || api == null) { else if (preferredApiName == randomApi.name || api == null) {
val validAPIs = context?.filterProviderByPreferredMedia() val validAPIs = context?.filterProviderByPreferredMedia()
if (validAPIs.isNullOrEmpty()) { if (validAPIs.isNullOrEmpty()) {
// Do not set USER_SELECTED_HOMEPAGE_API when there is no plugins loaded
loadAndCancel(noneApi) loadAndCancel(noneApi)
} else { } else {
val apiRandom = validAPIs.random() val apiRandom = validAPIs.random()
loadAndCancel(apiRandom) loadAndCancel(apiRandom)
setKey(HOMEPAGE_API, apiRandom.name) setKey(USER_SELECTED_HOMEPAGE_API, apiRandom.name)
} }
} else { } else {
setKey(USER_SELECTED_HOMEPAGE_API, api.name)
loadAndCancel(api) loadAndCancel(api)
} }
} }

View file

@ -16,7 +16,7 @@ import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.ui.APIRepository 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.setUpToolbar import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.HOMEPAGE_API import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
@ -116,7 +116,7 @@ class SettingsLang : PreferenceFragmentCompat() {
.putInt(getString(R.string.prefer_media_type_key), prefValues[it]) .putInt(getString(R.string.prefer_media_type_key), prefValues[it])
.apply() .apply()
removeKey(HOMEPAGE_API) removeKey(USER_SELECTED_HOMEPAGE_API)
// (context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) } // (context ?: AcraApplication.context)?.let { ctx -> app.initClient(ctx) }
} }
return@setOnPreferenceClickListener true return@setOnPreferenceClickListener true

View file

@ -11,7 +11,7 @@ import androidx.navigation.fragment.findNavController
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.utils.DataStore.removeKey import com.lagradost.cloudstream3.utils.DataStore.removeKey
import com.lagradost.cloudstream3.utils.HOMEPAGE_API import com.lagradost.cloudstream3.utils.USER_SELECTED_HOMEPAGE_API
import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar
import kotlinx.android.synthetic.main.fragment_setup_media.* import kotlinx.android.synthetic.main.fragment_setup_media.*
@ -52,7 +52,7 @@ class SetupFragmentMedia : Fragment() {
.apply() .apply()
// Regenerate set homepage // Regenerate set homepage
removeKey(HOMEPAGE_API) removeKey(USER_SELECTED_HOMEPAGE_API)
} }
next_btt?.setOnClickListener { next_btt?.setOnClickListener {

View file

@ -2,6 +2,7 @@ package com.lagradost.cloudstream3.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.Activity.RESULT_CANCELED
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -21,6 +22,7 @@ import android.provider.MediaStore
import android.text.Spanned import android.text.Spanned
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -41,13 +43,11 @@ import com.google.android.gms.cast.framework.CastState
import com.google.android.gms.common.ConnectionResult import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.common.wrappers.Wrappers import com.google.android.gms.common.wrappers.Wrappers
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.MainActivity.Companion.afterRepositoryLoadedEvent import com.lagradost.cloudstream3.MainActivity.Companion.afterRepositoryLoadedEvent
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.SearchResponse
import com.lagradost.cloudstream3.isMovieType
import com.lagradost.cloudstream3.mapper
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.plugins.RepositoryManager import com.lagradost.cloudstream3.plugins.RepositoryManager
import com.lagradost.cloudstream3.ui.WebviewFragment import com.lagradost.cloudstream3.ui.WebviewFragment
import com.lagradost.cloudstream3.ui.result.ResultFragment import com.lagradost.cloudstream3.ui.result.ResultFragment
@ -239,6 +239,7 @@ object AppUtils {
) )
} }
} }
fun Activity.loadRepository(url: String) { fun Activity.loadRepository(url: String) {
ioSafe { ioSafe {
val repo = RepositoryManager.parseRepository(url) ?: return@ioSafe val repo = RepositoryManager.parseRepository(url) ?: return@ioSafe
@ -259,6 +260,18 @@ object AppUtils {
} }
} }
private fun Context.hasWebView(): Boolean {
return this.packageManager.hasSystemFeature("android.software.webview")
}
private fun openWebView(fragment: Fragment?, url: String) {
if (fragment?.context?.hasWebView() == true)
normalSafeApiCall {
fragment
.findNavController()
.navigate(R.id.navigation_webview, WebviewFragment.newInstance(url))
}
}
/** /**
* If fallbackWebview is true and a fragment is supplied then it will open a webview with the url if the browser fails. * If fallbackWebview is true and a fragment is supplied then it will open a webview with the url if the browser fails.
@ -266,23 +279,34 @@ object AppUtils {
fun Context.openBrowser( fun Context.openBrowser(
url: String, url: String,
fallbackWebview: Boolean = false, fallbackWebview: Boolean = false,
fragment: Fragment? = null fragment: Fragment? = null,
) { ) {
try { try {
val intent = Intent(Intent.ACTION_VIEW) val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url) intent.data = Uri.parse(url)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
// activityResultRegistry is used to fall back to webview if a browser is missing
// On older versions the startActivity just crashes, but on newer android versions
// You need to check the result to make sure it failed
val activityResultRegistry = fragment?.activity?.activityResultRegistry
if (activityResultRegistry != null) {
activityResultRegistry.register(
url,
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_CANCELED && fallbackWebview) {
openWebView(fragment, url)
}
}.launch(intent)
} else {
ContextCompat.startActivity(this, intent, null) ContextCompat.startActivity(this, intent, null)
}
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
if (fallbackWebview) { if (fallbackWebview) {
try { openWebView(fragment, url)
fragment
?.findNavController()
?.navigate(R.id.navigation_webview, WebviewFragment.newInstance(url))
} catch (e: Exception) {
logError(e)
}
} }
} }
} }

View file

@ -13,7 +13,7 @@ const val DOWNLOAD_HEADER_CACHE = "download_header_cache"
//const val WATCH_HEADER_CACHE = "watch_header_cache" //const val WATCH_HEADER_CACHE = "watch_header_cache"
const val DOWNLOAD_EPISODE_CACHE = "download_episode_cache" const val DOWNLOAD_EPISODE_CACHE = "download_episode_cache"
const val VIDEO_PLAYER_BRIGHTNESS = "video_player_alpha_key" const val VIDEO_PLAYER_BRIGHTNESS = "video_player_alpha_key"
const val HOMEPAGE_API = "home_api_used" const val USER_SELECTED_HOMEPAGE_API = "home_api_used"
const val USER_PROVIDER_API = "user_custom_sites" const val USER_PROVIDER_API = "user_custom_sites"
const val PREFERENCES_NAME = "rebuild_preference" const val PREFERENCES_NAME = "rebuild_preference"

View file

@ -163,7 +163,7 @@
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/add_repo_button" android:id="@+id/add_repo_button"
android:nextFocusUp="@id/list_repositories" android:foreground="@drawable/outline_drawable"
android:nextFocusDown="@id/plugin_storage_appbar" android:nextFocusDown="@id/plugin_storage_appbar"
style="@style/ExtendedFloatingActionButton" style="@style/ExtendedFloatingActionButton"
android:text="@string/add_repository" android:text="@string/add_repository"

View file

@ -6,6 +6,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:nextFocusRight="@id/action_button"
android:background="@drawable/outline_drawable"
android:padding="20dp"> android:padding="20dp">
<ImageView <ImageView
@ -77,11 +79,16 @@
tools:visibility="visible" /> tools:visibility="visible" />
<ImageView <ImageView
android:padding="10dp"
android:background="@drawable/outline_drawable"
android:nextFocusLeft="@id/repository_item_root"
android:clickable="true"
android:id="@+id/action_button" android:id="@+id/action_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end" android:layout_gravity="center_vertical|end"
android:layout_marginStart="10dp" android:layout_marginStart="10dp"
tools:src="@drawable/ic_baseline_add_24" /> tools:src="@drawable/ic_baseline_add_24"
android:focusable="true" />
</LinearLayout> </LinearLayout>