Skip to content

Commit

Permalink
Implemented framework configuration where using a publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
rwbutler committed May 15, 2022
1 parent cb8ce3b commit c701ac4
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 27 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.3.0] - 2022-05-15
### Added
A Connectivity publisher now accepts a configuration object which can be used to configure the framework.

```swift
let publisher = Connectivity.Publisher(
configuration:
.init()
.configureURLSession(.default)
)
```

## [5.2.0] - 2022-05-14
### Added
- Fluent configuration API: Connectivity may now be configured by passing a `ConnectivityConfiguration` object to the initializer.
Expand Down
2 changes: 1 addition & 1 deletion Connectivity.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Connectivity'
s.version = '5.2.0'
s.version = '5.3.0'
s.swift_version = '5.0'
s.summary = 'Makes Internet connectivity detection more robust by detecting Wi-Fi networks without Internet access.'
s.description = <<-DESC
Expand Down
11 changes: 7 additions & 4 deletions Connectivity/Classes/Combine/ConnectivityPublisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import Foundation
public struct ConnectivityPublisher: Publisher {
public typealias Output = Connectivity
public typealias Failure = Never

public init() {}

private let configuration: ConnectivityConfiguration

public init(configuration: ConnectivityConfiguration = ConnectivityConfiguration()) {
self.configuration = configuration
}

public func receive<S: Subscriber>(subscriber: S)
where ConnectivityPublisher.Failure == S.Failure, ConnectivityPublisher.Output == S.Input {
let subscription = ConnectivitySubscription(subscriber: subscriber)
let subscription = ConnectivitySubscription(configuration: configuration, subscriber: subscriber)
subscriber.receive(subscription: subscription)
}
}
Expand Down
5 changes: 3 additions & 2 deletions Connectivity/Classes/Combine/ConnectivitySubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import Foundation

