-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config switcher and connection rewrite
- Loading branch information
1 parent
9534556
commit 9023a58
Showing
16 changed files
with
303 additions
and
131 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// BartConfig.swift | ||
// Proper | ||
// | ||
// Created by Elliott Williams on 8/15/17. | ||
// Copyright © 2017 Elliott Williams. All rights reserved. | ||
// | ||
|
||
import MapKit | ||
import ReactiveSwift | ||
|
||
struct BartConfig: ConfigProtocol { | ||
static let id = "bart" | ||
|
||
let agency = BartAgencyConfig() as AgencyConfig | ||
let app = SharedAppConfig() as AppConfig | ||
let connection = BartConnectionConfig() as ConnectionConfig | ||
let logging = SharedLoggingConfig() as LoggingConfig | ||
let ui = BartUIConfig() as UIConfig | ||
} | ||
|
||
struct BartAgencyConfig: AgencyConfig { | ||
let key = "bart" | ||
let name = "BART" | ||
let region = MKCoordinateRegionMakeWithDistance( | ||
CLLocationCoordinate2D(latitude: 37.784128, longitude: -122.4570273), 33_000, 20_000) | ||
let timeResolution = TimeInterval(30) | ||
|
||
let badgeForRoute: (MutableRoute) -> Property<String?> = { _ in .init(value: nil) } | ||
let titleForRoute: (MutableRoute) -> Property<String?> = { .init($0.name) } | ||
let titleForArrival: (Arrival) -> Property<String?> = { arrival in | ||
if let heading = arrival.heading { | ||
return Property(value: heading) | ||
} else { | ||
return Property(arrival.route.name) | ||
} | ||
} | ||
} | ||
|
||
struct BartConnectionConfig: ConnectionConfig { | ||
let server = URL(string: "ws:https://irene.local:32772/ws")! | ||
let realm = "realm1" | ||
let scheduleService = "providence" | ||
} | ||
|
||
struct BartUIConfig: UIConfig { | ||
let defaultBadgeColor = UIColor.gray | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// CitybusConfig.swift | ||
// Proper | ||
// | ||
// Created by Elliott Williams on 9/19/17. | ||
// Copyright © 2017 Elliott Williams. All rights reserved. | ||
// | ||
|
||
import MapKit | ||
import ReactiveSwift | ||
|
||
struct CitybusConfig: ConfigProtocol { | ||
static let id = "citybus" | ||
|
||
let agency = CitybusAgencyConfig() as AgencyConfig | ||
let app = SharedAppConfig() as AppConfig | ||
let connection = CitybusConnectionConfig() as ConnectionConfig | ||
let logging = SharedLoggingConfig() as LoggingConfig | ||
let ui = CitybusUIConfig() as UIConfig | ||
} | ||
|
||
struct CitybusAgencyConfig: AgencyConfig { | ||
let key = "citybus" | ||
let name = "CityBus" | ||
let region = MKCoordinateRegionMakeWithDistance( | ||
CLLocationCoordinate2D(latitude: 40.4088972, longitude: -86.927739), 9200, 1264) | ||
let timeResolution = TimeInterval(30) | ||
|
||
let badgeForRoute: (MutableRoute) -> Property<String?> = { _ in .init(value: nil) } | ||
let titleForRoute: (MutableRoute) -> Property<String?> = { .init($0.name) } | ||
let titleForArrival: (Arrival) -> Property<String?> = { arrival in | ||
if let heading = arrival.heading { | ||
return Property(value: heading) | ||
} else { | ||
return Property(arrival.route.name) | ||
} | ||
} | ||
} | ||
|
||
struct CitybusConnectionConfig: ConnectionConfig { | ||
let server = URL(string: "ws:https://irene.local:32771/ws")! | ||
let realm = "realm1" | ||
let scheduleService = "timetable" | ||
} | ||
|
||
struct CitybusUIConfig: UIConfig { | ||
let defaultBadgeColor = UIColor.gray | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// ConfigProtocols.swift | ||
// Proper | ||
// | ||
// Created by Elliott Williams on 8/15/17. | ||
// Copyright © 2017 Elliott Williams. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import MapKit | ||
import ReactiveSwift | ||
|
||
protocol ConfigProtocol { | ||
var agency: AgencyConfig { get } | ||
var app: AppConfig { get } | ||
var connection: ConnectionConfig { get } | ||
var logging: LoggingConfig { get } | ||
var ui: UIConfig { get } | ||
|
||
static var id: String { get } | ||
static func make() -> Self | ||
init() | ||
} | ||
|
||
protocol AgencyConfig { | ||
var key: String { get } | ||
var name: String { get } | ||
var region: MKCoordinateRegion { get } | ||
var timeResolution: TimeInterval { get } | ||
var badgeForRoute: (MutableRoute) -> Property<String?> { get } | ||
var titleForRoute: (MutableRoute) -> Property<String?> { get } | ||
var titleForArrival: (Arrival) -> Property<String?> { get } | ||
} | ||
|
||
protocol AppConfig { | ||
var key: String { get } | ||
var name: String { get } | ||
} | ||
|
||
protocol ConnectionConfig { | ||
var server: URL { get } | ||
var realm: String { get } | ||
var scheduleService: String { get } | ||
} | ||
|
||
protocol LoggingConfig { | ||
var ignoreSignalProducers: Set<String> { get } | ||
var logJSON: Bool { get } | ||
} | ||
|
||
protocol UIConfig { | ||
var defaultBadgeColor: UIColor { get } | ||
} | ||
|
||
extension ConfigProtocol { | ||
static func make() -> Self { return Self() } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// ConfigSwitcher.swift | ||
// Proper | ||
// | ||
// Created by Elliott Williams on 8/15/17. | ||
// Copyright © 2017 Elliott Williams. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import ReactiveSwift | ||
import Result | ||
|
||
typealias ConfigProperty = MutableProperty<ConfigProtocol> | ||
|
||
struct Config { | ||
typealias DefaultConfig = CitybusConfig | ||
|
||
static let knownConfigurations: [ConfigProtocol.Type] = [ | ||
CitybusConfig.self, | ||
BartConfig.self | ||
] | ||
|
||
static let shared: ConfigProperty = { | ||
let property = MutableProperty(stored() ?? DefaultConfig()) | ||
property.signal.observeValues(store) | ||
return property | ||
}() | ||
|
||
static var producer: SignalProducer<ConfigProtocol, NoError> { | ||
return shared.producer | ||
} | ||
|
||
@available(*, deprecated, message: "Avoid depending on global state") | ||
static var current: ConfigProtocol { | ||
return shared.value | ||
} | ||
} | ||
|
||
private extension Config { | ||
static func stored() -> ConfigProtocol? { | ||
guard let id = UserDefaults.standard.string(forKey: "selectedConfig") else { | ||
return nil | ||
} | ||
return knownConfigurations.first(where: { $0.id == id })?.make() | ||
} | ||
|
||
static func store(config: ConfigProtocol) { | ||
UserDefaults.standard.set(type(of: config).id, forKey: "selectedConfig") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// SharedConfig.swift | ||
// Proper | ||
// | ||
// Created by Elliott Williams on 8/15/17. | ||
// Copyright © 2017 Elliott Williams. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SharedAppConfig: AppConfig { | ||
let key = "proper" | ||
let name = "Proper Shark" | ||
} | ||
|
||
struct SharedLoggingConfig: LoggingConfig { | ||
let ignoreSignalProducers = Set([ | ||
"MDWamp.subscribeWithSignal", | ||
"MDWamp.callWithSignal", | ||
"Connection.connectionProducer", | ||
"Connection.subscribe", | ||
"MutableRoute.producer" | ||
]) | ||
let logJSON = false | ||
} |
Oops, something went wrong.