Colton Idle
02/26/2024, 4:36 AMColton Idle
02/26/2024, 4:39 AMColton Idle
02/26/2024, 4:40 AMimport android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.multibackstack.ui.ComposeWebView
import com.example.multibackstack.ui.rememberWebViewState
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MaterialTheme {
val navController = rememberNavController()
Scaffold(
bottomBar = {
BottomNavigation {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
items.forEach { screen ->
BottomNavigationItem(
icon = {
Icon(
Icons.Filled.Favorite,
contentDescription = null
)
},
label = { Text(screen.resourceId) },
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
onClick = {
navController.navigate(screen.route) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
}
)
}
}
}
) { innerPadding ->
NavHost(
navController,
startDestination = Screen.Profile.route,
Modifier.padding(innerPadding)
) {
composable(Screen.Profile.route) { Profile(navController) }
composable(Screen.FriendsList.route) { FriendsList(navController) }
}
}
}
}
}
}
@Composable
fun Profile(navController: NavController) {
Column {
val state = rememberWebViewState("<https://www.nytimes.com/>")
ComposeWebView(
state
)
}
}
@Composable
fun FriendsList(navController: NavController) {
Column {
val state = rememberWebViewState("<https://cnn.com>")
ComposeWebView(
state
)
}
}
sealed class Screen(val route: String, val resourceId: String) {
object Profile : Screen("profile", "R.string.profile")
object FriendsList : Screen("friendslist", "R.string.friends_list")
}
val items = listOf(
Screen.Profile,
Screen.FriendsList,
)
Colton Idle
02/26/2024, 4:43 AMval state1 = rememberWebViewState("<https://www.nytimes.com/>")
val state2 = rememberWebViewState("<https://cnn.com>")
NavHost(
navController,
startDestination = Screen.Profile.route,
Modifier.padding(innerPadding)
) {
Ben Trengrove [G]
02/26/2024, 5:03 AMBen Trengrove [G]
02/26/2024, 5:03 AMBen Trengrove [G]
02/26/2024, 5:03 AMColton Idle
02/26/2024, 5:19 AMColton Idle
02/26/2024, 5:20 AMColton Idle
02/26/2024, 5:31 AMColton Idle
02/26/2024, 5:31 AMBen Trengrove [G]
02/26/2024, 5:42 AMColton Idle
03/06/2024, 7:26 PMLaunchedEffect(navigator) {
val bundle = webViewState.viewState
if (bundle == null) {
// This is the first time load, so load the home page.
navigator.loadUrl("<https://bbc.com>")
}
}
something like this
LaunchedEffect(navigator) {
val bundle = webViewState.viewState
if (bundle == null) {
// This is the first time load, so load the home page.
navigator.loadUrl("<https://bbc.com>", "AUTH" to currentTokenSnapshotState)
}
}
but when the token changes... the url is (of course) not reloaded since its inside of that LaunchedEffect + bundle == null.
What would be your best guess on how to have a dynamic token in the header?