Skip to content

Instantly share code, notes, and snippets.

@cbedoy
Created April 23, 2015 04:27
Show Gist options
  • Save cbedoy/9c44a587cb3547ef9865 to your computer and use it in GitHub Desktop.
Save cbedoy/9c44a587cb3547ef9865 to your computer and use it in GitHub Desktop.
//
// PaymentOriginViewController.m
// Bills
//
// Created by Carlos Bedoy on 22/06/14.
// Copyright (c) 2014 Pademobile. All rights reserved.
//
#import "PaymentOriginViewController.h"
#import "Memento.h"
#import "Flurry.h"
@interface PaymentOriginViewController ()
@property (nonatomic, retain) id currentPaymentsOrigins;
@property (weak, nonatomic) IBOutlet UITableView *paymentOriginList;
@property (weak, nonatomic) IBOutlet UILabel *paymentOriginTitle;
@property (retain, nonatomic) id paymentOrigins;
@property (nonatomic, weak) IBOutlet UIButton *closeButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *addWalletButton;
@end
@implementation PaymentOriginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.currentPaymentsOrigins = [NSMutableArray new];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.paymentOriginTitle setText:NSLocalizedString(@"PaymentOriginView.PaymentOriginTitleLabel", @"PaymentOriginLabel")];
[self.cancelButton setTitle:NSLocalizedString(@"Cancelar", nil) forState:UIControlStateNormal];
[self.paymentOriginList setDelegate:self];
[self.paymentOriginList setDataSource:self];
[self.paymentOriginList reloadData];
[self.paymentOriginList setBackgroundColor:[UIColor clearColor]];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Salir",nil)
style:UIBarButtonItemStyleBordered
target:self
action:@selector(exit)];
self.navigationItem.leftBarButtonItem = backButton;
[self.addWalletButton setHidden:YES];
#ifdef WITHADDWALLET
[self.addWalletButton setHidden:NO];
#endif
[self.view setAccessibilityLabel:@"payments origin view"];
[self.cancelButton setAccessibilityLabel:@"payments origin cancel button"];
[self.addWalletButton setAccessibilityLabel:@"payments origin add wallet button"];
[self.paymentOriginList setAccessibilityLabel:@"payments origin table"];
[self.paymentOriginList setAccessibilityIdentifier:@"payments origin table"];
[self.serviceName setAccessibilityLabel:@"payments origin service label"];
[self.amountView setAccessibilityLabel:@"payments origin amount label"];
[self.currencyView setAccessibilityLabel:@"payments origin currency label"];
[self.paymentOriginTitle setAccessibilityLabel:@"payments origin wallet title"];
}
-(void)exit
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)closeModal{
[self.viewManager closeView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"PaymentOriginViewCell";
PaymentOriginViewCell *cell = (PaymentOriginViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PaymentOriginViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
id paymentOrigin = [self.currentPaymentsOrigins objectAtIndex:indexPath.row];
[cell.paymentOriginName setText:[paymentOrigin objectForKey:@"nombre"]];
#ifdef WALLETNAME
if([paymentOrigin[@"clase"]isEqualToString:@"origenes_de_fondos.SaldoPademobile"])
[cell.paymentOriginName setText:WALLETNAME];
#endif
if([paymentOrigin objectForKey:@"saldo_fmt"]){
[cell.paymentOriginValue setText:[paymentOrigin objectForKey:@"saldo_fmt"]];
}else{
if([paymentOrigin objectForKey:@"numerotarjeta"])
{
[cell.paymentOriginValue setText:[NSString stringWithFormat:@"**** %@", [paymentOrigin objectForKey:@"numerotarjeta"]]];
}
else
{
[cell.paymentOriginValue setText:@"****"];
}
}
[cell setImageDownloaderService:self.imageDownloaderService];
[cell setOriginIcon:[paymentOrigin objectForKey:@"imagen"]];
if([paymentOrigin[@"pendiente_revision"] boolValue])
{
[cell walletNotCertified];
[cell setUserInteractionEnabled:NO];
}
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithHue:1.0f saturation:0 brightness:1.0f alpha:0.2f];
[cell setSelectedBackgroundView:bgColorView];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int origins = 0;
self.currentPaymentsOrigins = [NSMutableArray new];
for (id paymentOrigin in self.paymentOrigins) {
if([paymentOrigin valueForKey:@"afiliado"]==nil){
[self.currentPaymentsOrigins addObject:paymentOrigin];
origins++;
}
}
return origins;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 88;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[self messageRepresentationHandler] showBaseMessage:K_LOADING];
dispatch_queue_t myBackgroundQ = dispatch_queue_create("savePaymentSelection", NULL);
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC);
dispatch_after(delay, myBackgroundQ, ^(void){
[self saveOriginSelection:[self.currentPaymentsOrigins objectAtIndex:(NSUInteger)[indexPath row]]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
});
}
- (void)setPaymentOriginsAndPresent:(id)paymentOrigins
{
if(paymentOrigins)
{
[self setPaymentOrigins:paymentOrigins];
[[self paymentOriginList] reloadData];
Memento * memento =[self.mementoHandler getTopMemento];
NSMutableDictionary * mementoInfo = [memento mementoData];
#ifdef WITHADDWALLET
if (![mementoInfo[@"addWallet"] boolValue])
{
if([mementoInfo[@"walletAdded"] boolValue] || [self validatePaymentsOrigin:paymentOrigins])
{
[Flurry logEvent:@"el usuario tiene monederos validos para la transaccion"];
[[self viewManager] switchView:PAYMENTORIGIN];
[self.messageRepresentationHandler hideMessage];
[self.mementoHandler setTransactionState:@{@"hasWallets": @true }withHandler:self];
}
else
{
[Flurry logEvent:@"el usuario no tiene monederos validos para la transaccion, se manda mostrar la vista de agregar monederos"];
Memento * memento =[self.mementoHandler getTopMemento];
NSMutableDictionary * mementoInfo = [memento mementoData];
mementoInfo[@"addWallet"]=@false;
[self.mementoHandler setTransactionState:@{@"hasWallets": @false }withHandler:self];
[self addPaymentOrigin];
}
}
else
{
[Flurry logEvent:@"se manda mostrar directamente la vista de agregar monedero"];
Memento * memento =[self.mementoHandler getTopMemento];
NSMutableDictionary * mementoInfo = [memento mementoData];
mementoInfo[@"addWallet"]=@false;
[self.mementoHandler setTransactionState:@{@"hasWallets": @false }withHandler:self];
[self addPaymentOrigin];
}
#else
[[self viewManager] switchView:PAYMENTORIGIN];
[self.messageRepresentationHandler hideMessage];
#endif
}
}
- (void)presentPaymentOrigins{
[[self paymentOriginList] reloadData];
[self.messageRepresentationHandler hideMessage];
}
-(void)saveOriginSelection: (id) originInfo
{
if(![self verifyIfWasCalledFromOtherApplication])
{
[[self checkpointHandler]checkPointPassed:@"El usuario ha seleccionado un monedero"];
NSDictionary *selection = @{@"origenSeleccionado":originInfo};
id memento=[self.mementoHandler getTopMemento];
NSMutableDictionary *mementoInfo = [memento mementoData];
mementoInfo[@"addWallet"]=@false;
[self.mementoHandler setTransactionState:selection withHandler:self];
[self.paymentOriginsTransactionHandler finishedSelectingPaymentOrigins];
}
else
{
[self cmdCancel:nil];
}
}
-(BOOL)verifyIfWasCalledFromOtherApplication
{
return [self.customUrlInformationHandler verifyIfWasCalledFromOtherApplication];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(self.useNavigationController)
{
[[[self navigationController] navigationBar] setHidden:NO];
[self.cancelButton setHidden:YES];
}
else
[self.navigationItem setHidesBackButton:YES];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
- (IBAction)cmdCancel:(id)sender
{
[self.messageRepresentationHandler showBaseMessage:K_LOADING];
[[self checkpointHandler]checkPointPassed:@"El usuario ha presionado el boton de cancelar desde la vista de origenes de fondo"];
id memento=[self.mementoHandler getTopMemento];
NSMutableDictionary *mementoInfo = [memento mementoData];
mementoInfo[@"addWallet"]=@false;
dispatch_queue_t myBackgroundQ = dispatch_queue_create("newCodtran", NULL);
dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC);
dispatch_after(delay, myBackgroundQ, ^(void){
[self.servicesBusinessController getsNewCodtranWithCallBack:^(id codCallBack) {
dispatch_async(dispatch_get_main_queue(), ^
{
[self.messageRepresentationHandler hideMessage];
[self.viewManager closeView];
});
}];
});
}
-(BOOL)validatePaymentsOrigin:(id)paymentsOrigin
{
BOOL hasAnAvailablePayment = false;
for(id payment in paymentsOrigin)
{
if([self validatePaymentOrigin:payment])
{
hasAnAvailablePayment =true;
break;
}
}
return hasAnAvailablePayment;
}
-(BOOL)validatePaymentOrigin:(id)paymentOrigin
{
if([paymentOrigin[@"saldo"] intValue] >0)
return true;
else if (![paymentOrigin[@"clase_base"] isKindOfClass:[NSNull class]])
{
if([paymentOrigin[@"clase_base"]isEqualToString: @"origenes_de_fondos.TarjetaConDatos"])
return true;
return false;
}
else
return false;
}
- (void)addPaymentOrigin
{
[[self creatorInformationDelegate] requestCreators];
}
- (IBAction)addWalletButtonPressed:(id)sender {
[Flurry logEvent:@"se presiono el boton de agregar monedero desde la vista de origenes de fondo"];
Memento * memento =[self.mementoHandler getTopMemento];
NSMutableDictionary * mementoInfo = [memento mementoData];
mementoInfo[@"addWallet"]=@true;
[self addPaymentOrigin];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment