Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated example project #2

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
"pins": [
{
"package": "CleevioCore",
"repositoryURL": "git@gitlab.cleevio.cz:cleevio-dev-ios/CleevioCore",
"repositoryURL": "https://github.com/cleevio/CleevioCore.git",
"state": {
"branch": null,
"revision": "176a1ac517a001a3fc31a75f656bb8c1ed11e854",
"version": "2.0.0"
"revision": "593b5d1011ad0aa6963ea2d8bf2ddc5e59caa917",
"version": "2.1.7"
}
},
{
"package": "CleevioUI",
"repositoryURL": "https://github.com/cleevio/CleevioUI.git",
"state": {
"branch": null,
"revision": "ff86db5f6e73f99ece752712c252effa10f4e205",
"version": "3.0.5"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1610"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B2DF64DA29DCB9F0004A2864"
BuildableName = "FlowPilotExample.app"
BlueprintName = "FlowPilotExample"
ReferencedContainer = "container:FlowPilotExample.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B2DF64DA29DCB9F0004A2864"
BuildableName = "FlowPilotExample.app"
BlueprintName = "FlowPilotExample"
ReferencedContainer = "container:FlowPilotExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B2DF64DA29DCB9F0004A2864"
BuildableName = "FlowPilotExample.app"
BlueprintName = "FlowPilotExample"
ReferencedContainer = "container:FlowPilotExample.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
9 changes: 7 additions & 2 deletions Example/Sources/BaseCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ typealias Router = FlowPilot.Router

@MainActor
class BaseCoordinator: RouterCoordinator {
let cancelBag = CancelBag()

deinit {
print("BaseCoordinator deinit")
}
}

@MainActor
class BaseResponseParametersCoordinator<Response, Parameters>: FlowPilot.ResponseParametersCoordinator<Response, Parameters> {
deinit {
print("BaseResponseParametersCoordinator deinit")
}
}
31 changes: 13 additions & 18 deletions Example/Sources/Scenes/Root/First/FirstCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CleevioCore
import FlowPilot

protocol FirstCoordinatorDelegate: AnyObject {
@MainActor func showSecondTap()
func showSecondView() async throws
}

@MainActor
Expand All @@ -24,25 +24,12 @@ final class FirstCoordinator: BaseCoordinator {
super.init(router: router)
}

override func start(animated: Bool) {
override func start(animated: Bool = true) {
let viewModel = FirstViewModel(count: counter)
viewModel.routingDelegate = self
let viewController = BaseHostingController(rootView: FirstView(viewModel: viewModel))

present(viewController, animated: animated)

viewModel.route
.sink(receiveValue: { [weak self] route in
guard let self else { return }
switch route {
case .dismiss:
self.dismiss()
case .continueLoop:
self.showFirstCoordinator()
case .secondView:
self.delegate?.showSecondTap()
}
})
.store(in: cancelBag)
}

func showFirstCoordinator() {
Expand All @@ -52,8 +39,16 @@ final class FirstCoordinator: BaseCoordinator {
}
}

extension FirstCoordinator: FirstCoordinatorDelegate {
func showSecondTap() {
extension FirstCoordinator: FirstCoordinatorDelegate, FirstViewModelRoutingDelegate {
func dismiss() {
self.dismiss(animated: true)
}

func continueLoop() async throws {
showFirstCoordinator()
}

func showSecondView() {
guard let viewController = viewControllers.first??.navigationController else { return }
let coordinator = SecondCoordinator(router: ModalRouter(parentViewController: viewController))

Expand Down
13 changes: 7 additions & 6 deletions Example/Sources/Scenes/Root/First/FirstView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import SwiftUI
import FlowPilot
import CleevioUI

struct FirstView: View {
@ObservedObject var viewModel: FirstViewModel
Expand All @@ -15,14 +16,14 @@ struct FirstView: View {
ScrollView {
Text("Hello, first coordinator World!")
Text("Count: \(viewModel.count)")
Button("Dismiss") {
viewModel.route.send(.dismiss)
AsyncButton("Dismiss") {
await viewModel.send(action: .dismiss)
}
Button("Continue loop") {
viewModel.route.send(.continueLoop)
AsyncButton("Continue loop") {
await viewModel.send(action: .continueLoop)
}
Button("Show second view") {
viewModel.route.send(.secondView)
AsyncButton("Show second view") {
await viewModel.send(action: .secondView)
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions Example/Sources/Scenes/Root/First/FirstViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,35 @@
import Foundation
import Combine

protocol FirstViewModelRoutingDelegate: AnyObject {
func dismiss()
func continueLoop() async throws
func showSecondView() async throws
}

final class FirstViewModel: ObservableObject {
var route: PassthroughSubject<Route, Never> = .init()
var count: Int

weak var routingDelegate: FirstViewModelRoutingDelegate?

init(count: Int) {
self.count = count
}
enum Route {

enum Action {
case dismiss
case continueLoop
case secondView
}

func send(action: Action) async {
switch action {
case .dismiss:
routingDelegate?.dismiss()
case .continueLoop:
try? await routingDelegate?.continueLoop()
case .secondView:
try? await routingDelegate?.showSecondView()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ResponseParametersCoordinator.swift
// CleevioCoordinators
//
// Created by Lukáš Valenta on 14.11.2024.
//

import Foundation
import CleevioCore

@MainActor
final class ResponseParametersCoordinator: BaseResponseParametersCoordinator<Bool, Bool> {
override func start(animated: Bool = true) {
let viewModel = ResponseParametersViewModel(value: parameters)
viewModel.routingDelegate = self

let viewController = BaseHostingController(rootView: ResponseParametersView(viewModel: viewModel))

present(viewController, animated: animated)
}
}

extension ResponseParametersCoordinator: ResponseParametersViewModelRoutingDelegate {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// ResponseParametersView.swift
// CleevioCoordinators
//
// Created by Lukáš Valenta on 14.11.2024.
//

import SwiftUI
import FlowPilot
import CleevioUI

@MainActor
struct ResponseParametersView: View {
@ObservedObject var viewModel: ResponseParametersViewModel

var body: some View {
ScrollView {
Text("Hello, ResponseParameters coordinator World!")
Toggle("Select value", isOn: $viewModel.value)
AsyncButton("Save") {
await viewModel.send(action: .save)
}
}
}
}

struct ResponseParametersView_Previews: PreviewProvider {
static var previews: some View {
CoordinatorPreview { router in
ResponseParametersCoordinator(parameters: true, router: router)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// ResponseParametersViewModel.swift
// CleevioCoordinators
//
// Created by Lukáš Valenta on 14.11.2024.
//

import Foundation
import Combine

protocol ResponseParametersViewModelRoutingDelegate: AnyObject {
func response(with: Bool)
}

final class ResponseParametersViewModel: ObservableObject {
weak var routingDelegate: ResponseParametersViewModelRoutingDelegate?

@Published var value: Bool

init(value: Bool) {
self.value = value
}

enum Action {
case save
}

func send(action: Action) async {
switch action {
case .save:
await routingDelegate?.response(with: value)
}
}
}
Loading