Skip to content
Merged
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
7 changes: 7 additions & 0 deletions Dietto/Dietto/Domain/Usecases/UserStorageUsecase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
//

import Foundation
import Combine

protocol UserStorageUsecase {
var changeEvent: CurrentValueSubject<Void, Never> { get }

func createUserData(_ user: UserEntity)
func getUserData() -> UserEntity?
func updateUserDefaultData(id: UUID, name: String, gender: Gender, height: Int)
Expand All @@ -18,6 +21,7 @@ protocol UserStorageUsecase {

final class UserStorageUsecaseImpl<Repository: StorageRepository>: UserStorageUsecase where Repository.T == UserDTO {
private let storage: Repository
var changeEvent: CurrentValueSubject<Void, Never> = .init(())

init(storage: Repository) {
self.storage = storage
Expand Down Expand Up @@ -66,6 +70,7 @@ final class UserStorageUsecaseImpl<Repository: StorageRepository>: UserStorageUs
dto.gender = gender.rawValue
dto.height = height
}
changeEvent.send()
}
catch {
print("\(#function) : \(error.localizedDescription)")
Expand All @@ -79,6 +84,7 @@ final class UserStorageUsecaseImpl<Repository: StorageRepository>: UserStorageUs
dto.targetWeight = weight
dto.targetDistance = distance
}
changeEvent.send()
}
catch {
print("\(#function) : \(error.localizedDescription)")
Expand All @@ -91,6 +97,7 @@ final class UserStorageUsecaseImpl<Repository: StorageRepository>: UserStorageUs
try storage.updateData(predicate: predicate) { dto in
dto.currentWeight = currentWeight
}
changeEvent.send()
}
catch {
print("\(#function) : \(error.localizedDescription)")
Expand Down
13 changes: 13 additions & 0 deletions Dietto/Dietto/Domain/Usecases/WeightHistoryUsecase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
protocol WeightHistoryUsecase {
func addNewWeight(weight: Int, date: Date)
func getWeightHistory(chartRange: ChartTimeType) -> [WeightEntity]
func updateWeightByDate(weight: Int, date: Date)
func deleteAllWeightHistory()
}

Expand All @@ -36,6 +37,18 @@ final class WeightHistoryUsecaseImpl<Repository: StorageRepository>: WeightHisto
}
}

func updateWeightByDate(weight: Int, date: Date) {
do {
let predicate = #Predicate<WeightDTO> { $0.date == date }
try storage.updateData(predicate: predicate) { dto in
dto.scale = weight
}
}
catch {
print("\(#function) : \(error.localizedDescription)")
}
}

