Skip to content

Commit

Permalink
Implement AppStorage; change default nav stack and tab animations to …
Browse files Browse the repository at this point in the history
…match iOS (none and slide)
  • Loading branch information
marcprux committed Sep 26, 2023
1 parent a1869d0 commit 17868a4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
20 changes: 17 additions & 3 deletions Sources/SkipUI/SkipUI/Containers/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// as published by the Free Software Foundation https://fsf.org

#if SKIP
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -73,8 +77,14 @@ public struct NavigationStack<Root> : View where Root: View {

// SKIP INSERT: val providedNavigator = LocalNavigator provides navigator.value
CompositionLocalProvider(providedNavigator) {
NavHost(navController: navController, startDestination: Navigator.rootRoute, modifier: context.modifier) {
composable(route: Navigator.rootRoute) { entry in
NavHost(navController: navController,
startDestination: Navigator.rootRoute,
//enterTransition: { EnterTransition.None },
exitTransition: { ExitTransition.None },
modifier: context.modifier) {
composable(route: Navigator.rootRoute,
enterTransition: { slideInHorizontally() },
exitTransition: { slideOutHorizontally() }) { entry in
if let state = navigator.value.state(for: entry) {
let entryContext = context.content(stateSaver: state.stateSaver)
ComposeEntry(navController: navController, destinations: destinations, destinationsDidChange: preferencesDidChange, isRoot: true, context: entryContext) { context in
Expand All @@ -86,7 +96,11 @@ public struct NavigationStack<Root> : View where Root: View {
composable(route: Navigator.route(for: destinationIndex, valueString: "{identifier}"), arguments: listOf(navArgument("identifier") { type = NavType.StringType })) { entry in
if let state = navigator.value.state(for: entry), let targetValue = state.targetValue {
let entryContext = context.content(stateSaver: state.stateSaver)
ComposeEntry(navController: navController, destinations: destinations, destinationsDidChange: preferencesDidChange, isRoot: false, context: entryContext) { context in
ComposeEntry(navController: navController,
destinations: destinations,
destinationsDidChange: preferencesDidChange,
isRoot: false,
context: entryContext) { context in
state.destination?(targetValue).Compose(context: context)
}
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/SkipUI/SkipUI/Containers/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// as published by the Free Software Foundation https://fsf.org

#if SKIP
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -21,6 +23,7 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController

#endif

public struct TabView<Content> : View where Content : View {
Expand Down Expand Up @@ -84,7 +87,10 @@ public struct TabView<Content> : View where Content : View {
}
}
) { padding in
NavHost(navController, startDestination: "0") {
NavHost(navController,
startDestination: "0",
enterTransition: { EnterTransition.None },
exitTransition: { ExitTransition.None }) {
// Use a constant number of routes. Changing routes causes a NavHost to reset its state
for tabIndex in 0..<100 {
composable(String(describing: tabIndex)) {
Expand Down
31 changes: 30 additions & 1 deletion Sources/SkipUI/SkipUI/Properties/AppStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,36 @@
// under the terms of the GNU Lesser General Public License 3.0
// as published by the Free Software Foundation https://fsf.org

// TODO: Process for use in SkipUI
#if SKIP
// Model AppStorage as a class rather than struct to avoid copy overhead on mutation
public final class AppStorage<Value> {
private var onUpdate: ((Value) -> Void)?

public init(initialValue: Value) {
wrappedValue = initialValue
}

public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}

public var wrappedValue: Value {
didSet {
onUpdate?(wrappedValue)
}
}

public var projectedValue: Binding<Value> {
return Binding(get: { self.wrappedValue }, set: { self.wrappedValue = $0 })
}

/// Used to keep the state value synchronized with an external Compose value.
public func sync(value: Value, onUpdate: @escaping (Value) -> Void) {
self.wrappedValue = value
self.onUpdate = onUpdate
}
}
#endif

#if !SKIP
import struct Foundation.URL
Expand Down

0 comments on commit 17868a4

Please sign in to comment.