ICRAppViewControllerManager.m 6.66 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
//
//  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"
mei's avatar
mei committed
15
#import "BusinessViewController.h"
mei's avatar
mei committed
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
#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 ];
}
freecui's avatar
freecui committed
98
  
mei's avatar
mei committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
- (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;
}
mei's avatar
mei committed
118
//   主页面
mei's avatar
mei committed
119 120 121 122 123 124 125 126 127
- (void)openMainFrame {
    if (m_window.rootViewController &&
        m_window.rootViewController == m_tabbarController) {
        return;
    }
    
    [m_arrTabBarBaseViewController removeAllObjects];
    [m_arrViewController removeAllObjects];
    
mei's avatar
mei committed
128 129
    [self createHomeViewController];//首页
    [self  createBusinessViewController];//业务
mei's avatar
mei committed
130 131 132 133
    [self createStoreViewController];
    [self createSyncViewController];
    [self createSystemViewController];
    
mei's avatar
mei committed
134
    
mei's avatar
mei committed
135 136 137 138
    if (!m_tabbarController) {
        m_tabbarController = [[IBTTabBarController alloc] init];
        m_tabbarController.delegate = self;
    }
mei's avatar
mei committed
139
//    添加  首页  门店  同步   系统
mei's avatar
mei committed
140
    [m_tabbarController setViewControllers:m_arrViewController];
mei's avatar
mei committed
141
//    默认首次进入
mei's avatar
mei committed
142
    [m_tabbarController setSelectedIndex:kCRHome];
mei's avatar
mei committed
143
//    底部bar
mei's avatar
mei committed
144 145 146
    m_window.rootViewController = m_tabbarController;
}

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

mei's avatar
mei committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169
#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;
}
mei's avatar
mei committed
170 171 172 173 174 175 176 177 178 179
-(void)createBusinessViewController
{
    NSString *nsTitle = ACETapBarItemNames[ kCRBusiness ];
    BusinessViewController *businessVCtrl=[[BusinessViewController alloc]init];
    [m_arrTabBarBaseViewController addObject:businessVCtrl];
    businessVCtrl.title=[IBTCommon localizableString:nsTitle];
    IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:businessVCtrl];
    [m_arrViewController addObject:navCtrl];
    
    navCtrl.title = nsTitle;
mei's avatar
mei committed
180

mei's avatar
mei committed
181
}
mei's avatar
mei committed
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
- (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