This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import RxCocoa | |
import RxSwift | |
class SettingsViewController: UIViewController { | |
@IBOutlet weak var settingSwitch1: UISwitch! | |
@IBOutlet weak var settingSwitch2: UISwitch! | |
@IBOutlet weak var settingSwitch3: UISwitch! | |
@IBOutlet weak var settingSwitch4: UISwitch! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func manage(change: Observable<Bool>) -> (value: Driver<ToggleValue>, isBusy: Driver<Bool>) { | |
let storage = self.storage | |
let isBusySubject = PublishSubject<Bool>() | |
let isBusy = isBusySubject | |
.asDriver(onErrorJustReturn: false) | |
.startWith(true) | |
let initialValue = storage | |
.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func manage(change: Observable<Bool>) -> (value: Driver<ToggleValue>, isBusy: Driver<Bool>) { | |
let storage = self.storage | |
let initialValue = storage | |
.read() | |
.map { ToggleValue.initial($0) } | |
.catchError { Observable.just( ToggleValue.unknown($0) ) } | |
let valueAfterSaving = change | |
.flatMap { valueToSave -> Observable<ToggleValue> in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func manage(change: Observable<Bool>) -> (value: Driver<ToggleValue>, isBusy: Driver<Bool>) { | |
// ... | |
let storage = self.storage | |
let valueAfterSaving = change | |
.flatMap { valueToSave -> Observable<ToggleValue> in | |
storage | |
.save(value: valueToSave) | |
.map { ToggleValue.updated(valueToSave) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func manage(change: Observable<Bool>) -> (value: Driver<ToggleValue>, isBusy: Driver<Bool>) { | |
// ... | |
let initialValue = storage | |
.read() | |
.map { ToggleValue.initial($0) } | |
.catchError { Observable.just( ToggleValue.unknown($0) ) } | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum ToggleValue { | |
/// Initial value read from storage | |
case initial(Bool) | |
/// Updated value after successfully saving in storage | |
case updated(Bool) | |
/// Fallback value after unsuccessfully saving in storage | |
case fallback(Bool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import RxCocoa | |
final class Toggle { | |
// MARK: - Properties | |
private let storage: ToggleStorage | |
// MARK: - Initializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
final class NetworkToggleStorage: ToggleStorage { | |
// MARK: - Properties | |
private var value: Bool | |
// MARK: - Initializers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
protocol ToggleStorage { | |
func read() -> Observable<Bool> | |
func save(value: Bool) -> Observable<Void> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift | |
import RxCocoa | |
final class Toggle { | |
func manage(change: Observable<Bool>) -> (value: Driver<ToggleValue>, | |
isBusy: Driver<Bool>) { | |
return (value: .empty(), isBusy: .empty()) // TODO | |
} | |
} |