-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppMonitor.m
More file actions
165 lines (127 loc) · 3.53 KB
/
AppMonitor.m
File metadata and controls
165 lines (127 loc) · 3.53 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// AppMonitor.m
// Meow Soundboard
//
// Created by Randall Salvo on 10/20/2018.
// Copyright © 2018 Candoran LLC. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "AppMonitor.h"
#define APP_RUNCOUNT @"APPMONITOR_APP_RUNCOUNT"
#define APP_RUNTIME @"APPMONITOR_APP_RUNTIME"
#define APP_TAPS @"APPMONITOR_TAPS"
@interface AppMonitor ()
@property (strong, nonatomic) NSDate *startTime;
@end
@implementation AppMonitor
static AppMonitor *sharedAppMonitor = nil;
static condition_block_t _cond = nil;
static action_block_t _acti = nil;
- (void)addAction:(action_block_t)action forCondition:(condition_block_t)condition;
{
NSAssert(nil == _cond && nil == _acti, @"AppMonitor conditional action already set.");
_cond = condition;
_acti = action;
}
- (void)doConditionalActions
{
if ((_cond && _acti) && _cond())
{
_acti();
}
}
// AppMonitor is a singleton. Access it via [AppMonitor sharedInstance] rather than [[AppMonitor alloc] init]
- (id)init
{
NSAssert(nil == sharedAppMonitor,
@"AppMonitor is a singleton. Access it via [AppMontior sharedInstance] rather than [[AppMonitor alloc] init]");
self = [super init];
if (self)
{
_startTime = [NSDate date];
}
return self;
}
- (NSInteger)cumulativeAppRunTime
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults integerForKey:APP_RUNTIME];
}
- (NSInteger)currentInstanceRunTime
{
return (NSInteger)(_startTime.timeIntervalSinceNow * -1);
}
- (void)logTap
{
@synchronized ([AppMonitor class])
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:self.cumulativeAppTaps + 1 forKey:APP_TAPS];
[defaults synchronize];
}
[self doConditionalActions];
}
- (id)objectForKey:(NSString *)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults objectForKey:key];
}
- (void)setObject:(id)value forKey:(NSString *)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
@synchronized ([AppMonitor class])
{
[defaults setObject:value forKey:key];
}
}
- (NSInteger)runCount
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults integerForKey:APP_RUNCOUNT];
}
- (void)start
{
_startTime = [NSDate date];
}
- (void)stop
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
@synchronized ([AppMonitor class])
{
[defaults setInteger:self.runCount + 1 forKey:APP_RUNCOUNT];
[defaults setInteger:self.cumulativeAppRunTime + self.currentInstanceRunTime forKey:APP_RUNTIME];
[defaults synchronize];
}
}
- (NSInteger)cumulativeAppTaps
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults integerForKey:APP_TAPS];
}
// This is a singleton class. Access it through sharedInstance.
+ (AppMonitor*)sharedInstance
{
if (nil == sharedAppMonitor)
{
@synchronized ([AppMonitor class])
{
AppMonitor *monitor = [[super allocWithZone:NULL] init];
if (nil == sharedAppMonitor)
{
sharedAppMonitor = monitor;
}
}
}
return sharedAppMonitor;
}
// Needed to make this a singleton class. Access it through sharedInstance.
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedInstance];
}
// Needed to make this a singleton class. Access it through sharedInstance.
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
@end