HomeViewController.m 9.83 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
//
//  HomeViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/3/1.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "HomeViewController.h"
#import "FunctionCollectionViewCell.h"
#import "MenuCollectionViewCell.h"
#import "CustomWKWebViewController.h"
#import "ResellerViewController.h"

@interface HomeViewController ()<iCarouselDataSource, iCarouselDelegate,UICollectionViewDataSource,UICollectionViewDelegate,StoreCodeDelegate>

@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) NSInteger currentIndex;
@property (nonatomic,strong) BannerResponse *homeResponse;
@property (nonatomic,assign) NSInteger timerNumber;

/**
 菜单
 */
@property (nonatomic,strong) NSArray *menuImagesArray;

/**
 功能
 */
@property (nonatomic,strong) NSArray *functionArray;


@end

@implementation HomeViewController

- (void)viewDidAppear:(BOOL)animated
{
    [self startAnimation];
    if (!self.homeResponse) {
        [self getDatasAction];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self stopAnimation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self setUpiCarouselView];
    [self getDatasAction];
    [self setUpCollectionView];
    [self perfectDepartInformation];
}

#pragma mark -获取滚动视图数据
- (void)getDatasAction
{
    WS(weakSelf);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkRequestWithURL:SERVERREQUESTURL(HOME_DATA) withRequestType:GET withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        dispatch_group_leave(group);
        if (RESULT(returnValue)) {
            weakSelf.homeResponse = [[BannerResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
        }else {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
        
    } withFailureBlock:^(NSError *error) {
        dispatch_group_leave(group);
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
    
    dispatch_group_enter(group);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(BANNERS_SPEED) withRequestType:GET withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        dispatch_group_leave(group);
        if (RESULT(returnValue)) {
            weakSelf.timerNumber = [returnValue[@"spend"] integerValue];
        }
        
    } withFailureBlock:^(NSError *error) {
        dispatch_group_leave(group);
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [XBLoadingView hideHUDViewWithDefault];
        [weakSelf.customScorllView reloadData];
    });
}

#pragma mark -设置setUpiCarouselView
- (void)setUpiCarouselView
{
    self.customScorllView.type = iCarouselTypeRotary;
    self.customScorllView.delegate = self;
    self.customScorllView.dataSource = self;
    self.customScorllView.pagingEnabled = YES;
    [self setUpBoxblurImage:self.scrollViewBackView];
}

#pragma mark -完善经销商信息
- (void)perfectDepartInformation
{
    //完善门店编号信息
    if ([BaseViewController isBlankString:[Shoppersmanager manager].shoppers.storeCode]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self perfectResellerInformation:^(ResellerViewController *resellerVc) {
                resellerVc.delegate = self;
            }];
        });
    }else {
        [self storeCodeBindedSuccess];
    }
}

#pragma mark -StoreCodeDelegate,门店编号绑定成功
- (void)storeCodeBindedSuccess
{
    //完善省市区信息
    if (![Shoppersmanager manager].shoppers.isHasStoreAddress) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self perfectStoreLocationInformation];
        });
    }
}

#pragma mark -设置setUpCollectionView
- (void)setUpCollectionView
{
    self.menuFlowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.menuFlowLayout.minimumInteritemSpacing = 10;
    self.functionFlowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.functionFlowLayout.minimumInteritemSpacing = 10;
}

#pragma mark -布局加载完成
- (void)viewDidLayoutSubviews
{
    self.menuFlowLayout.itemSize = CGSizeMake((ScreenWidth-70)/4.0, self.menuCollectionView.height-40);
    self.functionFlowLayout.itemSize = CGSizeMake((ScreenWidth-60)/5.0, self.functionCollectionView.height);
}

#pragma mark -添加模糊
- (void)setUpBoxblurImage:(UIImageView *)imageView
{
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    effectView.frame = imageView.bounds;
    [imageView addSubview:effectView];
}

