Skip to content

Commit

Permalink
Config switcher and connection rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottwilliams committed Sep 28, 2017
1 parent 9534556 commit 9023a58
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 131 deletions.
48 changes: 48 additions & 0 deletions Proper/Configuration/BartConfig.swift
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
}
48 changes: 48 additions & 0 deletions Proper/Configuration/CitybusConfig.swift
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
}
57 changes: 57 additions & 0 deletions Proper/Configuration/ConfigProtocols.swift
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() }
}
50 changes: 50 additions & 0 deletions Proper/Configuration/ConfigSwitcher.swift
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")
}
}
57 changes: 0 additions & 57 deletions Proper/Configuration/Configuration.swift

This file was deleted.

25 changes: 25 additions & 0 deletions Proper/Configuration/SharedConfig.swift
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
}
Loading

0 comments on commit 9023a58

Please sign in to comment.