ICRAppViewControllerManager.m 7.57 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"
陈俊俊's avatar
陈俊俊 committed
16
#import "ReportViewController.h"
mei's avatar
mei committed
17 18
#import "ICRSyncViewController.h"
#import "ICRSystemViewController.h"
19
#import "ReportDetailViewController.h"
mei's avatar
mei committed
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
@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];//业务
陈俊俊's avatar
陈俊俊 committed
130 131 132
    if ([IBTCommon checkIsPermission:Report_ACTION_Check]) {
        [self createReportViewController];
    }
mei's avatar
mei committed
133 134 135
    [self createSyncViewController];
    [self createSystemViewController];
    
136

mei's avatar
mei committed
137 138 139 140
    if (!m_tabbarController) {
        m_tabbarController = [[IBTTabBarController alloc] init];
        m_tabbarController.delegate = self;
    }
mei's avatar
mei committed
141
//    添加  首页  门店  同步   系统
mei's avatar
mei committed
142
    [m_tabbarController setViewControllers:m_arrViewController];
mei's avatar
mei committed
143
//    默认首次进入
mei's avatar
mei committed
144
    [m_tabbarController setSelectedIndex:kCRHome];
mei's avatar
mei committed
145
//    底部bar
mei's avatar
mei committed
146 147 148
    m_window.rootViewController = m_tabbarController;
}

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

mei's avatar
mei committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171
#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
172 173 174 175 176 177 178 179 180 181
-(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
182

mei's avatar
mei committed
183
}
陈俊俊's avatar
陈俊俊 committed
184
- (void)createReportViewController {
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
    //如果登陆的是子节点
    ICRUserUtil *user = [ICRUserUtil sharedInstance];
    if (user.belongOrgIsLeaf) {
        Compass *com = [[Compass alloc]init];
        com.orgCode = user.org_code;
        com.orgName = user.org_name;
        com.orgUuid = user.org_uuid;
        ReportDetailViewController *storeVCtrl = [[ReportDetailViewController alloc] init];
        storeVCtrl.compass = com;
        [m_arrTabBarBaseViewController addObject:storeVCtrl];
        storeVCtrl.title = com.orgName;
        
        NSString *nsTitle = ACETapBarItemNames[ kCRReport ];

        IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:storeVCtrl];
        [m_arrViewController addObject:navCtrl];

        navCtrl.title = nsTitle;
    }else{
        NSString *nsTitle = ACETapBarItemNames[ kCRReport ];
        ReportViewController *storeVCtrl = [[ReportViewController alloc] init];
        [m_arrTabBarBaseViewController addObject:storeVCtrl];
        storeVCtrl.title = [IBTCommon localizableString:nsTitle];
        
        IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:storeVCtrl];
        [m_arrViewController addObject:navCtrl];
        
        navCtrl.title = nsTitle;
    }
mei's avatar
mei committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
}

- (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