Skip to content

Instantly share code, notes, and snippets.

View t-unit's full-sized avatar

Tobias Ottenweller t-unit

View GitHub Profile
{
"products": [
{
"id": "1",
"name": "Spaghetti",
"description": "Spaghetti with tomato sauce",
"price": "10.00 Euro",
"max_basket_count": 1
},
{
class LocationObserver {
init(store: AppStore) {
[…]
store.subscribe(self) { subcription in
subcription.select { state in state.lastKnownLocation }
}
}
// Middleware.swift
func startMonitoring([…]) -> Action? {
[…]
if (appDidBecomeActive && isAuthorized) || (isSetAuthorized && !isAuthorized) {
locationManager.startMonitoringSignificantLocationChanges()
locationManager.requestLocation()
}
return action
// ViewController.swift
@IBAction private func requestAuthorizationButtonTouchUpInside(_ sender: Any) {
store.dispatch(RequestAuthorizationAction())
}
// Middleware.swift
private func requestAuthorization(action: Action, context: MiddlewareContext<AppState>, locationManager: LocationManager) -> Action? {
guard action is RequestAuthorizationAction else {
return action
extension ViewController: StoreSubscriber {
typealias StoreSubscriberStateType = AppState
func newState(state: AppState) {
[…]
authorizationNotDeterminedView.isHidden = state.authorizationStatus != .notDetermined
authorizationDeniedView.isHidden = (state.authorizationStatus == .authorizedAlways) || (state.authorizationStatus == .authorizedWhenInUse)
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
[…]
private let locationEmitter: LocationEmitter
override init() {
[…]
locationEmitter = LocationEmitter(locationManager: CLLocationManager(), store: appStore)
[…]
final class LocationEmitter: NSObject {
private let locationManager: CLLocationManager
private let store: AppStore
init(locationManager: CLLocationManager, store: AppStore) {
[…]
locationManager.delegate = self
[…]
}
// Loadable.swift
enum Loadable<T> {
case initial
case loading
case value(T)
case error(Error)
}
// AppState.swift
struct AppState: StateType {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let viewController = window?.rootViewController as! ViewController
viewController.store = appStore
return true
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private let placesService: PlacesServing
private let appStore: AppStore
override init() {