Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftfulRouting/Components/ModuleSupportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct ModuleSupportView<Content:View>: View {
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.animation(viewModel.currentTransition.animation, value: (viewModel.modules.last?.id ?? "") + viewModel.currentTransition.rawValue)
.animation(viewModel.currentTransition.animation, value: (viewModel.modules.last?.id ?? "") + viewModel.currentTransition.id)
.environmentObject(viewModel)

#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SwiftUI

struct SwipeBackSupportContainer<Content:View>: View {

var insertionTransition: TransitionOption = .trailing
var insertionTransition: TransitionOption = .trailing()
var swipeThreshold: CGFloat = 30
@ViewBuilder var content: () -> Content
var onDidSwipeBack: (() -> Void)? = nil
Expand Down Expand Up @@ -116,7 +116,7 @@ struct SwipeBackSupportContainer<Content:View>: View {
}

#Preview {
SwipeBackSupportContainer(insertionTransition: .trailing) {
SwipeBackSupportContainer(insertionTransition: .trailing()) {
Rectangle()
.fill(Color.blue)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct TransitionSupportView<Content:View>: View {
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.animation(currentTransition.animation, value: (transitions.last?.id ?? "") + currentTransition.rawValue)
.animation(currentTransition.animation, value: (transitions.last?.id ?? "") + currentTransition.id)
// .ifSatisfiesCondition(viewFrame == .zero, transform: { content in
// content
// .readingFrame(onChange: { frame in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public struct AnyRouter: Sendable, Router {
/// - onDismiss: Closure that triggers when transition is dismissed.
/// - destination: Destination screen.
@MainActor public func showTransition<T>(
_ transition: TransitionOption = .trailing,
_ transition: TransitionOption = .trailing(),
id: String = UUID().uuidString,
allowsSwipeBack: Bool = false,
onDismiss: (() -> Void)? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ModuleViewModel: ObservableObject {
@Published private(set) var modules: [AnyTransitionDestination] = [.root]

// The current TransitionOption for changing modules.
@Published private(set) var currentTransition: TransitionOption = .trailing
@Published private(set) var currentTransition: TransitionOption = .trailing()

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct RouterViewInternal<Content: View>: View, Router {
router: currentRouter,
transitions: viewModel.allTransitions[routerId] ?? [],
content: content,
currentTransition: viewModel.currentTransitions[routerId] ?? .trailing,
currentTransition: viewModel.currentTransitions[routerId] ?? .trailing(),
onDidSwipeBack: {
dismissTransition()
}
Expand Down Expand Up @@ -175,11 +175,14 @@ struct RouterViewInternal<Content: View>: View, Router {
}

var activeTransitions: [AnyTransitionDestination] {
viewModel.allTransitions[routerId] ?? []
let transitions = viewModel.allTransitions[routerId] ?? []
// Filter out the .root placeholder transition
return transitions.filter { $0 != .root }
}

var activeModules: [AnyTransitionDestination] {
moduleViewModel.modules
// Filter out the .root placeholder module
moduleViewModel.modules.filter { $0 != .root }
}

var activeTransitionQueue: [AnyTransitionDestination] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class RouterViewModel: ObservableObject {

// The current TransitionOption on each screen.
// While a transition is rendered, its .transition may change based on the next/previous transition.
@Published private(set) var currentTransitions: [String: TransitionOption] = [RouterViewModel.rootId: .trailing]
@Published private(set) var currentTransitions: [String: TransitionOption] = [RouterViewModel.rootId: .trailing()]

// Available transitions in queue, accessible via .showNextTransition()
@Published private(set) var availableTransitionQueue: [String: [AnyTransitionDestination]] = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct AnyTransitionDestination: Identifiable, Equatable {
/// - destination: Destination screen.
public init(
id: String = UUID().uuidString,
transition: TransitionOption = .trailing,
transition: TransitionOption = .trailing(),
allowsSwipeBack: Bool = false,
onDismiss: (() -> Void)? = nil,
destination: @escaping (AnyRouter) -> any View
Expand All @@ -36,7 +36,7 @@ public struct AnyTransitionDestination: Identifiable, Equatable {
}

static var root: AnyTransitionDestination {
AnyTransitionDestination(id: "root", transition: .trailing, destination: { _ in
AnyTransitionDestination(id: "root", transition: .trailing(), destination: { _ in
EmptyView()
})
}
Expand All @@ -52,7 +52,7 @@ public struct AnyTransitionDestination: Identifiable, Equatable {
public var eventParameters: [String: Any] {
[
"destination_id": id,
"destination_transition": transition.rawValue,
"destination_transition": transition.id,
"destination_allow_swipe_back": allowsSwipeBack,
"destination_has_on_dismiss": onDismiss != nil,
]
Expand Down
46 changes: 37 additions & 9 deletions Sources/SwiftfulRouting/Models/Transitions/TransitionOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@
import Foundation
import SwiftUI

public enum TransitionOption: String, CaseIterable {
case trailing, leading, top, bottom, identity
public enum TransitionOption: CaseIterable, Equatable {
public static var allCases: [TransitionOption] {
[.trailing(), .leading(), .top(), .bottom(), .identity]
}

case trailing(animation: Animation = .snappy)
case leading(animation: Animation = .snappy)
case top(animation: Animation = .snappy)
case bottom(animation: Animation = .snappy)
case identity

var id: String {
switch self {
case .trailing:
return "trailing"
case .leading:
return "leading"
case .top:
return "top"
case .bottom:
return "bottom"
case .identity:
return "identity"
}
}

var canSwipeBack: Bool {
switch self {
Expand All @@ -21,10 +44,10 @@ public enum TransitionOption: String, CaseIterable {

var animation: Animation? {
switch self {
case .trailing(let animation), .leading(let animation), .top(let animation), .bottom(let animation):
return animation
case .identity:
return .none
default:
return .smooth
}
}

Expand All @@ -49,11 +72,16 @@ public enum TransitionOption: String, CaseIterable {

var reversed: TransitionOption {
switch self {
case .trailing: return .leading
case .leading: return .trailing
case .top: return .bottom
case .bottom: return .top
case .identity: return .identity
case .trailing(let animation):
return .leading(animation: animation)
case .leading(let animation):
return .trailing(animation: animation)
case .top(let animation):
return .bottom(animation: animation)
case .bottom(let animation):
return .top(animation: animation)
case .identity:
return .identity
}
}

Expand Down