-
Notifications
You must be signed in to change notification settings - Fork 0
/
TxHistory.m
84 lines (71 loc) · 1.93 KB
/
TxHistory.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
//
// TxHistory.m
// Allowance
//
// Created by Pablo Collins on 7/24/10.
// Copyright 2010. All rights reserved.
//
#import "TxHistory.h"
@implementation TxHistory
- (void)setup {
int n = [self numberOfWeeks];
buckets = [[NSMutableArray alloc] initWithCapacity:n];
if ([a count] == 0) {
return;
}
for (int i = 0; i < n; i++) {
[buckets addObject:[[NSMutableArray alloc] init]];
}
int lastWk = [self lastWeek];
for (Transaction *t in a) {
NSUInteger weekNum = [Cal weekNumForDate:t.transactionDate];
int bucketNo = lastWk - weekNum;
NSMutableArray *bucket = [buckets objectAtIndex:bucketNo];
[bucket addObject:t];
}
}
- (id)initWithTransactions:(NSArray *)arry {
self = [super init];
a = arry;
[self setup];
return self;
}
- (NSUInteger)transactionCountInBucket:(NSUInteger)n {
return [a count] == 0 ? 0 : [[buckets objectAtIndex:n] count];
}
- (Transaction *)transactionAtIndexPath:(NSIndexPath *)indexPath {
if ([a count] == 0) {
return nil;
}
NSMutableArray *bucket = [buckets objectAtIndex:[indexPath section]];
return [bucket objectAtIndex:[indexPath row]];
}
- (NSUInteger)count {
return [a count];
}
- (Transaction *)earliestTransaction {
return [a lastObject];
}
- (Transaction *)latestTransaction {
return [a objectAtIndex:0];
}
- (NSInteger)firstWeek {
if (firstWeek == 0) {
Transaction *startTx = [self earliestTransaction];
NSDate *startDate = startTx.transactionDate;
firstWeek = [Cal weekNumForDate:startDate];
}
return firstWeek;
}
- (NSInteger)lastWeek {
if (lastWeek == 0) {
Transaction *endTx = [self latestTransaction];
NSDate *endDate = endTx.transactionDate;
lastWeek = [Cal weekNumForDate:endDate];
}
return lastWeek;
}
- (NSInteger)numberOfWeeks {
return [a count] == 0 ? 0 : [self lastWeek] - [self firstWeek] + 1;
}
@end