Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions OutRun/Controllers/Workout/NewWorkoutViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,19 @@ class NewWorkoutViewController: MapViewControllerWithConatinerView, WorkoutBuild
}
}
case .stop:
self.builder.finish { (success) in
self.builder.finish(completion: { (success) in
if success {
print("[NewWorkout] finished recording")
} else {
self.displayBuilderFailureError()
}
}
}, onSaveOrDiscard: { _ in
DispatchQueue.main.async {
self.dismiss(animated: true) {
print("[NewWorkout] dismissed after save or discard")
}
}
})
case .pauseOrContinue:
if self.builder.status == .paused {
self.builder.startOrResume { (success) in
Expand Down Expand Up @@ -314,7 +320,7 @@ class NewWorkoutViewController: MapViewControllerWithConatinerView, WorkoutBuild
title: LS("NewWorkoutViewController.Cancel.Error.Recording.Action.StopRecording"),
style: .destructive,
action: { _ in
self.builder.finish(shouldProvideCompletionActions: false) { (success) in
self.builder.finish(shouldProvideCompletionActions: false, completion: { (success) in
if success {
alert?.dismiss(animated: true) {
self.dismiss(animated: true) {
Expand All @@ -325,7 +331,7 @@ class NewWorkoutViewController: MapViewControllerWithConatinerView, WorkoutBuild
self.displayBuilderFailureError()
print("[NewWorkout] stop tracking failed")
}
}
})
}
),
(
Expand Down
6 changes: 4 additions & 2 deletions OutRun/Models/Workout/WorkoutBuilder/WorkoutBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ class WorkoutBuilder: ApplicationStateObserver {

/**
Stops and resets the `WorkoutBuilder` if this action is appropriate giving the recorded data to an instance of `WorkoutCompletionActionHandler`
- parameter shouldProvideCompletionActions: whether to display completion actions or save automatically
- parameter completion: a closure with a success boolean as a parameter indicating if the action was performed
- parameter onSaveOrDiscard: optional closure called when Save or Discard is selected (not called for Continue)
*/
public func finish(shouldProvideCompletionActions: Bool = true, completion: @escaping (Bool) -> Void) {
public func finish(shouldProvideCompletionActions: Bool = true, completion: @escaping (Bool) -> Void, onSaveOrDiscard: ((Bool) -> Void)? = nil) {

let completion = makeClosureThreadSafe(completion)
let timestamp = Date()
Expand All @@ -135,7 +137,7 @@ class WorkoutBuilder: ApplicationStateObserver {

if let snapshot = self.createSnapshot() {

let handler = WorkoutCompletionActionHandler(snapshot: snapshot, builder: self)
let handler = WorkoutCompletionActionHandler(snapshot: snapshot, builder: self, completionHandler: onSaveOrDiscard)

if shouldProvideCompletionActions {
handler.display()
Expand Down
12 changes: 11 additions & 1 deletion OutRun/Models/Workout/WorkoutCompletionActionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ class WorkoutCompletionActionHandler {
/// If `true` the `WorkoutCompletionActionHandler` did already perform an action, so no additional action should be taken
private var didPerformAction: Bool = false

/// Completion handler called when save or discard is selected
private var completionHandler: ((Bool) -> Void)?

/**
Initialises the `WorkoutCompletionActionHandler` with the needed snapshot of an `TempWorkout`
- parameter snapshot: a `TempWorkout` object to be saved, discarded or continued
- parameter builder: a `WorkoutBuilder` reference to continue the workout if needed
- parameter completionHandler: a closure to be called when save or discard is selected
*/
public init(snapshot: TempWorkout, builder: WorkoutBuilder) {
public init(snapshot: TempWorkout, builder: WorkoutBuilder, completionHandler: ((Bool) -> Void)? = nil) {

self.snapshot = snapshot
self.builder = builder
self.completionHandler = completionHandler

}

Expand Down Expand Up @@ -71,6 +77,8 @@ class WorkoutCompletionActionHandler {
banner.duration = 5
banner.show()

self.completionHandler?(false)

}

}
Expand Down Expand Up @@ -119,6 +127,8 @@ class WorkoutCompletionActionHandler {

self.didPerformAction = true

self.completionHandler?(false)

}

}