From 5d941993ff820cda81f5338ef0065ba78fa9f8e8 Mon Sep 17 00:00:00 2001 From: hryeong66 Date: Wed, 12 Jan 2022 13:57:51 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=85[CHORE]=20user=20default=EB=A1=9C=20?= =?UTF-8?q?=EB=A7=88=EC=A7=80=EB=A7=89=20=EA=B0=80=EC=A0=B8=EC=98=A8=20wor?= =?UTF-8?q?kout=20=EB=82=A0=EC=A7=9C=20=EC=A0=80=EC=9E=A5=20(#63)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Umpah-iOS/Global/Extensions/Date+.swift | 1 - .../Source/Service/SwimmingDataStorage.swift | 34 +++++++++++++++---- .../Source/ViewControllers/MainVC.swift | 2 ++ .../ViewModels/SwimmingDataViewModel.swift | 19 ++++++++--- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/Umpah-iOS/Umpah-iOS/Global/Extensions/Date+.swift b/Umpah-iOS/Umpah-iOS/Global/Extensions/Date+.swift index 1e3dd29..b5f5226 100644 --- a/Umpah-iOS/Umpah-iOS/Global/Extensions/Date+.swift +++ b/Umpah-iOS/Umpah-iOS/Global/Extensions/Date+.swift @@ -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 } } diff --git a/Umpah-iOS/Umpah-iOS/Source/Service/SwimmingDataStorage.swift b/Umpah-iOS/Umpah-iOS/Source/Service/SwimmingDataStorage.swift index 5ef4f16..9377005 100644 --- a/Umpah-iOS/Umpah-iOS/Source/Service/SwimmingDataStorage.swift +++ b/Umpah-iOS/Umpah-iOS/Source/Service/SwimmingDataStorage.swift @@ -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{ @@ -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, @@ -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) } } diff --git a/Umpah-iOS/Umpah-iOS/Source/ViewControllers/MainVC.swift b/Umpah-iOS/Umpah-iOS/Source/ViewControllers/MainVC.swift index 503aefe..baa970e 100644 --- a/Umpah-iOS/Umpah-iOS/Source/ViewControllers/MainVC.swift +++ b/Umpah-iOS/Umpah-iOS/Source/ViewControllers/MainVC.swift @@ -73,6 +73,7 @@ final class MainVC: BaseViewController { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) decideTopConstraint(of: .normal) + swimmingViewModel.getNewSwimmingData() } // MARK: - Override Method @@ -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) diff --git a/Umpah-iOS/Umpah-iOS/Source/ViewModels/SwimmingDataViewModel.swift b/Umpah-iOS/Umpah-iOS/Source/ViewModels/SwimmingDataViewModel.swift index c27a47b..b82e155 100644 --- a/Umpah-iOS/Umpah-iOS/Source/ViewModels/SwimmingDataViewModel.swift +++ b/Umpah-iOS/Umpah-iOS/Source/ViewModels/SwimmingDataViewModel.swift @@ -15,7 +15,6 @@ class SwimmingDataViewModel{ lazy var swimmingWorkoutList: [SwimWorkoutData] = [] { didSet { semaphore.signal() - print("semaphore 시드널 불림") } } let swimmingSubject = PublishSubject<[SwimmingWorkoutData]>() @@ -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{ @@ -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(),