From 2436570d96af09ee6e5a9cdd9e4ff82d977e05af Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 24 May 2022 09:10:40 +0300 Subject: [PATCH] Update templates: remove iOS 14 availability checks and stateActions + reducer. --- .../ViewModel/StateStoreViewModel.swift | 34 ++----------------- .../HomeScreen/HomeScreenViewModel.swift | 4 +-- .../LoginScreen/LoginScreenViewModel.swift | 4 +-- .../LoginScreenViewModelProtocol.swift | 2 -- .../RoomScreen/RoomScreenViewModel.swift | 4 +-- .../TemplateSimpleScreenViewModel.swift | 4 +-- ...emplateSimpleScreenViewModelProtocol.swift | 2 -- 7 files changed, 6 insertions(+), 48 deletions(-) diff --git a/ElementX/Sources/Other/SwiftUI/ViewModel/StateStoreViewModel.swift b/ElementX/Sources/Other/SwiftUI/ViewModel/StateStoreViewModel.swift index cc4a7dd33..fc10cd0b3 100644 --- a/ElementX/Sources/Other/SwiftUI/ViewModel/StateStoreViewModel.swift +++ b/ElementX/Sources/Other/SwiftUI/ViewModel/StateStoreViewModel.swift @@ -29,7 +29,6 @@ import Combine /// A similar approach is taken in libraries like [CombineFeedback](https://github.com/sergdort/CombineFeedback). /// It provides a nice layer of consistency and also safety. As we are not passing the `ViewModel` to the view directly, shortcuts/hacks /// can't be made into the `ViewModel`. -@available(iOS 14, *) @dynamicMemberLookup class ViewModelContext: ObservableObject { // MARK: - Properties @@ -65,14 +64,13 @@ class ViewModelContext: ObservableObject { } } -/// A common ViewModel implementation for handling of `State`, `StateAction`s and `ViewAction`s +/// A common ViewModel implementation for handling of `State` and `ViewAction`s /// /// Generic type State is constrained to the BindableState protocol in that it may contain (but doesn't have to) /// a specific portion of state that can be safely bound to. /// If we decide to add more features to our state management (like doing state processing off the main thread) /// we can do it in this centralised place. -@available(iOS 14, *) -class StateStoreViewModel { +class StateStoreViewModel { typealias Context = ViewModelContext @@ -104,34 +102,6 @@ class StateStoreViewModel { .store(in: &cancellables) } - /// Send state actions to modify the state within the reducer. - /// - Parameter action: The state action to send to the reducer. - @available(*, deprecated, message: "Mutate state directly instead") - func dispatch(action: StateAction) { - Self.reducer(state: &context.viewState, action: action) - } - - /// Send state actions from a publisher to modify the state within the reducer. - /// - Parameter actionPublisher: The publisher that produces actions to be sent to the reducer - @available(*, deprecated, message: "Mutate state directly instead") - func dispatch(actionPublisher: AnyPublisher) { - actionPublisher.sink { [weak self] action in - guard let self = self else { return } - Self.reducer(state: &self.context.viewState, action: action) - } - .store(in: &cancellables) - } - - /// Override to handle mutations to the `State` - /// - /// A redux style reducer, all modifications to state happen here. - /// - Parameters: - /// - state: The `inout` state to be modified, - /// - action: The action that defines which state modification should take place. - class func reducer(state: inout State, action: StateAction) { - // Default implementation, -no-op - } - /// Override to handles incoming `ViewAction`s from the `ViewModel`. /// - Parameter viewAction: The `ViewAction` to be processed in `ViewModel` implementation. func process(viewAction: ViewAction) { diff --git a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift index a8e72c352..94a3d2284 100644 --- a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift +++ b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift @@ -17,11 +17,9 @@ import SwiftUI import Combine -@available(iOS 14, *) typealias HomeScreenViewModelType = StateStoreViewModel -@available(iOS 14, *) + class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol { private let attributedStringBuilder: AttributedStringBuilderProtocol diff --git a/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModel.swift b/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModel.swift index 841f89295..118fb9a9f 100644 --- a/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModel.swift +++ b/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModel.swift @@ -16,11 +16,9 @@ import SwiftUI -@available(iOS 14, *) typealias LoginScreenViewModelType = StateStoreViewModel -@available(iOS 14, *) + class LoginScreenViewModel: LoginScreenViewModelType, LoginScreenViewModelProtocol { // MARK: - Properties diff --git a/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModelProtocol.swift b/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModelProtocol.swift index 5f7c4f418..9e6d82518 100644 --- a/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModelProtocol.swift +++ b/ElementX/Sources/Screens/LoginScreen/LoginScreenViewModelProtocol.swift @@ -17,8 +17,6 @@ import Foundation protocol LoginScreenViewModelProtocol { - var completion: ((LoginScreenViewModelResult) -> Void)? { get set } - @available(iOS 14, *) var context: LoginScreenViewModelType.Context { get } } diff --git a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift index b2493955b..5d82219a1 100644 --- a/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift +++ b/ElementX/Sources/Screens/RoomScreen/RoomScreenViewModel.swift @@ -16,11 +16,9 @@ import SwiftUI -@available(iOS 14, *) typealias RoomScreenViewModelType = StateStoreViewModel -@available(iOS 14, *) + class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol { private struct Constants { diff --git a/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModel.swift b/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModel.swift index b67286866..6adbf3741 100644 --- a/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModel.swift +++ b/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModel.swift @@ -16,11 +16,9 @@ import SwiftUI -@available(iOS 14, *) typealias TemplateSimpleScreenViewModelType = StateStoreViewModel -@available(iOS 14, *) + class TemplateSimpleScreenViewModel: TemplateSimpleScreenViewModelType, TemplateSimpleScreenViewModelProtocol { // MARK: - Properties diff --git a/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModelProtocol.swift b/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModelProtocol.swift index 3c08f5292..d32b2596a 100644 --- a/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModelProtocol.swift +++ b/Tools/Scripts/Templates/SimpleScreenExample/ElementX/TemplateSimpleScreenViewModelProtocol.swift @@ -17,8 +17,6 @@ import Foundation protocol TemplateSimpleScreenViewModelProtocol { - var completion: ((TemplateSimpleScreenViewModelResult) -> Void)? { get set } - @available(iOS 14, *) var context: TemplateSimpleScreenViewModelType.Context { get } }