KWMProductDetailVC.m 15 KB
Newer Older
houweibin 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
//
//  KWMProductDetailVC.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/1/11.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMProductDetailVC.h"
#import "KWMProductDetailView.h"
#import "KWMBottomView.h"
#import "KWMUserModel.h"
#import "PhotoBroswerVC.h"
#import "KWMStringUtil.h"
#import "KWMShopCartData.h"
#import "KWMRecommendView.h"
#import "KWMInformationVC.h"
#import "KWMBrandCaramelVC.h"
#import "KWMSelectSizeVC.h"
#import "KWMShopCartVC.h"
#import "KWMBeforePayVC.h"
#import "KWMLoginVC.h"
#import "MZFormSheetPresentationViewController.h"
#import "KWMShareVC.h"
#import "KWMImageUtil.h"
lee committed
26
#import "KWMShoppingCart.h"
houweibin committed
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



@interface KWMProductDetailVC ()<KWMBottomViewDelegate,KWMProductDetailViewDelegate,KWMSelectSizeVCDelegate,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *svContent;

@property (nonatomic) KWMProductDetailView *productDetailView;

@property (nonatomic) KWMBottomView *recommendView;

@property (nonatomic) BUYCollection *collectionSameType;

@property (nonatomic) BUYCollection *collectionSameBrand;

@property (weak, nonatomic) IBOutlet UIButton *btnShopCart;

@property (weak, nonatomic) IBOutlet UIButton *btnShare;

- (IBAction)onClickBackBtn:(id)sender;

- (IBAction)onClickShopCartBtn:(id)sender;

- (IBAction)onClickShare:(id)sender;

@end

@implementation KWMProductDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initScrollView];
    [self initRecommendProduct];
lee committed
60
//    [self initShopCartCount];
houweibin committed
61 62 63 64 65 66 67 68 69 70 71 72 73
    //刷新product,如果是缓存的product,因为没有字典,所以会发生错误,需要重新请求一个有字典的product
    if(self.product && self.product.identifier && !self.product.JSONDictionary){
        [self getProductBy:nil product:self.product];
    }
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
lee committed
74
    [self initShopCartCount];
houweibin committed
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
}

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

- (void)initScrollView{
    CGFloat w = UI_SCREEN_WIDTH;
    CGFloat h = UI_SCREEN_HEITHT-60;
    for (int i = 0;i < 2;i++){
        if(i ==  0){
            self.productDetailView = [[KWMProductDetailView alloc] init];
            self.productDetailView.frame = CGRectMake(i*w, 0, w, h);
            self.productDetailView.delegate = self;
            [self.svContent addSubview:self.productDetailView];
            [self.productDetailView setData:self.product];
        }else{
            self.recommendView = [[KWMBottomView alloc] init];
            self.recommendView.frame = CGRectMake(i*w, 0, w, h);
            self.recommendView.delegate = self;
            [self.svContent addSubview:self.recommendView];
        }
    }
    self.svContent.delegate = self;
    self.svContent.contentSize = CGSizeMake(w*2,0);
    self.svContent.pagingEnabled = YES;
    if(self.product && self.product.imagesArray && self.product.imagesArray.count>0){
        NSInteger imageCount = self.product.imagesArray.count;
        [self.recommendView setCount:imageCount];
    }
}

-(void)onClickBackBtn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)onClickShopCartBtn:(id)sender{
    KWMShopCartVC *shopCartVC = (KWMShopCartVC *)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:shopCartVC animated:YES];
}

-(void)onClickShare:(id)sender{
    if(!self.product){
        return;
    }
    KWMShareVC *shareVC = (KWMShareVC *)[KWMBaseVC findControllerBy:[KWMShareVC kwmTag] fromStoryboard:@"New"];
    CGSize size = [UIScreen mainScreen].bounds.size;
    [self showPresentation:shareVC size:size tapOutsideClose:YES style:MZFormSheetPresentationTransitionStyleFade];
    shareVC.shareUrl = [NSString stringWithFormat:@"https://www.cemarose.com/products/%@",self.product.handle];
    shareVC.imageUrl = [KWMImageUtil getProductImageUrl3:self.product.identifier ImageSize:SmallImage Position:1];
    shareVC.productName = self.product.title;
}

- (void)initShopCartCount{
lee committed
130 131
    NSNumber *count = [[KWMShoppingCart sharedInstance] count];
    [self.btnShopCart setTitle:count.stringValue forState:UIControlStateNormal];
houweibin committed
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
}

- (void)setProductId:(NSNumber *)productId{
    _productId = productId;
    [self getProductBy:self.productId product:nil];
}