@available(OSX 10.15, iOS 13.0, tvOS 13.0, *)
class ConnectivitySubscription<S: Subscriber>: Subscription where S.Input == Connectivity, S.Failure == Never {
private let connectivity = Connectivity()
private let connectivity: Connectivity
private var subscriber: S?

init(subscriber: S) {
init(configuration: ConnectivityConfiguration, subscriber: S) {
connectivity = Connectivity(configuration: configuration)
self.subscriber = subscriber
startNotifier(with: subscriber)
}
Expand Down
8 changes: 4 additions & 4 deletions Example/Connectivity.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
INFOPLIST_FILE = Connectivity/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 5.2.0;
MARKETING_VERSION = 5.3.0;
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -804,7 +804,7 @@
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
INFOPLIST_FILE = Connectivity/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MARKETING_VERSION = 5.2.0;
MARKETING_VERSION = 5.3.0;
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -865,7 +865,7 @@
INFOPLIST_FILE = Connectivity_Example_macOS/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 5.2.0;
MARKETING_VERSION = 5.3.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example-macOS";
Expand Down Expand Up @@ -897,7 +897,7 @@
INFOPLIST_FILE = Connectivity_Example_macOS/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 5.2.0;
MARKETING_VERSION = 5.3.0;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.rwbutler.demo.Connectivity-Example-macOS";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
19 changes: 11 additions & 8 deletions Example/Connectivity/CombineViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import UIKit
@available(iOS 13.0, *)
class CombineViewController: UIViewController {
// MARK: Outlets

@IBOutlet var activityIndicator: UIActivityIndicatorView!
@IBOutlet var notifierButton: UIButton!
@IBOutlet var statusLabel: UILabel!

// MARK: Status

fileprivate var isCheckingConnectivity: Bool = false
private var cancellable: AnyCancellable?
}
Expand All @@ -39,9 +39,12 @@ extension CombineViewController {
private extension CombineViewController {
func startConnectivityChecks() {
activityIndicator.startAnimating()
let publisher = Connectivity.Publisher()
let publisher = Connectivity.Publisher(
configuration:
.init()
.configureURLSession(.default)
).eraseToAnyPublisher()
cancellable = publisher.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
.sink(receiveCompletion: { [weak self] _ in
guard let strongSelf = self else {
return
Expand All @@ -55,14 +58,14 @@ private extension CombineViewController {
isCheckingConnectivity = true
updateNotifierButton(isCheckingConnectivity: isCheckingConnectivity)
}

func stopConnectivityChecks() {
activityIndicator.stopAnimating()
cancellable?.cancel()
isCheckingConnectivity = false
updateNotifierButton(isCheckingConnectivity: isCheckingConnectivity)
}

func updateConnectionStatus(_ status: Connectivity.Status) {
switch status {
case .connectedViaWiFi, .connectedViaCellular, .connected, .connectedViaEthernet:
Expand All @@ -74,7 +77,7 @@ private extension CombineViewController {
}
statusLabel.text = status.description
}

func updateNotifierButton(isCheckingConnectivity: Bool) {
let buttonText = isCheckingConnectivity ? "Stop notifier" : "Start notifier"
let buttonTextColor = isCheckingConnectivity ? UIColor.red : UIColor.darkGreen
Expand Down
8 changes: 4 additions & 4 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Connectivity's objective is to solve the captive portal problem whereby an iOS d
To learn more about how to use Connectivity, take a look at the [keynote presentation](https://github.com/rwbutler/Connectivity/blob/master/docs/presentations/connectivity.pdf), check out the [blog post](https://medium.com/@rwbutler/solving-the-captive-portal-problem-on-ios-9a53ba2b381e), or make use of the table of contents below:

- [Features](#features)
- [What's New in Connectivity 5.2.0?](#whats-new-in-connectivity-520)
- [What's New in Connectivity 5.3.0?](#whats-new-in-connectivity-530)
- [Hyperconnectivity](#hyperconnectivity)
- [Installation](#installation)
- [Cocoapods](#cocoapods)
Expand Down Expand Up @@ -51,9 +51,19 @@ To learn more about how to use Connectivity, take a look at the [keynote present
- [x] Polling connectivity checks may be performed where a constant network connection is required (optional).
- [x] Combine support via [`Connectivity.Publisher`](https://github.com/rwbutler/Connectivity/blob/master/Connectivity/Classes/Combine/ConnectivityPublisher.swift).

## What's new in Connectivity 5.2.0?
## What's new in Connectivity 5.3.0?

Connectivity 5.2.0 provides a new fluent interface for configuring the Connectivity framework. See [Configuration](#configuration) for more information.
Connectivity 5.3.0 provides a new fluent interface for configuring the Connectivity framework. See [Configuration](#configuration) for more information.

This allows you to configure the framework when making use of Combine publishers e.g.

```swift
let publisher = Connectivity.Publisher(
configuration:
.init()
.configureURLSession(.default)
)
```

## Hyperconnectivity

Expand Down Expand Up @@ -214,7 +224,15 @@ Then remember to call `connectivity.stopNotifier()` when you are done.
### Combine
Connectivity provides support for [Combine](https://developer.apple.com/documentation/combine) via the provision of [`Connectivity.Publisher`](https://github.com/rwbutler/Connectivity/blob/master/Connectivity/Classes/Combine/ConnectivityPublisher.swift) which allows a client to subscribe and be notified of changes in Internet connectivity.

The provided sample application includes [CombineViewController.swift](./Example/Connectivity/CombineViewController.swift) illustrating how to use Connectivity with the Combine framework.
The provided sample application includes [CombineViewController.swift](./Example/Connectivity/CombineViewController.swift) illustrating how to use Connectivity with the Combine framework e.g.

```swift
let publisher = Connectivity.Publisher(
configuration:
.init()
.configureURLSession(.default)
)
```

Note: You do not need to call `startNotifier` when using `ConnectivityPublisher` - this will be done for you automatically on subscription.

Expand Down

0 comments on commit c701ac4

Please sign in to comment.