Skip to content

Commit

Permalink
🔀:: #23 from MaeumgaGym/refactoring/#22-codeEdit
Browse files Browse the repository at this point in the history
파일 분리 작업
  • Loading branch information
jjunhaa0211 authored Dec 28, 2023
2 parents 049f51f + 972d9d0 commit 73e7813
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 208 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveX/RxSwift",
"state" : {
"revision" : "b4307ba0b6425c0ba4178e138799946c3da594f8",
"version" : "6.5.0"
"revision" : "9dcaa4b333db437b0fbfaf453fad29069044a8b4",
"version" : "6.6.0"
}
}
],
Expand Down
12 changes: 5 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import PackageDescription
let package = Package(
name: "MindGymKit",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.watchOS(.v6),
.tvOS(.v13),
.iOS(.v13)
],
products: [
.library(name: "MindGymKit", targets: ["MindGymKit"]),
.library(name: "MindGymKit", targets: ["MindGymKit"])
],
dependencies: [
.package(url: "https://github.com/ReactiveX/RxSwift", from: "6.5.0"),
Expand All @@ -21,8 +18,9 @@ let package = Package(
.target(
name: "MindGymKit",
dependencies: [
"RxSwift",
]),
"RxSwift"
]
),
.testTarget(
name: "MindGymKitTests",
dependencies: ["MindGymKit"]),
Expand Down
49 changes: 49 additions & 0 deletions Sources/MindGymKit /HealthKitStepCountProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import UIKit
import HealthKit
import RxSwift

// MARK: - MindGymKit
public protocol StepCountProvider {
func fetchTodayStepCount(completion: @escaping (Double?) -> Void)
}

open class HealthKitStepCountProvider: StepCountProvider {
private let healthStore = HKHealthStore()

public init() {}

public func fetchTodayStepCount(completion: @escaping (Double?) -> Void) {
guard HKHealthStore.isHealthDataAvailable() else {
completion(nil)
return
}

let stepType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
let calendar = Calendar.current
let now = Date()
let startOfDay = calendar.startOfDay(for: now)
let endOfDay = calendar.date(byAdding: .day, value: 1, to: startOfDay)!
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: endOfDay, options: .strictStartDate)

healthStore.requestAuthorization(toShare: nil, read: [stepType]) { (success, error) in
if success {
let query = HKStatisticsQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum) { query, result, error in
if let result = result, let sum = result.sumQuantity() {
completion(sum.doubleValue(for: HKUnit.count()))
} else {
completion(nil)
if let error = error {
print("Error fetching step count: \(error.localizedDescription)")
}
}
}
self.healthStore.execute(query)
} else {
completion(nil)
if let error = error {
print("Error requesting HealthKit authorization: \(error.localizedDescription)")
}
}
}
}
}
70 changes: 70 additions & 0 deletions Sources/MindGymKit /MindGaGymKitTimer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import UIKit
import RxSwift

// MARK: - MindGaGymKitTimer
open class MindGaGymKitTimer: NSObject, TimerControl {

private var initCounter: Double = 0.0
private var counter: Double = 0.0
private var timer: Timer?
private var lapTimes: [Double] = []
private let timerSubject = PublishSubject<String>()
private let recordSubject = PublishSubject<[String]>()

public var timeUpdate: Observable<String> {
return timerSubject.asObservable()
}

public func setting(count: Double) {
initCounter = count
self.counter = count
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}

public func start() {
timer = Timer.scheduledTimer(withTimeInterval: 0.035, repeats: true) { [weak self] _ in
guard let self = self else { return }
self.counter -= 0.035
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}
}

public func stop() {
timer?.invalidate()
timer = nil
}

public func restart() {
counter = initCounter
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}

public func reset() {
counter = 0.0
initCounter = 0.0
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}

public func record() {
lapTimes.append(counter)
let lapTimesString = lapTimes.map { timeString(from: $0) }
recordSubject.onNext(lapTimesString)
}

private func timeString(from counter: Double) -> String {
let hours: String = String(format: "%02d", Int(counter / 3600))
let minutes: String = String(format: "%02d", Int(counter / 60))
let seconds: String = String(format: "%02d", Int(counter.truncatingRemainder(dividingBy: 60)))
if counter / 3600 >= 1 {
return "\(hours) : \(minutes) : \(seconds)"
} else if counter / 60 > 1 {
return "\(minutes) : \(seconds)"
} else {
return "\(minutes) : \(seconds)"
}
}
}
199 changes: 0 additions & 199 deletions Sources/MindGymKit /MindGymKit.swift

This file was deleted.

23 changes: 23 additions & 0 deletions Sources/MindGymKit /MindGymStopWatchKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import UIKit
import RxSwift

// MARK: - MindGymStopWatchKit
open class MindGymStopWatchKit {
public let mainStopwatch: TimerControl = Stopwatch()

public func startTimer() {
mainStopwatch.start()
}

public func stopTimer() {
mainStopwatch.stop()
}

public func resetTimer() {
mainStopwatch.reset()
}

public func recordTime() {
(mainStopwatch as? Stopwatch)?.record()
}
}
Loading

0 comments on commit 73e7813

Please sign in to comment.