//KWMProductDetailViewDelegate
-(void)kwm_onClickImage:(NSMutableArray *)imageArr currentIndex:(NSInteger)currentIndex{
    //避免循环引用
    __weak KWMProductDetailVC *weakSelf=self;
    NSArray<NSString *> *imags = imageArr;
    [PhotoBroswerVC show:self type:PhotoBroswerVCTypePush index:currentIndex isShowSaveBtn:YES photoModelBlock:^NSArray *{
        NSArray *localImages = imags;
        NSMutableArray *modelsM = [NSMutableArray arrayWithCapacity:localImages.count];
        for (NSUInteger i = 0; i< localImages.count; i++) {
            PhotoModel *pbModel=[[PhotoModel alloc] init];
            pbModel.mid = i + 1;
            //源frame
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height)];
            pbModel.sourceImageView = imageV;
            pbModel.image_HD_U = [[KWMAPIManager sharedManager] toAbsoluteUrl:localImages[i]];
            [modelsM addObject:pbModel];
        }
        return modelsM;
    }];
}

-(void)kwm_onClickInformation{
    KWMInformationVC *informationVC = (KWMInformationVC *)[KWMBaseVC findControllerBy:[KWMInformationVC kwmTag] fromStoryboard:@"Main"];
    informationVC.inforStr = NSLocalizedString(@"getAndReturnGoods",@"getAndReturnGoods");
    informationVC.inforTitleStr = NSLocalizedString(@"getAndReturnGoodsTitle",@"getAndReturnGoodsTitle");
    informationVC.titleStr = @"发货与退货";
    [self.navigationController pushViewController:informationVC animated:YES];
}

-(void)kwm_onClickSizeBtn{
    KWMSelectSizeVC *editVC = (KWMSelectSizeVC *)[KWMBaseVC findControllerBy:[KWMSelectSizeVC kwmTag] fromStoryboard:@"ShopCart"];
    editVC.delegate = self;
    editVC.product = self.product;
lee committed
172 173
    
//    NSDictionary *shopCartDict = self.product.JSONDictionary;
houweibin committed
174 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
    editVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    editVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    editVC.providesPresentationContextTransitionStyle = YES;
    editVC.definesPresentationContext = YES;
    [self presentViewController:editVC animated:YES completion:nil];
}

-(void)kwm_goToBrandVC{
    if(self.product && self.product.vendor){
        KWMBrandCaramelVC *brandCaramelVC = (KWMBrandCaramelVC *)[KWMBrandCaramelVC findControllerBy:[KWMBrandCaramelVC kwmTag] fromStoryboard:@"Brand"];
        NSString *brand = self.product.vendor;
        brandCaramelVC.brand = brand;
        brandCaramelVC.handle = [KWMStringUtil deleteOtherCharExceptLetterWithLine:brand];
        brandCaramelVC.isSale = NO;
        [self.navigationController pushViewController:brandCaramelVC animated:YES];
    }
}

//KWMBottomViewDelegate
-(void)kwm_loadNewPage:(BOOL)isSameBrand{
    if(isSameBrand && self.collectionSameBrand!=nil){
        [self requestProducts:self.collectionSameBrand.identifier isSameBrand:isSameBrand];
    }else if(self.collectionSameType!=nil){
        [self requestProducts:self.collectionSameType.identifier isSameBrand:isSameBrand];
    }
}

-(void)kwm_clickCollectionCell:(BUYProduct *)product{
    if(product == nil){
        return;
    }
    self.product = product;
    [self.productDetailView setData:product];
    [self.svContent setContentOffset:CGPointMake(0, 0) animated:YES];
    if(self.product && self.product.imagesArray && self.product.imagesArray.count>0){
        NSInteger imageCount = self.product.imagesArray.count;
        [self.recommendView setCount:imageCount];
    }
    [self initRecommendProduct];
}

lee committed
215 216
#pragma mark - KWMSelectSizeVCDelegate

lee committed
217
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
lee committed
218
    __weak KWMProductDetailVC *this = self;
