Skip to content

Commit

Permalink
Add AlertPresenter and presenting alerts on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Piechocki committed Oct 23, 2020
1 parent b68887a commit d085486
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ExampleApp/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
8D72C58A2539C65000456D1A /* RxBluetoothKit in Frameworks */ = {isa = PBXBuildFile; productRef = 8D72C5892539C65000456D1A /* RxBluetoothKit */; };
8D72C58F2539C87300456D1A /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D72C58E2539C87300456D1A /* WelcomeView.swift */; };
8D896D472542D7C500FD5FE5 /* AlertPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D896D462542D7C500FD5FE5 /* AlertPresenter.swift */; };
8DA476A625399CFA00B79A0A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DA476A525399CFA00B79A0A /* AppDelegate.swift */; };
8DA476A825399CFA00B79A0A /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DA476A725399CFA00B79A0A /* SceneDelegate.swift */; };
8DA476AA25399CFA00B79A0A /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DA476A925399CFA00B79A0A /* WelcomeViewController.swift */; };
Expand All @@ -22,6 +23,7 @@

/* Begin PBXFileReference section */
8D72C58E2539C87300456D1A /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = "<group>"; };
8D896D462542D7C500FD5FE5 /* AlertPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertPresenter.swift; sourceTree = "<group>"; };
8DA476A225399CFA00B79A0A /* ExampleApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
8DA476A525399CFA00B79A0A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
8DA476A725399CFA00B79A0A /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -53,6 +55,7 @@
children = (
8DA476A525399CFA00B79A0A /* AppDelegate.swift */,
8DA476A725399CFA00B79A0A /* SceneDelegate.swift */,
8D896D462542D7C500FD5FE5 /* AlertPresenter.swift */,
);
path = Application;
sourceTree = "<group>";
Expand Down Expand Up @@ -211,6 +214,7 @@
8DB15AA6253D801C00DECF92 /* CentralViewController.swift in Sources */,
8DB15AAC253D938700DECF92 /* PeripheralViewController.swift in Sources */,
8DA476A625399CFA00B79A0A /* AppDelegate.swift in Sources */,
8D896D472542D7C500FD5FE5 /* AlertPresenter.swift in Sources */,
8DA476A825399CFA00B79A0A /* SceneDelegate.swift in Sources */,
8DB15AAF253D93A800DECF92 /* PeripheralView.swift in Sources */,
8D72C58F2539C87300456D1A /* WelcomeView.swift in Sources */,
Expand Down
19 changes: 19 additions & 0 deletions ExampleApp/ExampleApp/Application/AlertPresenter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import UIKit

class AlertPresenter {

static func presentError(with message: String, on viewController: UIViewController?) {
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alert.addAction(
UIAlertAction(
title: "Ok",
style: .cancel,
handler: { action in
viewController?.dismiss(animated: true)
}
)
)
viewController?.present(alert, animated: true)
}

}
14 changes: 10 additions & 4 deletions ExampleApp/ExampleApp/Screens/Central/CentralViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CentralViewController: UIViewController {

Observable.combineLatest(managerIsOn, Observable.just(manager)) { $1 }
.flatMap { $0.scanForPeripherals(withServices: [serviceUuid]) }
.timeout(.seconds(7), scheduler: MainScheduler.instance)
.take(1)
.flatMap { $0.peripheral.establishConnection() }
.do(onNext: { [weak self] _ in self?.centralView.readValueLabel.isEnabled = true })
Expand All @@ -56,10 +57,15 @@ class CentralViewController: UIViewController {
.flatMap { $0.discoverCharacteristics([characteristicUuid]) }
.flatMap { Observable.from($0) }
.flatMap { $0.observeValueUpdateAndSetNotification() }
.subscribe(onNext: { [weak self] in
guard let data = $0.value, let string = String(data: data, encoding: .utf8) else { return }
self?.updateValue(string)
})
.subscribe(
onNext: { [weak self] in
guard let data = $0.value, let string = String(data: data, encoding: .utf8) else { return }
self?.updateValue(string)
},
onError: { [weak self] in
AlertPresenter.presentError(with: $0.localizedDescription, on: self?.navigationController)
}
)
.disposed(by: disposeBag)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ class PeripheralViewController: UIViewController {
Observable.combineLatest(managerIsOn, Observable.just(manager)) { $1 }
.flatMap { $0.add(service) }
.flatMap { [manager] in manager.startAdvertising($0.advertisingData) }
.subscribe(onNext: { [weak self] in
print("advertising started! \($0)")
self?.setUpdate(enabled: true)
})
.subscribe(
onNext: { [weak self] in
print("advertising started! \($0)")
self?.setUpdate(enabled: true)
},
onError: { [weak self] in
AlertPresenter.presentError(with: $0.localizedDescription, on: self?.navigationController)
}
)
.disposed(by: disposeBag)
}

Expand Down

0 comments on commit d085486

Please sign in to comment.