Last active
April 25, 2016 10:16
-
-
Save jon-cotton/b73874e428c0a0f84a4c7b64c634d95a to your computer and use it in GitHub Desktop.
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
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