ICRAppViewControllerManager.m 5.99 KB
Newer Older
mei's avatar
mei committed
1 2
//
//  ICRAppViewControllerManager.m
mei's avatar
mei committed
3
//  XFFruit
mei's avatar
mei committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
//
//  Created by Xummer on 3/23/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "ICRAppViewControllerManager.h"
#import "ICRUIAppearance.h"

#import "IBTUINavigationController.h"
#import "ICRLoginViewController.h"
#import "ICRHomeViewController.h"
#import "ICRStoreViewController.h"
#import "ICRSyncViewController.h"
#import "ICRSystemViewController.h"

@interface ICRAppViewControllerManager ()
<
    UITabBarControllerDelegate
>
@end

@implementation ICRAppViewControllerManager

+ (UINavigationController *)getCurrentNavigationController {
    return nil;
}

+ (IBTTabBarController *)getTabBarController {
    return [[self getAppViewControllerManager] getTabBarController];
}

+ (ICRAppViewControllerManager *)getAppViewControllerManager {
    static ICRAppViewControllerManager *_sharedMgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        _sharedMgr = [[ICRAppViewControllerManager alloc] initWithWindow:keyWindow];
    });
    
    return _sharedMgr;
}

- (id)initWithWindow:(UIWindow *)window {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    [ICRUIAppearance CustomAppearance];
    
    m_window = window;
    m_arrTabBarBaseViewController = [NSMutableArray array];
    m_arrViewController = [NSMutableArray array];
    
    // Automatic login
    ICRUserUtil *userUtil = [ICRUserUtil sharedInstance];
    if ([userUtil isLogin]) {
        [self openMainFrame];
    }
    else {
        [self openFirstView];
    }
    
    return self;
}

- (void)dealloc {
    m_window = nil;
    m_arrViewController = nil;
    m_arrTabBarBaseViewController = nil;
    m_tabbarController = nil;
}

#pragma mark - Public Method
- (CGSize)getRootViewSize {
    return m_window.rootViewController.view.frame.size;
}

- (IBTTabBarController *)getTabBarController {
    return m_tabbarController;
}

- (NSUInteger)getCurTabBarIndex {
    return m_tabbarController.selectedIndex;
}

- (UIViewController *)getTabBarBaseViewController:(CRTapBarItemIndex)index {
    if (index > kCRSystem || index > [m_arrTabBarBaseViewController count]) {
        return nil;
    }
    
    return m_arrTabBarBaseViewController[ index ];
}

- (void)openFirstView {
    if ([m_window.rootViewController isKindOfClass:[IBTUINavigationController class]]) {
        IBTUINavigationController *navCtrl = (IBTUINavigationController *)m_window.rootViewController;
        
        if ([[navCtrl.viewControllers lastObject] isKindOfClass:[ICRLoginViewController class]]) {
            return;
        }
        else if ([[navCtrl.viewControllers firstObject] isKindOfClass:[ICRLoginViewController class]]) {
            [navCtrl popToRootViewControllerAnimated:YES];
            return;
        }
    }
    
    ICRLoginViewController *loginCtrl = [[ICRLoginViewController alloc] init];
    IBTUINavigationController *naviCtrl = [[IBTUINavigationController alloc] initWithRootViewController:loginCtrl];
    naviCtrl.navigationBarHidden = YES;
    
    m_window.rootViewController = naviCtrl;
}

- (void)openMainFrame {
    if (m_window.rootViewController &&
        m_window.rootViewController == m_tabbarController) {
        return;
    }
    
    [m_arrTabBarBaseViewController removeAllObjects];
    [m_arrViewController removeAllObjects];
    
    [self createHomeViewController];
    [self createStoreViewController];
    [self createSyncViewController];
    [self createSystemViewController];
    
    if (!m_tabbarController) {
        m_tabbarController = [[IBTTabBarController alloc] init];
        m_tabbarController.delegate = self;
    }
    
    [m_tabbarController setViewControllers:m_arrViewController];
    [m_tabbarController setSelectedIndex:kCRHome];
    
    m_window.rootViewController = m_tabbarController;
}

mei's avatar
mei committed
143 144 145 146 147 148 149 150 151
#pragma mark - Actions
- (void)doLogout {
    ICRUserUtil *userUtil = [ICRUserUtil sharedInstance];
    [userUtil logout];
    [ICRDataBaseController CleanUpDBPath];
    
    [self openFirstView];
}

mei's avatar
mei committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#pragma mark - Creation
- (void)createHomeViewController {
    
    NSString *nsTitle = ACETapBarItemNames[ kCRHome ];
    
    ICRHomeViewController *homeVCtrl = [[ICRHomeViewController alloc] init];
    [m_arrTabBarBaseViewController addObject:homeVCtrl];
    homeVCtrl.title = [IBTCommon localizableString:nsTitle];
    
    IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:homeVCtrl];
    [m_arrViewController addObject:navCtrl];
    
    navCtrl.title = nsTitle;
}

- (void)createStoreViewController {
    NSString *nsTitle = ACETapBarItemNames[ kCRStore ];
    
    ICRStoreViewController *storeVCtrl = [[ICRStoreViewController alloc] init];
    [m_arrTabBarBaseViewController addObject:storeVCtrl];
    storeVCtrl.title = [IBTCommon localizableString:nsTitle];
    
    IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:storeVCtrl];
    [m_arrViewController addObject:navCtrl];
    
    navCtrl.title = nsTitle;
}

- (void)createSyncViewController {
    NSString *nsTitle = ACETapBarItemNames[ kCRSync ];
    
    ICRSyncViewController *syncVCtrl = [[ICRSyncViewController alloc] init];
    [m_arrTabBarBaseViewController addObject:syncVCtrl];
    syncVCtrl.title = [IBTCommon localizableString:nsTitle];
    
    IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:syncVCtrl];
    [m_arrViewController addObject:navCtrl];
    
    navCtrl.title = nsTitle;
}

- (void)createSystemViewController {
    NSString *nsTitle = ACETapBarItemNames[ kCRSystem ];
    
    ICRSystemViewController *systemVCtrl = [[ICRSystemViewController alloc] init];
    [m_arrTabBarBaseViewController addObject:systemVCtrl];
    systemVCtrl.title = [IBTCommon localizableString:nsTitle];
    
    IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:systemVCtrl];
    [m_arrViewController addObject:navCtrl];
    
    navCtrl.title = nsTitle;
}

@end