forked from omz/AppSales-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaymentsViewController.m
214 lines (181 loc) · 8.49 KB
/
PaymentsViewController.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// PaymentsViewController.m
// AppSales
//
// Created by Ole Zorn on 31.07.11.
// Copyright 2011 omz:software. All rights reserved.
//
#import "PaymentsViewController.h"
#import "YearView.h"
#import "ASAccount.h"
#import "CurrencyManager.h"
@implementation PaymentsViewController
@synthesize scrollView, pageControl;
- (id)initWithAccount:(ASAccount *)paymentAccount
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
account = [paymentAccount retain];
self.title = NSLocalizedString(@"Payments", nil);
self.tabBarItem.image = [UIImage imageNamed:@"Payments.png"];
self.hidesBottomBarWhenPushed = [[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPad;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deletePayments:)] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[account addObserver:self forKeyPath:@"payments" options:NSKeyValueObservingOptionNew context:nil];
}
}
return self;
}
- (void)loadView
{
[super loadView];
self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.alwaysBounceHorizontal = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
[self.view addSubview:scrollView];
self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 15, self.view.bounds.size.width, 10)] autorelease];
pageControl.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
pageControl.userInteractionEnabled = NO;
[self.view addSubview:pageControl];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self reloadData];
}
- (void)reloadData
{
for (UIView *v in [NSArray arrayWithArray:self.scrollView.subviews]) [v removeFromSuperview];
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
NSMutableDictionary *paymentsByYear = [NSMutableDictionary dictionary];
NSMutableDictionary *sumsByYear = [NSMutableDictionary dictionary];
NSSet *allPayments = account.payments;
for (NSManagedObject *payment in allPayments) {
NSNumber *year = [payment valueForKey:@"year"];
NSMutableDictionary *paymentsForYear = [paymentsByYear objectForKey:year];
if (!paymentsForYear) {
paymentsForYear = [NSMutableDictionary dictionary];
[paymentsByYear setObject:paymentsForYear forKey:year];
}
NSNumber *month = [payment valueForKey:@"month"];
NSMutableArray *paymentsForMonth = [paymentsForYear objectForKey:month];
if (!paymentsForMonth) {
paymentsForMonth = [NSMutableArray array];
[paymentsForYear setObject:paymentsForMonth forKey:month];
}
[paymentsForMonth addObject:payment];
float currentSum = [[sumsByYear objectForKey:year] floatValue];
[sumsByYear setObject:[NSNumber numberWithFloat:currentSum + [[payment valueForKey:@"amount"] floatValue]] forKey:year];
}
NSMutableDictionary *labelsByYear = [NSMutableDictionary dictionary];
for (NSNumber *year in paymentsByYear) {
NSDictionary *paymentsForYear = [paymentsByYear objectForKey:year];
for (NSNumber *month in paymentsForYear) {
NSArray *payments = [paymentsForYear objectForKey:month];
if ([payments count] > 0) {
NSNumber *sum = [payments valueForKeyPath:@"@sum.amount"];
NSString *currency = [[payments objectAtIndex:0] valueForKey:@"currency"];
NSString *label = [NSString stringWithFormat:@"%@%@", [numberFormatter stringFromNumber:sum], [[CurrencyManager sharedManager] currencySymbolForCurrency:currency]];
NSMutableDictionary *labelsForYear = [labelsByYear objectForKey:year];
if (!labelsForYear) {
labelsForYear = [NSMutableDictionary dictionary];
[labelsByYear setObject:labelsForYear forKey:year];
}
[labelsForYear setObject:label forKey:month];
}
}
}
NSArray *sortedYears = [[paymentsByYear allKeys] sortedArrayUsingSelector:@selector(compare:)];
if ([sortedYears count] == 0) {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *currentYearComponents = [calendar components:NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger currentYear = [currentYearComponents year];
sortedYears = [NSArray arrayWithObject:[NSNumber numberWithInteger:currentYear]];
}
CGFloat x = 0.0;
for (NSNumber *year in sortedYears) {
YearView *yearView = [[[YearView alloc] initWithFrame:CGRectMake(x, 0, scrollView.bounds.size.width, scrollView.bounds.size.height - 10)] autorelease];
yearView.year = [year integerValue];
yearView.labelsByMonth = [labelsByYear objectForKey:year];
if ([allPayments count] > 0) {
//We assume that all payments have the same currency:
yearView.footerText = [NSString stringWithFormat:@"\u2211 %@%@",
[[CurrencyManager sharedManager] currencySymbolForCurrency:[[allPayments anyObject] valueForKey:@"currency"]],
[numberFormatter stringFromNumber:[sumsByYear objectForKey:year]]];
}
yearView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[scrollView addSubview:yearView];
x += scrollView.bounds.size.width;
}
scrollView.contentSize = CGSizeMake(scrollView.bounds.size.width * [sortedYears count], 0);
[scrollView setContentOffset:CGPointMake(scrollView.contentSize.width - scrollView.bounds.size.width, 0)];
self.pageControl.numberOfPages = [sortedYears count];
self.pageControl.currentPage = pageControl.numberOfPages - 1;
}
- (void)viewWillAppear:(BOOL)animated
{
[self reloadData];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self reloadData];
CATransition *fadeTransition = [CATransition animation];
fadeTransition.duration = duration;
fadeTransition.type = kCATransitionFade;
fadeTransition.removedOnCompletion = YES;
fadeTransition.fillMode = kCAFillModeForwards;
//for (CALayer *aLayer in self.scrollView.layer.sublayers)
// [aLayer removeAllAnimations];
[self.scrollView.layer addAnimation:fadeTransition forKey:@"transition"];
}
- (void)deletePayments:(id)sender
{
UIActionSheet *deletePaymentsSheet = [[[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Do you want to delete all payments for this account? Payments will be reloaded from iTunes Connect when sales reports are downloaded.", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:NSLocalizedString(@"Delete Payments", nil) otherButtonTitles:nil] autorelease];
[deletePaymentsSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != actionSheet.cancelButtonIndex) {
account.payments = [NSSet set];
[[account managedObjectContext] save:NULL];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self.navigationController popViewControllerAnimated:YES];
}
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
account.paymentsBadge = [NSNumber numberWithInteger:0];
if ([account.managedObjectContext hasChanges]) {
[account.managedObjectContext save:NULL];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{
self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5) / scrollView.bounds.size.width;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[account removeObserver:self forKeyPath:@"payments"];
}
[scrollView release];
[pageControl release];
[account release];
[super dealloc];
}
@end