KWMHomeVC.m 5.1 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
//  KWMHomeVC.m
//  iCemarose
//
//  Created by HouWeiBin on 16/8/22.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMHomeVC.h"
#import "KWMShopCartVC.h"
#import "KWMGuideVC.h"
#import "KWMBlogCell.h"
#import "KWMSearchBar.h"
#import "KWMSearchFeedbackVC.h"
#import "KWMShopCartData.h"
#import "KWMBlogDetailVC.h"
#import "KWMUserModel.h"
#import "KWMLoginVC.h"
lee committed
19
#import "KWMShoppingCart.h"
houweibin committed
20 21 22 23 24 25 26 27 28

@interface KWMHomeVC ()
@property (nonatomic)KWMSearchBar *searchBar;
@property (nonatomic)NSArray *dataArr;

@end

@implementation KWMHomeVC

u  
lee committed
29
+ (NSString *)kwmTag{
houweibin committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    return @"KWMHomeVC";
}

- (void)viewDidLoad {
    KWMUserModel *user = [KWMUserModel shareUser];
    if (user.status != 1) {
        //登录
        KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
        [self presentViewController:nav animated:YES completion:nil];
    }
    
    
    [super viewDidLoad];
    _dataArr = [NSArray array];
    [self requestBlogAPI];
u  
lee committed
46
    [self.api loginWebSuccess:nil failure:nil];
houweibin committed
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
}

- (UITableView *)targetTableView{
    return self.tbvBlog;
}
- (void)loadData{
    [self requestBlogAPI];
}

- (void)reLoadData{
    [self requestBlogAPI];
}

//刷新用户
//- (void)refreshCustomer:(BUYCustomer *)customer{
//    [self.client updateCustomer:customer callback:^(BUYCustomer * _Nullable customer, NSError * _Nullable error) {
//         if (customer != nil &&error == nil) {
//             [[NSUserDefaults standardUserDefaults] setObject:customer forKey:@"customer"];
//         }else if (error != nil) {
//             [self showError:error];
//         }
//    }];
//}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [self initHeaderView];
    if (_searchBar) {
lee committed
76 77
        NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
        _searchBar.count = count;
houweibin committed
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 174 175 176 177 178
    }
    [_searchBar resumeView];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [_searchBar removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)awakeFromNib{
    [super awakeFromNib];
    self.title = @"首页";
}

//请求api
- (void)requestBlogAPI{
    __weak KWMHomeVC *weakSelf = self;
    [self.api getAllBlog:nil success:^(NSURLSessionDataTask *task, KWMBlogResult *result) {
        [weakSelf hideLoading];
        if (result != nil) {
            _dataArr =  [[result.blogs reverseObjectEnumerator] allObjects];
            [self.tbvBlog reloadData];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self showError:error];
        NSLog(@"error:%@",error);
    }];
}

#pragma  mark UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat imageHeight = 211.0/375.0 * UI_SCREEN_WIDTH;
    CGFloat defaultHeight = 339;
    
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    if (article.imageScr == nil) {
        imageHeight = 0;
    }
    
    if(indexPath.row == 0) {
        defaultHeight = 354;
    }
    return ( defaultHeight - 221 + imageHeight);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMBlogCell";
    
    KWMBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    
    cell.article = article;
    [cell setData:indexPath.row];
    return cell;
}

//点击了某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    KWMBlogDetailVC *blogDetailVC = (KWMBlogDetailVC *)[KWMBlogDetailVC findControllerBy:@"KWMBlogDetailVC" fromStoryboard:@"Home"];
    KWMArticlesResult *article = [_dataArr objectAtIndex:indexPath.row];
    blogDetailVC.article = article;
    [self.navigationController pushViewController:blogDetailVC animated:YES];
}

- (void)initHeaderView{
    //添加頭部header
    _searchBar = [[KWMSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    _searchBar.delegate = self;
    _searchBar.btnSearch.hidden = YES;
    [self.view addSubview:_searchBar];
}


#pragma mark -- KWMSearchBarDelegate

- (void)kwm_onClickShopCar{
    //购物车
    KWMShopCartVC *contactVC = (KWMShopCartVC *)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:contactVC animated:YES];
}

@end