-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLAccountViewController.m
199 lines (161 loc) · 5.98 KB
/
PLAccountViewController.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// PLSettingsViewController.m
// Plate
//
// Created by emileleon on 12/5/13.
// Copyright (c) 2013 Plate SF. All rights reserved.
//
#import "PLAccountViewController.h"
#import "PLAccountStore.h"
#import "PLAccount.h"
#import "PLPlateStore.h"
#import "PLOrder.h"
#import "PLPlateStore.h"
@interface PLAccountViewController ()
@end
@implementation PLAccountViewController
- (void)loadOrderHistory
{
[[PLPlateStore sharedStore] getOrderHistory:^(NSMutableArray *orders, NSError *err) {
if (!err) {
NSString *orderHistorySummary = [NSString stringWithFormat:@""];
for (PLOrder *order in orders) {
NSString *orderSummary = [NSString stringWithFormat:@"%@ - $%f\n%@\n\n", @"November 12, 2014", [order cost], [order pickupOption]];
orderHistorySummary = [orderHistorySummary stringByAppendingString:orderSummary];
}
if ([orderHistorySummary length] == 0) {
orderHistorySummary = @"No order history";
}
self.textRecentOrders.text = orderHistorySummary;
} else {
UIAlertView *av =[[UIAlertView alloc]
initWithTitle:@"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[av show];
}
}];
}
- (void)loadPayment
{
[[PLPlateStore sharedStore] getPayment:^(PLPayment *payment, NSError *err) {
if (!err) {
NSString *paymentSummary = [NSString stringWithFormat:@""];
paymentSummary = [NSString stringWithFormat:@"%@ ****%@ %@/%@", [payment cardType], [payment lastFourDigits], [payment expiryMonth], [payment expiryYear]];
if ([paymentSummary length] == 0) {
paymentSummary = @"No payments";
}
self.textPayments.text = paymentSummary;
} else {
UIAlertView *av =[[UIAlertView alloc]
initWithTitle:@"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[av show];
}
}];
}
- (void)setState
{
PLAccount *user = [[PLPlateStore sharedStore] getAccount];
if (user) { // logged in
self.labelWelcome.text = @"Hello ";
self.labelUsername.text = user.login;
self.buttonLogin.hidden = true;
self.buttonLogout.hidden = false;
// display payment info
self.labelSavedPayments.hidden = false;
self.textPayments.hidden = false;
self.buttonAddNewPayment.hidden = false;
[self loadPayment];
// display order history
self.labelRecentOrders.hidden = false;
self.textRecentOrders.hidden = false;
[self loadOrderHistory];
} else { // not logged in
self.labelWelcome.text = @"";
self.labelUsername.text = @"";
self.buttonLogin.hidden = false;
self.buttonLogout.hidden = true;
// hide payment info
self.labelSavedPayments.hidden = true;
self.textPayments.hidden = true;
self.buttonAddNewPayment.hidden = true;
// hide order history
self.labelRecentOrders.hidden = true;
self.textRecentOrders.hidden = true;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)login:(id)sender {
_pllsvc = [[PLLoginSignUpViewController alloc]init];
[[self pllsvc] setDelegate:self];
[self presentViewController:_pllsvc animated:YES completion:nil];
}
- (IBAction)addPayment:(id)sender {
NSLog(@"Displaying payment modal from AcctVC");
_plpvc = [[PLPaymentViewController alloc]init];
_plpvc.displaySaveToAccount = false;
[[self plpvc] setDelegate:self];
[self presentViewController:_plpvc animated:YES completion:nil];
}
- (IBAction)logout:(id)sender {
[[PLPlateStore sharedStore] logout:^(NSError *err) {
if (!err) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"UserLoggedOut" object:self];
[self setState];
} else {
UIAlertView *av =[[UIAlertView alloc]
initWithTitle:@"Error"
message:[err localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[av show];
}
}];
}
- (void)dismissModal
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)userLoggedIn
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)paymentAdded
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"Settings"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setState)
name:@"UserLoggedIn" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setState)
name:@"UserLoggedOut" object:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"Settings";
[self setState];
}
@end