func deleteAllWeightHistory() {
do {
try storage.deleteAll()
Expand Down
42 changes: 34 additions & 8 deletions Dietto/Dietto/Presentation/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ final class HomeViewModel {
var chartTimeType: ChartTimeType = .weekly
var bodyScaleHistory: [WeightEntity] = []
var pedometerData: PedometerModel?
var currentDistance: Float {
get {
if let distance = pedometerData?.distance {
return distance <= Float(userData.targetDistance) ? distance : Float(userData.targetDistance)
}
else { return 0 }
}
}

var userData: UserEntity

Expand All @@ -39,7 +47,7 @@ final class HomeViewModel {
private let weightHistroyUsecase: WeightHistoryUsecase
private let userStorageUsecase: UserStorageUsecase
private var bag = Set<AnyCancellable>()


init(
pedometerUsecase: PedometerUsecase = PedometerUsecaseImpl(pedometer: PedometerRepositoryImpl()),
Expand All @@ -52,14 +60,22 @@ final class HomeViewModel {
self.userStorageUsecase = userStorageUsecase
if let userData = userStorageUsecase.getUserData() {
self.userData = userData
#warning("데이터 없으면 온보딩으로")
}
else { fatalError("데이터 없음") }
bodyScaleHistoryFetch(type: chartTimeType)

self.userStorageUsecase.changeEvent
.receive(on: DispatchQueue.main)
.sink {[weak self] in
if let data = self?.userStorageUsecase.getUserData() {
self?.userData = data
}
}
.store(in: &bag)
}

func fetchPedometer() {
guard bag.isEmpty else { return }
// guard bag.isEmpty else { return }
pedometerUsecase.startLivePedometerData()
.receive(on: DispatchQueue.main)
.sink { [weak self] pedometer in
Expand All @@ -74,15 +90,18 @@ final class HomeViewModel {
}

func updateCurrentBodyScale(_ value: String) {
guard let value = Int(value) else {
guard let value = Int(value),
let lastModifiedDate = bodyScaleHistory.last?.date else {
print("\(#function) : FAILED to update current body scale")
return
}

#warning("업데이트 한 날짜가 같으면 기존 데이터 replace")
weightHistroyUsecase.addNewWeight(weight: value, date: Date())
userStorageUsecase.updateCurrentWeight(id: userData.id, currentWeight: value)
userData.currentWeight = value
if compareDate(Date(), lastModifiedDate) {
weightHistroyUsecase.updateWeightByDate(weight: value, date: lastModifiedDate)
}
else {
weightHistroyUsecase.addNewWeight(weight: value, date: Date())
}
bodyScaleHistoryFetch(type: chartTimeType)
}

Expand Down Expand Up @@ -112,6 +131,13 @@ final class HomeViewModel {
}
}
}

private func compareDate(_ date1: Date, _ date2: Date) -> Bool {
let date1 = Calendar.current.dateComponents([.year, .month, .day], from: date1)
let date2 = Calendar.current.dateComponents([.year, .month, .day], from: date2)

return date1.year == date2.year && date1.month == date2.month && date1.day == date2.day
}
}


Expand Down
2 changes: 1 addition & 1 deletion Dietto/Dietto/Presentation/Home/View/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct HomeView: View {
if let pedometer = viewModel.pedometerData {
ActivityTable(
currentSteps: pedometer.steps,
currentDistance: pedometer.distance,
currentDistance: viewModel.currentDistance,
targetDistance: viewModel.userData.targetDistance
)
}
Expand Down
24 changes: 13 additions & 11 deletions Dietto/Dietto/Presentation/Home/View/SubViews/ActivityTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ struct ActivityTable: View {
.padding([.leading, .trailing], 8)

VStack {
Gauge(value: currentDistance, in: 0...Float(targetDistance)) {
Text("Goal")
.font(.pretendardBold12)
.foregroundStyle(.text)
}
.animation(.easeInOut(duration: 0.25), value: currentDistance)
.gaugeStyle(.accessoryCircularCapacity)
.tint(.appMain)
.scaleEffect(2)
.padding()
Gauge(
value: currentDistance,
in: 0...Float(targetDistance)) {
Text("Goal")
.font(.pretendardBold12)
.foregroundStyle(.text)
}
.animation(.easeInOut(duration: 0.25), value: currentDistance)
.gaugeStyle(.accessoryCircularCapacity)
.tint(.appMain)
.scaleEffect(2)
.padding()

// Divider().padding(0)
// Divider().padding(0)

HStack(spacing: 4) {
Text("걸음 수").font(.pretendardBold20).foregroundStyle(.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,5 @@ struct WeightHistoryView: View {
}
}
.padding()
// .onAppear(perform: viewModel.chartAnimate)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import Combine

final class OnboardingViewModel: ObservableObject {
@AppStorage("isFirstLaunch") private var isFirstLaunch: Bool = true
Expand Down Expand Up @@ -34,6 +35,7 @@ final class OnboardingViewModel: ObservableObject {

private let userStorageUsecase: UserStorageUsecase
private let weightHistroyUsecase: WeightHistoryUsecase
private var bag = Set<AnyCancellable>()

init(
weightHistroyUsecase: WeightHistoryUsecase = WeightHistoryUsecaseImpl(repository: StorageRepositoryImpl<WeightDTO>()),
Expand All @@ -52,6 +54,21 @@ final class OnboardingViewModel: ObservableObject {
targetWeight = user.targetWeight
targetDistance = user.targetDistance
}

self.userStorageUsecase.changeEvent
.receive(on: DispatchQueue.main)
.sink {[weak self] in
if let user = self?.userStorageUsecase.getUserData() {
self?.currentUserId = user.id
self?.name = user.name
self?.gender = user.gender
self?.height = String(user.height)
self?.weight = String(user.currentWeight)
self?.targetWeight = user.targetWeight
self?.targetDistance = user.targetDistance
}
}
.store(in: &bag)
}

//MARK: - 프로필 설정
Expand Down