美文网首页
通往三方库的最佳实践: MBProgress 用类方法,处理gi

通往三方库的最佳实践: MBProgress 用类方法,处理gi

作者: BoxDeng | 来源:发表于2017-09-04 18:58 被阅读0次

    JSONModel

    // 全部设置为 可选

    +(BOOL)propertyIsOptional:(NSString *)propertyName{
        return  YES;
    }
    

    // 自定义匹配

    + (JSONKeyMapper*) keyMapper
    {
        return [[JSONKeyMapper alloc] initWithModelToJSONDictionary: @{@"idOrderDetails": @"id" }];
    }
    

    // 获取model ,立刻 处理掉,否则,每次要用到,都要转换。

    #pragma mark - for Money Pay
    - (void) setPayAmountWithNSNumber: (NSNumber *)argu{
        self.payAmount = @(argu.integerValue/100);
    }
    
    
    #pragma mark - for Geography
    
    - (void)setRecipientProvinceWithNSString: (NSString *) argu{
        NSArray * tempArray = self.allDictrictArray;
        NSArray * filteredArray = [tempArray filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"(areaCode == %@)", argu]];
        NSDictionary * subTempDict = [filteredArray firstObject ];
        self.recipientProvince = [subTempDict valueForKey: @"areaName" ];
    }
    
    

    YTKNetwork

    // Chain Request

    
    - (void)parseMineData{
        __weak typeof(self) weakSelf = self;
        [MBProgressHUD showHUDAddedTo: self.view animated: YES ];
        GetUserInfoAPI * getUserInfoAPI = [[GetUserInfoAPI alloc] init ];
        YTKChainRequest * chainReq = [[YTKChainRequest alloc] init];
        [chainReq addRequest: getUserInfoAPI callback:^(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest) {
            GetUserInfoAPI * resultAPI = (GetUserInfoAPI *)baseRequest;
            NSError * goodsError;
            weakSelf.theUserModel = [[UserModel alloc] initWithDictionary: resultAPI.responseJSONObject error: &goodsError ] ;
            if(weakSelf.theUserModel && resultAPI.responseJSONObject){
               ......
                GetUserCountDateAPI * getUserCountDateAPI = [[GetUserCountDateAPI alloc] init];
                [chainRequest addRequest: getUserCountDateAPI callback:^(YTKChainRequest * _Nonnull chainRequest, YTKBaseRequest * _Nonnull baseRequest) {
                }];
            }
            
        }];
        chainReq.delegate = self;
        [chainReq start];
    }
    
    

    // # pragma mark - YTK Chain Request Delegate

    
    
    - (void)chainRequestFinished:(YTKChainRequest *)chainRequest{
        YTKBaseRequest * finalAPI = [chainRequest.requestArray lastObject ];
        if([finalAPI isKindOfClass: [GetUserCountDateAPI class ] ]){
            NSError * goodsError;
            UserCountDateModel * tempModel = [[UserCountDateModel alloc] initWithData: finalAPI.responseData error: &goodsError ];
    ......
        }
    }
    
    
    - (void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest *)request{
        [MBProgressHUD hideHUDForView: self.view animated: YES ];
        if([request isKindOfClass: [GetUserInfoAPI class]]){
            NSString* errResponse = [[NSString alloc] initWithData:(NSData *)request.error.userInfo[kAFNetworkingOperationFailingURLResponseDataError] encoding: NSUTF8StringEncoding];
            NSLog(@"%@", errResponse);
            NSString * errorStr = request.error.localizedDescription;
            if([errorStr containsString: @"401" ]){
              ......
            }]
    ......
        }else if([request isKindOfClass: [GetUserCountDateAPI class ] ]){
      ......
        }
    }
    
    

    // YTK Delegate

    - (void)requestFinished:(__kindof YTKBaseRequest *)request{
        if([request isKindOfClass: [RefreshTokenAPI class] ]){
    ......
        }else if([request isKindOfClass: [GetUserCountDateAPI class ] ]){
            NSError * goodsError;
    ......
        }
    }
    
    
    - (void)requestFailed:(__kindof YTKBaseRequest *)request{
    ......
    }
    
    
    
    

    MBProgressHUD

    MBProgress 用类方法,处理gif 动画
    // 显示
    + (instancetype)showDengHUDAddedTo:(UIView *)view animated:(BOOL)animated {
        MBProgressHUD *hud = [[self alloc] initWithView: view];
        hud.removeFromSuperViewOnHide = YES;
        hud.mode = MBProgressHUDModeCustomView;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"loading.gif" ofType:@"gif" ];
        NSData *data = [NSData dataWithContentsOfFile:path];
        UIImage *image = [UIImage sd_animatedGIFWithData:data]; // 要配合 SDWebImage
        hud.customView = [[UIImageView alloc] initWithImage: image];
        hud.square = YES;
        [view addSubview:hud];
        [hud showAnimated:animated];
        return hud;
    }
    
    // 隐藏
    + (BOOL)hideDengHUDForView:(UIView *)view animated:(BOOL)animated {
        MBProgressHUD *hud = [self HUDForView:view];
        if (hud != nil) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                hud.removeFromSuperViewOnHide = YES;
                [hud hideAnimated:animated];
            });
            return YES;
        }
        return NO;
    }
    

    // 添加 备用view, [UIApplication sharedApplication ].keyWindow, 然后有 先退出控制器,再出现toast 的 效果。

    + (void)showHubWithStr: (NSString *)str withView: (UIView *) aView
    {
        UIView * tempView;
        if (aView) {
            tempView = aView;
        }else{
            tempView = [UIApplication sharedApplication ].keyWindow;
        }
        MBProgressHUD * logHudThree = [MBProgressHUD showHUDAddedTo: tempView animated: YES ];
        logHudThree.mode = MBProgressHUDModeText;
        logHudThree.label.text = NSLocalizedString(str, @"HUD message title");
        [logHudThree hideAnimated: YES afterDelay: 1.5f ];
        
    }
    
    

    相关文章

      网友评论

          本文标题:通往三方库的最佳实践: MBProgress 用类方法,处理gi

          本文链接:https://www.haomeiwen.com/subject/puoejxtx.html