-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChoice.m
178 lines (141 loc) · 4.47 KB
/
Choice.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
177
178
//
// Choice.m
// AvocadoTest1
//
// Created by Jake on 12-01-11.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Choice.h"
#import "Option.h"
#import "Utilities.h"
#import "MenuComponent.h"
NSString* CHOICE_PRICE_CHANGED = @"CroutonLabs/ChoicePriceChanged";
NSString* CHOICE_SELECTED_CHANGED = @"CroutonLabs/ChoiceSelectedChanged";
NSString* CHOICE_FREE_CHANGED = @"CroutonLabs/ChoiceFreeChanged";
NSString* CHOICE_CHANGED = @"CroutonLabs/ChoiceChanged";
@implementation Choice
@synthesize selected;
@synthesize isFree;
-(Choice *)initFromData:(NSDictionary *)inputData option:(Option *)anOption
{
self = [super initFromData:inputData];
NSDecimalNumber* priceCents = [[NSDecimalNumber alloc] initWithInteger:[[inputData objectForKey:@"price_cents"] intValue]];
basePrice = [priceCents decimalNumberByMultiplyingByPowerOf10:-2];
selected = [[inputData objectForKey:@"selected"] intValue];
isFree = NO;
return self;
}
-(void) basePriceRecovery:(NSCoder *)decoder
{
switch (harddiskDataVersion) {
case VERSION_0_0_0:
//fall through to next
case VERSION_1_0_0:
//fall through to next
case VERSION_1_0_1:
basePrice = [decoder decodeObjectForKey:@"price"];
case VERSION_1_1_0:
break;
default:
break;
}
}
- (MenuComponent *)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder:decoder])
{
selected = [[decoder decodeObjectForKey:@"selected"] intValue];
isFree = [[decoder decodeObjectForKey:@"free"] boolValue];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[super encodeWithCoder:encoder];
[encoder encodeObject:[NSString stringWithFormat:@"%i", selected] forKey:@"selected"];
[encoder encodeObject:[NSString stringWithFormat:@"%i", isFree] forKey:@"free"];
}
- (Choice *)copy
{
Choice *aChoice = [[Choice alloc] init];
aChoice->name = name;
aChoice->desc = desc;
aChoice->primaryKey = primaryKey;
aChoice->basePrice = basePrice;
aChoice->selected = selected;
aChoice->isFree = isFree;
return aChoice;
}
- (void)setBasePrice:(NSDecimalNumber *)aPrice
{
basePrice = aPrice;
[[NSNotificationCenter defaultCenter] postNotificationName:CHOICE_PRICE_CHANGED object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:CHOICE_CHANGED object:self];
}
-(void)setSelected:(BOOL)isSelected
{
selected = isSelected;
[[NSNotificationCenter defaultCenter] postNotificationName:CHOICE_SELECTED_CHANGED object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:CHOICE_CHANGED object:self];
}
-(void)setSelectedUnsafe:(BOOL)isSelected
{
selected = isSelected;
}
-(void)toggleSelected
{
if(selected)
[self setSelected:NO];
else
[self setSelected:YES];
}
-(void)setIsFree:(BOOL)free
{
isFree = free;
[[NSNotificationCenter defaultCenter] postNotificationName:CHOICE_FREE_CHANGED object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:CHOICE_CHANGED object:self];
}
-(BOOL)isFree
{
return isFree||[basePrice isEqualToNumber:[NSNumber numberWithInt:0]];
}
-(void)setIsFreeUnsafe:(BOOL)free
{
isFree = free;
}
-(NSDecimalNumber *)price
{
if(isFree)
return [NSDecimalNumber decimalNumberWithString:@"0"];
return basePrice;
}
-(NSComparisonResult)comparePrice:(Choice*)other
{
return [basePrice compare:other->basePrice];
}
-(BOOL)isEffectivelyEqual:(id)aChoice
{
if (![aChoice isKindOfClass:[Choice class]])
return NO;
if(! (primaryKey == [aChoice primaryKey]))
return NO;
if(selected != [aChoice selected])
return NO;
return YES;
}
-(NSDictionary*)orderRepresentation
{
return [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:primaryKey] forKey:@"choice"];
}
- (NSString *) descriptionWithIndent:(NSInteger) indentLevel
{
NSString *padString = [@"" stringByPaddingToLength:(indentLevel*4) withString:@" " startingAtIndex:0];
NSMutableString *output = [[NSMutableString alloc] initWithCapacity:0];
[output appendFormat:@"%@choice:\n",padString];
[output appendString:[super descriptionWithIndent:indentLevel]];
[output appendFormat:@"%@base price:%@ \n",padString,basePrice];
[output appendFormat:@"%@selected:%i \n",padString,selected];
[output appendFormat:@"%@is free:%i \n",padString,isFree];
return output;
}
@end