diff --git a/CODE_STRUCTURE.md b/CODE_STRUCTURE.md new file mode 100644 index 0000000..0eceff7 --- /dev/null +++ b/CODE_STRUCTURE.md @@ -0,0 +1,179 @@ +# Code Consistency & Project Structure +In our project the syntax check is done by [swiftlint]. However, the architecture we use is the [TCA] (The Composable Architecture) and therefore we need to follow some basic principles and rules to hold structure of the files as well as project. This document describes rules for the code as well as project files/folders structure we agreed on. We encourage you to kindly follow these principles. + +# Structure of the TCA Store file +For the Store and View as well as folder name we focus on features. So the is the bare bone of the TCA code, uppercased! + +## Store file +The TCA consists of 5 blocks (state, action, environment, reducer and store). The TCA code lives in the Store file. We defined the following order of sections in this file: +1. Typealiases for the Reducer, Store and a ViewStore. +2. State +3. Action +4. Environment - always present even when no dependencies are defined = never use Void +5. Reducer holding private `Hashable` Ids, always use `static let default` reducer. Try to avoid `default: return .none` for the actions, we want the switch exhaustiveness check. +6. Store +7. Viewstore +8. Placeholders + +Any extension needed is defined under the relevant section except placeholders, those are separated at the very end of the file. Example: + +```swift +// MARK: - State + +struct State: Equatable { } + +extension State { + // anything but placeholder +} + +// MARK: - Action +// ... +``` + +## Structure of Store & View files + +Use the appropriate name for a feature you want to build. The must be bare bone of any TCA definition including the folder encapsulating the swift file(s). Example: + +```swift +// Project structure +// Folder woth following files: + +// Store.swift +struct State: Equatable { } +enum Action: Equatable { } +struct Environment + +// View.swift +struct : View { + let store: Store + + var body: some View { + WithViewStore(store) { viewStore in EmptyView() } + } +} + +struct _Previews: PreviewProvider { + static var previews: some View { + (store: .placeholder) + } +} +``` + +Do not omit Previews! + +## Navigation +Navigation is done using routes. Such mechanism consists of Route definition + state and Route action to update the state. +```swift +struct State +enum Route { + case +} +var route: Route? +} + +enum Action { + case updateRoute(State.Route?) +} +``` + +## XCode Template +We created a TCA template that automatically prepares the TCA files and code with the rules defined in this document. You can download it [here]. + +To add them to Xcode, follow the next steps: +1. open Finder +2. press cmd + shift + G +3. paste `/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates` +4. copy paste the TCA.xctemplate to the `MultiPlatform/Source/` + +The template is customizable and consists of 3 sections: + +1. TCA Store text field. This is essentially the +2. View selector of either +a. Empty (= TCA file with no navigation included) +b. Navigation (= TCA file with the navigation) +3. Reducer selector of either +a. Standalone (= only simple `default` reducer is created) +b. Combined (= the `default` reducer is created so it combines more reducers) +4. Checkbox which allows you to create also a View file if requested. + +## Project Structure +The project follows the structure detailed below: +``` +Models + .swift +Wrappers + Wrapped.swift +Utils + .swift +Dependencies + .swift +Features + + Store.swift + View.swift + Views (optional) +UI Components + + .swift +Resources + Generated (by swiftgen) + Fonts + +``` + +## Models +Here are the models used in the project, usually data containers shared across several places. +Examples: +- Transaction struct - holding the data for the transaction +- StoredWallet struct - holding the secured and sensitive data for the wallet + +## Wrappers +Here are the wrapped modules either from the SDK, iOS itself or wallet custom modules. Purpose of wrappers is to provide `live` implementation and other versions used testing purposes, usually one of the following: +- `mock` - mocked data +- `throwing` - throwing errors +- `test` - specific setup for particular tests + +Examples of the wrappers: +- SDK: Mnemonic, DerivingTool, SDKSynchronizer, ... +- iOS: FileManager, UserDefaults, NotificationCenter, ... +- wallet: DatabaseFiles, WalletStorage, ... + +## Utils +Here are the utilities used in the project. Usually helpers or extensions of the build in types. +Examples: +- helpers: navigation links, bindings, view modifiers, ... +- types: `String` extensions for the conversions, `UInt`/`Int64` extensions to handle balance computations, etc.``` + +## Dependencies +Modules used as dependencies by the reducers are located here. Some dependencies are already living in the SDK and only a wrapper is implemented (stored in the wrappers folder) for it. Sometimes the dependency is implemented in a way that it doesn't need to be wrapped (typically it uses some wrapped helper). In that case, the `live` vs. `mocked` static instances are implemented directly within the same file as the dependency. + +## Features vs. UI Components +We distinguish between smaller building blocks used in the composed complex UIs vs. screens and flows - understand features. Essentially we talk about two topics: +1. Features: typically views representing entire screeens, the whole flows, complex UIs and business logic that are usually not shared or used many times. +2. UI Components: smaller building blocks typically used across the application and features, like custom controls (buttons, input fields) or visual elements. Even when used just once, the nature of a UI component is to be reused so it's not forming standalone feature. + +Examples: +- features: Send Flow, Onboarding Flow, Settings Screen, Scan Screen, Profile Screen, ... +- UI Components: buttons, chips, button styles, view modifiers, drawer, custom textfields, shapes, ... + + When implementing something new, just ask yourself "Am I building something that can be shared and used several times or am I building a standalone feature?". + +In case of a *flow feature* the following structure is used: +```swift +Flow + FlowStore.swift + FlowView.swift + Views + View.swift + View.swift + View.swift +``` + +## Resources +All project resources should be placed in this folder. Images, fonts, generated files (by swiftgen for example), sounds, assets, string files, ... + +[//]: # (These are reference links used in the body of this note and get stripped out when the markdown processor does its job. There is no need to format nicely because it shouldn't be seen. Thanks SO - http://stackoverflow.com/questions/4823468/store-comments-in-markdown-syntax) + +[TCA]: +[swiftlint]: +[here]: diff --git a/xctemplates/Router Templates/Router.xctemplate/TemplateIcon.png b/xctemplates/Router Templates/Router.xctemplate/TemplateIcon.png deleted file mode 100644 index 4109abf..0000000 Binary files a/xctemplates/Router Templates/Router.xctemplate/TemplateIcon.png and /dev/null differ diff --git a/xctemplates/Router Templates/Router.xctemplate/TemplateIcon@2x.png b/xctemplates/Router Templates/Router.xctemplate/TemplateIcon@2x.png deleted file mode 100644 index 243d71d..0000000 Binary files a/xctemplates/Router Templates/Router.xctemplate/TemplateIcon@2x.png and /dev/null differ diff --git a/xctemplates/Router Templates/Router.xctemplate/TemplateInfo.plist b/xctemplates/Router Templates/Router.xctemplate/TemplateInfo.plist deleted file mode 100644 index 57045b4..0000000 --- a/xctemplates/Router Templates/Router.xctemplate/TemplateInfo.plist +++ /dev/null @@ -1,17 +0,0 @@ -{ - Kind = "Xcode.IDEFoundation.TextSubstitutionFileTemplateKind"; - Platforms = ( - "com.apple.platform.iphoneos", - "com.apple.platform.macosx", - ); - Options = ( - { - Description = "The name of the module to create"; - Identifier = productName; - Name = Router; - Required = YES; - Type = text; - Default = Router; - }, - ); -} diff --git a/xctemplates/Router Templates/Router.xctemplate/___FILEBASENAME___Router.swift b/xctemplates/Router Templates/Router.xctemplate/___FILEBASENAME___Router.swift deleted file mode 100644 index f39c22f..0000000 --- a/xctemplates/Router Templates/Router.xctemplate/___FILEBASENAME___Router.swift +++ /dev/null @@ -1,19 +0,0 @@ -//___FILEHEADER___ - -import Foundation -import SwiftUI - -class ___FILEBASENAMEASIDENTIFIER___: Router { - var services: Services - - init(services: Services) { - self.services = services - } - - func rootView() -> some View { - // Add your content here - NavigationView { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) - } - } -} diff --git a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateIcon.png b/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateIcon.png deleted file mode 100644 index e40618e..0000000 Binary files a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateIcon.png and /dev/null differ diff --git a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateIcon@2x.png b/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateIcon@2x.png deleted file mode 100644 index beb319f..0000000 Binary files a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateIcon@2x.png and /dev/null differ diff --git a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateInfo.plist b/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateInfo.plist deleted file mode 100644 index f0b4f3b..0000000 --- a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/TemplateInfo.plist +++ /dev/null @@ -1,16 +0,0 @@ -{ - Kind = "Xcode.IDEFoundation.TextSubstitutionFileTemplateKind"; - Platforms = ( - "com.apple.platform.iphoneos", - ); - Options = ( - { - Description = "The name of the module to create"; - Identifier = productName; - Name = Module; - Required = YES; - Type = text; - Default = Module; - }, - ); -} \ No newline at end of file diff --git a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/___FILEBASENAME___Screen.swift b/xctemplates/Router Templates/SwiftUI Screen.xctemplate/___FILEBASENAME___Screen.swift deleted file mode 100644 index 11ecb9a..0000000 --- a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/___FILEBASENAME___Screen.swift +++ /dev/null @@ -1,23 +0,0 @@ -//___FILEHEADER___ - -import SwiftUI - -protocol ___FILEBASENAMEASIDENTIFIER___Router: AnyObject { - -} - -struct ___FILEBASENAMEASIDENTIFIER___: View { - @ObservedObject var viewModel: ___FILEBASENAME___ViewModel - - @State var router: ___FILEBASENAMEASIDENTIFIER___Router? - - var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) - } -} - -struct ___FILEBASENAMEASIDENTIFIER___Previews: PreviewProvider { - static var previews: some View { - ___FILEBASENAMEASIDENTIFIER___(viewModel: ___FILEBASENAME___ViewModel(services: MockServices())) - } -} diff --git a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/___FILEBASENAME___ScreenViewModel.swift b/xctemplates/Router Templates/SwiftUI Screen.xctemplate/___FILEBASENAME___ScreenViewModel.swift deleted file mode 100644 index 7a0a70f..0000000 --- a/xctemplates/Router Templates/SwiftUI Screen.xctemplate/___FILEBASENAME___ScreenViewModel.swift +++ /dev/null @@ -1,9 +0,0 @@ -//___FILEHEADER___ - -import Foundation -import Combine - -class ___FILEBASENAMEASIDENTIFIER___: BaseViewModel, ObservableObject { - - -} diff --git a/xctemplates/TCA.xctemplate/EmptyCombined/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/EmptyCombined/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..de3dfeb --- /dev/null +++ b/xctemplates/TCA.xctemplate/EmptyCombined/___FILEBASENAME___Store.swift @@ -0,0 +1,71 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer.combine( + [ + reducer + ] + ) + .debug() + + private static let reducer = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} diff --git a/xctemplates/TCA.xctemplate/EmptyCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/EmptyCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..6e4041d --- /dev/null +++ b/xctemplates/TCA.xctemplate/EmptyCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift @@ -0,0 +1,79 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer.combine( + [ + reducer + ] + ) + .debug() + + private static let reducer = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} + +extension ___VARIABLE_productName:identifier___Store { + static let placeholder = ___VARIABLE_productName:identifier___Store( + initialState: .placeholder, + reducer: .default, + environment: ___VARIABLE_productName:identifier___Environment() + ) +} diff --git a/xctemplates/TCA.xctemplate/EmptyCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift b/xctemplates/TCA.xctemplate/EmptyCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift new file mode 100644 index 0000000..d82c5e9 --- /dev/null +++ b/xctemplates/TCA.xctemplate/EmptyCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift @@ -0,0 +1,20 @@ +//___FILEHEADER___ + +import SwiftUI +import ComposableArchitecture + +struct ___VARIABLE_productName:identifier___: View { + let store: ___VARIABLE_productName:identifier___Store + + var body: some View { + WithViewStore(store) { viewStore in + Text("Hello, World!") + } + } +} + +struct ___VARIABLE_productName:identifier____Previews: PreviewProvider { + static var previews: some View { + ___VARIABLE_productName:identifier___(store: .placeholder) + } +} diff --git a/xctemplates/TCA.xctemplate/EmptyStandalone/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/EmptyStandalone/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..3297544 --- /dev/null +++ b/xctemplates/TCA.xctemplate/EmptyStandalone/___FILEBASENAME___Store.swift @@ -0,0 +1,64 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} diff --git a/xctemplates/TCA.xctemplate/EmptyStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/EmptyStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..d20b501 --- /dev/null +++ b/xctemplates/TCA.xctemplate/EmptyStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift @@ -0,0 +1,72 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} + +extension ___VARIABLE_productName:identifier___Store { + static let placeholder = ___VARIABLE_productName:identifier___Store( + initialState: .placeholder, + reducer: .default, + environment: ___VARIABLE_productName:identifier___Environment() + ) +} diff --git a/xctemplates/TCA.xctemplate/EmptyStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift b/xctemplates/TCA.xctemplate/EmptyStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift new file mode 100644 index 0000000..d82c5e9 --- /dev/null +++ b/xctemplates/TCA.xctemplate/EmptyStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift @@ -0,0 +1,20 @@ +//___FILEHEADER___ + +import SwiftUI +import ComposableArchitecture + +struct ___VARIABLE_productName:identifier___: View { + let store: ___VARIABLE_productName:identifier___Store + + var body: some View { + WithViewStore(store) { viewStore in + Text("Hello, World!") + } + } +} + +struct ___VARIABLE_productName:identifier____Previews: PreviewProvider { + static var previews: some View { + ___VARIABLE_productName:identifier___(store: .placeholder) + } +} diff --git a/xctemplates/TCA.xctemplate/NavigationCombined/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/NavigationCombined/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..78c3022 --- /dev/null +++ b/xctemplates/TCA.xctemplate/NavigationCombined/___FILEBASENAME___Store.swift @@ -0,0 +1,85 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture +import SwiftUI + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + enum Route: Equatable { + } + + var route: Route? +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + case updateRoute(___VARIABLE_productName:identifier___State.Route?) +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer.combine( + [ + reducer + ] + ) + .debug() + + private static let reducer = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + case .updateRoute(let route): + state.route = route + return .none + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + func bindingForRoute(_ route: ___VARIABLE_productName:identifier___State.Route) -> Binding { + self.binding( + get: { $0.route == route }, + send: { isActive in + return .updateRoute(isActive ? route : nil) + } + ) + } +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} diff --git a/xctemplates/TCA.xctemplate/NavigationCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/NavigationCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..157ce02 --- /dev/null +++ b/xctemplates/TCA.xctemplate/NavigationCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift @@ -0,0 +1,93 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture +import SwiftUI + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + enum Route: Equatable { + } + + var route: Route? +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + case updateRoute(___VARIABLE_productName:identifier___State.Route?) +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer.combine( + [ + reducer + ] + ) + .debug() + + private static let reducer = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + case .updateRoute(let route): + state.route = route + return .none + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + func bindingForRoute(_ route: ___VARIABLE_productName:identifier___State.Route) -> Binding { + self.binding( + get: { $0.route == route }, + send: { isActive in + return .updateRoute(isActive ? route : nil) + } + ) + } +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} + +extension ___VARIABLE_productName:identifier___Store { + static let placeholder = ___VARIABLE_productName:identifier___Store( + initialState: .placeholder, + reducer: .default, + environment: ___VARIABLE_productName:identifier___Environment() + ) +} diff --git a/xctemplates/TCA.xctemplate/NavigationCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift b/xctemplates/TCA.xctemplate/NavigationCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift new file mode 100644 index 0000000..d82c5e9 --- /dev/null +++ b/xctemplates/TCA.xctemplate/NavigationCombinedgenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift @@ -0,0 +1,20 @@ +//___FILEHEADER___ + +import SwiftUI +import ComposableArchitecture + +struct ___VARIABLE_productName:identifier___: View { + let store: ___VARIABLE_productName:identifier___Store + + var body: some View { + WithViewStore(store) { viewStore in + Text("Hello, World!") + } + } +} + +struct ___VARIABLE_productName:identifier____Previews: PreviewProvider { + static var previews: some View { + ___VARIABLE_productName:identifier___(store: .placeholder) + } +} diff --git a/xctemplates/TCA.xctemplate/NavigationStandalone/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/NavigationStandalone/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..56463db --- /dev/null +++ b/xctemplates/TCA.xctemplate/NavigationStandalone/___FILEBASENAME___Store.swift @@ -0,0 +1,78 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture +import SwiftUI + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + enum Route: Equatable { + } + + var route: Route? +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + case updateRoute(___VARIABLE_productName:identifier___State.Route?) +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + case .updateRoute(let route): + state.route = route + return .none + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + func bindingForRoute(_ route: ___VARIABLE_productName:identifier___State.Route) -> Binding { + self.binding( + get: { $0.route == route }, + send: { isActive in + return .updateRoute(isActive ? route : nil) + } + ) + } +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} diff --git a/xctemplates/TCA.xctemplate/NavigationStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift b/xctemplates/TCA.xctemplate/NavigationStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift new file mode 100644 index 0000000..ac61035 --- /dev/null +++ b/xctemplates/TCA.xctemplate/NavigationStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___Store.swift @@ -0,0 +1,86 @@ +//___FILEHEADER___ + +import Foundation +import ComposableArchitecture +import SwiftUI + +typealias ___VARIABLE_productName:identifier___Reducer = Reducer<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action, ___VARIABLE_productName:identifier___Environment> +typealias ___VARIABLE_productName:identifier___Store = Store<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> +typealias ___VARIABLE_productName:identifier___ViewStore = ViewStore<___VARIABLE_productName:identifier___State, ___VARIABLE_productName:identifier___Action> + +// MARK: - State + +struct ___VARIABLE_productName:identifier___State: Equatable { + enum Route: Equatable { + } + + var route: Route? +} + +// MARK: - Action + +enum ___VARIABLE_productName:identifier___Action: Equatable { + case updateRoute(___VARIABLE_productName:identifier___State.Route?) +} + +// MARK: - Environment + +struct ___VARIABLE_productName:identifier___Environment { + +} + +extension ___VARIABLE_productName:identifier___Environment { + static let live = ___VARIABLE_productName:identifier___Environment( + ) + + static let mock = ___VARIABLE_productName:identifier___Environment( + ) +} + +// MARK: - Reducer + +extension ___VARIABLE_productName:identifier___Reducer { + static let `default` = ___VARIABLE_productName:identifier___Reducer { state, action, environment in + switch action { + case .updateRoute(let route): + state.route = route + return .none + } + + return .none + } +} + +// MARK: - Store + +extension ___VARIABLE_productName:identifier___Store { + +} + +// MARK: - ViewStore + +extension ___VARIABLE_productName:identifier___ViewStore { + func bindingForRoute(_ route: ___VARIABLE_productName:identifier___State.Route) -> Binding { + self.binding( + get: { $0.route == route }, + send: { isActive in + return .updateRoute(isActive ? route : nil) + } + ) + } +} + +// MARK: - Placeholders + +extension ___VARIABLE_productName:identifier___State { + static let placeholder = ___VARIABLE_productName:identifier___State( + ) +} + +extension ___VARIABLE_productName:identifier___Store { + static let placeholder = ___VARIABLE_productName:identifier___Store( + initialState: .placeholder, + reducer: .default, + environment: ___VARIABLE_productName:identifier___Environment() + ) +} diff --git a/xctemplates/TCA.xctemplate/NavigationStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift b/xctemplates/TCA.xctemplate/NavigationStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift new file mode 100644 index 0000000..d82c5e9 --- /dev/null +++ b/xctemplates/TCA.xctemplate/NavigationStandalonegenerateViewFile/___FILEBASENAME___/___FILEBASENAME___View.swift @@ -0,0 +1,20 @@ +//___FILEHEADER___ + +import SwiftUI +import ComposableArchitecture + +struct ___VARIABLE_productName:identifier___: View { + let store: ___VARIABLE_productName:identifier___Store + + var body: some View { + WithViewStore(store) { viewStore in + Text("Hello, World!") + } + } +} + +struct ___VARIABLE_productName:identifier____Previews: PreviewProvider { + static var previews: some View { + ___VARIABLE_productName:identifier___(store: .placeholder) + } +} diff --git a/xctemplates/TCA.xctemplate/TemplateInfo.plist b/xctemplates/TCA.xctemplate/TemplateInfo.plist new file mode 100644 index 0000000..3e4a9de --- /dev/null +++ b/xctemplates/TCA.xctemplate/TemplateInfo.plist @@ -0,0 +1,102 @@ + + + + + SupportsSwiftPackage + + Kind + Xcode.IDEFoundation.TextSubstitutionFileTemplateKind + Description + The Composable Architecture pre-filled swift file. + Summary + The Composable Architecture pre-filled swift file. + SortOrder + 1 + Image + + FileTypeIcon + swift + + AllowedTypes + + public.swift-source + + Platforms + + DefaultCompletionName + File + Options + + + Description + The name of the module to create + Identifier + productName + Name + TCA Store + Required + YES + Type + text + Default + + + + Identifier + viewType + Required + + Name + View type: + Description + The type of store to create + Type + popup + Default + Empty + Values + + Empty + Navigation + + + + Identifier + reducerType + Required + + Name + Reducer type: + Description + The type of the reducer to be pre-generated. + Type + popup + Default + Standalone + Values + + Standalone + Combined + + + + Identifier + generateViewFile + Name + Also create a view TCA file + Description + Create a TCA file with the same name + Type + checkbox + RequiredOptions + + viewType + + Empty + Navigation + + + + + +