Skip to content

Commit

Permalink
Refactor deep links without janky broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed Aug 7, 2021
1 parent 83730a5 commit d7a73c1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 74 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ dependencies {

implementation 'androidx.activity:activity-compose:1.3.1'
implementation 'androidx.core:core-ktx:1.7.0-alpha01'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation "androidx.webkit:webkit:1.4.0"

// Jetpack Compose
Expand Down
34 changes: 0 additions & 34 deletions app/src/main/kotlin/com/livetl/android/ui/BroadcastReceiver.kt

This file was deleted.

21 changes: 5 additions & 16 deletions app/src/main/kotlin/com/livetl/android/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.view.WindowCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.NavHostController
import com.google.accompanist.insets.ProvideWindowInsets
import com.livetl.android.ui.navigation.MainNavHost
import com.livetl.android.ui.navigation.Route
import com.livetl.android.ui.navigation.navigateToPlayer
import com.livetl.android.ui.theme.LiveTLTheme
import com.livetl.android.util.PreferencesHelper
import com.livetl.android.util.powerManager
Expand All @@ -27,6 +28,7 @@ class MainActivity : ComponentActivity() {
@Inject
lateinit var prefs: PreferencesHelper

private lateinit var navController: NavHostController
private var wakeLock: PowerManager.WakeLock? = null

@SuppressLint("WakelockTimeout")
Expand All @@ -43,7 +45,7 @@ class MainActivity : ComponentActivity() {
setContent {
LiveTLTheme {
ProvideWindowInsets {
MainNavHost(
navController = MainNavHost(
startRoute = startRoute,
setKeepScreenOn = this::setKeepScreenOn,
setFullscreen = this::setFullscreen,
Expand Down Expand Up @@ -103,20 +105,7 @@ class MainActivity : ComponentActivity() {
}
}

// We broadcast intents that the Compose nav host then handles
private fun handleVideoIntent(data: String?) {
data?.let {
val intent = Intent().apply {
action = DEEP_LINK_INTENT
putExtra(DEEP_LINK_INTENT_EXTRA, data)
}

LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
}

companion object {
const val DEEP_LINK_INTENT = "LiveTL::DeepLink"
const val DEEP_LINK_INTENT_EXTRA = "data"
data?.let { navController.navigateToPlayer(it) }
}
}
36 changes: 13 additions & 23 deletions app/src/main/kotlin/com/livetl/android/ui/navigation/MainNavHost.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.livetl.android.ui.navigation

import android.content.IntentFilter
import android.annotation.SuppressLint
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.compose.rememberNavController
import androidx.navigation.plusAssign
import com.google.accompanist.navigation.material.ModalBottomSheetLayout
import com.google.accompanist.navigation.material.rememberBottomSheetNavigator
import com.livetl.android.ui.BroadcastReceiver
import com.livetl.android.ui.MainActivity
import com.livetl.android.ui.screen.about.AboutScreen
import com.livetl.android.ui.screen.about.LicensesScreen
import com.livetl.android.ui.screen.home.HomeScreen
Expand All @@ -27,23 +26,24 @@ import com.livetl.android.ui.screen.settings.SettingsViewModel
import com.livetl.android.ui.screen.welcome.WelcomeScreen
import com.livetl.android.ui.screen.welcome.WelcomeViewModel

fun NavHostController.navigateToPlayer(urlOrId: String) {
navigate("${Route.Player.id}?urlOrId=$urlOrId") {
launchSingleTop = true
popUpTo(Route.Home.id) {}
}
}

@SuppressLint("ComposableNaming")
@Composable
fun MainNavHost(
startRoute: Route,
setKeepScreenOn: (Boolean) -> Unit,
setFullscreen: (Boolean) -> Unit,
) {
): NavHostController {
val navController = rememberNavController()
val bottomSheetNavigator = rememberBottomSheetNavigator()
navController.navigatorProvider += bottomSheetNavigator

fun navigateToPlayer(urlOrId: String) {
navController.navigate("${Route.Player.id}?urlOrId=$urlOrId") {
launchSingleTop = true
popUpTo(Route.Home.id) {}
}
}

fun navigateBack() {
navController.popBackStack()
}
Expand All @@ -59,7 +59,7 @@ fun MainNavHost(
val homeViewModel = hiltViewModel<HomeViewModel>()

HomeScreen(
navigateToPlayer = { navigateToPlayer(it) },
navigateToPlayer = { navController.navigateToPlayer(it) },
navigateToSettings = { navController.navigate(Route.Settings.id) },
navigateToAbout = { navController.navigate(Route.About.id) },
viewModel = homeViewModel,
Expand Down Expand Up @@ -112,15 +112,5 @@ fun MainNavHost(
}
}

BroadcastReceiver(
intentFilter = INTENT_FILTER,
receiver = {
val data = it.getStringExtra(MainActivity.DEEP_LINK_INTENT_EXTRA)!!
navigateToPlayer(data)
}
)
}

private val INTENT_FILTER = IntentFilter().apply {
addAction(MainActivity.DEEP_LINK_INTENT)
return navController
}

0 comments on commit d7a73c1

Please sign in to comment.