1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Persephone/Reducers/PlayerReducer.swift

47 lines
1.1 KiB
Swift

//
// PlayerReducer.swift
// Persephone
//
// Created by Daniel Barber on 2019/4/19.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Cocoa
import ReSwift
func playerReducer(action: Action, state: PlayerState?) -> PlayerState {
var state = state ?? PlayerState()
switch action {
case let action as UpdateStatusAction:
state.status = action.status
state.state = action.status.state
state.totalTime = action.status.totalTime
state.elapsedTimeMs = action.status.elapsedTimeMs
if state.state == .playing {
AppDelegate.trackTimer.start(elapsedTimeMs: state.elapsedTimeMs)
} else {
AppDelegate.trackTimer.stop(elapsedTimeMs: state.elapsedTimeMs)
}
case let action as UpdateElapsedTimeAction:
state.elapsedTimeMs = action.elapsedTimeMs
default:
break
}
return state
}
func updateElapsedTime(_ timer: Timer) {
guard let userInfo = timer.userInfo as? Dictionary<String, Any>,
let elapsedTimeMs = userInfo["elapsedTimeMs"] as? UInt
else { return }
AppDelegate.store.dispatch(
UpdateElapsedTimeAction(elapsedTimeMs: elapsedTimeMs)
)
}