diff --git a/RiotSwiftUI/Modules/Common/Avatar/View/AvatarImage.swift b/RiotSwiftUI/Modules/Common/Avatar/View/AvatarImage.swift index ff7950111..b143d4d30 100644 --- a/RiotSwiftUI/Modules/Common/Avatar/View/AvatarImage.swift +++ b/RiotSwiftUI/Modules/Common/Avatar/View/AvatarImage.swift @@ -19,7 +19,6 @@ import SwiftUI struct AvatarImage: View { @Environment(\.theme) var theme: ThemeSwiftUI - @Environment(\.dependencies) var dependencies: DependencyContainer @EnvironmentObject var viewModel: AvatarViewModel var mxContentUri: String? @@ -43,7 +42,6 @@ struct AvatarImage: View { .frame(maxWidth: CGFloat(size.rawValue), maxHeight: CGFloat(size.rawValue)) .clipShape(Circle()) .onAppear { - viewModel.inject(dependencies: dependencies) viewModel.loadAvatar( mxContentUri: mxContentUri, matrixItemId: matrixItemId, diff --git a/RiotSwiftUI/Modules/Common/Avatar/View/SpaceAvatarImage.swift b/RiotSwiftUI/Modules/Common/Avatar/View/SpaceAvatarImage.swift index c6fe8da0b..2662831e1 100644 --- a/RiotSwiftUI/Modules/Common/Avatar/View/SpaceAvatarImage.swift +++ b/RiotSwiftUI/Modules/Common/Avatar/View/SpaceAvatarImage.swift @@ -19,7 +19,6 @@ import SwiftUI struct SpaceAvatarImage: View { @Environment(\.theme) var theme: ThemeSwiftUI - @Environment(\.dependencies) var dependencies: DependencyContainer @EnvironmentObject var viewModel: AvatarViewModel var mxContentUri: String? @@ -59,7 +58,6 @@ struct SpaceAvatarImage: View { ) }) .onAppear { - viewModel.inject(dependencies: dependencies) viewModel.loadAvatar( mxContentUri: mxContentUri, matrixItemId: matrixItemId, diff --git a/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift b/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift index f6372c14f..10055738d 100644 --- a/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift +++ b/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift @@ -19,7 +19,7 @@ import DesignKit import Foundation /// Simple ViewModel that supports loading an avatar image -class AvatarViewModel: InjectableObject, ObservableObject { +final class AvatarViewModel: ObservableObject { private let avatarService: AvatarServiceProtocol @Published private(set) var viewState = AvatarViewState.empty diff --git a/RiotSwiftUI/Modules/Common/DependencyInjection/DependencyContainer.swift b/RiotSwiftUI/Modules/Common/DependencyInjection/DependencyContainer.swift deleted file mode 100644 index d09fa87f4..000000000 --- a/RiotSwiftUI/Modules/Common/DependencyInjection/DependencyContainer.swift +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation - -/// Used for storing and resolving dependencies at runtime. -struct DependencyContainer { - // Stores the dependencies with type information removed. - private var dependencyStore: [String: Any] = [:] - - /// Resolve a dependency by type. - /// - /// Given a particular `Type` (Inferred from return type), - /// generate a key and retrieve from storage. - /// - /// - Returns: The resolved dependency. - func resolve() -> T { - let key = String(describing: T.self) - guard let t = dependencyStore[key] as? T else { - fatalError("No provider registered for type \(T.self)") - } - return t - } - - /// Register a dependency. - /// - /// Given a dependency, generate a key from it's `Type` and save in storage. - /// - Parameter dependency: The dependency to register. - mutating func register(dependency: T) { - let key = String(describing: T.self) - dependencyStore[key] = dependency - } -} diff --git a/RiotSwiftUI/Modules/Common/DependencyInjection/DependencyContainerKey.swift b/RiotSwiftUI/Modules/Common/DependencyInjection/DependencyContainerKey.swift deleted file mode 100644 index 9e5403b25..000000000 --- a/RiotSwiftUI/Modules/Common/DependencyInjection/DependencyContainerKey.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation -import SwiftUI - -/// An Environment Key for retrieving runtime dependencies. -/// -/// Dependencies are to be injected into `ObservableObjects` -/// that are owned by a View (i.e. `@StateObject`'s, such as ViewModels owned by the View). -private struct DependencyContainerKey: EnvironmentKey { - static let defaultValue = DependencyContainer() -} - -extension EnvironmentValues { - var dependencies: DependencyContainer { - get { self[DependencyContainerKey.self] } - set { self[DependencyContainerKey.self] = newValue } - } -} diff --git a/RiotSwiftUI/Modules/Common/DependencyInjection/Inject.swift b/RiotSwiftUI/Modules/Common/DependencyInjection/Inject.swift deleted file mode 100644 index d45907eeb..000000000 --- a/RiotSwiftUI/Modules/Common/DependencyInjection/Inject.swift +++ /dev/null @@ -1,43 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation - -/// A property wrapped used to inject from the dependency container on the instance, to instance properties. -/// -/// ``` -/// @Inject var someClass: SomeClass -/// ``` -@propertyWrapper struct Inject { - static subscript(_enclosingInstance instance: T, - wrapped wrappedKeyPath: ReferenceWritableKeyPath, - storage storageKeyPath: ReferenceWritableKeyPath) -> Value { - get { - // Resolve dependencies from enclosing instance's `dependencies` property - let v: Value = instance.dependencies.resolve() - return v - } - set { - fatalError("Only subscript get is supported for injection") - } - } - - @available(*, unavailable, message: "This property wrapper can only be applied to classes") - var wrappedValue: Value { - get { fatalError("wrappedValue get not used") } - set { fatalError("wrappedValue set not used. \(newValue)") } - } -} diff --git a/RiotSwiftUI/Modules/Common/DependencyInjection/Injectable.swift b/RiotSwiftUI/Modules/Common/DependencyInjection/Injectable.swift deleted file mode 100644 index b05b966e4..000000000 --- a/RiotSwiftUI/Modules/Common/DependencyInjection/Injectable.swift +++ /dev/null @@ -1,30 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation - -/// A protocol for classes that can be injected with a dependency container -protocol Injectable: AnyObject { - var dependencies: DependencyContainer! { get set } -} - -extension Injectable { - /// Used to inject the dependency container into an Injectable. - /// - Parameter dependencies: The `DependencyContainer` to inject. - func inject(dependencies: DependencyContainer) { - self.dependencies = dependencies - } -} diff --git a/RiotSwiftUI/Modules/Common/DependencyInjection/InjectableObject.swift b/RiotSwiftUI/Modules/Common/DependencyInjection/InjectableObject.swift deleted file mode 100644 index eab3cdcdf..000000000 --- a/RiotSwiftUI/Modules/Common/DependencyInjection/InjectableObject.swift +++ /dev/null @@ -1,22 +0,0 @@ -// -// Copyright 2021 New Vector Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation - -/// Class that can be extended that supports injection and the `@Inject` property wrapper. -open class InjectableObject: Injectable { - var dependencies: DependencyContainer! -}