-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
545 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
MVVM.Demo.SwiftUI/UI/ColorWizard/ColorWizardCoordinator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// ColorWizardCoordinator.swift | ||
// MVVM.Demo.SwiftUI | ||
// | ||
// Created by Jason Lew-Rapai on 11/17/21. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import Swinject | ||
|
||
protocol ColorWizardCoordinatorDelegate: AnyObject { | ||
func colorWizardCoordinatorDidComplete(_ source: ColorWizardCoordinator) | ||
} | ||
|
||
class ColorWizardCoordinator: ViewModel { | ||
private let resolver: Resolver | ||
|
||
private weak var delegate: ColorWizardCoordinatorDelegate? | ||
private var configurationViewModel: ColorWizardConfigurationViewModel! | ||
|
||
@Published var colorWizardPageCoordinator: ColorWizardPageCoordinator! | ||
|
||
private var cancelBag = CancelBag() | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
init(resolver: Resolver) { | ||
self.resolver = resolver | ||
} | ||
|
||
func setup(configuration: ColorWizardConfiguration, delegate: ColorWizardCoordinatorDelegate) -> Self { | ||
self.delegate = delegate | ||
self.configurationViewModel = ColorWizardConfigurationViewModel(configuration: configuration) | ||
|
||
if let firstPageViewModel = self.configurationViewModel.pages.first { | ||
self.colorWizardPageCoordinator = self.resolver.resolve(ColorWizardPageCoordinator.self)! | ||
.setup(currentPageViewModel: firstPageViewModel, delegate: self) | ||
} else { | ||
fatalError() | ||
} | ||
|
||
return self | ||
} | ||
} | ||
|
||
// MARK: ColorWizardPageCoordinatorDelegate | ||
extension ColorWizardCoordinator: ColorWizardPageCoordinatorDelegate { | ||
func colorWizardPageCoordinator(_ source: ColorWizardPageCoordinator, canMoveBackFromIndex index: Int) -> Bool { | ||
return index != 0 | ||
} | ||
|
||
func colorWizardPageCoordinator(_ source: ColorWizardPageCoordinator, canMoveForwardFromIndex index: Int) -> Bool { | ||
return self.configurationViewModel.pages.count > index + 1 | ||
} | ||
|
||
func colorWizardPageCoordinator(_ source: ColorWizardPageCoordinator, canCompleteFromIndex index: Int) -> Bool { | ||
return self.configurationViewModel.pages.count == index + 1 | ||
} | ||
|
||
func colorWizardPageCoordinator(_ source: ColorWizardPageCoordinator, didMoveBackFromIndex index: Int) { | ||
fatalError("A back command should never reach this coordinator.") | ||
} | ||
|
||
func colorWizardPageCoordinator(_ source: ColorWizardPageCoordinator, nextPageAfterIndex index: Int) -> ColorWizardConfigurationViewModel.PageViewModel? { | ||
let newIndex = index + 1 | ||
guard newIndex < self.configurationViewModel.pages.count else { | ||
return nil | ||
} | ||
return self.configurationViewModel.pages[newIndex] | ||
} | ||
|
||
func colorWizardPageCoordinator(_ source: ColorWizardPageCoordinator, didCompleteFromIndex index: Int) { | ||
self.delegate?.colorWizardCoordinatorDidComplete(self) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
MVVM.Demo.SwiftUI/UI/ColorWizard/ColorWizardCoordinatorView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// ColorWizardCoordinatorView.swift | ||
// MVVM.Demo.SwiftUI | ||
// | ||
// Created by Jason Lew-Rapai on 11/17/21. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ColorWizardCoordinatorView: View { | ||
@ObservedObject var coordiantor: ColorWizardCoordinator | ||
|
||
var body: some View { | ||
ColorWizardPageCoordinatorView(coordinator: self.coordiantor.colorWizardPageCoordinator) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
MVVM.Demo.SwiftUI/UI/ColorWizard/Configuration/ColorWizardConfiguration+Mock.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// ColorWizardConfiguration+Mock.swift | ||
// MVVM.Demo.SwiftUI | ||
// | ||
// Created by Jason Lew-Rapai on 11/17/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension ColorWizardConfiguration { | ||
static func mock() -> ColorWizardConfiguration { | ||
ColorWizardConfiguration(pages: [ | ||
.page("First Color", color: .green), | ||
.page("Second Color", color: .orange), | ||
.page("Third Color", color: .systemIndigo), | ||
.page("Fourth Color", color: .pink), | ||
.page("Fifth Color", color: .purple), | ||
.page("Summary", colors: [ | ||
.green, | ||
.orange, | ||
.systemIndigo, | ||
.pink, | ||
.purple, | ||
]), | ||
]) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
MVVM.Demo.SwiftUI/UI/ColorWizard/Configuration/ColorWizardConfiguration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// ColorWizardConfiguration.swift | ||
// MVVM.Demo.SwiftUI | ||
// | ||
// Created by Jason Lew-Rapai on 11/17/21. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ColorWizardConfiguration { | ||
let pages: [Page] | ||
} | ||
|
||
extension ColorWizardConfiguration { | ||
struct Page { | ||
let title: String | ||
let color: Color? | ||
let colors: [Color] | ||
|
||
static func page(_ title: String, color: Color? = nil, colors: [Color] = []) -> Page { | ||
Page(title: title, color: color, colors: colors) | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
MVVM.Demo.SwiftUI/UI/ColorWizard/Configuration/ColorWizardConfigurationViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// ColorWizardConfigurationViewModel.swift | ||
// MVVM.Demo.SwiftUI | ||
// | ||
// Created by Jason Lew-Rapai on 11/17/21. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import SwiftUI | ||
|
||
class ColorWizardConfigurationViewModel: ViewModel { | ||
let pages: [PageViewModel] | ||
|
||
init(configuration: ColorWizardConfiguration) { | ||
var pageViewModels: [PageViewModel] = [] | ||
for (index, page) in configuration.pages.enumerated() { | ||
pageViewModels.append(PageViewModel(page: page, index: index)) | ||
} | ||
self.pages = pageViewModels | ||
} | ||
} | ||
|
||
extension ColorWizardConfigurationViewModel { | ||
class PageViewModel: ViewModel { | ||
let index: Int | ||
let title: String | ||
let color: Color? | ||
let colors: [ColorViewModel] | ||
This comment has been minimized.
Sorry, something went wrong.
aoverfield
|
||
|
||
init(page: ColorWizardConfiguration.Page, index: Int) { | ||
self.index = index | ||
self.title = page.title | ||
self.color = page.color | ||
self.colors = page.colors.map { ColorViewModel(color: $0) } | ||
} | ||
} | ||
|
||
class ColorViewModel: ViewModel { | ||
let id: String = UUID().uuidString | ||
let color: Color | ||
|
||
init(color: Color) { | ||
self.color = color | ||
} | ||
} | ||
} |
Oops, something went wrong.
Doesn't seem to be used?