Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Feature: automatic injection #43

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Swifjection Changelog

## [0.8.0] - 06.03.2019
### Added
* Automatic injection with `AutoInjectable` protocol using:

```Swift
class MyClass: AutoInjectable {

var property1: MyOtherClass!
var property2: MyOtherClass!

var injectableProperties: [InjectableProperty] {
return [
requires(\MyClass.property1),
requires(\MyClass.property2)
]
}

}
```

### Changed:
* Updated to Swift 4.2
* Separated initialization from `Injectable` protocol by adding `Creatable` and `InjectCreatable` protocols with `init()` and `init(injector: Injecting)` respectively

## [0.7.0] - 10.04.2017
### Added
* Subscript implementation for injecting objects using:
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import UIKit
Expand All @@ -12,7 +12,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var injector: Injecting?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
injector = SwifjectorFactory(bindings: MyBindings()).injector

if let injector = self.injector, let rootViewController = window?.rootViewController as? ViewController {
Expand Down
28 changes: 28 additions & 0 deletions Example/Example/Classes/AutoInjectableObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
import Swifjection

class AutoInjectableObject: Creatable, AutoInjectable {

var foo: Foo?
var fie: Fie?
var fie2: Fie?
var singleton: ExampleSingleton?
var object: MyObject?

var injectableProperties: [InjectableProperty] {
return [
requires(\AutoInjectableObject.foo),
requires(\AutoInjectableObject.fie),
requires(\AutoInjectableObject.fie2),
requires(\AutoInjectableObject.singleton),
requires(\AutoInjectableObject.object)
]
}

required init() {}

}
15 changes: 9 additions & 6 deletions Example/Example/Classes/Bar.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
import Swifjection

class Bar: Injectable {
class Bar: Creatable, Injectable {
var foo: Foo
var fie: Fie?
var fie2: Fie?
Expand All @@ -16,11 +16,14 @@ class Bar: Injectable {
self.foo = foo
}

convenience required init?(injector: Injecting) {
guard let foo = injector.getObject(withType: Foo.self) else {
return nil
required convenience init() {
self.init(foo: Foo())
}

func injectDependencies(injector: Injecting) {
if let foo = injector.getObject(withType: Foo.self) {
self.foo = foo
}
self.init(foo: foo)
fie = injector.getObject(withType: Fie.self)
fie2 = injector.getObject(withType: Fie.self)
singleton = injector.getObject(withType: ExampleSingleton.self)
Expand Down
8 changes: 3 additions & 5 deletions Example/Example/Classes/ExampleSingleton.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
import Swifjection

class ExampleSingleton: Injectable {
required init?(injector: Injecting) {

}
class ExampleSingleton: Creatable {
required init() {}
}

8 changes: 3 additions & 5 deletions Example/Example/Classes/Fee.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
import Swifjection

protocol Fie {}

class Fee: Fie, Injectable {
convenience required init?(injector: Injecting) {
self.init()
}
class Fee: Fie, Creatable {
required init() {}
}
8 changes: 3 additions & 5 deletions Example/Example/Classes/Foo.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
import Swifjection

class Foo: Injectable {
convenience required init?(injector: Injecting) {
self.init()
}
class Foo: Creatable {
required init() {}
}
35 changes: 35 additions & 0 deletions Example/Example/Classes/InjectCreatableObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
import Swifjection

class InjectCreatableObject: InjectCreatable {

var injector: Injecting

var foo: Foo?
var fie: Fie?
var fie2: Fie?
var singleton: ExampleSingleton?
var object: MyObject?

required init?(injector: Injecting) {
guard let foo = injector.getObject(withType: Foo.self),
let fie = injector.getObject(withType: Fie.self),
let fie2 = injector.getObject(withType: Fie.self),
let singleton = injector.getObject(withType: ExampleSingleton.self),
let object = injector.getObject(withType: MyObject.self) else {
assertionFailure("Could not initialize all stored properties")
return nil
}
self.injector = injector
self.foo = foo
self.fie = fie
self.fie2 = fie2
self.singleton = singleton
self.object = object
}

}
2 changes: 1 addition & 1 deletion Example/Example/Classes/MyObject.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/MyBindings.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import Foundation
Expand Down
21 changes: 15 additions & 6 deletions Example/Example/UI/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
//
// Copyright © 2017 Applause Inc. All rights reserved.
// Copyright © 2019 Applause Inc. All rights reserved.
//

import UIKit
import Swifjection

class ViewController: UIViewController, Injectable {
class ViewController: UIViewController, Creatable, Injectable {

var bar: Bar?
var singleton: ExampleSingleton?

required convenience init?(injector: Injecting) {
self.init()
}
var automaticallyInjectableObject: AutoInjectableObject?
var injectCreatableObject: InjectCreatableObject?

func injectDependencies(injector: Injecting) {
bar = injector.getObject(withType: Bar.self)
singleton = injector.getObject(withType: ExampleSingleton.self)
automaticallyInjectableObject = injector.getObject(withType: AutoInjectableObject.self)
injectCreatableObject = injector.getObject(withType: InjectCreatableObject.self)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

NSLog("\(String(describing: bar))")
NSLog("\(String(describing: singleton))")
NSLog("\(String(describing: automaticallyInjectableObject))")
NSLog("\(String(describing: injectCreatableObject))")
}
}

2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
platform :ios, '8.0'
use_frameworks!
inhibit_all_warnings!

Expand All @@ -11,4 +12,3 @@ end
target 'Example' do
pod 'Swifjection', :path => '../'
end

21 changes: 13 additions & 8 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
PODS:
- Nimble (6.0.0)
- Quick (1.0.0)
- Swifjection (0.6.0)
- Nimble (8.0.0)
- Quick (2.0.0)
- Swifjection (0.8.0)

DEPENDENCIES:
- Nimble
- Quick
- Swifjection (from `../`)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- Nimble
- Quick

EXTERNAL SOURCES:
Swifjection:
:path: "../"

SPEC CHECKSUMS:
Nimble: 8fb9b4b34c535b2f32f52c6b9852d6f02d48610b
Quick: 8024e4a47e6cc03a9d5245ef0948264fc6d27cff
Swifjection: 646e6894215df1e285a7302b0da84ee1e7cda041
Nimble: a0e6f95b4b91a4f66b6da3512b6bebc07c9cbf49
Quick: ce1276c7c27ba2da3cb2fd0cde053c3648b3b22d
Swifjection: abbad47bcb5642b8d7e8439bfd883d5b8c729fba

PODFILE CHECKSUM: 87c6229fe28cbb901644f29cda1bc34a2c6b2996
PODFILE CHECKSUM: 596a246ecfe7fa4bcb9d4811922607e0ef4f49c2

COCOAPODS: 1.2.0
COCOAPODS: 1.6.1
Loading