通往三方庫的最佳實踐: MBProgress 用類方法,處理gif 動畫,以及普通效果; JSONModel,的常用配置

JSONModel

// 全部設(shè)置為 可選

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

// 自定義匹配

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

// 獲取model ,立刻 處理掉,否則,每次要用到,都要轉(zhuǎn)換。

#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, 然后有 先退出控制器,再出現(xiàn)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 ];
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容