Skip to content

Commit

Permalink
Make IdentifiableRoutingState pass Identifiable
Browse files Browse the repository at this point in the history
Add all navigation types to identifiable routing
  • Loading branch information
Daeda88 committed Feb 21, 2022
1 parent 073f922 commit bbd8d36
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
91 changes: 61 additions & 30 deletions stencils/Navigation.stencil
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import SwiftUI
{% if argument.includePartialSheet %}import PartialSheet{% endif %}

struct Navigation<NavigationContent: View>: ViewModifier {

enum NavigationType {
case fullscreen
case hud
case push
case replace
case sheet
case cover
enum NavigationType {
case fullscreen
case hud(blurRadius: CGFloat = 3, opacity: Double = 0.5)
case push
case replace
case sheet
case cover
{% if argument.includePartialSheet %}
case partialSheet(style: PartialSheetStyle)
case partialSheet(style: PartialSheetStyle)
{% endif %}
}
}

struct Navigation<NavigationContent: View>: ViewModifier {

@ObservedObject var state: RoutingState

Expand All @@ -39,11 +39,11 @@ struct Navigation<NavigationContent: View>: ViewModifier {
}
content
}
case .hud:
case let .hud(blurRadius, opacity):
Group {
content
.blur(radius: state.isPresented ? 3 : 0)
.opacity(state.isPresented ? 0.5 : 1.0)
.blur(radius: state.isPresented ? blurRadius : 0)
.opacity(state.isPresented ? opacity : 1.0)
.disabled(state.isPresented)
if state.isPresented {
LazyView(navigationContent())
Expand Down Expand Up @@ -98,22 +98,22 @@ struct Navigation<NavigationContent: View>: ViewModifier {
}
}

struct ItemNavigation<ID : Equatable, NavigationContent: View>: ViewModifier {

enum NavigationType {
case fullscreen
case push
case sheet
}
struct ItemNavigation<ID: Identifiable, NavigationContent: View>: ViewModifier {

@ObservedObject var state: IdentifiableRoutingState<ID>

private let id: ID
private let type: ItemNavigation.NavigationType
private let id: ID.ID
private let type: NavigationType
private let navigationContent: () -> NavigationContent
private let didSelect: () -> ()
private let didSelect: () -> Void

init(state: IdentifiableRoutingState<ID>, id: ID, type: ItemNavigation.NavigationType, didSelect: @escaping () -> (), @ViewBuilder navigationContent: @escaping () -> NavigationContent) {
init(
state: IdentifiableRoutingState<ID>,
id: ID.ID,
type: NavigationType,
didSelect: @escaping () -> Void,
@ViewBuilder navigationContent: @escaping () -> NavigationContent
) {
self.id = id
self.state = state
self.type = type
Expand Down Expand Up @@ -142,10 +142,41 @@ struct ItemNavigation<ID : Equatable, NavigationContent: View>: ViewModifier {
content
}
}
case let .hud(blurRadius, opacity):
Group {
content
.blur(radius: isActive.wrappedValue ? blurRadius : 0)
.opacity(isActive.wrappedValue ? opacity : 1.0)
.disabled(isActive.wrappedValue)
if isActive.wrappedValue {
LazyView(navigationContent())
}
}
case .fullscreen:
content.modifier(FullScreenModifier(isPresented: isActive, builder: { LazyView(navigationContent()) }))
case .sheet:
content.sheet(isPresented: isActive) { LazyView(navigationContent()) }
case .replace:
if isActive.wrappedValue {
LazyView(navigationContent())
} else {
content
}
case .cover:
Group {
content
if isActive.wrappedValue {
LazyView(navigationContent())
}
}
{% if argument.includePartialSheet %}
case .partialSheet(let style):
content
.addPartialSheet(style: style)
.partialSheet(isPresented: isActive) {
LazyView(navigationContent())
}
{% endif %}
}
}
}
Expand All @@ -168,7 +199,7 @@ struct FullScreenModifier<V: View>: ViewModifier {
extension View {
func navigation<NavigationContent: View>(
state: RoutingState,
type: Navigation<NavigationContent>.NavigationType,
type: NavigationType,
@ViewBuilder content: @escaping () -> NavigationContent
) -> some View {
ModifiedContent(
Expand All @@ -181,11 +212,11 @@ extension View {
)
}

func navigation<ID : Equatable, NavigationContent: View>(
func navigation<ID: Identifiable, NavigationContent: View>(
state: IdentifiableRoutingState<ID>,
id: ID,
type: ItemNavigation<ID, NavigationContent>.NavigationType,
didSelect: @escaping () -> (),
id: ID.ID,
type: NavigationType,
didSelect: @escaping () -> Void,
@ViewBuilder content: @escaping () -> NavigationContent
) -> some View {
ModifiedContent(
Expand Down
19 changes: 8 additions & 11 deletions stencils/RoutingState.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ class ObjectRoutingState<T: Equatable>: RoutingState {
}
}

class IdentifiableRoutingState<ID : Equatable> : RoutingState {
private(set) var id: ID?

class IdentifiableRoutingState<ID: Identifiable>: RoutingState, Identifiable {
private(set) var id: ID.ID?
func show(_ id: ID, animated: Bool = false) {
let force: Bool
if self.id != id {
self.id = id
if self.id != id.id {
self.id = id.id
force = true
} else {
force = false
Expand All @@ -86,16 +85,14 @@ class IdentifiableRoutingState<ID : Equatable> : RoutingState {
}
}

class IdentifiableObjectRoutingState<T, ID>: IdentifiableRoutingState<ID> where T : Equatable, T : Identifiable, T.ID == ID {
class IdentifiableObjectRoutingState<T>: IdentifiableRoutingState<T> where T: Equatable, T: Identifiable {

private(set) var object: T?
override var id: ID? {
get {
object?.id
}
override var id: T.ID? {
return object?.id
}

func show(_ object: T, animated: Bool = false) {
override func show(_ object: T, animated: Bool = false) {
let force: Bool
if self.object != object {
self.object = object
Expand Down

0 comments on commit bbd8d36

Please sign in to comment.