Skip to content

Instantly share code, notes, and snippets.

@jon-cotton
Last active April 25, 2016 10:16
Show Gist options
  • Save jon-cotton/b73874e428c0a0f84a4c7b64c634d95a to your computer and use it in GitHub Desktop.
Save jon-cotton/b73874e428c0a0f84a4c7b64c634d95a to your computer and use it in GitHub Desktop.
enum AlertsStatusUpdateAction: ActionCreatingAction {
case requestUpdate(AlertsService)
case updateFinishedWithAlerts([InAppAlert])
case updateFinishedWithError(AlertsError)
func execute(state: AppState) -> AppState {
var mutableState = state
switch self {
case .requestUpdate(_):
mutableState.inAppAlerts.updateState = .updating
case .updateFinishedWithAlerts(let alerts):
mutableState.inAppAlerts.alerts = alerts
mutableState.inAppAlerts.updateState = .notUpdating
case .updateFinishedWithError(let error):
mutableState.inAppAlerts.previousUpdateError = error
mutableState.inAppAlerts.updateState = .notUpdating
}
return mutableState
}
func createAction(state: AppState, completion: (Action?) -> ()) {
switch self {
case .requestUpdate(let alertsService):
if state.inAppAlerts.updateState != .updating {
alertsService.latestAlerts { alerts in
var action: AlertsStatusUpdateAction
if let alerts = alerts {
action = .updateFinishedWithAlerts(alerts)
} else {
action = .updateFinishedWithError(.alertsUpdateFailed)
}
completion(action)
}
}
case .updateFinishedWithAlerts(_), .updateFinishedWithError(_):
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment