Implemented navigateWhenReady for url handling.

This commit is contained in:
Koen J 2025-05-29 16:07:14 +02:00
commit dae50c3bc3

View file

@ -205,7 +205,7 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
} }
try { try {
runBlocking { lifecycleScope.launch {
handleUrlAll(content) handleUrlAll(content)
} }
} catch (e: Throwable) { } catch (e: Throwable) {
@ -282,7 +282,11 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
runBlocking { runBlocking {
StatePlatform.instance.updateAvailableClients(this@MainActivity); try {
StatePlatform.instance.updateAvailableClients(this@MainActivity);
} catch (e: Throwable) {
Logger.e(TAG, "Unhandled exception in updateAvailableClients", e)
}
} }
//Preload common files to memory //Preload common files to memory
@ -709,7 +713,7 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
"VIDEO" -> { "VIDEO" -> {
val url = intent.getStringExtra("VIDEO"); val url = intent.getStringExtra("VIDEO");
navigate(_fragVideoDetail, url); navigateWhenReady(_fragVideoDetail, url);
} }
"IMPORT_OPTIONS" -> { "IMPORT_OPTIONS" -> {
@ -727,11 +731,11 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
"Sources" -> { "Sources" -> {
runBlocking { runBlocking {
StatePlatform.instance.updateAvailableClients(this@MainActivity, true) //Ideally this is not needed.. StatePlatform.instance.updateAvailableClients(this@MainActivity, true) //Ideally this is not needed..
navigate(_fragMainSources); navigateWhenReady(_fragMainSources);
} }
}; };
"BROWSE_PLUGINS" -> { "BROWSE_PLUGINS" -> {
navigate(_fragBrowser, BrowserFragment.NavigateOptions("https://plugins.grayjay.app/phone.html", mapOf( navigateWhenReady(_fragBrowser, BrowserFragment.NavigateOptions("https://plugins.grayjay.app/phone.html", mapOf(
Pair("grayjay") { req -> Pair("grayjay") { req ->
StateApp.instance.contextOrNull?.let { StateApp.instance.contextOrNull?.let {
if (it is MainActivity) { if (it is MainActivity) {
@ -749,8 +753,12 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
try { try {
if (targetData != null) { if (targetData != null) {
runBlocking { lifecycleScope.launch(Dispatchers.Main) {
handleUrlAll(targetData) try {
handleUrlAll(targetData)
} catch (e: Throwable) {
Logger.e(TAG, "Unhandled exception in handleUrlAll", e)
}
} }
} }
} catch (ex: Throwable) { } catch (ex: Throwable) {
@ -778,10 +786,10 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
startActivity(intent); startActivity(intent);
} else if (url.startsWith("grayjay://video/")) { } else if (url.startsWith("grayjay://video/")) {
val videoUrl = url.substring("grayjay://video/".length); val videoUrl = url.substring("grayjay://video/".length);
navigate(_fragVideoDetail, videoUrl); navigateWhenReady(_fragVideoDetail, videoUrl);
} else if (url.startsWith("grayjay://channel/")) { } else if (url.startsWith("grayjay://channel/")) {
val channelUrl = url.substring("grayjay://channel/".length); val channelUrl = url.substring("grayjay://channel/".length);
navigate(_fragMainChannel, channelUrl); navigateWhenReady(_fragMainChannel, channelUrl);
} }
} }
@ -851,9 +859,9 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
Logger.i(TAG, "handleUrl(url=$url) found video client"); Logger.i(TAG, "handleUrl(url=$url) found video client");
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
if (position > 0) if (position > 0)
navigate(_fragVideoDetail, UrlVideoWithTime(url, position.toLong(), true)); navigateWhenReady(_fragVideoDetail, UrlVideoWithTime(url, position.toLong(), true));
else else
navigate(_fragVideoDetail, url); navigateWhenReady(_fragVideoDetail, url);
_fragVideoDetail.maximizeVideoDetail(true); _fragVideoDetail.maximizeVideoDetail(true);
} }
@ -861,7 +869,7 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
} else if (StatePlatform.instance.hasEnabledChannelClient(url)) { } else if (StatePlatform.instance.hasEnabledChannelClient(url)) {
Logger.i(TAG, "handleUrl(url=$url) found channel client"); Logger.i(TAG, "handleUrl(url=$url) found channel client");
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
navigate(_fragMainChannel, url); navigateWhenReady(_fragMainChannel, url);
delay(100); delay(100);
_fragVideoDetail.minimizeVideoDetail(); _fragVideoDetail.minimizeVideoDetail();
}; };
@ -869,7 +877,7 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
} else if (StatePlatform.instance.hasEnabledPlaylistClient(url)) { } else if (StatePlatform.instance.hasEnabledPlaylistClient(url)) {
Logger.i(TAG, "handleUrl(url=$url) found playlist client"); Logger.i(TAG, "handleUrl(url=$url) found playlist client");
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
navigate(_fragMainRemotePlaylist, url); navigateWhenReady(_fragMainRemotePlaylist, url);
delay(100); delay(100);
_fragVideoDetail.minimizeVideoDetail(); _fragVideoDetail.minimizeVideoDetail();
}; };
@ -1096,13 +1104,13 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
return fragCurrent is T; return fragCurrent is T;
} }
fun navigate(segment: MainFragment, parameter: Any? = null, withHistory: Boolean = true, isBack: Boolean = false) { fun navigateWhenReady(segment: MainFragment, parameter: Any? = null, withHistory: Boolean = true, isBack: Boolean = false) {
if (lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) { if (lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
navigateNow(segment, parameter, withHistory, isBack) navigate(segment, parameter, withHistory, isBack)
} else { } else {
lifecycleScope.launch { lifecycleScope.launch {
lifecycle.withStateAtLeast(Lifecycle.State.RESUMED) { lifecycle.withStateAtLeast(Lifecycle.State.RESUMED) {
navigateNow(segment, parameter, withHistory, isBack) navigate(segment, parameter, withHistory, isBack)
} }
} }
} }
@ -1113,7 +1121,7 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
* A parameter can be provided which becomes available in the onShow of said fragment * A parameter can be provided which becomes available in the onShow of said fragment
*/ */
@SuppressLint("CommitTransaction") @SuppressLint("CommitTransaction")
fun navigateNow(segment: MainFragment, parameter: Any? = null, withHistory: Boolean = true, isBack: Boolean = false) { fun navigate(segment: MainFragment, parameter: Any? = null, withHistory: Boolean = true, isBack: Boolean = false) {
Logger.i(TAG, "Navigate to $segment (parameter=$parameter, withHistory=$withHistory, isBack=$isBack)") Logger.i(TAG, "Navigate to $segment (parameter=$parameter, withHistory=$withHistory, isBack=$isBack)")
if (segment != fragCurrent) { if (segment != fragCurrent) {