2010-09-30 12:27:22 -05:00
//
// MGMVoiceUser . m
// VoiceMob
//
// Created by Mr . Gecko on 9 / 28 / 10.
2013-07-20 06:56:17 -05:00
// Copyright ( c ) 2011 Mr . Gecko ' s Media ( James Coleman ) . http : // mrgeckosmedia . com /
2010-09-30 12:27:22 -05:00
//
# import "MGMVoiceUser.h"
# import "MGMVoicePad.h"
# import "MGMVoiceContacts.h"
2010-09-30 22:52:12 -05:00
# import "MGMVoiceSMS.h"
# import "MGMVoiceInbox.h"
2010-09-30 12:27:22 -05:00
# import "MGMProgressView.h"
# import "MGMAccountController.h"
2013-07-20 06:56:17 -05:00
# import "MGMController.h"
2010-09-30 12:27:22 -05:00
# import "MGMVMAddons.h"
# import < MGMUsers / MGMUsers . h >
2010-10-01 22:03:15 -05:00
# import < VoiceBase / VoiceBase . h >
2010-09-30 12:27:22 -05:00
2013-07-20 06:56:17 -05:00
const int MGMVUKeypadTabIndex = 0 ;
const int MGMVUContactsTabIndex = 1 ;
const int MGMVUSMSTabIndex = 2 ;
const int MGMVUInboxTabIndex = 3 ;
NSString * const MGMVUCurrentTab = @ "MGMVUCurrentTab" ;
NSString * const MGMLastUserPhoneKey = @ "MGMLastUserPhone" ;
2010-10-02 22:00:01 -05:00
2010-09-30 12:27:22 -05:00
@ implementation MGMVoiceUser
+ ( id ) voiceUser : ( MGMUser * ) theUser accountController : ( MGMAccountController * ) theAccountController {
return [ [ [ self alloc ] initWithUser : theUser accountController : theAccountController ] autorelease ] ;
}
- ( id ) initWithUser : ( MGMUser * ) theUser accountController : ( MGMAccountController * ) theAccountController {
2013-07-20 06:56:17 -05:00
if ( ( self = [ super init ] ) ) {
2010-09-30 12:27:22 -05:00
accountController = theAccountController ;
user = [ theUser retain ] ;
2013-07-20 06:56:17 -05:00
[ self registerSettings ] ;
2010-09-30 22:52:12 -05:00
2013-07-20 06:56:17 -05:00
if ( [ user isStarted ] ) {
currentTab = [ [ user settingForKey : MGMVUCurrentTab ] intValue ] ;
tabObjects = [ NSMutableArray new ] ;
[ tabObjects addObject : [ MGMVoicePad tabWithVoiceUser : self ] ] ;
[ tabObjects addObject : [ MGMVoiceContacts tabWithVoiceUser : self ] ] ;
[ tabObjects addObject : [ MGMVoiceSMS tabWithVoiceUser : self ] ] ;
[ tabObjects addObject : [ MGMVoiceInbox tabWithVoiceUser : self ] ] ;
2010-10-02 22:00:01 -05:00
instance = [ [ MGMInstance instanceWithUser : user delegate : self ] retain ] ;
2013-07-20 06:56:17 -05:00
}
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( becameActive ) name : UIApplicationDidBecomeActiveNotification object : nil ] ;
2010-09-30 12:27:22 -05:00
}
return self ;
}
- ( void ) dealloc {
2013-07-20 06:56:17 -05:00
# if releaseDebug
NSLog ( @ "%s Releasing" , __PRETTY _FUNCTION __ ) ;
# endif
[ [ NSNotificationCenter defaultCenter ] removeObserver : self ] ;
2010-09-30 12:27:22 -05:00
[ self releaseView ] ;
2013-07-20 06:56:17 -05:00
[ tabObjects release ] ;
[ callTimer invalidate ] ;
[ callTimer release ] ;
[ callCancelView release ] ;
[ optionsNumber release ] ;
[ instance stop ] ;
[ instance release ] ;
[ user release ] ;
[ currentPhoneNumber release ] ;
[ optionsNumber release ] ;
2010-09-30 12:27:22 -05:00
[ super dealloc ] ;
}
2013-07-20 06:56:17 -05:00
- ( void ) registerSettings {
NSMutableDictionary * settings = [ NSMutableDictionary dictionary ] ;
[ settings setObject : [ NSNumber numberWithInt : 0 ] forKey : MGMLastUserPhoneKey ] ;
[ settings setObject : [ NSNumber numberWithInt : MGMVUKeypadTabIndex ] forKey : MGMVUCurrentTab ] ;
[ user registerSettings : settings ] ;
}
2010-09-30 12:27:22 -05:00
- ( MGMAccountController * ) accountController {
return accountController ;
}
- ( MGMUser * ) user {
return user ;
}
- ( MGMInstance * ) instance {
return instance ;
}
- ( NSString * ) title {
if ( [ instance isLoggedIn ] )
return [ [ instance userNumber ] readableNumber ] ;
return [ user settingForKey : MGMUserName ] ;
}
- ( NSString * ) areaCode {
if ( ! [ instance isLoggedIn ] )
return nil ;
return [ instance userAreaCode ] ;
}
- ( UIView * ) view {
if ( view = = nil ) {
if ( ! [ [ NSBundle mainBundle ] loadNibNamed : [ [ UIDevice currentDevice ] appendDeviceSuffixToString : @ "VoiceUser" ] owner : self options : nil ] ) {
NSLog ( @ "Unable to load Voice User" ) ;
} else {
[ tabView addSubview : [ [ tabObjects objectAtIndex : currentTab ] view ] ] ;
[ tabBar setSelectedItem : [ [ tabBar items ] objectAtIndex : currentTab ] ] ;
2010-10-02 22:00:01 -05:00
if ( ! [ instance isLoggedIn ] ) {
2010-09-30 12:27:22 -05:00
CGSize contentSize = [ view frame ] . size ;
progressView = [ [ MGMProgressView alloc ] initWithFrame : CGRectMake ( 0 , 0 , contentSize . width , contentSize . height ) ] ;
[ progressView setProgressTitle : @ "Logging In" ] ;
[ view addSubview : progressView ] ;
[ progressView startProgess ] ;
[ progressView becomeFirstResponder ] ;
} else {
[ self setInstanceInfo ] ;
2013-07-20 06:56:17 -05:00
if ( unreadCount ! = 0 )
[ [ [ tabBar items ] objectAtIndex : MGMVUInboxTabIndex ] setBadgeValue : [ [ NSNumber numberWithInt : unreadCount ] stringValue ] ] ;
else
[ [ [ tabBar items ] objectAtIndex : MGMVUInboxTabIndex ] setBadgeValue : nil ] ;
2010-10-02 22:00:01 -05:00
}
2010-09-30 12:27:22 -05:00
}
}
return view ;
}
2010-10-02 22:00:01 -05:00
- ( NSArray * ) tabObjects {
return tabObjects ;
}
2010-09-30 22:52:12 -05:00
- ( UIView * ) tabView {
return tabView ;
}
- ( UITabBar * ) tabBar {
return tabBar ;
}
2010-09-30 12:27:22 -05:00
- ( void ) releaseView {
2013-07-20 06:56:17 -05:00
# if releaseDebug
NSLog ( @ "%s Releasing" , __PRETTY _FUNCTION __ ) ;
# endif
[ [ tabObjects objectAtIndex : currentTab ] releaseView ] ;
[ view release ] ;
view = nil ;
[ tabView release ] ;
tabView = nil ;
[ tabBar release ] ;
tabBar = nil ;
[ progressView stopProgess ] ;
[ progressView release ] ;
progressView = nil ;
2010-09-30 12:27:22 -05:00
}
- ( void ) loginError : ( NSError * ) theError {
2013-07-20 06:56:17 -05:00
UIAlertView * alert = [ [ UIAlertView new ] autorelease ] ;
[ alert setTitle : @ "Error logging in" ] ;
[ alert setMessage : [ theError localizedDescription ] ] ;
[ alert addButtonWithTitle : MGMOkButtonTitle ] ;
[ alert show ] ;
2010-09-30 12:27:22 -05:00
2013-07-20 06:56:17 -05:00
[ verificationView release ] ;
verificationView = nil ;
[ verificationField release ] ;
verificationField = nil ;
[ progressView stopProgess ] ;
[ progressView removeFromSuperview ] ;
[ progressView release ] ;
progressView = nil ;
}
- ( void ) loginVerificationRequested {
[ verificationView release ] ;
verificationView = [ UIAlertView new ] ;
[ verificationView setTitle : @ "Account Verification" ] ;
[ verificationView setMessage : @ " " ] ;
[ verificationView addButtonWithTitle : @ "Cancel" ] ;
[ verificationView addButtonWithTitle : @ "Verify" ] ;
[ verificationView setCancelButtonIndex : 1 ] ;
[ verificationView setDelegate : self ] ;
[ verificationField release ] ;
verificationField = [ [ UITextField alloc ] initWithFrame : CGRectMake ( 12.0 , 45.0 , 260.0 , 25.0 ) ] ;
[ verificationField setBorderStyle : UITextBorderStyleLine ] ;
[ verificationField setBackgroundColor : [ UIColor whiteColor ] ] ;
[ verificationField setKeyboardType : UIKeyboardTypeNumbersAndPunctuation ] ;
[ verificationView addSubview : verificationField ] ;
[ verificationView show ] ;
[ verificationField becomeFirstResponder ] ;
2010-09-30 12:27:22 -05:00
}
- ( void ) loginSuccessful {
2013-07-20 06:56:17 -05:00
[ verificationView release ] ;
verificationView = nil ;
[ verificationField release ] ;
verificationField = nil ;
[ progressView stopProgess ] ;
[ progressView setNeedsDisplay ] ;
2010-09-30 12:27:22 -05:00
[ self setInstanceInfo ] ;
if ( [ accountController isCurrent : self ] )
2013-07-20 06:56:17 -05:00
[ accountController setTitle : [ self title ] ] ;
2010-09-30 12:27:22 -05:00
if ( progressView ! = nil ) {
[ UIView beginAnimations : nil context : nil ] ;
[ UIView setAnimationDuration : 1.0 ] ;
[ UIView setAnimationCurve : UIViewAnimationCurveEaseInOut ] ;
[ UIView setAnimationDelegate : self ] ;
[ UIView setAnimationDidStopSelector : @ selector ( progressFadeAnimationDidStop : finished : context : ) ] ;
[ progressView setAlpha : 0.0 ] ;
[ UIView commitAnimations ] ;
}
}
- ( void ) setInstanceInfo {
2013-07-20 06:56:17 -05:00
[ [ tabObjects objectAtIndex : MGMVUKeypadTabIndex ] updateInfo ] ;
2010-09-30 12:27:22 -05:00
}
- ( void ) progressFadeAnimationDidStop : ( NSString * ) animationID finished : ( NSNumber * ) finished context : ( void * ) context {
2013-07-20 06:56:17 -05:00
[ progressView removeFromSuperview ] ;
[ progressView release ] ;
progressView = nil ;
}
- ( void ) becameActive {
[ instance checkPhones ] ;
}
- ( void ) updatedUserPhones {
[ [ tabObjects objectAtIndex : MGMVUKeypadTabIndex ] updateInfo ] ;
2010-09-30 12:27:22 -05:00
}
- ( void ) updatedContacts {
2013-07-20 06:56:17 -05:00
[ [ tabObjects objectAtIndex : MGMVUContactsTabIndex ] updatedContacts ] ;
}
- ( void ) updateUnreadCount : ( int ) theCount {
unreadCount = theCount ;
[ accountController setBadge : unreadCount forInstance : instance ] ;
if ( unreadCount ! = 0 )
[ [ [ tabBar items ] objectAtIndex : MGMVUInboxTabIndex ] setBadgeValue : [ [ NSNumber numberWithInt : unreadCount ] stringValue ] ] ;
else
[ [ [ tabBar items ] objectAtIndex : MGMVUInboxTabIndex ] setBadgeValue : nil ] ;
2010-10-02 22:00:01 -05:00
}
- ( void ) updateSMS {
2013-07-20 06:56:17 -05:00
[ [ tabObjects objectAtIndex : MGMVUSMSTabIndex ] checkSMSMessages ] ;
}
- ( void ) updateVoicemail {
[ [ tabObjects objectAtIndex : MGMVUInboxTabIndex ] checkVoicemail ] ;
}
- ( void ) updateCredit : ( NSString * ) theCredit {
[ [ tabObjects objectAtIndex : MGMVUKeypadTabIndex ] setCredit : theCredit ] ;
2010-09-30 12:27:22 -05:00
}
- ( BOOL ) isPlacingCall {
return ( callTimer ! = nil ) ;
}
2013-07-20 06:56:17 -05:00
- ( void ) donePlacingCall {
[ callTimer fire ] ;
}
- ( NSString * ) currentPhoneNumber {
return currentPhoneNumber ;
}
2010-09-30 12:27:22 -05:00
- ( void ) call : ( NSString * ) theNumber {
2013-07-20 06:56:17 -05:00
if ( [ [ instance userPhoneNumbers ] count ] <= 0 ) {
UIAlertView * alert = [ [ UIAlertView new ] autorelease ] ;
[ alert setTitle : @ "Call Failed" ] ;
[ alert setMessage : @ "You need to have a phone number setup with your Google Voice account. To add one, visit voice.google.com and in the settings add a phone number. Once you got a phone number setup with Google Voice, reopen VoiceMob." ] ;
[ alert addButtonWithTitle : MGMOkButtonTitle ] ;
[ alert show ] ;
return ;
}
[ currentPhoneNumber release ] ;
currentPhoneNumber = [ theNumber copy ] ;
2010-09-30 12:27:22 -05:00
placingCall = YES ;
callTimer = [ [ NSTimer scheduledTimerWithTimeInterval : 20.0 target : self selector : @ selector ( callTimer ) userInfo : nil repeats : NO ] retain ] ;
2013-07-20 06:56:17 -05:00
[ instance placeCall : theNumber usingPhone : [ [ user settingForKey : MGMLastUserPhoneKey ] intValue ] delegate : self ] ;
2010-09-30 12:27:22 -05:00
callCancelView = [ UIAlertView new ] ;
[ callCancelView setTitle : @ "Placing Call" ] ;
[ callCancelView addButtonWithTitle : @ "Cancel Call" ] ;
[ callCancelView setDelegate : self ] ;
[ callCancelView show ] ;
}
2013-07-20 06:56:17 -05:00
- ( void ) alertView : ( UIAlertView * ) theAlertView clickedButtonAtIndex : ( NSInteger ) theIndex {
if ( theAlertView = = callCancelView ) {
[ currentPhoneNumber release ] ;
currentPhoneNumber = nil ;
placingCall = NO ;
[ callTimer invalidate ] ;
[ callTimer release ] ;
callTimer = nil ;
2010-09-30 12:27:22 -05:00
[ callCancelView release ] ;
callCancelView = nil ;
[ instance cancelCallWithDelegate : self ] ;
2013-07-20 06:56:17 -05:00
} else if ( theAlertView = = verificationView ) {
if ( theIndex = = 1 )
[ instance verifyWithCode : [ verificationField text ] ] ;
else
[ instance cancelVerification ] ;
2010-09-30 12:27:22 -05:00
}
}
- ( void ) call : ( NSDictionary * ) theInfo didFailWithError : ( NSError * ) theError {
2013-07-20 06:56:17 -05:00
[ currentPhoneNumber release ] ;
currentPhoneNumber = nil ;
2010-09-30 12:27:22 -05:00
placingCall = NO ;
2013-07-20 06:56:17 -05:00
[ callTimer fire ] ;
UIAlertView * alert = [ [ UIAlertView new ] autorelease ] ;
[ alert setTitle : @ "Call Failed" ] ;
[ alert setMessage : [ theError localizedDescription ] ] ;
[ alert addButtonWithTitle : MGMOkButtonTitle ] ;
[ alert show ] ;
2010-09-30 12:27:22 -05:00
}
- ( void ) callDidFinish : ( NSDictionary * ) theInfo {
2013-07-20 06:56:17 -05:00
[ currentPhoneNumber release ] ;
currentPhoneNumber = nil ;
2010-09-30 12:27:22 -05:00
placingCall = NO ;
NSLog ( @ "YEA! We Made The Call!" ) ;
}
- ( void ) callCancel : ( NSDictionary * ) theInfo didFailWithError : ( NSError * ) theError {
2013-07-20 06:56:17 -05:00
UIAlertView * alert = [ [ UIAlertView new ] autorelease ] ;
[ alert setTitle : @ "Call Cancel Failed" ] ;
[ alert setMessage : [ theError localizedDescription ] ] ;
[ alert addButtonWithTitle : MGMOkButtonTitle ] ;
[ alert show ] ;
2010-09-30 12:27:22 -05:00
}
- ( void ) callTimer {
2013-07-20 06:56:17 -05:00
[ callTimer invalidate ] ;
[ callTimer release ] ;
callTimer = nil ;
2010-09-30 12:27:22 -05:00
[ callCancelView dismissWithClickedButtonIndex : 0 animated : YES ] ;
[ callCancelView release ] ;
callCancelView = nil ;
}
2013-07-20 06:56:17 -05:00
- ( void ) tabBar : ( UITabBar * ) theTabBar didSelectItem : ( UITabBarItem * ) theItem {
int tabIndex = [ [ tabBar items ] indexOfObject : theItem ] ;
2010-09-30 12:27:22 -05:00
if ( tabIndex = = currentTab )
return ;
2013-07-20 06:56:17 -05:00
if ( tabIndex ! = MGMVUSMSTabIndex && tabIndex ! = MGMVUInboxTabIndex ) {
[ accountController setTitle : [ self title ] ] ;
2010-10-02 22:00:01 -05:00
[ accountController setItems : [ accountController accountItems ] animated : YES ] ;
2013-07-20 06:56:17 -05:00
}
2010-09-30 22:52:12 -05:00
2010-09-30 12:27:22 -05:00
id tab = [ tabObjects objectAtIndex : currentTab ] ;
currentTab = tabIndex ;
2013-07-20 06:56:17 -05:00
[ user setSetting : [ NSNumber numberWithInt : currentTab ] forKey : MGMVUCurrentTab ] ;
2010-09-30 12:27:22 -05:00
id newTab = [ tabObjects objectAtIndex : currentTab ] ;
2013-07-20 06:56:17 -05:00
CGRect tabFrame = [ [ newTab view ] frame ] ;
tabFrame . size = [ tabView frame ] . size ;
[ [ newTab view ] setFrame : tabFrame ] ;
2010-09-30 12:27:22 -05:00
[ tabView addSubview : [ newTab view ] ] ;
[ [ tab view ] removeFromSuperview ] ;
[ tab releaseView ] ;
}
2013-07-20 06:56:17 -05:00
- ( void ) showOptionsForNumber : ( NSString * ) theNumber {
optionsNumber = [ theNumber copy ] ;
UIActionSheet * theAction = [ [ UIActionSheet new ] autorelease ] ;
[ theAction addButtonWithTitle : @ "Call" ] ;
[ theAction addButtonWithTitle : @ "SMS" ] ;
[ theAction addButtonWithTitle : @ "Reverse Lookup" ] ;
[ theAction addButtonWithTitle : @ "Cancel" ] ;
[ theAction setCancelButtonIndex : 3 ] ;
[ theAction setDelegate : self ] ;
[ theAction showInView : view ] ;
}
- ( void ) actionSheet : ( UIActionSheet * ) actionSheet clickedButtonAtIndex : ( NSInteger ) buttonIndex {
if ( buttonIndex = = 0 )
[ self call : optionsNumber ] ;
else if ( buttonIndex = = 1 )
[ [ tabObjects objectAtIndex : MGMVUSMSTabIndex ] messageWithNumber : optionsNumber instance : instance ] ;
else if ( buttonIndex = = 2 )
[ [ accountController controller ] showReverseLookupWithNumber : optionsNumber ] ;
[ optionsNumber release ] ;
optionsNumber = nil ;
}
2010-09-30 12:27:22 -05:00
@ end