Skip to content

Commit

Permalink
✨ (ContentKit): Add new ActivityExerciseManager to replace ActivitySe…
Browse files Browse the repository at this point in the history
…quenceManager
  • Loading branch information
ladislas committed Feb 21, 2024
1 parent 6f26b9a commit 0eaa170
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
71 changes: 71 additions & 0 deletions Modules/ContentKit/Sources/Activity/ActivityExerciseManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Leka - iOS Monorepo
// Copyright APF France handicap
// SPDX-License-Identifier: Apache-2.0

public class ActivityExerciseManager {
// MARK: Lifecycle

public init(activity: Activity) {
var copyOfActivity = activity

if copyOfActivity.exercisePayload.options.shuffleExercises {
copyOfActivity.exercisePayload.exerciseGroups = copyOfActivity.exercisePayload.exerciseGroups.map {
Activity.ExercisesPayload.ExerciseGroup(exercises: $0.exercises.shuffled())
}
}

if copyOfActivity.exercisePayload.options.shuffleGroups {
copyOfActivity.exercisePayload.exerciseGroups.shuffle()
}

self.activity = copyOfActivity
}

// MARK: Public

public var currentGroupIndex: Int = 0
public var currentExerciseIndexInCurrentGroup: Int = 0

public var totalGroups: Int {
self.activity.exercisePayload.exerciseGroups.count
}

public var totalExercisesInCurrentGroup: Int {
self.activity.exercisePayload.exerciseGroups[self.currentGroupIndex].exercises.count
}

public var currentExercise: Exercise {
self.activity.exercisePayload.exerciseGroups[self.currentGroupIndex].exercises[self.currentExerciseIndexInCurrentGroup]
}

public var isFirstExercise: Bool {
self.currentExerciseIndexInCurrentGroup == 0 && self.currentGroupIndex == 0
}

public var isLastExercise: Bool {
self.currentExerciseIndexInCurrentGroup == self.activity.exercisePayload.exerciseGroups[self.currentGroupIndex].exercises.count - 1
&& self.currentGroupIndex == self.activity.exercisePayload.exerciseGroups.count - 1
}

public func moveToNextExercise() {
if self.currentExerciseIndexInCurrentGroup < self.activity.exercisePayload.exerciseGroups[self.currentGroupIndex].exercises.count - 1 {
self.currentExerciseIndexInCurrentGroup += 1
} else if self.currentGroupIndex < self.activity.exercisePayload.exerciseGroups.count - 1 {
self.currentGroupIndex += 1
self.currentExerciseIndexInCurrentGroup = 0
}
}

public func moveToPreviousExercise() {
if self.currentExerciseIndexInCurrentGroup > 0 {
self.currentExerciseIndexInCurrentGroup -= 1
} else if self.currentGroupIndex > 0 {
self.currentGroupIndex -= 1
self.currentExerciseIndexInCurrentGroup = self.activity.exercisePayload.exerciseGroups[self.currentGroupIndex].exercises.count - 1
}
}

// MARK: Private

private let activity: Activity
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ActivityViewViewModel: ObservableObject {
// MARK: Lifecycle

public init(activity: ActivityDeprecated) {
self.sequenceManager = ActivitySequenceManager(activity: activity)
self.sequenceManager = ActivityExerciseManager(activity: activity)

self.currentActivity = activity

Expand Down Expand Up @@ -116,7 +116,7 @@ public class ActivityViewViewModel: ObservableObject {

// MARK: Private

private let sequenceManager: ActivitySequenceManager
private let sequenceManager: ActivityExerciseManager

private var cancellables: Set<AnyCancellable> = []

Expand Down

0 comments on commit 0eaa170

Please sign in to comment.