Skip to content

Instantly share code, notes, and snippets.

View t-unit's full-sized avatar

Tobias Ottenweller t-unit

View GitHub Profile
func fetchPlaces(action: Action, context: MiddlewareContext<AppState>) -> Action? {
// create places service
// perform network request
// dispatch set places action on success
}
func create(Dependency) -> Middleware
func fetchPlaces(service: PlacesServing) -> SimpleMiddleware<AppState> {
return { (action, context) in
guard
let placesAction = action as? PlacesAction,
case .fetch = placesAction
else {
return action
}
let placesService = PlacesService(
locale: .current,
apiKey: "<key here>",
fetcher: NetworkFetcher(session: .shared, decoder: JSONDecoder())
)
let appStore = AppStore(
reducer: appReducer,
state: nil,
middleware: [
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
private let placesService: PlacesServing
private let appStore: AppStore
override init() {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let viewController = window?.rootViewController as! ViewController
viewController.store = appStore
return true
}
// Loadable.swift
enum Loadable<T> {
case initial
case loading
case value(T)
case error(Error)
}
// AppState.swift
struct AppState: StateType {
final class LocationEmitter: NSObject {
private let locationManager: CLLocationManager
private let store: AppStore
init(locationManager: CLLocationManager, store: AppStore) {
[…]
locationManager.delegate = self
[…]
}
class AppDelegate: UIResponder, UIApplicationDelegate {
[…]
private let locationEmitter: LocationEmitter
override init() {
[…]
locationEmitter = LocationEmitter(locationManager: CLLocationManager(), store: appStore)
[…]
extension ViewController: StoreSubscriber {
typealias StoreSubscriberStateType = AppState
func newState(state: AppState) {
[…]
authorizationNotDeterminedView.isHidden = state.authorizationStatus != .notDetermined
authorizationDeniedView.isHidden = (state.authorizationStatus == .authorizedAlways) || (state.authorizationStatus == .authorizedWhenInUse)
}
}