Skip to content

Commit

Permalink
✨ (GEK): Link gameplay state changes with ActivityView
Browse files Browse the repository at this point in the history
- add ExerciseSharedData struct
- pass data to view and view models
- enable Continue button on .completed
  • Loading branch information
ladislas committed Oct 27, 2023
1 parent 001b185 commit e09f3b2
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Leka - iOS Monorepo
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import SwiftUI

public class ExerciseSharedData: ObservableObject {
@Published var state: ExerciseState = .idle
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension GameplaySelectAllRightAnswers where ChoiceModelType == GameplaySelecti
func process(_ choice: ChoiceModelType) {
if choice.choice.isRightAnswer {
updateChoice(choice, state: .rightAnswer)
state.send(.completed)
} else {
updateChoice(choice, state: .wrongAnswer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public struct ActivityView: View {
.buttonStyle(.borderedProminent)
.tint(.green)
.padding()
.disabled(viewModel.currentExerciseSharedData.state != .completed)
}
.ignoresSafeArea(.all, edges: .bottom)
.navigationBarTitleDisplayMode(.inline)
Expand Down Expand Up @@ -79,7 +80,7 @@ public struct ActivityView: View {
private func currentExerciseInterface() -> some View {
switch viewModel.currentExerciseInterface {
case .touchToSelect:
TouchToSelectView(exercise: viewModel.currentExercise)
TouchToSelectView(exercise: viewModel.currentExercise, data: viewModel.currentExerciseSharedData)
.id(viewModel.currentExerciseIndexInSequence)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

import Combine
import ContentKit
import SwiftUI

public class ActivityViewViewModel: ObservableObject {

private let sequenceManager: ActivitySequenceManager

private var cancellables: Set<AnyCancellable> = []

@Published var currentActivity: Activity

@Published var totalSequences: Int
Expand All @@ -19,6 +22,7 @@ public class ActivityViewViewModel: ObservableObject {

@Published var currentExercise: Exercise
@Published var currentExerciseInterface: Exercise.Interface
@Published var currentExerciseSharedData = ExerciseSharedData()

public init(activity: Activity) {
self.sequenceManager = ActivitySequenceManager(activity: activity)
Expand All @@ -33,6 +37,8 @@ public class ActivityViewViewModel: ObservableObject {

self.currentExercise = sequenceManager.currentExercise
self.currentExerciseInterface = sequenceManager.currentExercise.interface

subscribeToCurrentExerciseSharedDataUpdates()
}

func moveToNextExercise() {
Expand Down Expand Up @@ -61,4 +67,13 @@ public class ActivityViewViewModel: ObservableObject {
totalExercisesInCurrentSequence = sequenceManager.totalExercisesInCurrentSequence
}

private func subscribeToCurrentExerciseSharedDataUpdates() {
self.currentExerciseSharedData.objectWillChange
.receive(on: DispatchQueue.main)
.sink {
self.objectWillChange.send()
}
.store(in: &cancellables)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public struct TouchToSelectView: View {
self._viewModel = StateObject(wrappedValue: TouchToSelectViewViewModel(choices: choices))
}

public init(exercise: Exercise) {
public init(exercise: Exercise, data: ExerciseSharedData) {
guard case .selection(let payload) = exercise.payload else {
fatalError("Exercise payload is not .selection")
}

self._viewModel = StateObject(wrappedValue: TouchToSelectViewViewModel(choices: payload.choices))
self._viewModel = StateObject(
wrappedValue: TouchToSelectViewViewModel(choices: payload.choices, shared: data))
}

public var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import SwiftUI
class TouchToSelectViewViewModel: ObservableObject {

@Published var choices: [GameplaySelectionChoiceModel] = []
@ObservedObject var exercicesSharedData: ExerciseSharedData

private let gameplay: GameplaySelectAllRightAnswers<GameplaySelectionChoiceModel>
private var cancellables: Set<AnyCancellable> = []

init(choices: [SelectionChoice]) {
init(choices: [SelectionChoice], shared: ExerciseSharedData? = nil) {
self.gameplay = GameplaySelectAllRightAnswers(
choices: choices.map { GameplaySelectionChoiceModel(choice: $0) })
self.exercicesSharedData = shared ?? ExerciseSharedData()

subscribeToGameplaySelectionChoicesUpdates()
subscribeToGameplayStateUpdates()
}

public func onChoiceTapped(choice: GameplaySelectionChoiceModel) {
Expand All @@ -32,4 +36,13 @@ class TouchToSelectViewViewModel: ObservableObject {
.store(in: &cancellables)
}

private func subscribeToGameplayStateUpdates() {
gameplay.state
.receive(on: DispatchQueue.main)
.sink {
self.exercicesSharedData.state = $0
}
.store(in: &cancellables)
}

}

0 comments on commit e09f3b2

Please sign in to comment.