#pragma mark -<iCarouselDataSource,iCarouselDelegate>
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.homeResponse.list.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view
{
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth*0.7, ScreenHeight*0.35-20)];
        UIImageView *imageView = (UIImageView *)view;
        TOBannerEntity *entity = self.homeResponse.list[index];
        [imageView sd_setImageWithURL:[NSURL URLWithString:entity.attachment] placeholderImage:REPLACEIMAGE];
        view.contentMode = UIViewContentModeScaleToFill;
174
        [view addCorner:5];
曹云霄's avatar
曹云霄 committed
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 207 208 209 210 211 212 213 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    }
    return view;
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    NSInteger index = carousel.currentItemIndex;
    self.currentIndex = index;
    TOBannerEntity *entity = self.homeResponse.list[carousel.currentItemIndex];
    [self.scrollViewBackView sd_setImageWithURL:[NSURL URLWithString:entity.attachment] placeholderImage:REPLACEIMAGE];
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    TOBannerEntity *entity = self.homeResponse.list[carousel.currentItemIndex];
    if (![[self class] isBlankString:entity.url]) {
        CustomWKWebViewController *webView = [[CustomWKWebViewController alloc] init];
        webView.urlString = entity.url;
        [self presentViewController:webView animated:YES completion:nil];
    }
}


#pragma mark -<UICollectionViewDelegate,UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if ([collectionView isEqual:self.menuCollectionView]) {
        return self.menuImagesArray.count;
    }
    return self.functionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.menuCollectionView]) {
        MenuCollectionViewCell *menuCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCollectionViewCell" forIndexPath:indexPath];
        menuCell.menuImageView.image = TCImage(self.menuImagesArray[indexPath.item]);
        return menuCell;
    }
    FunctionCollectionViewCell *functionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FunctionCollectionViewCell" forIndexPath:indexPath];
    [functionCell.functionButton setTitle:self.functionArray[indexPath.item] forState:UIControlStateNormal];
    return functionCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([collectionView isEqual:self.menuCollectionView]) {
        switch (indexPath.item) {
            case ExperienceCentre:
            {
                [Notification postNotificationName:OPENFOLLOWHEARTVC object:nil];
            }
                break;
            case ProductLibrary:
            {
                [SHARED_APPDELEGATE.tabBarController setSelectedIndex:9];
            }
                break;
            case StudyGuides:
            {
                if ([[Shoppersmanager manager].shoppers.employee.userKey rangeOfString:@"学习人员"].location == NSNotFound) {
                    [XBLoadingView showHUDViewWithText:@"没有学习中心权限,无法进入"];return;
                }
                [SHARED_APPDELEGATE.tabBarController setSelectedIndex:12];
            }
                break;
            case CustomerInformation:
            {
                [SHARED_APPDELEGATE.tabBarController switchSelectedIndex:CUSTOMER];
                [SHARED_APPDELEGATE.tabBarController setSelectedIndex:3];
            }
                break;
                
            default:
                break;
        }
    }
}


#pragma mark -启动定时器
- (void)startAnimation
{
    if (!_timer)
    {
        NSInteger number = self.timerNumber?self.timerNumber:5;
        self.timer = [NSTimer timerWithTimeInterval:number
                                             target:self
                                           selector:@selector(refreshItem)
                                           userInfo:nil
                                            repeats:YES];
        
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

#pragma mark -刷新item
- (void)refreshItem
{
    [self.customScorllView  scrollToItemAtIndex:self.currentIndex+1 animated:YES];
}

#pragma mark -停止定时器
- (void)stopAnimation
{
    [_timer invalidate];
    _timer = nil;
}

#pragma mark -lazy
- (NSArray *)menuImagesArray
{
    if (!_menuImagesArray ) {
        _menuImagesArray = @[@"ExperienceCentre",@"ProductLibrary",@"StudyGuides",@"CustomerInformation"];
    }
    return _menuImagesArray;
}

- (NSArray *)functionArray
{
    if (!_functionArray) {
        _functionArray  = @[@"欧普O2O销售平台",@"活动信息一手掌握",@"场景体验一站式购买",@"创新的在线学习体验",@"灯光设计定制服务"];
    }
    return _functionArray;
}



@end