46ea61d2ec37e4ac48ef5726413d59e22ca883f3
[atutor.git] / mods / atsocial_iphone_app / Classes / LauncherViewController.m
1 //
2 //  LauncherViewController.m
3 //  ATutor
4 //
5 //  Created by Quang Anh Do on 25/05/2010.
6 //  Copyright 2010 Quang Anh Do. All rights reserved.
7 //
8
9 #import "LauncherViewController.h"
10 #import "ATutorAppDelegate.h"
11 #import "CommonFunctions.h"
12
13 @interface LauncherViewController (Private)
14
15 - (BOOL)isLoggedIn;
16 - (void)logout;
17
18 @end
19
20 @implementation LauncherViewController
21
22 @synthesize launcherView;
23 @synthesize logoutButton;
24 @synthesize consumer;
25
26 - (id)init {
27         if (self = [super init]) {
28                 self.consumer = [(ATutorAppDelegate *)[[UIApplication sharedApplication] delegate] consumer];
29         }
30         
31         return self;
32 }
33
34 - (void)dealloc {
35         [launcherView release];
36         [logoutButton release];
37         [consumer release];
38         
39     [super dealloc];
40 }
41
42 - (void)viewDidLoad {
43         [super viewDidLoad];
44         
45         logoutButton = [[UIBarButtonItem alloc] initWithTitle:TTLocalizedString(@"Logout", @"") 
46                                                                                                         style:UIBarButtonItemStyleBordered 
47                                                                                                    target:self action:@selector(logout)];
48         
49         self.title = TTLocalizedString(@"ATutor Social", @"");
50 }
51
52 - (void)viewWillAppear:(BOOL)animated {
53         [super viewWillAppear:animated];
54         self.navigationItem.rightBarButtonItem = [self isLoggedIn] ? logoutButton : nil;
55 }
56
57 - (void)didReceiveMemoryWarning {
58     // Releases the view if it doesn't have a superview.
59     [super didReceiveMemoryWarning];
60     
61     // Release any cached data, images, etc that aren't in use.
62 }
63
64 - (void)viewDidUnload {
65     [super viewDidUnload];
66     // Release any retained subviews of the main view.
67     // e.g. self.myOutlet = nil;
68 }
69
70 - (void)loadView {
71         [super loadView];
72         
73         launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
74         
75         launcherView.delegate = self;
76         launcherView.backgroundColor = [UIColor colorWithRed:0.875 green:0.871 blue:0.925 alpha:1.000];
77         launcherView.columnCount = 2;
78         
79         // Attempt to restore data if exists
80         [self restorePages];
81         
82         [self.view addSubview:launcherView];
83 }
84
85 #pragma mark -
86 #pragma mark TTLauncherViewDelegate
87
88 - (void)launcherViewDidBeginEditing:(TTLauncherView*)launcher {
89         [self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
90                                                                                                                                                                                           target:launcherView
91                                                                                                                                                                                           action:@selector(endEditing)] autorelease] 
92                                                                           animated:YES];
93 }
94
95 - (void)launcherViewDidEndEditing:(TTLauncherView*)launcher {
96         [self.navigationItem setRightBarButtonItem:logoutButton animated:YES];
97
98         // Persist data the ugly way
99         NSData *pages = [NSKeyedArchiver archivedDataWithRootObject:launcherView.pages];
100         [[NSUserDefaults standardUserDefaults] setObject:pages forKey:@"launcher.pages"];
101 }
102
103 - (void)launcherView:(TTLauncherView*)launcher didSelectItem:(TTLauncherItem*)item {
104         if ([item.title isEqualToString:TTLocalizedString(@"Activities", @"")]) {
105                 [[TTNavigator navigator] openURLAction:[[TTURLAction actionWithURLPath:@"atutor://activities"] applyAnimated:YES]];
106         } else if ([item.title isEqualToString:TTLocalizedString(@"Contacts", @"")]) {
107                 [[TTNavigator navigator] openURLAction:[[TTURLAction actionWithURLPath:@"atutor://contacts"] applyAnimated:YES]];
108         } else if ([item.title isEqualToString:TTLocalizedString(@"Gadgets", @"")]) {
109                 [[TTNavigator navigator] openURLs:
110                  [NSString stringWithFormat:@"%@/mods/_standard/social/applications.php", kATutorURL], nil];
111         } else if ([item.title isEqualToString:TTLocalizedString(@"Groups", @"")]) {
112                 [[TTNavigator navigator] openURLAction:[[TTURLAction actionWithURLPath:@"atutor://groups"] applyAnimated:YES]];
113         }
114 }
115
116 #pragma mark -
117 #pragma mark QAWebControllerDelegate
118
119 - (void)didFinishAuthorizationInWebViewController:(QAWebController *)webViewController {
120         [consumer finishAuthProcess];
121         
122         [[(ATutorAppDelegate *)[[UIApplication sharedApplication] delegate] helper] fetchContactList];
123 }
124
125 #pragma mark -
126 #pragma mark Misc
127
128 - (void)restorePages {
129         NSData *pages = [[NSUserDefaults standardUserDefaults] objectForKey:@"launcher.pages"];
130         if (pages != nil) {
131                 launcherView.pages = [NSKeyedUnarchiver unarchiveObjectWithData:pages];
132         } else {
133                 for (NSString *module in [NSArray arrayWithObjects:@"Activities", @"Contacts", @"Gadgets", @"Groups", nil]) {
134                         [launcherView addItem:[[[TTLauncherItem alloc] initWithTitle:TTLocalizedString(module, @"") 
135                                                                                                                                    image:[NSString stringWithFormat:@"bundle://%@.png", module]
136                                                                                                                                          URL:[NSString stringWithFormat:@"atutor://modules/%@", module] 
137                                                                                                                            canDelete:NO] autorelease] 
138                                                  animated:NO];
139                 }
140         }
141 }
142
143 - (BOOL)isLoggedIn {
144         return consumer.accessToken != nil;
145 }
146
147 - (void)logout {
148         [consumer clearAuthentication];
149         
150         [self.navigationItem setRightBarButtonItem:nil animated:YES];
151         
152         alertMessage(@"", @"You have been logged out");
153 }
154
155 @end