HomeViewController.m 9.83 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9
//
//  HomeViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/3/1.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "HomeViewController.h"
10 11
#import "FunctionCollectionViewCell.h"
#import "MenuCollectionViewCell.h"
曹云霄's avatar
曹云霄 committed
12
#import "CustomWKWebViewController.h"
13
#import "ResellerViewController.h"
曹云霄's avatar
曹云霄 committed
14

15
@interface HomeViewController ()<iCarouselDataSource, iCarouselDelegate,UICollectionViewDataSource,UICollectionViewDelegate,StoreCodeDelegate>
曹云霄's avatar
曹云霄 committed
16 17 18 19

@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) NSInteger currentIndex;
@property (nonatomic,strong) BannerResponse *homeResponse;
20 21 22 23 24 25 26 27 28 29 30 31
@property (nonatomic,assign) NSInteger timerNumber;

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

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

曹云霄's avatar
曹云霄 committed
32 33 34 35 36 37 38 39

@end

@implementation HomeViewController

- (void)viewDidAppear:(BOOL)animated
{
    [self startAnimation];
40 41 42
    if (!self.homeResponse) {
        [self getDatasAction];
    }
曹云霄's avatar
曹云霄 committed
43 44 45 46 47 48 49 50 51 52 53 54
}

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

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self setUpiCarouselView];
    [self getDatasAction];
55
    [self setUpCollectionView];
56
    [self perfectDepartInformation];
曹云霄's avatar
曹云霄 committed
57 58
}

59
#pragma mark -获取滚动视图数据
曹云霄's avatar
曹云霄 committed
60 61 62
- (void)getDatasAction
{
    WS(weakSelf);
63 64
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
曹云霄's avatar
曹云霄 committed
65
    [XBLoadingView showHUDViewWithDefault];
66
    [HTTP networkRequestWithURL:SERVERREQUESTURL(HOME_DATA) withRequestType:GET withParameter:nil withReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
67
        
68
        dispatch_group_leave(group);
曹云霄's avatar
曹云霄 committed
69 70
        if (RESULT(returnValue)) {
            weakSelf.homeResponse = [[BannerResponse alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
71
        }else {
72
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
73 74 75
        }
        
    } withFailureBlock:^(NSError *error) {
76
        dispatch_group_leave(group);
77 78 79 80
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
    
    dispatch_group_enter(group);
81
    [HTTP networkRequestWithURL:SERVERREQUESTURL(BANNERS_SPEED) withRequestType:GET withParameter:nil withReturnValueBlock:^(id returnValue) {
82 83 84 85
        
        dispatch_group_leave(group);
        if (RESULT(returnValue)) {
            weakSelf.timerNumber = [returnValue[@"spend"] integerValue];
曹云霄's avatar
曹云霄 committed
86 87 88
        }
        
    } withFailureBlock:^(NSError *error) {
89
        dispatch_group_leave(group);
曹云霄's avatar
曹云霄 committed
90 91
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
92 93 94 95
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [XBLoadingView hideHUDViewWithDefault];
        [weakSelf.customScorllView reloadData];
    });
曹云霄's avatar
曹云霄 committed
96 97 98 99 100
}

#pragma mark -设置setUpiCarouselView
- (void)setUpiCarouselView
{
101
    self.customScorllView.type = iCarouselTypeRotary;
曹云霄's avatar
曹云霄 committed
102 103 104 105
    self.customScorllView.delegate = self;
    self.customScorllView.dataSource = self;
    self.customScorllView.pagingEnabled = YES;
    [self setUpBoxblurImage:self.scrollViewBackView];
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

#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) {
128
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
曹云霄's avatar
曹云霄 committed
129
            [self perfectStoreLocationInformation];
130
        });
131
    }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
}

#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);
曹云霄's avatar
曹云霄 committed
148 149 150 151 152
}

#pragma mark -添加模糊
- (void)setUpBoxblurImage:(UIImageView *)imageView
{
153
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
曹云霄's avatar
曹云霄 committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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)
    {
169
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth*0.7, ScreenHeight*0.35-20)];
曹云霄's avatar
曹云霄 committed
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
        UIImageView *imageView = (UIImageView *)view;
        TOBannerEntity *entity = self.homeResponse.list[index];
        [imageView sd_setImageWithURL:[NSURL URLWithString:entity.attachment] placeholderImage:REPLACEIMAGE];
        view.contentMode = UIViewContentModeScaleToFill;
        [view setMyCorner:5];
    }
    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];
    }
}


198 199
#pragma mark -<UICollectionViewDelegate,UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
曹云霄's avatar
曹云霄 committed
200
{
201 202 203 204
    if ([collectionView isEqual:self.menuCollectionView]) {
        return self.menuImagesArray.count;
    }
    return self.functionArray.count;
曹云霄's avatar
曹云霄 committed
205 206
}

207
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
曹云霄's avatar
曹云霄 committed
208
{
209 210 211 212 213 214 215 216
    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;
曹云霄's avatar
曹云霄 committed
217 218
}

219
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
曹云霄's avatar
曹云霄 committed
220
{
221 222 223 224 225 226 227 228 229 230 231 232 233 234
    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:
            {
235
                if ([[Shoppersmanager manager].shoppers.employee.userKey rangeOfString:@"学习人员"].location == NSNotFound) {
236 237
                    [XBLoadingView showHUDViewWithText:@"没有学习中心权限,无法进入"];return;
                }
238 239 240 241 242 243 244 245 246 247 248 249 250 251
                [SHARED_APPDELEGATE.tabBarController setSelectedIndex:12];
            }
                break;
            case CustomerInformation:
            {
                [SHARED_APPDELEGATE.tabBarController switchSelectedIndex:CUSTOMER];
                [SHARED_APPDELEGATE.tabBarController setSelectedIndex:3];
            }
                break;
                
            default:
                break;
        }
    }
曹云霄's avatar
曹云霄 committed
252 253 254 255 256 257 258 259
}


#pragma mark -启动定时器
- (void)startAnimation
{
    if (!_timer)
    {
260 261
        NSInteger number = self.timerNumber?self.timerNumber:5;
        self.timer = [NSTimer timerWithTimeInterval:number
曹云霄's avatar
曹云霄 committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                                             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;
}

284 285 286 287 288 289 290 291
#pragma mark -lazy
- (NSArray *)menuImagesArray
{
    if (!_menuImagesArray ) {
        _menuImagesArray = @[@"ExperienceCentre",@"ProductLibrary",@"StudyGuides",@"CustomerInformation"];
    }
    return _menuImagesArray;
}
曹云霄's avatar
曹云霄 committed
292

293 294 295 296 297 298 299
- (NSArray *)functionArray
{
    if (!_functionArray) {
        _functionArray  = @[@"欧普O2O销售平台",@"活动信息一手掌握",@"场景体验一站式购买",@"创新的在线学习体验",@"灯光设计定制服务"];
    }
    return _functionArray;
}
曹云霄's avatar
曹云霄 committed
300 301 302 303



@end