AppDelegate.m 6.09 KB
//
//  AppDelegate.m
//  LifeLog
//
//  Created by Nguyen Van Phong on 7/25/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "HomeViewController.h"
#import "HistoryViewController.h"
#import "RankingViewController.h"
#import "MapViewController.h"
#import "SNSViewController.h"
#import "ServerAPI.h"
#import "Utilities.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

+ (AppDelegate *)sharedAppDelegate {
  return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser];
    User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
    if (user == nil) {
        LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:loginVC];
        self.window.rootViewController = navigation;
        [self.window makeKeyAndVisible];
    }
    else {
        [self gotoMainMenu];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoLogin) name:kNotificationToken object:nil];
    return YES;
}

- (void)gotoLogin {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUser];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kToken];
    [[NSUserDefaults standardUserDefaults] synchronize];
    LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:loginVC];
    self.window.rootViewController = navigation;
    [self.window makeKeyAndVisible];
    [Utilities showErrorMessage:@"Token is invalid" withViewController:self.window.rootViewController];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)gotoMainMenu
{
    UITabBarController *tabBarViewController = [[UITabBarController alloc] init];
  
    HomeViewController *homeVC = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    UINavigationController *naviToday = [[UINavigationController alloc] initWithRootViewController:homeVC];
    naviToday.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"lifelog.tapbar.today", @"") image:[UIImage imageNamed:@"tapbar_today"] tag:1];
    naviToday.navigationBar.hidden = YES;
  
    HistoryViewController *historyVC = [[HistoryViewController alloc] initWithNibName:@"HistoryViewController" bundle:nil];
    UINavigationController *naviHistory = [[UINavigationController alloc] initWithRootViewController:historyVC];
    naviHistory.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"lifelog.tapbar.history", @"") image:[UIImage imageNamed:@"tapbar_history"] tag:2];
    naviHistory.navigationBar.hidden = YES;

    RankingViewController *rankingVC = [[RankingViewController alloc] initWithNibName:@"RankingViewController" bundle:nil];
    UINavigationController *naviRanking = [[UINavigationController alloc] initWithRootViewController:rankingVC];
    naviRanking.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"lifelog.tapbar.ranking", @"") image:[UIImage imageNamed:@"tapbar_ranking"] tag:3];
    naviRanking.navigationBar.hidden = YES;

    MapViewController *mapVC = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
    UINavigationController *naviMap = [[UINavigationController alloc] initWithRootViewController:mapVC];
    naviMap.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"lifelog.tapbar.map", @"") image:[UIImage imageNamed:@"tapbar_map"] tag:4];
  
    SNSViewController *snsVC = [[SNSViewController alloc] initWithNibName:@"SNSViewController" bundle:nil];
    UINavigationController *naviSNS = [[UINavigationController alloc] initWithRootViewController:snsVC];
    naviSNS.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"lifelog.tapbar.sns", @"") image:[UIImage imageNamed:@"tapbar_sns"] tag:5];
    naviSNS.navigationBar.hidden = YES;

    tabBarViewController.viewControllers = [NSArray arrayWithObjects:naviToday, naviHistory, naviRanking, naviMap, naviSNS, nil];
  
    self.window.rootViewController = tabBarViewController;
    [self.window makeKeyAndVisible];
}

@end