diff options
Diffstat (limited to 'iosApp')
-rw-r--r-- | iosApp/iosApp.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate | bin | 29011 -> 36126 bytes | |||
-rw-r--r-- | iosApp/iosApp/Authentication/LoginView.swift | 12 | ||||
-rw-r--r-- | iosApp/iosApp/Shared/Inject.swift | 25 | ||||
-rw-r--r-- | iosApp/iosApp/Shared/Resolver.swift | 36 | ||||
-rw-r--r-- | iosApp/iosApp/iOSApp.swift | 42 |
6 files changed, 113 insertions, 10 deletions
diff --git a/iosApp/iosApp.xcodeproj/project.pbxproj b/iosApp/iosApp.xcodeproj/project.pbxproj index 898c277..60172de 100644 --- a/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/iosApp/iosApp.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; }; 2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; }; 7555FF83242A565900829871 /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7555FF82242A565900829871 /* LoginView.swift */; }; + E38F241527A242870069FC45 /* Inject.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38F241427A242870069FC45 /* Inject.swift */; }; + E38F241727A242C70069FC45 /* Resolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = E38F241627A242C70069FC45 /* Resolver.swift */; }; E3E77EDF279D2B5A00150070 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E3E77EDE279D2B5A00150070 /* Localizable.strings */; }; E3E77EE6279E6CE400150070 /* FlowCollector.swift in Sources */ = {isa = PBXBuildFile; fileRef = E3E77EE5279E6CE400150070 /* FlowCollector.swift */; }; /* End PBXBuildFile section */ @@ -35,6 +37,8 @@ 7555FF7B242A565900829871 /* iosApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iosApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7555FF82242A565900829871 /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; }; 7555FF8C242A565B00829871 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; + E38F241427A242870069FC45 /* Inject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Inject.swift; sourceTree = "<group>"; }; + E38F241627A242C70069FC45 /* Resolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Resolver.swift; sourceTree = "<group>"; }; E3E77EDE279D2B5A00150070 /* Localizable.strings */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = "<group>"; }; E3E77EE5279E6CE400150070 /* FlowCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlowCollector.swift; sourceTree = "<group>"; }; /* End PBXFileReference section */ @@ -108,6 +112,8 @@ isa = PBXGroup; children = ( E3E77EE5279E6CE400150070 /* FlowCollector.swift */, + E38F241427A242870069FC45 /* Inject.swift */, + E38F241627A242C70069FC45 /* Resolver.swift */, ); path = Shared; sourceTree = "<group>"; @@ -209,6 +215,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + E38F241727A242C70069FC45 /* Resolver.swift in Sources */, + E38F241527A242870069FC45 /* Inject.swift in Sources */, E3E77EE6279E6CE400150070 /* FlowCollector.swift in Sources */, 2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */, 7555FF83242A565900829871 /* LoginView.swift in Sources */, diff --git a/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate b/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate Binary files differindex a056117..fe8ed03 100644 --- a/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate +++ b/iosApp/iosApp.xcodeproj/project.xcworkspace/xcuserdata/avalos.xcuserdatad/UserInterfaceState.xcuserstate diff --git a/iosApp/iosApp/Authentication/LoginView.swift b/iosApp/iosApp/Authentication/LoginView.swift index 7b1e63e..9e7a64f 100644 --- a/iosApp/iosApp/Authentication/LoginView.swift +++ b/iosApp/iosApp/Authentication/LoginView.swift @@ -2,21 +2,13 @@ import SwiftUI import shared struct LoginContainerView: View { + @Inject private var sessionController: SessionController + @State private var username = "" @State private var password = "" @State private var server = "" - private let sessionManager = SessionManager( - defaultBaseUrl: "https://etbsa.net/api" - ) - - private var sessionController: SessionController - init() { - sessionController = SessionController( - sessionApi: SessionApi(sessionManager: sessionManager), - usersApi: UsersApi(sessionManager: sessionManager)) - sessionController.loginStateFlow.collect(collector: Collector<SessionController.LoginState?> { state in print("State is: \(state?.debugDescription ?? "")") }) { (unit, error) in diff --git a/iosApp/iosApp/Shared/Inject.swift b/iosApp/iosApp/Shared/Inject.swift new file mode 100644 index 0000000..0aaa388 --- /dev/null +++ b/iosApp/iosApp/Shared/Inject.swift @@ -0,0 +1,25 @@ +// +// Inject.swift +// iosApp +// +// Created by Iván on 26/01/22. +// Copyright © 2022 orgName. All rights reserved. +// + +import Foundation +import shared + +// Source: https://www.kiloloco.com/articles/004-dependency-injection-via-property-wrappers/ +// Source: https://medium.com/swlh/c1f02f06cd51 +@propertyWrapper +struct Inject<T: Injectable> { + var injectable: T + + init() { + injectable = Resolver.shared.resolve() + } + + public var wrappedValue: T { + get { return injectable } + } +} diff --git a/iosApp/iosApp/Shared/Resolver.swift b/iosApp/iosApp/Shared/Resolver.swift new file mode 100644 index 0000000..411dc13 --- /dev/null +++ b/iosApp/iosApp/Shared/Resolver.swift @@ -0,0 +1,36 @@ +// +// Resolver.swift +// iosApp +// +// Created by Iván on 26/01/22. +// Copyright © 2022 orgName. All rights reserved. +// + +import Foundation +import shared + +// Source: https://www.kiloloco.com/articles/004-dependency-injection-via-property-wrappers/ +// Source: https://medium.com/swlh/c1f02f06cd51 +class Resolver { + + private var storage = [String: (Resolver) -> Injectable]() + + static let shared = Resolver() + private init() {} + + func add(_ type: Injectable.Type, factory: @escaping (Resolver) -> Injectable) { + let key = String(reflecting: type.self) + storage[key] = factory + } + + func resolve<T: Injectable>() -> T { + let key = String(reflecting: T.self) + + guard let injectable = storage[key]?(self) as? T else { + fatalError("\(key) has not been added as an injectable object.") + } + + return injectable + } + +} diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift index 26c93f1..61c6afc 100644 --- a/iosApp/iosApp/iOSApp.swift +++ b/iosApp/iosApp/iOSApp.swift @@ -1,8 +1,50 @@ +import Foundation import SwiftUI import shared @main struct iOSApp: App { + + init() { + /* Dependency injections */ + + Resolver.shared.add(SessionManager.self) { resolver in + return SessionManager(defaultBaseUrl: NSLocalizedString("app-server-url", comment: "")) + } + Resolver.shared.add(SessionApi.self) { resolver in + return SessionApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(UsersApi.self) { resolver in + return UsersApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(DevicesApi.self) { resolver in + return DevicesApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(PositionsApi.self) { resolver in + return PositionsApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(CommandsApi.self) { resolver in + return CommandsApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(ReportsApi.self) { resolver in + return ReportsApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(GeofencesApi.self) { resolver in + return GeofencesApi(sessionManager: resolver.resolve()) + } + Resolver.shared.add(SessionController.self) { resolver in + return SessionController(sessionApi: resolver.resolve(), usersApi: resolver.resolve()) + } + Resolver.shared.add(UnitsController.self) { resolver in + return UnitsController(devicesApi: resolver.resolve(), positionsApi: resolver.resolve()) + } + Resolver.shared.add(GeofencesController.self) { resolver in + return GeofencesController(geofencesApi: resolver.resolve()) + } + Resolver.shared.add(ReportController.self) { resolver in + return ReportController(reportsApi: resolver.resolve(), geofencesApi: resolver.resolve()) + } + } var body: some Scene { WindowGroup { |