lee committed
219 220
    if (!buyNow) {
        [[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) {
lee committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            if (cart.items.firstObject && !cart.items.firstObject.product_id) {
                KWMShopCartModel *model = cart.items.firstObject;
                model.imageStr = [KWMImageUtil getProductImageUrl:this.product.identifier ImageSize:NormalImage];
                model.name = this.product.title;
                model.size = variant.title;
                model.price = variant.price.floatValue;
            }
           if (!error) {
               [this showToast:@"添加成功"];
               [this.btnShopCart setTitle:cart.item_count.stringValue forState:UIControlStateNormal];
               callback(YES);
           }else{
               [this showError:error];
               callback(NO);
           }
lee committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
            }];
        return;
    }
    callback(YES);
    KWMUserModel *useModel = [KWMUserModel shareUser];
    if (!useModel.status) {
        //登录
        KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    [self showLoading];
    [self.api tmpCartWithVariantId:identifier quantity:quantity success:^(NSURLSessionDataTask *task, NSString *cartCookie) {
        [this hideLoading];
        KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"];
        beforePayVC.cartCookie = cartCookie;
        [this.navigationController pushViewController:beforePayVC animated:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [this showError:error];
lee committed
256
    }];
lee committed
257
    
lee committed
258 259
}

lee committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
////KWMSelectSizeVCDelegate
//-(void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
//    [self initShopCartCount];
//}
//
//-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
//    if(fastBuyCart == nil){
//        return;
//    }
//    KWMUserModel *useModel = [KWMUserModel shareUser];
//    if (useModel.status) {
//        KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"];
//        beforePayVC.fastPayCart = fastBuyCart;
//        [self.navigationController pushViewController:beforePayVC animated:YES];}
//    //用户未登录
//    else{
//        KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
//        [self presentViewController:nav animated:YES completion:nil];
//    }
//}
houweibin committed
281

lee committed
282 283
#pragma mark - KWMSelectSizeVCDelegate End

houweibin committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
-(void)initRecommendProduct{
    if(self.product == nil){
        return;
    }
    [self.recommendView clear];
    [self.recommendView setBrandString:self.product.vendor];
    NSString *handle = [KWMStringUtil deleteOtherCharExceptLetterWithLine:self.product.vendor];
    //该商品的品牌不为nil
    if(self.product.vendor){
        [self requestCollection:handle isSameBrand:YES];
    }
    //该商品的类型不为nil
    if(self.product.productType){
        [self requestCollection:self.product.productType isSameBrand:NO];
    }
}

//获取相同类型商品collection/相同品牌商品collection
-(void)requestCollection:(NSString *)handle isSameBrand:(BOOL)isSameBrand{
    __weak KWMProductDetailVC *weakSelf = self;
    [self.client getCollectionByHandle:handle completion:^(BUYCollection *collection, NSError *error) {
        if(!error && collection!=nil){
            if(isSameBrand){
                [weakSelf hideLoading];
                weakSelf.collectionSameBrand = collection;
            }else{
                weakSelf.collectionSameType = collection;
            }
            [self requestProducts:collection.identifier isSameBrand:isSameBrand];
        }else if(error != nil){
            [self showError:error];
        }
    }];
}

//获取某个商品集合,并将其设置到猜你喜欢view中
-(void)requestProducts:(NSNumber *)collectionId isSameBrand:(BOOL)isSameBrand{
    __weak KWMProductDetailVC *weakSelf = self;
    NSInteger targetPage;
    if(isSameBrand){
        if(self.recommendView.isLoadingSameBrand){
            return;
        }
        targetPage = self.recommendView.pageSameBrand + 1;
        self.recommendView.isLoadingSameBrand = YES;
    }else{
        if(self.recommendView.isLoadingSameType){
            return;
        }
        targetPage = self.recommendView.pageSameType + 1;
        self.recommendView.isLoadingSameType = YES;
    }
    
    [self.client getProductsPage:targetPage inCollection:collectionId withTags:nil sortOrder:BUYCollectionSortCollectionDefault completion:^(NSArray<BUYProduct *> *products, NSUInteger page, BOOL reachedEnd, NSError *error) {
        if(isSameBrand){
            self.recommendView.isLoadingSameBrand = NO;
        }else{
            self.recommendView.isLoadingSameType = NO;
        }
        if(!error && products!=nil){
            if(isSameBrand){
                weakSelf.recommendView.pageSameBrand += 1;
            }else{
                weakSelf.recommendView.pageSameType += 1;
            }
            [weakSelf.recommendView setData:products isSameBrand:isSameBrand];
        }else if(error != nil){
            [self showError:error];
        }
    }];
}

//查找商品API
- (void)getProductBy:(NSNumber *)productId product:(BUYProduct *)product{
    if(productId == nil && product == nil){
        return;
    }
    [self showLoading];
    [self.client getProductById:productId?productId:product.identifier completion:^(BUYProduct * _Nullable product, NSError * _Nullable error) {
        [self hideLoading];
        if (product != nil && error == nil) {
            self.product = product;
            if(productId){
                [self kwm_clickCollectionCell:product];
            }
        }else{
            self.product = nil;
            [self showError:error];
        }
    }];
}

- (void)showPresentation:(UIViewController *)vc size:(CGSize)size tapOutsideClose:(BOOL)tapOutsideClose style:(MZFormSheetPresentationTransitionStyle)style {
    MZFormSheetPresentationViewController *mz = [[MZFormSheetPresentationViewController alloc] initWithContentViewController:vc];
    mz.presentationController.shouldDismissOnBackgroundViewTap = tapOutsideClose;
    mz.presentationController.movementActionWhenKeyboardAppears = MZFormSheetActionWhenKeyboardAppearsCenterVertically;
    mz.contentViewControllerTransitionStyle = style;
    if (size.width > 0 && size.height > 0) {
        mz.presentationController.contentViewSize = size;
    }
    mz.presentationController.shouldCenterVertically = YES;
    
    [self presentViewController:mz
                       animated:YES
                     completion:nil];
}

@end