Commit 8d4e5642 authored by Wang Yang's avatar Wang Yang

list_update_2

parent cd7b1f3a
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
@import GMBase; @import GMBase;
@interface GMListViewModel : WMFetchDataViewModel @interface GMListViewModel : GMListViewModel
@end @end
// //
// WMListViewModel.h // GMListViewModel.h
// Gengmei // Gengmei
// 带列表的ViewController对于的ViewModel // 带列表的ViewController对于的ViewModel
// Created by Thierry on 3/5/15. // Created by Thierry on 3/5/15.
...@@ -10,53 +10,17 @@ ...@@ -10,53 +10,17 @@
/** /**
* @brief WMTableView协议 加载更多时,使用start_num来作为分页标记,还是使用offset
*/
@protocol WMFetchDataModelProtocol <NSObject>
@optional
/**
* @brief 构造Http请求的params的值,如果不构造,则params为nil
*/
- (void)buildParams;
/**
* @brief 当接口返回的数据类型为字典,将远程字典数据映射到本地模型
*
* @param dic 远程获取的字典
*/
- (void)buildModelWithRemoteDic:(NSDictionary *)dic;
/** - GMListViewPageTypeStartNum: 请求数据时会使用start_num
* @brief 当接口返回的数据类型为数组,将远程数组数据映射到本地模型 - GMListViewPageTypeOffset: 请求数据时会使用offset
*
* @param dic 远程获取的数组
*/ */
- (void)buildModelWithRemoteArray:(NSArray *)array; typedef NS_ENUM(NSUInteger, GMListViewPageType) {
GMListViewPageTypeStartNum,
GMListViewPageTypeOffset,
};
/** @interface GMListViewModel : WMViewModel {
* @author wangyang, 15-11-25 15:11:53
*
* @brief 解析去重数组,并且添加到 self.dataArray 中
* @param array JSON 中的 Object 数组,也可以是转成 Model 的 array
* @param buildBlock 用这个把 dic 转为 Model 再返回。可以为NULL。为NULL时一般用于 array 已经是转成 Model 的 array
* @since 5.6.0
*/
- (void)deduplicationForArray:(NSArray *)array buildModelBlock:(id (^)(NSDictionary *dic))buildBlock;
/**
* @author wangyang, 16-01-14 16:01:53
*
* @brief 将数组去重,然后返回组织好数据的新model数组
* @param array 待去重的数据
* @param buildBlock 解析器
* @return 使用解析器组织好Model的数据
* @since 5.8.0
*/
- (NSArray *)deduplicationArrayFromArray:(NSArray *)array buildModelBlock:(id (^)(NSDictionary *dic))buildBlock;
@end
@interface WMFetchDataViewModel : WMViewModel<WMFetchDataModelProtocol>{
/** @brief 列表数据远程请求Url地址 */ /** @brief 列表数据远程请求Url地址 */
NSString *_remoteUrl; NSString *_remoteUrl;
/** @brief http远程请求的参数 */ /** @brief http远程请求的参数 */
...@@ -84,23 +48,41 @@ ...@@ -84,23 +48,41 @@
@property (nonatomic, copy) NSArray *notDeduplicate; @property (nonatomic, copy) NSArray *notDeduplicate;
// 去重时被去掉的数据总计。在上拉加载更多时,这个数目仍要被用上,否则在某种极端的情况下,startNum会与服务器对不上,导致出现没有数据返回 // 去重时被去掉的数据总计。在上拉加载更多时,这个数目仍要被用上,否则在某种极端的情况下,startNum会与服务器对不上,导致出现没有数据返回
@property (nonatomic,assign) NSInteger deleteCount; @property (nonatomic, assign) NSInteger deleteCount;
/** @brief 记录服务器返回的字段 message 用来显示空白文案*/ /** @brief 记录服务器返回的字段 message 用来显示空白文案*/
@property (nonatomic, strong) NSString *message; @property (nonatomic, strong) NSString *message;
/** @brief 获取数据失败信息*/ /** @brief 获取数据失败信息*/
@property (nonatomic,strong) NSString *fetchDataErrorMsg; @property (nonatomic, strong) NSString *fetchDataErrorMsg;
/** @brief 获取数据为空信息*/ /** @brief 获取数据为空信息*/
@property (nonatomic,strong) NSString *fetchDataNilMsg; @property (nonatomic, strong) NSString *fetchDataNilMsg;
/** @brief 获取http数据分页第一页起始位置,默认为0,下一页为startNum+每页的数据条数(默认为10) */ /** @brief 获取http数据分页第一页起始位置,默认为0,下一页为startNum+每页的数据条数(默认为10) */
@property (nonatomic,assign) NSInteger startNum; @property (nonatomic, assign) NSInteger startNum;
/** @brief 请求返回的数据,用于TableView的DataSource */ /** @brief 请求返回的数据,用于TableView的DataSource */
/** @brief 请求数据是否成功 */@property (nonatomic,strong) NSMutableArray *dataArray; @property (nonatomic, strong) NSMutableArray *dataArray;
/**
加载更多时,使用的分页类型,默认是start_num
*/
@property (nonatomic, assign) GMListViewPageType pageType;
@property (nonatomic,strong) NSNumber *fetchDataSuccess; /**
第一次请求数据时、下拉刷新、重新加载数据,该属性都会为YES。数据请求结束并且页面已经更新,该值为NO
*/
@property (nonatomic, assign, readonly) BOOL isRefreshing;
/**
上拉加载更多数据时为YES
*/
@property (nonatomic, assign, readonly) BOOL isLoadingMore;
/**
第一次请求数据时会为YES
*/
@property (nonatomic, assign, readonly) BOOL isFirstLoad;
/** /**
* @brief 获取远程数据 * @brief 获取远程数据
...@@ -122,6 +104,7 @@ ...@@ -122,6 +104,7 @@
* @brief 在确认需要刷新时如果需要做一些删除数据的操作,在这里做就可以。 * @brief 在确认需要刷新时如果需要做一些删除数据的操作,在这里做就可以。
*/ */
- (void)clearData; - (void)clearData;
/** /**
* @brief 处理下拉刷新 * @brief 处理下拉刷新
*/ */
......
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
// Copyright (c) 2015 Wanmeichuangyi. All rights reserved. // Copyright (c) 2015 Wanmeichuangyi. All rights reserved.
// //
#import "WMFetchDataViewModel.h" #import "GMListViewModel.h"
@import GMNetworking; @import GMNetworking;
@implementation WMFetchDataViewModel @implementation GMListViewModel
{ {
} }
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
self = [super init]; self = [super init];
if (self) { if (self) {
_remoteUrl = @""; _remoteUrl = @"";
_pageType = GMListViewPageTypeStartNum;
_dataArray = [[NSMutableArray alloc] init]; _dataArray = [[NSMutableArray alloc] init];
_startNum = 0; _startNum = 0;
_params = nil; _params = nil;
...@@ -43,48 +43,36 @@ ...@@ -43,48 +43,36 @@
return self.startNum == 0; return self.startNum == 0;
} }
- (void)fetchRemoteData{
/**
TODO:
1. 使用fetchRemoteDataComplete,在ListController中删除KVO
2. 在complete最后改变isRefreshing等标记位
3. 让刷新与加载更多不能同时进行
4. 。。。
*/
- (void)fetchRemoteData {
if (_remoteUrl.length == 0) { if (_remoteUrl.length == 0) {
return; return;
} }
// TODO: response.data 需要考虑
[self buildParams]; [self buildParams];
// [GMNetworking ]; [GMNetworking requestOCWithApi:_remoteUrl method:GMHTTPMethodGet parameters:_params completion:^(GMResponseOC * response) {
// [[WMNetService sharedInstance] sendRequestWithURLString:_remoteUrl parameters:_params method:_httpMethod success:^(id responseObject) {
// if ([self shouldClearDataForResponse:responseObject]) { if ([self shouldClearDataForResponse:response.data]) {
// [self clearData]; [self clearData];
// } }
//
// //某些接口返回的直接是数组 if (response.isSuccess) {
// if ([responseObject isKindOfClass:[NSArray class]]) { [self buildModelWithRemoteDic:response.data];
// } else {
// NSArray *array = (NSArray *)responseObject; self.message = response.message;
// [self buildModelWithRemoteArray:array]; }
// [self setValue:@YES forKey:@"fetchDataSuccess"];
//
// }else if([responseObject isKindOfClass:[NSDictionary class]]){ }];
//
// NSDictionary *dic = (NSDictionary *)responseObject;
// NSInteger code = [[dic objectForKey:@"error"] integerValue];
// self.message = responseObject[@"message"];
// if (code == APIStatusCodeSuccess) {
// [self buildModelWithRemoteDic:dic];
// [self setValue:@YES forKey:@"fetchDataSuccess"];
//
// }else if (code == APIStatusCodeEmpty){
// self.message = responseObject[@"message"];
// [self setValue:@YES forKey:@"fetchDataSuccess"];
//
// }else{
// [self setValue:@NO forKey:@"fetchDataSuccess"];
// }
// }
//
// } failed:^(NSString *errorMsg) {
// [self setValue:@NO forKey:@"fetchDataSuccess"];
// }];
} }
......
...@@ -7,17 +7,17 @@ ...@@ -7,17 +7,17 @@
// //
#import "WMBaseViewController.h" #import "WMBaseViewController.h"
#import "WMFetchDataViewModel.h" #import "GMListViewModel.h"
@interface WMBaseCollectionController : WMBaseViewController <UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>{ @interface WMBaseCollectionController : WMBaseViewController <UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>{
BOOL _needHeaderRefresh; BOOL _needHeaderRefresh;
BOOL _needFooterRefresh; BOOL _needFooterRefresh;
WMFetchDataViewModel *_viewModel; GMListViewModel *_viewModel;
UICollectionView *_collectionView; UICollectionView *_collectionView;
UICollectionViewLayout *_collectionViewLayout; UICollectionViewLayout *_collectionViewLayout;
} }
@property (nonatomic, strong) WMFetchDataViewModel *viewModel; @property (nonatomic, strong) GMListViewModel *viewModel;
/** @brief 控制当前VC的viewmodel是不是立即请求网络 **/ /** @brief 控制当前VC的viewmodel是不是立即请求网络 **/
@property (nonatomic , assign) BOOL immediateLoad; @property (nonatomic , assign) BOOL immediateLoad;
/** /**
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
- (void)initController{ - (void)initController{
[super initController]; [super initController];
self.viewModel = [[WMFetchDataViewModel alloc] init]; self.viewModel = [[GMListViewModel alloc] init];
_needHeaderRefresh = YES; _needHeaderRefresh = YES;
_needFooterRefresh = YES; _needFooterRefresh = YES;
_immediateLoad = YES; _immediateLoad = YES;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#import "WMBaseViewController.h" #import "WMBaseViewController.h"
#import <UITableView_FDTemplateLayoutCell/UITableView+FDTemplateLayoutCell.h> #import <UITableView_FDTemplateLayoutCell/UITableView+FDTemplateLayoutCell.h>
#import "WMFetchDataViewModel.h" #import "GMListViewModel.h"
@interface WMBaseListViewController : WMBaseViewController<UITableViewDelegate,UITableViewDataSource> @interface WMBaseListViewController : WMBaseViewController<UITableViewDelegate,UITableViewDataSource>
{ {
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
} }
/** @brief viewModel*/ /** @brief viewModel*/
@property (nonatomic,strong) WMFetchDataViewModel *viewModel; @property (nonatomic,strong) GMListViewModel *viewModel;
@property (nonatomic, strong) UITableView *table; @property (nonatomic, strong) UITableView *table;
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
[super initController]; [super initController];
_tableStyle = UITableViewStylePlain; _tableStyle = UITableViewStylePlain;
_cellIdentifier = @""; _cellIdentifier = @"";
self.viewModel = [[WMFetchDataViewModel alloc] init]; self.viewModel = [[GMListViewModel alloc] init];
_needHeaderRefresh = YES; _needHeaderRefresh = YES;
_needFooterRefresh = YES; _needFooterRefresh = YES;
_immediateLoad = YES; _immediateLoad = YES;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment