#import "Savings.h"
@implementation Savings
#pragma mark -
#pragma mark View lifecycle
@synthesize tableView, transactions, displayedTransactions, mySearchBar;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Savings";
NSArray *Transactions = [[NSUserDefaults standardUserDefaults] objectForKey:@"Transactions"];
if (Transactions) {
self.transactions = Transactions;
self.displayedTransactions = Transactions;
} else {
self.transactions = [NSArray array];
self.displayedTransactions = [NSArray array];
}
Balance = [[[NSUserDefaults standardUserDefaults] stringForKey:@"SavingsBalance"] intValue];
NSString *BalanceString = [NSString stringWithFormat:@"$%@", [[NSUserDefaults standardUserDefaults] stringForKey:@"SavingsBalance"]];
if ([BalanceString isEqualToString:@"$0"]) {
BalanceString = @"$0.00";
}
TotalBalanceLabel.text = BalanceString;
UIBarButtonItem *AddTransation = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTransation)];
AddTransation.style = UIBarButtonItemStylePlain;
self.navigationItem.rightBarButtonItem = AddTransation;
[AddTransation release];
Searching = NO;
self.mySearchBar.delegate = self;
self.mySearchBar.showsCancelButton = YES;
}
- (void)NewTransactionDidFinish:(NewTransaction *)controllerNT {
[self dismissModalViewControllerAnimated:YES];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"DidCanel"]) {
[self AddData];
}
}
-(void)AddData {
int i;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"SavingsNewTransactionAdd"]) {
i = [[NSUserDefaults standardUserDefaults] integerForKey:@"SavingsNewTransactionAmount"];
} else {
int x = [[NSUserDefaults standardUserDefaults] integerForKey:@"SavingsNewTransactionAmount"] * 2;
i = [[NSUserDefaults standardUserDefaults] integerForKey:@"SavingsNewTransactionAmount"] - x;
}
NSString *TransactionTitle = [NSString stringWithFormat:@"%@-Amount", [[NSUserDefaults standardUserDefaults] stringForKey:@"SavingsNewTransactionTitle"]];
[[NSUserDefaults standardUserDefaults] setInteger:i forKey:TransactionTitle];
NSString *TransactionTime = [NSString stringWithFormat:@"%@-Time", [[NSUserDefaults standardUserDefaults] stringForKey:@"SavingsNewTransactionTitle"]];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:TransactionTime];
NSMutableArray *mutableTransactions = [transactions mutableCopy];
NSString *NewObjectTitle = [[NSUserDefaults standardUserDefaults] stringForKey:@"SavingsNewTransactionTitle"];
[mutableTransactions insertObject:NewObjectTitle atIndex:0];
// Update user defaults.
[[NSUserDefaults standardUserDefaults] setObject:mutableTransactions forKey:@"Transactions"];
// Set self's recent searches to the new recents array, and reload the table view.
self.transactions = mutableTransactions;
self.displayedTransactions = mutableTransactions;
[tableView reloadData];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"SavingsNewTransactionAdd"]) {
Balance -= i;
} else {
Balance += i;
}
NSString *BalanceString = [[NSString alloc] initWithFormat:@"%d", Balance];
[[NSUserDefaults standardUserDefaults] setObject:BalanceString forKey:@"SavingsBalance"];
NSString *BalanceString2 = [NSString stringWithFormat:@"$%@", BalanceString];
TotalBalanceLabel.text = BalanceString2;
}
-(void)addTransation {
NewTransaction *controllerNT = [[NewTransaction alloc] initWithNibName:@"NewTransaction" bundle:nil];
controllerNT.delegate = self;
controllerNT.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controllerNT animated:YES];
[controllerNT release];
}
-(IBAction)Search {
if (Searching) {
Searching = NO;
[UIView beginAnimations:nil context:nil];
mySearchBar.center = CGPointMake(mySearchBar.center.x, mySearchBar.center.y-44);
self.tableView.frame = CGRectMake(0.0, 0.0, 320, 372);
[UIView commitAnimations];
mySearchBar.text = @"";
[self filterResultsUsingString:mySearchBar.text];
} else {
Searching = YES;
[UIView beginAnimations:nil context:nil];
mySearchBar.center = CGPointMake(mySearchBar.center.x, mySearchBar.center.y+44);
self.tableView.frame = CGRectMake(0.0, 44, 320, 328);
[UIView commitAnimations];
}
}
// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.mySearchBar resignFirstResponder];
[self filterResultsUsingString:self.mySearchBar.text];
}
// called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.mySearchBar resignFirstResponder];
Searching = NO;
[UIView beginAnimations:nil context:nil];
mySearchBar.center = CGPointMake(mySearchBar.center.x, mySearchBar.center.y-44);
self.tableView.frame = CGRectMake(0.0, 0.0, 320, 372);
[UIView commitAnimations];
mySearchBar.text = @"";
[self filterResultsUsingString:mySearchBar.text];
}
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSString *BalanceString = [[NSString alloc] initWithFormat:@"%d", Balance];
[[NSUserDefaults standardUserDefaults] setObject:BalanceString forKey:@"SavingsBalance"];
}
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayedTransactions count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *TransactionTime = [NSString stringWithFormat:@"%@-Time", [displayedTransactions objectAtIndex:indexPath.row]];
NSString *Time = [[NSUserDefaults standardUserDefaults] stringForKey:TransactionTime];
cell.textLabel.text = [displayedTransactions objectAtIndex:indexPath.row];
cell.detailTextLabel.text = Time;
NSString *TransactionAmount = [NSString stringWithFormat:@"%@-Amount", [displayedTransactions objectAtIndex:indexPath.row]];
NSString *Detail = [[NSUserDefaults standardUserDefaults] stringForKey:TransactionAmount];
NSString *Amount = [NSString stringWithFormat:@"$%@", Detail];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:Amount forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];
CGRect frame = CGRectMake(0.0, 0.0, 70, 25);
button.frame = frame;
button.enabled = NO;
cell.accessoryView = button;
return cell;
}
- (void)filterResultsUsingString:(NSString *)filterString {
/*
If the search string is zero-length, then restore the recent searches, otherwise create a predicate to filter the recent searches using the search string.
*/
if ([filterString length] == 0) {
self.displayedTransactions = transactions;
} else {
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@", filterString];
NSArray *filteredRecentSearches = [transactions filteredArrayUsingPredicate:filterPredicate];
self.displayedTransactions = filteredRecentSearches;
}
[self.tableView reloadData];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *mutableRecents = [transactions mutableCopy];
NSString *TransactionTitle = [NSString stringWithFormat:@"%@-Amount", [displayedTransactions objectAtIndex:indexPath.row]];
if ([[NSUserDefaults standardUserDefaults] integerForKey:TransactionTitle]>0) {
Balance -= [[NSUserDefaults standardUserDefaults] integerForKey:TransactionTitle];
} else {
}
NSString *BalanceString = [[NSString alloc] initWithFormat:@"%d", Balance];
[[NSUserDefaults standardUserDefaults] setObject:BalanceString forKey:@"SavingsBalance"];
NSString *BalanceString2 = [NSString stringWithFormat:@"$%@", BalanceString];
TotalBalanceLabel.text = BalanceString2;
[mutableRecents removeObject:[displayedTransactions objectAtIndex:indexPath.row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath.row, nil] withRowAnimation:UITableViewRowAnimationRight];
// Update user defaults.
[[NSUserDefaults standardUserDefaults] setObject:mutableRecents forKey:@"Transactions"];
// Set self's recent searches to the new recents array, and reload the table view.
self.transactions = mutableRecents;
self.displayedTransactions = mutableRecents;
[tableView reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
#pragma mark -
#pragma mark Table view delegate
- (void)ViewingTransactionDidFinish:(ViewTransaction *)controllerVT {
[self dismissModalViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[NSUserDefaults standardUserDefaults] setObject:[displayedTransactions objectAtIndex:indexPath.row] forKey:@"Transaction"];
ViewTransaction *controllerVT = [[ViewTransaction alloc] initWithNibName:@"ViewTransaction" bundle:nil];
controllerVT.delegate = self;
controllerVT.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controllerVT animated:YES];
[controllerVT release];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
self.mySearchBar = nil;
self.transactions = nil;
self.displayedTransactions = nil;
}
- (void)dealloc {
[tableView release];
[mySearchBar release];
[super dealloc];
}
@end