Skip to content

Commit

Permalink
Merge pull request #5 from kvyatkovskys/feature/v_0.1.1
Browse files Browse the repository at this point in the history
Protocol fixes and improvements
  • Loading branch information
kvyatkovskys authored Feb 14, 2024
2 parents 7908f2b + 9de4153 commit c8debad
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct ContentCoordinatorView: View {

@StateObject private var coordinator = ContentCoordinator()

var body: some View {
Expand Down
7 changes: 4 additions & 3 deletions DemoFlowCoordinators/DemoFlowCoordinators/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import SwiftUI

struct ContentView: View {

@ObservedObject var vm: ContentViewModel

var body: some View {
Expand All @@ -31,12 +30,14 @@ struct ContentView: View {
Button("Open Cover") {
vm.openCoverFirst()
}
Button("Open Link First") {
Button("Open Link") {
vm.openFirstLink()
}
Button("Open Complex Link") {
Button("Complex Btn Link") {
vm.openComplexLink()
}
NavigationLink("Complex Nav Link",
value: ContentViewModel.LinkType.linkSecondCoordinator)
}
.padding()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct DetailNavigationLinkView: View {

var body: some View {
Button("Go to Root") {
coordinator.goToRoot()
coordinator.popToRoot()
}
.navigationTitle("Detail View")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ struct SecondContentView: View {

var body: some View {
VStack(spacing: 30) {
Button("Go to Back") {
vm.goToBack()
Button("Pop View") {
vm.popView()
}
Button("Open Detail") {
vm.openDetail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ final class SecondContentViewModel: ObservableObject {
self.title = title
}

func goToBack() {
coordinator.goToBack()
func popView() {
coordinator.popView()
}

func openDetail() {
Expand Down
57 changes: 43 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SwiftUI flow coordinator to control navigation in your App.
- Create a `CoordinatorView` with the created coordinator.


To work with navigationLink use `.navigationDestination(for: NavigationLinkType.self)`, `.navigationDestination(item: $item)` and `NavigationLink(:)` doesn't work.
To work with navigationLink use `.navigationDestination(for: NavigationLinkType.self)`. The `.navigationDestination(item: $item)` doesn't work.


```swift
Expand All @@ -46,20 +46,7 @@ final class ContentCoordinator: FlowCoordinator<ContentViewModel.SheetType, Cont
}
}

final class ContentViewModel: ObservableObject {
private let coordinator: ContentCoordinator

init(coordinator: ContentCoordinator) {
self.coordinator = coordinator
}

func openFirstLink() {
coordinator.linkType = .linkFirstWithParams("First Link View")
}
}

struct ContentCoordinatorView: View {

@StateObject private var coordinator = ContentCoordinator()

var body: some View {
Expand All @@ -86,7 +73,49 @@ struct ContentCoordinatorView: View {
}
}
}
}

final class ContentViewModel: ObservableObject {
private let coordinator: ContentCoordinator

init(coordinator: ContentCoordinator) {
self.coordinator = coordinator
}

func openFirstLink() {
coordinator.linkType = .linkFirstWithParams("First Link View")
}
}

struct ContentView: View {
@ObservedObject var vm: ContentViewModel

var body: some View {
VStack(spacing: 30) {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Button("Open Sheet") {
vm.openSheetFirst(autoClose: false)
}
Button("Open Auto Close Sheet") {
vm.openSheetFirst(autoClose: true)
}
Button("Open Cover") {
vm.openCoverFirst()
}
Button("Open Link First") {
vm.openFirstLink()
}
Button("Open Complex Btn Link") {
vm.openComplexLink()
}
NavigationLink("Open Complex Nav Link",
value: ContentViewModel.LinkType.linkSecondCoordinator)
}
.padding()
}
}
```

Expand Down
51 changes: 31 additions & 20 deletions Sources/KVKFlowCoordinators/KVKFlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,45 @@ public protocol FlowProtocol: ObservableObject {
var cancellable: Set<AnyCancellable> { get set }
var parentFlowCoordinator: (any FlowProtocol)? { get set }

func goToRoot()
func goToBack()
func pushToLink(_ link: any LinkContentType)
func popToRoot()
func popView()
func pushTo(_ link: any FlowTypeProtocol)
}

extension FlowProtocol {

@available(swift, obsoleted: 0.1.1, renamed: "popToRoot")
public func goToRoot() {
popToRoot()
}

@available(swift, obsoleted: 0.1.1, renamed: "pop")
public func goToBack() {
popView()
}

@available(swift, obsoleted: 0.1.1, renamed: "pushTo")
public func pushToLink(_ link: any FlowTypeProtocol) {
pushTo(link)
}

public func popToRoot() {
if let parentFlowCoordinator {
parentFlowCoordinator.path = NavigationPath()
} else {
path = NavigationPath()
}
}

public func goToBack() {
public func popView() {
if let parentPath = parentFlowCoordinator?.path, !parentPath.isEmpty {
parentFlowCoordinator?.path.removeLast()
} else if !path.isEmpty {
path.removeLast()
}
}

public func pushToLink(_ link: any LinkContentType) {
public func pushTo(_ link: any FlowTypeProtocol) {
if let parentFlowCoordinator {
parentFlowCoordinator.path.append(link)
} else {
Expand All @@ -70,11 +85,7 @@ public struct FlowEmptyType: FlowTypeProtocol {
}
}

public typealias SheetContentType = Identifiable & Hashable
public typealias LinkContentType = Identifiable & Hashable
public typealias CoverContentType = Identifiable & Hashable

open class FlowCoordinator<Sheet: SheetContentType, Link: LinkContentType, Cover: CoverContentType>: FlowProtocol {
open class FlowCoordinator<Sheet: FlowTypeProtocol, Link: FlowTypeProtocol, Cover: FlowTypeProtocol>: FlowProtocol {
public typealias S = Sheet
public typealias N = Link
public typealias C = Cover
Expand All @@ -91,7 +102,7 @@ open class FlowCoordinator<Sheet: SheetContentType, Link: LinkContentType, Cover
$linkType
.compactMap { $0 }
.sink { [weak self] link in
self?.pushToLink(link)
self?.pushTo(link)
}
.store(in: &cancellable)
}
Expand All @@ -101,7 +112,7 @@ open class FlowCoordinator<Sheet: SheetContentType, Link: LinkContentType, Cover
}
}

open class SheetCoordinator<Sheet: SheetContentType>: FlowProtocol {
open class SheetCoordinator<Sheet: FlowTypeProtocol>: FlowProtocol {
public typealias S = Sheet
public typealias N = FlowEmptyType
public typealias C = FlowEmptyType
Expand All @@ -118,7 +129,7 @@ open class SheetCoordinator<Sheet: SheetContentType>: FlowProtocol {
}
}

open class LinkCoordinator<Link: LinkContentType>: FlowProtocol {
open class LinkCoordinator<Link: FlowTypeProtocol>: FlowProtocol {
public typealias S = FlowEmptyType
public typealias N = Link
public typealias C = FlowEmptyType
Expand All @@ -135,7 +146,7 @@ open class LinkCoordinator<Link: LinkContentType>: FlowProtocol {
$linkType
.compactMap { $0 }
.sink { [weak self] link in
self?.pushToLink(link)
self?.pushTo(link)
}
.store(in: &cancellable)
}
Expand All @@ -145,7 +156,7 @@ open class LinkCoordinator<Link: LinkContentType>: FlowProtocol {
}
}

open class CoverCoordinator<Cover: CoverContentType>: FlowProtocol {
open class CoverCoordinator<Cover: FlowTypeProtocol>: FlowProtocol {
public typealias S = FlowEmptyType
public typealias N = FlowEmptyType
public typealias C = Cover
Expand All @@ -162,7 +173,7 @@ open class CoverCoordinator<Cover: CoverContentType>: FlowProtocol {
}
}

open class SheetAndLinkCoordinator<Sheet: SheetContentType, Link: LinkContentType>: FlowProtocol {
open class SheetAndLinkCoordinator<Sheet: FlowTypeProtocol, Link: FlowTypeProtocol>: FlowProtocol {
public typealias S = Sheet
public typealias N = Link
public typealias C = FlowEmptyType
Expand All @@ -179,7 +190,7 @@ open class SheetAndLinkCoordinator<Sheet: SheetContentType, Link: LinkContentTyp
$linkType
.compactMap { $0 }
.sink { [weak self] link in
self?.pushToLink(link)
self?.pushTo(link)
}
.store(in: &cancellable)
}
Expand All @@ -189,7 +200,7 @@ open class SheetAndLinkCoordinator<Sheet: SheetContentType, Link: LinkContentTyp
}
}

open class SheetAndCoverCoordinator<Sheet: SheetContentType, Cover: CoverContentType>: FlowProtocol {
open class SheetAndCoverCoordinator<Sheet: FlowTypeProtocol, Cover: FlowTypeProtocol>: FlowProtocol {
public typealias S = Sheet
public typealias N = FlowEmptyType
public typealias C = Cover
Expand All @@ -202,7 +213,7 @@ open class SheetAndCoverCoordinator<Sheet: SheetContentType, Cover: CoverContent
public var parentFlowCoordinator: (any FlowProtocol)?
}

open class LinkAndCoverCoordinator<Link: LinkContentType, Cover: CoverContentType>: FlowProtocol {
open class LinkAndCoverCoordinator<Link: FlowTypeProtocol, Cover: FlowTypeProtocol>: FlowProtocol {
public typealias S = FlowEmptyType
public typealias N = Link
public typealias C = Cover
Expand All @@ -218,7 +229,7 @@ open class LinkAndCoverCoordinator<Link: LinkContentType, Cover: CoverContentTyp
$linkType
.compactMap { $0 }
.sink { [weak self] link in
self?.pushToLink(link)
self?.pushTo(link)
}
.store(in: &cancellable)
}
Expand Down

0 comments on commit c8debad

Please sign in to comment.