Skip to content

Commit 0b4247f

Browse files
authored
Feature/weight/delete (#87)
* feat: domain * feat: data * feat: mockdata * feat: test * feat: presentation
1 parent 74d00bc commit 0b4247f

File tree

9 files changed

+93
-3
lines changed

9 files changed

+93
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// DeleteWeightUsecaseTests.swift
3+
// AppTests
4+
//
5+
// Created by Happymoonday on 8/13/24.
6+
//
7+
8+
import XCTest
9+
import Domain
10+
import MockData
11+
12+
final class DeleteWeightUsecaseTests: XCTestCase {
13+
func testExample() {
14+
let repository = WeightRepositoryMock()
15+
let usecase = DeleteWeightUsecase(weightRepository: repository)
16+
usecase.implement(weight: WEIGHTS[0])
17+
XCTAssertEqual(repository.get().count, WEIGHTS.count - 1)
18+
}
19+
}

dg-muscle-ios/sources/DataLayer/Weight/Repository/WeightRepositoryImpl.swift

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public final class WeightRepositoryImpl: WeightRepository {
4848
}
4949
}
5050

51+
public func delete(weight: Domain.WeightDomain) {
52+
Task {
53+
let hkWeights = try await fetchWeights()
54+
guard let target = hkWeights.first(where: { hkWeight in
55+
hkWeight.quantity.doubleValue(for: HKUnit.gramUnit(with: .kilo)) == weight.value &&
56+
hkWeight.startDate == weight.date
57+
}) else { return }
58+
try await healthStore.delete(target)
59+
guard let removeIndex = _weights.firstIndex(where: { $0.date == weight.date && $0.value == weight.value }) else { return }
60+
_weights.remove(at: removeIndex)
61+
}
62+
}
63+
5164
private func requestPermission() async throws {
5265
return try await withCheckedThrowingContinuation { continuation in
5366
healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in

dg-muscle-ios/sources/Domain/Weight/Repository/WeightRepository.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ public protocol WeightRepository {
1313

1414
func get() -> [WeightDomain]
1515
func post(weight: WeightDomain)
16+
func delete(weight: WeightDomain)
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// DeleteWeightUsecase.swift
3+
// Domain
4+
//
5+
// Created by Happymoonday on 8/13/24.
6+
//
7+
8+
import Foundation
9+
10+
public final class DeleteWeightUsecase {
11+
private let weightRepository: WeightRepository
12+
13+
public init(weightRepository: WeightRepository) {
14+
self.weightRepository = weightRepository
15+
}
16+
17+
public func implement(weight: WeightDomain) {
18+
weightRepository.delete(weight: weight)
19+
}
20+
}

dg-muscle-ios/sources/MockData/Weight/Repository/WeightRepositoryMock.swift

+5
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ public final class WeightRepositoryMock: WeightRepository {
2424
public func post(weight: Domain.WeightDomain) {
2525
_weights.append(weight)
2626
}
27+
28+
public func delete(weight: Domain.WeightDomain) {
29+
guard let removeIndex = _weights.firstIndex(where: { $0.date == weight.date && $0.value == weight.value }) else { return }
30+
_weights.remove(at: removeIndex)
31+
}
2732
}

dg-muscle-ios/sources/Presentation/Weight/Model/WeightPresentation.swift

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ struct WeightPresentation: Hashable {
2020
date = domain.date
2121
yyyyMMdd = domain.yyyyMMdd
2222
}
23+
24+
var domain: Domain.WeightDomain {
25+
.init(
26+
value: value,
27+
unit: unit.domain,
28+
date: date
29+
)
30+
}
2331
}
2432

2533
extension WeightPresentation {
@@ -35,5 +43,14 @@ extension WeightPresentation {
3543
self = .lbs
3644
}
3745
}
46+
47+
var domain: Domain.WeightDomain.Unit {
48+
switch self {
49+
case .kg:
50+
return .kg
51+
case .lbs:
52+
return .lbs
53+
}
54+
}
3855
}
3956
}

dg-muscle-ios/sources/Presentation/Weight/View/WeightListView/WeightListView.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public struct WeightListView: View {
4444

4545
VStack {
4646
ForEach(viewModel.sections, id: \.self) { section in
47-
WeightSectionView(section: section)
47+
WeightSectionView(section: section) { weight in
48+
viewModel.delete(weight: weight)
49+
}
4850
}
4951
}
5052
}

dg-muscle-ios/sources/Presentation/Weight/View/WeightListView/WeightListViewModel.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ final class WeightListViewModel: ObservableObject {
1818
let subscribeWeightsUsecase: SubscribeWeightsUsecase
1919
let groupWeightsByGroupUsecase: GroupWeightsByGroupUsecase
2020
let filterWeightsOneYearRangeUsecase: FilterWeightsOneYearRangeUsecase
21+
let deleteWeightUsecase: DeleteWeightUsecase
2122

2223
init(weightRepository: WeightRepository) {
2324
getWeightsRangeUsecase = .init()
24-
subscribeWeightsUsecase = .init(weightRepository: weightRepository)
2525
groupWeightsByGroupUsecase = .init()
2626
filterWeightsOneYearRangeUsecase = .init()
27+
deleteWeightUsecase = .init(weightRepository: weightRepository)
28+
subscribeWeightsUsecase = .init(weightRepository: weightRepository)
2729

2830
bind()
2931
}
3032

33+
func delete(weight: WeightPresentation) {
34+
deleteWeightUsecase.implement(weight: weight.domain)
35+
}
36+
3137
private func bind() {
3238
subscribeWeightsUsecase
3339
.implement()

dg-muscle-ios/sources/Presentation/Weight/View/WeightSection/WeightSectionView.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import MockData
1212
struct WeightSectionView: View {
1313
let section: WeightSection
1414

15+
let deleteAction: ((WeightPresentation) -> ())?
16+
1517
var body: some View {
1618
VStack(alignment: .leading) {
1719
HStack {
@@ -58,6 +60,11 @@ struct WeightSectionView: View {
5860
Divider()
5961
}
6062
}
63+
.contextMenu {
64+
Button("Delete") {
65+
deleteAction?(weight)
66+
}
67+
}
6168
}
6269
}
6370
}
@@ -82,6 +89,6 @@ struct WeightSectionView: View {
8289
let group = usecase.implement(weights: GetWeightsWithoutDuplicatesUsecase().implement(weights: WeightRepositoryMock().get()))
8390
let section = WeightSection(yyyyMM: "202407", weights: group["202407"]!.map({ .init(domain: $0) }))
8491

85-
return WeightSectionView(section: section)
92+
return WeightSectionView(section: section, deleteAction: nil)
8693
.preferredColorScheme(.dark)
8794
}

0 commit comments

Comments
 (0)