Skip to content

Commit

Permalink
feat: Manage Dependency Injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercen-Lee committed Mar 13, 2024
1 parent 6d22a03 commit 39f40fc
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Projects/App/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ let project = Project(
resources: ["Resource/**"],
scripts: [.swiftLint],
dependencies: [
.external(name: "Moya")
.external(name: "Moya"),
.external(name: "Swinject"),
.external(name: "FlowKit")
]
)
]
Expand Down
6 changes: 5 additions & 1 deletion Projects/App/Source/Application/AppMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
//

import SwiftUI
import FlowKit

@main
struct AppMain: App {

@Inject private var flow: FlowProvider

var body: some Scene {
WindowGroup {
MainView()
flow.present()
}
}
}
18 changes: 18 additions & 0 deletions Projects/App/Source/Application/DI/Assembly/FlowAssembly.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// FlowAssembly.swift
// DodamDodam
//
// Created by Mercen on 3/13/24.
//

import Swinject
import FlowKit

class FlowAssembly: Assembly {

func assemble(container: Container) {
container.register(FlowProvider.self) { _ in
FlowProvider(rootView: MainView())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ViewModelAssembly.swift
// DodamDodam
//
// Created by Mercen on 3/13/24.
//

import Swinject

class ViewModelAssembly: Assembly {

func assemble(container: Container) {
container.register(MainViewModel.self) { _ in
MainViewModel()
}
}
}
22 changes: 22 additions & 0 deletions Projects/App/Source/Application/DI/Injection/Inject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Inject.swift
// DodamDodam
//
// Created by Mercen on 3/13/24.
//

import Swinject

@propertyWrapper
public struct Inject<T> {

public let wrappedValue: T = {
if let value = DependencyProvider.shared.container.resolve(T.self) {
return value
} else {
fatalError("Injection error: \(T.self)")
}
}()

public init() { }
}
31 changes: 31 additions & 0 deletions Projects/App/Source/Application/DI/Injection/InjectObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// InjectObject.swift
// DodamDodam
//
// Created by Mercen on 3/13/24.
//

import SwiftUI
import Swinject

@propertyWrapper
public struct InjectObject<T>: DynamicProperty where T: ObservableObject {

@StateObject private var object: T = {
if let value = DependencyProvider.shared.container.resolve(T.self) {
return value
} else {
fatalError("Injection error: \(T.self)")
}
}()

public var wrappedValue: T {
object
}

public var projectedValue: ObservedObject<T>.Wrapper {
$object
}

public init() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// DependencyProvider.swift
// DodamDodam
//
// Created by Mercen on 3/13/24.
//

import Swinject

class DependencyProvider {

static let shared = DependencyProvider()

let container = Container()
let assembler: Assembler

init() {
Container.loggingFunction = nil
assembler = Assembler(
[
FlowAssembly(),
ViewModelAssembly()
],
container: container
)
}
}
5 changes: 4 additions & 1 deletion Projects/App/Source/Feature/Main/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import SwiftUI

struct MainView: View {

@InjectObject var viewModel: MainViewModel

var body: some View {
Text("Hello, world")
Text("Hello, world!")
}
}

Expand Down
14 changes: 14 additions & 0 deletions Projects/App/Source/Feature/Main/MainViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// MainViewModel.swift
// DodamDodam
//
// Created by Mercen on 3/13/24.
//

import Combine
import FlowKit

class MainViewModel: ObservableObject {

@Inject var flow: FlowProvider
}
3 changes: 2 additions & 1 deletion Projects/App/Source/Shared/Extension/FontExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import SwiftUI

extension Font {
public extension Font {

static let heading1: Self = .system(size: 32, weight: .bold)
static let heading2: Self = .system(size: 28, weight: .bold)
static let heading3: Self = .system(size: 24, weight: .bold)
Expand Down
3 changes: 2 additions & 1 deletion Projects/App/Source/Shared/Extension/MoyaExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import Moya

extension MoyaProvider {
public extension MoyaProvider {

func request(_ target: Target) async -> Result<Response, MoyaError> {
await withCheckedContinuation { continuation in
request(target) {
Expand Down
3 changes: 2 additions & 1 deletion Projects/App/Source/Shared/Extension/TaskExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import Foundation

extension Task where Success == Never, Failure == Never {
public extension Task where Success == Never, Failure == Never {

static func sleep(seconds: Double) async throws {
let duration = UInt64(seconds * 1_000_000_000)
try await Task.sleep(nanoseconds: duration)
Expand Down
18 changes: 18 additions & 0 deletions Tuist/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "5.9.0"
}
},
{
"identity" : "flowkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Mercen-Lee/FlowKit",
"state" : {
"branch" : "main",
"revision" : "325681f69d3881740dab68730c195558405d5ab2"
}
},
{
"identity" : "moya",
"kind" : "remoteSourceControl",
Expand All @@ -35,6 +44,15 @@
"revision" : "9dcaa4b333db437b0fbfaf453fad29069044a8b4",
"version" : "6.6.0"
}
},
{
"identity" : "swinject",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Swinject/Swinject.git",
"state" : {
"revision" : "463cb2d659c8ae34899d18057ea5b1bb86fc3178",
"version" : "2.8.4"
}
}
],
"version" : 2
Expand Down
4 changes: 3 additions & 1 deletion Tuist/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import PackageDescription
let package = Package(
name: "PackageName",
dependencies: [
.package(url: "https://github.com/Moya/Moya", from: "15.0.0")
.package(url: "https://github.com/Moya/Moya", from: "15.0.0"),
.package(url: "https://github.com/Swinject/Swinject.git", from: "2.8.0"),
.package(url: "https://github.com/Mercen-Lee/FlowKit", branch: "main")
]
)

0 comments on commit 39f40fc

Please sign in to comment.