-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrackerManager.swift
92 lines (77 loc) · 2.89 KB
/
TrackerManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// TrackerManager.swift
// visitgo_ios
//
// Created by Roman Ustiantcev on 12/7/18.
// Copyright © 2018 Roman Ustiantcev. All rights reserved.
//
import CoreMotion
public protocol TrackerProtocol {
func checkIfActivityEnabled() -> Bool
func checkIfStepsCountEnabled() -> Bool
func countSteps(completionHandler: @escaping(NSNumber?)-> Void)
func startTrackingActivityType(completionHandler: @escaping(CMMotionActivity?)->Void)
func distance(completionHandler: @escaping(NSNumber?)-> Void)
func currentPace(completionHandler: @escaping(NSNumber?)-> Void)
func ascendingFloors(completionHandler: @escaping(NSNumber?)-> Void)
func descendingFloors(completionHandler: @escaping(NSNumber?)-> Void)
}
final class TrackerManager: NSObject, TrackerProtocol {
public static var core = TrackerManager()
private let activityManager = CMMotionActivityManager()
private let pedometer = CMPedometer()
func checkIfActivityEnabled() -> Bool {
return CMMotionActivityManager.isActivityAvailable()
}
func checkIfStepsCountEnabled() -> Bool {
return CMPedometer.isStepCountingAvailable()
}
func stopPedometr() {
pedometer.stopUpdates()
}
func stopActivityManager() {
activityManager.stopActivityUpdates()
}
func countSteps(completionHandler: @escaping(NSNumber?)-> Void) {
pedometer.startUpdates(from: Date()) { (data, error) in
DispatchQueue.main.async {
completionHandler(data?.numberOfSteps)
}
}
}
func startTrackingActivityType(completionHandler: @escaping(CMMotionActivity?)->Void) {
activityManager.startActivityUpdates(to: OperationQueue.main) { (activity) in
DispatchQueue.main.async {
completionHandler(activity)
}
}
}
func distance(completionHandler: @escaping(NSNumber?)-> Void) {
pedometer.startUpdates(from: Date()) { (data, error) in
DispatchQueue.main.async {
completionHandler(data?.distance)
}
}
}
func currentPace(completionHandler: @escaping(NSNumber?)-> Void) {
pedometer.startUpdates(from: Date()) { (data, error) in
DispatchQueue.main.async {
completionHandler(data?.currentPace)
}
}
}
func ascendingFloors(completionHandler: @escaping(NSNumber?)-> Void) {
pedometer.startUpdates(from: Date()) { (data, error) in
DispatchQueue.main.async {
completionHandler(data?.floorsAscended)
}
}
}
func descendingFloors(completionHandler: @escaping(NSNumber?)-> Void) {
pedometer.startUpdates(from: Date()) { (data, error) in
DispatchQueue.main.async {
completionHandler(data?.floorsDescended)
}
}
}
}