-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/exercise db add exercise/add (#90)
* CheckAddableExerciseUsecase * RegisterRapidExerciseUsecase * refactor * RapidExerciseDetailViewModel * feat: DI * feat: binding * chore * fix * fix: sorting
- Loading branch information
1 parent
e8a4636
commit 898f295
Showing
7 changed files
with
260 additions
and
60 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
dg-muscle-ios/Tests/Rapid/CheckAddableExerciseUsecaseTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// CheckAddableExerciseUsecaseTests.swift | ||
// AppTests | ||
// | ||
// Created by Happymoonday on 8/13/24. | ||
// | ||
|
||
import XCTest | ||
import Domain | ||
import MockData | ||
|
||
final class CheckAddableExerciseUsecaseTests: XCTestCase { | ||
func testExample() { | ||
let usecase = CheckAddableExerciseUsecase() | ||
let exercise = RAPID_EXERCISES[0] | ||
XCTAssertTrue(usecase.implement(exercise: exercise)) | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
dg-muscle-ios/sources/Domain/Rapid/Usecase/CheckAddableExerciseUsecase.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// CheckAddableExerciseUsecase.swift | ||
// Domain | ||
// | ||
// Created by Happymoonday on 8/13/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class CheckAddableExerciseUsecase { | ||
|
||
public init() { } | ||
|
||
public func implement(exercise: RapidExerciseDomain) -> Bool { | ||
var result: Bool = false | ||
|
||
switch exercise.bodyPart { | ||
|
||
case .back, | ||
.chest, | ||
.lowerArms, | ||
.lowerLegs, | ||
.shoulders, | ||
.upperArms, | ||
.waist, | ||
.upperLegs: | ||
result = true | ||
case .cardio, .neck: | ||
break | ||
} | ||
|
||
return result | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
dg-muscle-ios/sources/Domain/Rapid/Usecase/RegisterRapidExerciseUsecase.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// RegisterRapidExerciseUsecase.swift | ||
// Domain | ||
// | ||
// Created by Happymoonday on 8/13/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class RegisterRapidExerciseUsecase { | ||
private let exerciseRepository: ExerciseRepository | ||
|
||
public init(exerciseRepository: ExerciseRepository) { | ||
self.exerciseRepository = exerciseRepository | ||
} | ||
|
||
public func implement(exercise: RapidExerciseDomain) async throws { | ||
var exerciseDomain: Exercise? | ||
|
||
var parts: [Exercise.Part] = [] | ||
|
||
switch exercise.bodyPart { | ||
case .back: | ||
parts.append(.back) | ||
case .chest: | ||
parts.append(.chest) | ||
case .lowerArms, .upperArms: | ||
parts.append(.arm) | ||
case .lowerLegs, .upperLegs: | ||
parts.append(.leg) | ||
case .shoulders: | ||
parts.append(.shoulder) | ||
case .waist: | ||
parts.append(.core) | ||
case .cardio, .neck: | ||
break | ||
} | ||
|
||
guard parts.isEmpty == false else { return } | ||
|
||
exerciseDomain = .init( | ||
id: exercise.name.filter({ $0.isLetter }), | ||
name: exercise.name, | ||
parts: parts, | ||
favorite: true | ||
) | ||
|
||
guard let exerciseDomain else { return } | ||
try await exerciseRepository.post(exerciseDomain) | ||
} | ||
} |
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
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
48 changes: 48 additions & 0 deletions
48
...uscle-ios/sources/Presentation/Rapid/View/SearchDetail/RapidExerciseDetailViewModel.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// | ||
// RapidExerciseDetailViewModel.swift | ||
// Weight | ||
// | ||
// Created by Happymoonday on 8/13/24. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import Domain | ||
|
||
final class RapidExerciseDetailViewModel: ObservableObject { | ||
let data: RapidExercisePresentation | ||
@Published var showsSecondaryMuscles: Bool = false | ||
@Published var showsAddButton: Bool = false | ||
@Published var snackbarMessage: String? | ||
@Published var loading: Bool = false | ||
|
||
private let checkAddableExerciseUsecase: CheckAddableExerciseUsecase | ||
private let registerRapidExerciseUsecase: RegisterRapidExerciseUsecase | ||
|
||
init( | ||
exercise: Domain.RapidExerciseDomain, | ||
exerciseRepository: ExerciseRepository | ||
) { | ||
data = .init(domain: exercise) | ||
|
||
checkAddableExerciseUsecase = .init() | ||
registerRapidExerciseUsecase = .init(exerciseRepository: exerciseRepository) | ||
|
||
showsAddButton = checkAddableExerciseUsecase.implement(exercise: data.domain) | ||
} | ||
|
||
@MainActor | ||
func add() { | ||
Task { | ||
guard loading == false else { return } | ||
loading = true | ||
do { | ||
try await registerRapidExerciseUsecase.implement(exercise: data.domain) | ||
snackbarMessage = "Exercise Registered!" | ||
} catch { | ||
snackbarMessage = error.localizedDescription | ||
} | ||
loading = false | ||
} | ||
} | ||
} |