-
-
Save lelandrichardson/5b70430ea383600eb0dba5a62172148c to your computer and use it in GitHub Desktop.
Compose Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.Ambient | |
import androidx.navigation.NavController | |
object Ambients { | |
val Navigator = Ambient.of<ComposableNavigator>() | |
val NavController = Ambient.of<NavController>() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.compose.ambient | |
import androidx.compose.effectOf | |
import com.example.reddit.Ambients | |
fun <T> navArg(name: String) = effectOf<T> { | |
val nav = +ambient(Ambients.NavController) | |
val entry = nav.getBackStackEntry(nav.currentDestination!!.id) | |
val args = entry.arguments | |
val arg = args?.get(name) ?: error("No argument found with name $name") | |
@Suppress("UNCHECKED_CAST") | |
arg as T | |
} | |
fun <T> optionalNavArg(name: String) = effectOf<T?> { | |
val nav = +ambient(Ambients.NavController) | |
val entry = nav.getBackStackEntry(nav.currentDestination!!.id) | |
val args = entry.arguments | |
val arg = args?.get(name) | |
@Suppress("UNCHECKED_CAST") | |
arg as? T | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.compose.Composable | |
import androidx.compose.disposeComposition | |
import androidx.navigation.NavController | |
import androidx.navigation.NavGraphBuilder | |
import androidx.navigation.createGraph | |
import androidx.navigation.plusAssign | |
import androidx.ui.core.setContent | |
import com.example.reddit.Ambients | |
abstract class ComposeActivity : Activity() { | |
@Composable | |
abstract fun content(content: @Composable () -> Unit) | |
abstract fun NavGraphBuilder.graph() | |
abstract val initialRoute: Int | |
private val graphId = 100 | |
private var navController: NavController? = null | |
private val navigator = ComposableNavigator() | |
override fun onBackPressed() { | |
if (navController?.popBackStack() == true) { | |
Log.v("Navigation", "Successfully navigated back") | |
} else { | |
super.onBackPressed() | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val controller = NavController(this).also { | |
it.navigatorProvider += navigator | |
it.graph = it.createGraph(graphId, initialRoute) { | |
graph() | |
} | |
} | |
navController = controller | |
setContent { | |
Ambients.NavController.Provider(controller) { | |
Ambients.Navigator.Provider(navigator) { | |
content(navigator.current) | |
} | |
} | |
} | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
navController = null | |
disposeComposition() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example usage | |
class MainActivity : ComposeActivity() { | |
override val initialRoute = R.id.screen_one | |
override fun NavGraphBuilder.graph() { | |
route(R.id.screen_one) { | |
val arg1 = +optionalNavArg<String>("someArg") | |
ScreenOne(arg1) | |
} | |
route(R.id.screen_two) { | |
val id = +navArg<String>("id") | |
ScreenTwo(id) | |
} | |
route(R.id.screen_three) { | |
ScreenThree() | |
} | |
} | |
@Composable | |
override fun content(content: @Composable () -> Unit) { | |
AppTheme { | |
Scaffold { | |
content() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment