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

Commit

Permalink
introduced ReachabilityListener as a class to wrap around the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
anho committed Oct 5, 2018
1 parent 4bb1a8c commit 32cc5a5
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 34 deletions.
12 changes: 12 additions & 0 deletions ReachabilityUI/ReachabilityUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
42726B0B21665E4B00863414 /* ReachabilityProtocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42726B0221665E4B00863414 /* ReachabilityProtocols.swift */; };
42726B0C21665E4B00863414 /* ReachabilityViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42726B0321665E4B00863414 /* ReachabilityViewController.swift */; };
42726B0D21665E4B00863414 /* ReachabilityInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42726B0421665E4B00863414 /* ReachabilityInteractor.swift */; };
42726B102167710900863414 /* ReachabilityListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42726B0F2167710900863414 /* ReachabilityListener.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -32,6 +33,7 @@
42726B0221665E4B00863414 /* ReachabilityProtocols.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReachabilityProtocols.swift; sourceTree = "<group>"; };
42726B0321665E4B00863414 /* ReachabilityViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReachabilityViewController.swift; sourceTree = "<group>"; };
42726B0421665E4B00863414 /* ReachabilityInteractor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReachabilityInteractor.swift; sourceTree = "<group>"; };
42726B0F2167710900863414 /* ReachabilityListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReachabilityListener.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -64,6 +66,7 @@
42726AC921665DCD00863414 /* ReachabilityUI */ = {
isa = PBXGroup;
children = (
42726B0E2167704700863414 /* Model */,
42726AFD21665E4B00863414 /* Reachability */,
42726AFA21665E4B00863414 /* Shared */,
42726ACA21665DCD00863414 /* ReachabilityUI.h */,
Expand Down Expand Up @@ -95,6 +98,14 @@
path = Reachability;
sourceTree = "<group>";
};
42726B0E2167704700863414 /* Model */ = {
isa = PBXGroup;
children = (
42726B0F2167710900863414 /* ReachabilityListener.swift */,
);
path = Model;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -181,6 +192,7 @@
42726B0621665E4B00863414 /* RechabilityUIRepository.swift in Sources */,
42726B0B21665E4B00863414 /* ReachabilityProtocols.swift in Sources */,
42726B0521665E4B00863414 /* ReachabilityRepository.swift in Sources */,
42726B102167710900863414 /* ReachabilityListener.swift in Sources */,
42726B0C21665E4B00863414 /* ReachabilityViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
32 changes: 32 additions & 0 deletions ReachabilityUI/ReachabilityUI/Model/ReachabilityListener.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ReachabilityListener.swift
// ReachabilityUI
//
// Created by Andrei Hogea on 05/10/2018.
// Copyright © 2018 Nodes Aps. All rights reserved.
//

import Foundation

public protocol ReachabilityListenerProtocol {
func listen(_ closure: @escaping Listener)
}

public final class ReachabilityListener: ReachabilityListenerProtocol {

private let ReachabilityListenerRepository: ReachabilityListenerRepository
let id: Int

init(ReachabilityListenerRepository: ReachabilityListenerRepository, id: Int) {
self.ReachabilityListenerRepository = ReachabilityListenerRepository
self.id = id
}

public func listen(_ closure: @escaping Listener) {
ReachabilityListenerRepository.addListener(closure, id: id)
}

deinit {
ReachabilityListenerRepository.removeListener(for: id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ReachabilityCoordinator {
// MARK: - Properties
private let window: UIWindow

private var reachabilityUIRepository: ReachabilityUIRepository
private var reachabilityListenerFactoryProtocol: ReachabilityListenerFactoryProtocol
var hasNavigationBar: Bool
private var configuration: ReachabilityConfiguration
private var vc: ReachabilityViewController! //prevent ViewController from deallocating by holding a reference
Expand All @@ -27,18 +27,18 @@ public class ReachabilityCoordinator {
/// - dependencies
/// - height of the UIViewController. Default value is 30
///
public init(window: UIWindow, reachabilityUIRepository: ReachabilityUIRepository, hasNavigationBar: Bool = true, with configuration: ReachabilityConfiguration) {
public init(window: UIWindow, reachabilityListenerFactoryProtocol: ReachabilityListenerFactoryProtocol, hasNavigationBar: Bool = true, with configuration: ReachabilityConfiguration) {
self.window = window
self.hasNavigationBar = hasNavigationBar
self.reachabilityUIRepository = reachabilityUIRepository
self.reachabilityListenerFactoryProtocol = reachabilityListenerFactoryProtocol
self.configuration = configuration
}

/// Starts the ReachabilityCoordinator and by registering observers and adding the
/// ReachabilityViewController to the window
///
public func start() {
let interactor = ReachabilityInteractor(reachabilityUIRepository: reachabilityUIRepository)
let interactor = ReachabilityInteractor(reachabilityListenerFactoryProtocol: reachabilityListenerFactoryProtocol)
let presenter = ReachabilityPresenter(interactor: interactor,
coordinator: self)
let vc = ReachabilityViewController.instantiate(with: presenter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ class ReachabilityInteractor {
// MARK: - Properties

weak var output: ReachabilityInteractorOutput?
private var reachabilityUIRepository: ReachabilityUIRepository
private var reachabilityListenerFactoryProtocol: ReachabilityListenerFactoryProtocol
private var listener: ReachabilityListenerProtocol!


// MARK: - Init

init(reachabilityUIRepository: ReachabilityUIRepository) {
self.reachabilityUIRepository = reachabilityUIRepository
init(reachabilityListenerFactoryProtocol: ReachabilityListenerFactoryProtocol) {
self.reachabilityListenerFactoryProtocol = reachabilityListenerFactoryProtocol
}

deinit {
reachabilityUIRepository.removeListener(for: "\(ReachabilityInteractor.self)")
}
}

// MARK: - Business Logic -

// PRESENTER -> INTERACTOR
extension ReachabilityInteractor: ReachabilityInteractorInput {
func perform(_ request: Reachability.ReachabilityListener.Request) {
let listener: ReachabilityListener = { [weak self] isConnected in
let listener = reachabilityListenerFactoryProtocol.makeListener()
self.listener = listener
listener.listen { [weak self] isConnected in
self?.output?.present(Reachability.ReachabilityListener.Response(isConnected: isConnected))
}
reachabilityUIRepository.addListener(listener: listener, for: "\(ReachabilityInteractor.self)")
}

}
41 changes: 30 additions & 11 deletions ReachabilityUI/ReachabilityUI/Shared/RechabilityUIRepository.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ReachabilityUIRepository.swift
// ReachabilityListenerRepository.swift
// ReachabilityUIDemo
//
// Created by Andrei Hogea on 03/10/2018.
Expand All @@ -8,30 +8,39 @@

import Foundation

public typealias ReachabilityListener = (_ isConnected: Bool) -> Void
public typealias Listener = (_ isConnected: Bool) -> Void

public protocol ReachabilityUIRepository: class {
func addListener(listener: @escaping ReachabilityListener, for id: String)
func removeListener(for id: String)
// MARK: - Public protocols

public protocol ReachabilityListenerFactoryProtocol: class {
func makeListener() -> ReachabilityListenerProtocol
}

// MARK: - Internal protocols

protocol ReachabilityListenerRepository: class {
func addListener(_ listener: @escaping Listener, id: Int)
func removeListener(for id: Int)
}

protocol ReachabilityDelegate: class {
func networkStatusChanged(_ isConnected: Bool)
}

public protocol HasReachabilityUIRepository {
var reachabilityUIRepository: ReachabilityUIRepository { get set }
public protocol HasReachabilityListenerRepository {
var reachabilityListenerFactoryProtocol: ReachabilityListenerFactoryProtocol { get set }
}

public final class ReachabilityUIManager: ReachabilityUIRepository {
public final class ReachabilityUIManager: ReachabilityListenerRepository {
public static let shared = ReachabilityUIManager()

private var listenerCount: Int = 0
private var isConnected = false {
didSet {
notify()
}
}
private var listeners: [String: ReachabilityListener] = [:]
private var listeners: [Int: Listener] = [:]

// MARK: - Init

Expand All @@ -42,12 +51,12 @@ public final class ReachabilityUIManager: ReachabilityUIRepository {

// MARK: - Listeners

public func addListener(listener: @escaping ReachabilityListener, for id: String) {
func addListener(_ listener: @escaping Listener, id: Int) {
listeners[id] = listener
notify()
}

public func removeListener(for id: String) {
func removeListener(for id: Int) {
listeners[id] = nil
}

Expand All @@ -58,6 +67,16 @@ public final class ReachabilityUIManager: ReachabilityUIRepository {
}
}

// MARK: - ReachabilityDelegate

extension ReachabilityUIManager: ReachabilityListenerFactoryProtocol {
public func makeListener() -> ReachabilityListenerProtocol {
listenerCount += 1
return ReachabilityListener(ReachabilityListenerRepository: self, id: listenerCount)
}
}


// MARK: - ReachabilityDelegate

extension ReachabilityUIManager: ReachabilityDelegate {
Expand Down
2 changes: 1 addition & 1 deletion ReachabilityUIDemo/Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "nodes-ios/Reachability-UI" "0.1"
github "nodes-ios/Reachability-UI" "0.2.2"
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@
isa = PBXNativeTarget;
buildConfigurationList = 42BDD04E2164E1570090F1F3 /* Build configuration list for PBXNativeTarget "ReachabilityUIDemo" */;
buildPhases = (
42F9A95921665A9200A40B43 /* Carthage */,
42BDD0382164E1550090F1F3 /* Sources */,
42BDD0392164E1550090F1F3 /* Frameworks */,
42BDD03A2164E1550090F1F3 /* Resources */,
42F9A94C21663CC500A40B43 /* Embed Frameworks */,
42F9A95921665A9200A40B43 /* Carthage */,
);
buildRules = (
);
Expand Down
13 changes: 4 additions & 9 deletions ReachabilityUIDemo/ReachabilityUIDemo/Shared/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@ import Foundation
import ReachabilityUI

typealias FullDependencies =
HasReachabilityUIRepository & HasReachabilityRepository
HasReachabilityUIRepository

struct Dependencies {
public static let shared = Dependencies()

public var reachabilityRepository: ReachabilityRepository
public var reachabilityUIEmbedableRepository: ReachabilityUIEmbedableRepository
public var reachabilityUIControlRepository: ReachabilityUIControlRepository
public var reachabilityUIEmbedableRepository: ReachabilityUIRepository

init() {
let reachabilityUIManager = ReachabilityUIManager()
reachabilityUIEmbedableRepository = reachabilityUIManager
reachabilityUIControlRepository = reachabilityUIManager
reachabilityRepository = ReachabilityManager()
reachabilityRepository.setup(reachabilityUIManager)
reachabilityUIRepository = reachabilityUIManager
}

}

extension Dependencies: HasReachabilityUIRepository, HasReachabilityRepository {
extension Dependencies: HasReachabilityUIRepository {

}

0 comments on commit 32cc5a5

Please sign in to comment.