-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from futuredapp/feature/predictive-back-gesture
Predictive back gesture and navigation update
- Loading branch information
Showing
23 changed files
with
498 additions
and
105 deletions.
There are no files selected for viewing
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
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
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
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
12 changes: 12 additions & 0 deletions
12
app/src/main/kotlin/app/futured/androidprojecttemplate/navigation/NavRouter.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.futured.androidprojecttemplate.navigation | ||
|
||
interface NavRouter { | ||
fun popBackStack() | ||
fun navigateBack(popUpToDestination: Destination, inclusive: Boolean = false) | ||
|
||
fun navigateToDetail(title: String, subtitle: String? = null, value: String? = null) | ||
|
||
fun <T> navigateBackWithResult(key: String, value: T) | ||
fun <T> setCurrentResult(key: String, value: T) | ||
fun <T> subscribeForResult(key: String, callback: (T) -> Unit) | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/kotlin/app/futured/androidprojecttemplate/navigation/NavRouterImpl.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package app.futured.androidprojecttemplate.navigation | ||
|
||
import androidx.navigation.NavController | ||
import app.futured.androidprojecttemplate.tools.extensions.subscribeForResult | ||
import timber.log.Timber | ||
|
||
/** | ||
* Class that triggers navigation actions on provided [navController]. | ||
*/ | ||
class NavRouterImpl(private val navController: NavController) : NavRouter { | ||
override fun popBackStack() { | ||
navController.navigateUp() | ||
} | ||
|
||
override fun navigateBack(popUpToDestination: Destination, inclusive: Boolean) { | ||
navController.popBackStack(route = popUpToDestination.route, inclusive = inclusive) | ||
} | ||
|
||
override fun navigateToDetail(title: String, subtitle: String?, value: String?) = | ||
Destination.Detail.buildRoute(title, subtitle, value).execute() | ||
|
||
override fun <T> navigateBackWithResult(key: String, value: T) { | ||
navController.previousBackStackEntry?.savedStateHandle?.also { | ||
it[key] = value | ||
navController.popBackStack() | ||
} | ||
} | ||
|
||
override fun <T> setCurrentResult(key: String, value: T) { | ||
navController.currentBackStackEntry?.savedStateHandle?.also { | ||
it[key] = value | ||
} | ||
} | ||
|
||
override fun <T> subscribeForResult(key: String, callback: (T) -> Unit) { | ||
navController.currentBackStackEntry?.savedStateHandle?.subscribeForResult<T>(key) { callback(it) } | ||
} | ||
|
||
private fun String.execute( | ||
popUpToDestinationRoute: String? = null, | ||
isInclusive: Boolean = true, | ||
) { | ||
Timber.d("## Navigate to $this, popupTo $popUpToDestinationRoute, inclusive $isInclusive") | ||
if (popUpToDestinationRoute != null) { | ||
navController.navigate(this) { | ||
popUpTo(popUpToDestinationRoute) { | ||
inclusive = isInclusive | ||
} | ||
} | ||
} else { | ||
navController.navigate(this) | ||
} | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
app/src/main/kotlin/app/futured/androidprojecttemplate/navigation/NavigationDestinations.kt
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
app/src/main/kotlin/app/futured/androidprojecttemplate/tools/extensions/GeneralExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package app.futured.androidprojecttemplate.tools.extensions | ||
|
||
inline fun <T> T?.ifNull(defaultValue: () -> T): T = this ?: defaultValue.invoke() | ||
|
||
inline fun <T> withValue(receiver: T, block: T.() -> Unit) { | ||
receiver.block() | ||
} | ||
|
||
inline fun <reified T : Any> withNonNullValue(receiver: T?, block: (T) -> Unit) { | ||
if (receiver != null) block(receiver) | ||
} | ||
|
||
inline fun <T> safe(block: () -> T): T? { | ||
return try { | ||
block() | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
} | ||
|
||
fun <T> T?.orThrow(): T = this ?: error("UnexpectedError") // Dev error | ||
|
||
inline fun <reified T> ifElseNull(predicate: Boolean, block: () -> T?) = if (predicate) block.invoke() else null | ||
|
||
inline fun <T> T.runWith(block: (T) -> Unit) { | ||
block(this) | ||
} |
44 changes: 44 additions & 0 deletions
44
.../kotlin/app/futured/androidprojecttemplate/tools/extensions/SavedStateHandleExtensions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package app.futured.androidprojecttemplate.tools.extensions | ||
|
||
import android.util.Base64.NO_PADDING | ||
import android.util.Base64.NO_WRAP | ||
import android.util.Base64.URL_SAFE | ||
import android.util.Base64.decode | ||
import android.util.Base64.encodeToString | ||
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.SavedStateHandle | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
inline fun <reified T> SavedStateHandle.getSerializedArgument(key: String): T? = get<String>(key)?.deserializeFromNavArgument() | ||
|
||
inline fun <reified T> SavedStateHandle.getRequiredSerializedArgument(key: String): T = | ||
getSerializedArgument(key) ?: error("Required parameter is not present") | ||
|
||
inline fun <reified T> SavedStateHandle.getArgument(key: String): T? = safe { get<T>(key) } | ||
|
||
inline fun <reified T> SavedStateHandle.getRequiredArgument(key: String): T = | ||
getArgument(key) ?: getSerializedArgument(key) ?: error("Required parameter is not present") | ||
|
||
inline fun <reified T : Any> T.serializeAsNavArgument(): String? = | ||
encodeToString(Json.encodeToString(this).encodeToByteArray(), NO_PADDING or NO_WRAP or URL_SAFE) | ||
|
||
inline fun <reified T> String.deserializeFromNavArgument(): T? = | ||
safe { Json.decodeFromString(decode(this, NO_PADDING or NO_WRAP or URL_SAFE).decodeToString()) } | ||
|
||
fun <T> SavedStateHandle.subscribeForResult(key: String, callback: (T) -> Unit) { | ||
val liveData = this.getLiveData<T>(key) | ||
val observer = object : Observer<T> { | ||
override fun onChanged(t: T) { | ||
if (t != null) { | ||
val value = remove<T>(key) | ||
if (value != null) { | ||
callback(t) | ||
} | ||
liveData.removeObserver(this) | ||
subscribeForResult(key, callback) | ||
} | ||
} | ||
} | ||
liveData.observeForever(observer) | ||
} |
28 changes: 18 additions & 10 deletions
28
app/src/main/kotlin/app/futured/androidprojecttemplate/ui/NavGraph.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
package app.futured.androidprojecttemplate.ui | ||
|
||
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.rememberNavController | ||
import app.futured.androidprojecttemplate.navigation.Destination | ||
import app.futured.androidprojecttemplate.navigation.NavigationDestinations | ||
import app.futured.androidprojecttemplate.navigation.NavigationDestinationsImpl | ||
import app.futured.androidprojecttemplate.navigation.composable | ||
import app.futured.androidprojecttemplate.ui.screens.detail.DetailScreen | ||
import app.futured.androidprojecttemplate.ui.screens.home.HomeScreen | ||
import app.futured.androidprojecttemplate.navigation.NavRouter | ||
import app.futured.androidprojecttemplate.navigation.NavRouterImpl | ||
import app.futured.androidprojecttemplate.navigation.composableDialog | ||
import app.futured.androidprojecttemplate.navigation.composableScreen | ||
import app.futured.androidprojecttemplate.navigation.dialogs | ||
import app.futured.androidprojecttemplate.navigation.screens | ||
|
||
@Composable | ||
fun NavGraph( | ||
navController: NavHostController = rememberNavController(), | ||
navigation: NavigationDestinations = remember { NavigationDestinationsImpl(navController) }, | ||
navigation: NavRouter = remember { NavRouterImpl(navController) }, | ||
) { | ||
LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher?.let { | ||
navController.navigateUp() | ||
} | ||
|
||
NavHost( | ||
navController = navController, | ||
startDestination = Destination.Home.route, | ||
) { | ||
composable(Destination.Home) { | ||
HomeScreen(navigation) | ||
// Destinations without navbar at the bottom | ||
screens.forEach { destination -> | ||
composableScreen(destination) { destination.destinationScreen(navigation) } | ||
} | ||
|
||
composable(Destination.Detail) { | ||
DetailScreen(navigation) | ||
// Dialogs | ||
dialogs.forEach { destination -> | ||
composableDialog(destination) { destination.destinationScreen(navigation) } | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...c/main/kotlin/app/futured/androidprojecttemplate/ui/components/AddFloatingActionButton.kt
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
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
Oops, something went wrong.