-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllowanceAppDelegate.m
176 lines (146 loc) · 5.63 KB
/
AllowanceAppDelegate.m
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
166
167
168
169
170
171
172
173
174
175
176
//
// AllowanceAppDelegate.m
// Allowance
//
// Created by Pablo Collins on 6/10/10.
//
#import "AllowanceAppDelegate.h"
@implementation AllowanceAppDelegate
@synthesize window, navController, kid;
static AllowanceAppDelegate *sharedInstance;
static NSNumberFormatter *currencyFormatter;
+ (NSNumberFormatter *)currencyFormatter {
if (currencyFormatter == nil) {
currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
}
return currencyFormatter;
}
+ (NSString *)priceFromDouble:(double)d {
NSString *out = [[self currencyFormatter] stringFromNumber:[NSNumber numberWithDouble:d]];
return out;
}
+ (NSInteger)paymentDay {
return [[NSUserDefaults standardUserDefaults] integerForKey:@"payDay"];
}
+ (NSString *)delimiter {
return [[NSUserDefaults standardUserDefaults] objectForKey:@"delimeter"];
}
+ (NSString *)currency {
return [[NSUserDefaults standardUserDefaults] objectForKey:@"currency"];
}
+ (Kid *)currentKid {
return sharedInstance.kid;
}
+ (void)setCurrentKid:(Kid *)kid {
sharedInstance.kid = kid;
}
+ (Account *)masterAccount {
static Account *masterAcct;
if (masterAcct == nil) {
masterAcct = [Account masterAccount];
}
return masterAcct;
}
+ (UIButton *)newButtonWithTitle:(NSString *)title
target:(id)target
selector:(SEL)selector
frame:(CGRect)frame
image:(UIImage *)image
imagePressed:(UIImage *)imagePressed
darkTextColor:(BOOL)darkTextColor
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
// or you can do this:
// UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal];
if (darkTextColor) {
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = [UIColor clearColor];
return button;
}
static NSNumber *numCentPlaces;
+ (int)numCentPlaces {
if (numCentPlaces == nil) {
NSLocale *l = [NSLocale currentLocale];
NSString *cc = [l objectForKey:NSLocaleCurrencyCode];
int n;
double rounding;
if (cc == nil) { //getting nils here suddenly on simulator
numCentPlaces = [NSNumber numberWithInt:2];
} else {
CFNumberFormatterGetDecimalInfoForCurrencyCode((__bridge CFStringRef)cc, &n, &rounding);
numCentPlaces = [NSNumber numberWithInt:n];
}
}
return [numCentPlaces intValue];
}
static NSNumber *priceDenominator;
+ (int)priceDenominator {
if (priceDenominator == nil) {
priceDenominator = [NSNumber numberWithInt:pow(10, [AllowanceAppDelegate numCentPlaces])];
}
return [priceDenominator intValue];
}
- (id)init {
self = [super init];
sharedInstance = (AllowanceAppDelegate *)self;
return self;
}
- (void)updateAccounts {
NSArray *kids = [[ModelManager sharedInstance] findAll:@"Kid"];
NSMutableArray *a = [[NSMutableArray alloc] initWithCapacity:[kids count]];
for (Kid *k in kids) {
int wks = [k updateAccount];
if (wks) {
[a addObject:[NSNumber numberWithInt:wks]];
}
}
if ([a count]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Allowance Credited"
message:nil
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)setupDefaults {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int payDay = [defaults integerForKey:@"payDay"];
if (payDay == 0) {
//first time log in
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome to Kiddy Bank"
message:@"To start, create an account by touching the '+' button at the top right."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[defaults setInteger:1 forKey:@"payDay"];
}
[Account findOrCreateMasterAccount];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:navController.view];
[window makeKeyAndVisible];
[self setupDefaults];
return YES;
}
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[self updateAccounts];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self updateAccounts];
}
@end