Skip to content

Commit

Permalink
✅[CHORE] user default로 마지막 가져온 workout 날짜 저장 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
hryeong66 committed Jan 12, 2022
1 parent 5340cda commit 5d94199
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
1 change: 0 additions & 1 deletion Umpah-iOS/Umpah-iOS/Global/Extensions/Date+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extension Date{
//formatter.timeZone = TimeZone.init(abbreviation: "UTC")
formatter.timeZone = TimeZone(identifier: "Asia/Seoul")
let localTime = formatter.string(from: self)

return localTime
}
}
34 changes: 27 additions & 7 deletions Umpah-iOS/Umpah-iOS/Source/Service/SwimmingDataStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ class SwimmingDataStorage{
print("signal 불림")
}
}
let startDate = Calendar.current.date(byAdding: .year, value: -1, to: Date())
let datePredicate = HKQuery.predicateForSamples(withStart: Calendar.current.date(byAdding: .year, value: -1, to: Date()),

let datePredicate = HKQuery.predicateForSamples(withStart: Calendar.current.date(byAdding: .year,
value: -1,
to: Date()),
end: Date(),
options: .strictEndDate)
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)

func getlastestWorkoutDate() -> Date?{
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone(identifier: "Asia/Seoul")
if let lastestData = UserDefaults.standard.string(forKey: "lastestWorkoutDate") {
print("저장된 이전 날짜가 있음 \(lastestData)")
return formatter.date(from: lastestData)
}
return nil
}


//MARK: 가장먼저 source를 가져와야함.
func loadWorkoutHKSource(completion: @escaping (Bool, Error?) -> Void){
let sampleType = HKObjectType.workoutType()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictEndDate)

let sourceQuery = HKSourceQuery.init(sampleType: sampleType,
samplePredicate: datePredicate){ (query, result, error) in
guard let sources = result else{
Expand Down Expand Up @@ -57,11 +69,15 @@ class SwimmingDataStorage{
}
print("source data 시작")
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
//let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictEndDate)
let startDate = getlastestWorkoutDate() ?? Calendar.current.date(byAdding: .year,
value: -1,
to: Date())
let datePredicate = HKQuery.predicateForSamples(withStart: startDate,
end: Date(), options: .strictEndDate)
let swimmingPredicate = HKQuery.predicateForWorkouts(with: .swimming)
let sourcePredicate = HKQuery.predicateForObjects(from: sourceSet)
let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [swimmingPredicate, sourcePredicate])

let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [swimmingPredicate,
sourcePredicate, datePredicate])
let query = HKSampleQuery(sampleType: HKObjectType.workoutType(),
predicate: compound,
limit: 0,
Expand Down Expand Up @@ -103,6 +119,10 @@ class SwimmingDataStorage{
swimmingWorkoutList.append(swimming)
swimming.display()
}
UserDefaults.standard.set(swimmingWorkoutList[swimmingWorkoutList.count - 1].endDate,
forKey: "lastestWorkoutDate")
print()
print("마지막 날짜 설정 \(UserDefaults.standard.string(forKey: "lastestWorkoutDate"))")
completion(swimmingWorkoutList, nil)
}
}
Expand Down
2 changes: 2 additions & 0 deletions Umpah-iOS/Umpah-iOS/Source/ViewControllers/MainVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ final class MainVC: BaseViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
decideTopConstraint(of: .normal)
swimmingViewModel.getNewSwimmingData()
}

// MARK: - Override Method
Expand Down Expand Up @@ -488,6 +489,7 @@ extension MainVC{
print("\($0.display())")
print("count = \($0.recordLabsList.count)")
}
UserDefaults.standard.set(workoutList.last?.startWorkoutDate, forKey: "lastestStartWorkoutDate")
print("----------------------------")
let param = SwimmingRecordRequest(userID: 1,
workoutList: workoutList)
Expand Down
19 changes: 15 additions & 4 deletions Umpah-iOS/Umpah-iOS/Source/ViewModels/SwimmingDataViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class SwimmingDataViewModel{
lazy var swimmingWorkoutList: [SwimWorkoutData] = [] {
didSet {
semaphore.signal()
print("semaphore 시드널 불림")
}
}
let swimmingSubject = PublishSubject<[SwimmingWorkoutData]>()
Expand All @@ -33,6 +32,21 @@ class SwimmingDataViewModel{
}
}

func getNewSwimmingData(){
print("----------------getNewSwimmingData---------------------")
print("lastest swiming date = \(UserDefaults.standard.string(forKey: "lastestWorkoutDate"))")
swimmingStorage.loadWorkoutHKSource { completed, error in
print("complete = \(completed)")
if completed {
self.swimmingStorage.refineSwimmingWorkoutData(completion: { workoutList, error in
self.swimmingWorkoutList = workoutList
self.getStrokeAndDistanceData()
self.getHeartRateData()
})
}
}
}

func refineSwimmingDataForServer(){
var swimmingDataList : [SwimmingWorkoutData] = []
if swimmingWorkoutList.count <= 0{
Expand All @@ -42,11 +56,8 @@ class SwimmingDataViewModel{

swimmingWorkoutList.forEach{ swimming in
swimming.isCompleted().bind(onNext: { isComplete in
print("swimming.metadata = \(swimming.metadata["HKLapLength"]), \(type(of: swimming.metadata["HKLapLength"]))")
let labsData = swimming.metadata["HKLapLength"] as? String
print("labsData = \(labsData)")
let perLab = Int(labsData?.components(separatedBy: " m")[0] ?? "25")
print("perLab = \(perLab), type = \(type(of: perLab))")
var recordList: [RecordLab] = []
swimming.strokeList.forEach{ stroke in
let record = RecordLab(date: stroke.startDate.toKoreaTime(),
Expand Down

0 comments on commit 5d94199

Please sign in to comment.