ICRTaskListViewController.m 9.16 KB
Newer Older
mei's avatar
mei 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 26 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 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
//
//  ICRTaskListViewController.m
//  XFFruit
//
//  Created by Xummer on 4/9/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "ICRTaskListViewController.h"
#import "ICRTaskDetailViewController.h"
#import "ICRTaskResultViewController.h"

#import "ICRTaskItemView.h"
#import "ICRTask.h"

typedef NS_ENUM(NSUInteger, TaskSegmentsType) {
    kTaskSegUnHandled = 0,
    kTaskSegHandled,
    kTaskSegMine,
};

static NSString *TaskCellIdentifier = @"TaskCell";

@interface ICRTaskListViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    IBTScrollViewRefreshDelegate
>

@property (strong, nonatomic) IBTRefreshTableView *m_tableView;
@property (strong, nonatomic) IBTSegmentContainer *m_segContainer;
@property (weak,   nonatomic) IBTSegmentedControl *m_segmentControl;

@property (strong, nonatomic) NSArray *m_arrAllData;
@property (strong, nonatomic) NSArray *m_arrData;

@end

@implementation ICRTaskListViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    self.title = [IBTCommon localizableString:@"Task List"];
    
    [self initSegmentConatinerFromOriginY:0];
    [self initTableView];
    
    [self fetchTaskList];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self fetchDataMore:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

#pragma mark - Setter
- (void)setM_arrAllData:(NSArray *)arrAllData {
    if ([_m_arrAllData isEqualToArray:arrAllData]) {
        return;
    }
    
    _m_arrAllData = arrAllData;
    
    [self updateDisplayDataWithType:_m_segmentControl.selectedSegmentIndex];
}

#pragma mark - Private Method
- (void)initTableView {
    CGFloat dy = CGRectGetMaxY(_m_segContainer.frame);
    CGRect frame = self.view.bounds;
    frame.origin.y = dy;
    frame.size.height -= CGRectGetMinY(frame);
    
    self.m_tableView =
    [[IBTRefreshTableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    [_m_tableView registerClass:[IBTTableViewCell class]
       forCellReuseIdentifier:TaskCellIdentifier];
    [_m_tableView autoresizingWithStrechFullSize];
    _m_tableView.rowHeight = ICR_TASK_ITEM_HEIGTH;
    _m_tableView.dataSource = self;
    _m_tableView.delegate = self;
    _m_tableView.refreshDelegate = self;
    [_m_tableView addRefreshControlWithText:[IBTCommon localizableString:@""]];
    [_m_tableView addLoadMoreFootWithText:[IBTCommon localizableString:@"Load more"]];
    [self.view addSubview:_m_tableView];
}

- (void)initSegmentConatinerFromOriginY:(CGFloat)y {
    
    if (_m_segContainer) {
        return;
    }
    
    NSArray *arrSegments =
    @[ [IBTCommon localizableString:@"Pending"],
       [IBTCommon localizableString:@"Handled"],
       [IBTCommon localizableString:@"Post by Me"], ];
    self.m_segContainer = [[IBTSegmentContainer alloc] initWithItems:arrSegments];
    self.m_segmentControl = _m_segContainer.segmentControl;
    _m_segmentControl.selectedSegmentIndex = 0;
    _m_segContainer.frame = (CGRect){
        .origin.x = 0,
        .origin.y = y,
        .size.width = CGRectGetWidth(self.view.bounds),
        .size.height = IBT_SEGMENT_CONTAINER_DEFUALT_HEIGHT
    };
    [self.view addSubview:_m_segContainer];
    
    [_m_segmentControl addTarget:self action:@selector(onSegmentsTapped:)
                forControlEvents:UIControlEventValueChanged];
}

#pragma mark - Action
- (void)onSegmentsTapped:(id)sender {
    UISegmentedControl *segmentedCtrl = sender;
    [self updateDisplayDataWithType:segmentedCtrl.selectedSegmentIndex];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [_m_arrData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:TaskCellIdentifier forIndexPath:indexPath];
    
    [self configureCell:cell forRowAtIndexPath:indexPath];
    
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    id data = _m_arrData[ indexPath.row ];
    
    UIView *contentView = cell.contentView;
    
    ICRTaskItemView *itemView = [contentView viewWithClass:[ICRTaskItemView class]];
    if (!itemView) {
        itemView = [[ICRTaskItemView alloc] initWithFrame:contentView.bounds];
        [itemView autoresizingWithStrechFullSize];
        [contentView addSubview:itemView];
    }
    [itemView updateWithTask:data];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ICRTask *data = _m_arrData[ indexPath.row ];
   
    if (data.processResult) {
        [self pushToTaskReault:data];
    }
    else if ([data.state isEqualToString:TaskState[ kICRTaskLocalCreated ]])
    {
        [self pushToTaskDetail:data];
    }
    else if ([data.state isEqualToString:TaskState[ kICRTaskLocalFinished ]])
    {
        [self pushToTaskReault:data];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _m_tableView) {
        [_m_tableView tableViewDidScroll:scrollView];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (scrollView == _m_tableView) {
        [_m_tableView tableviewDidEndDragging:scrollView];
    }
}

#pragma mark - IBTScrollViewRefreshDelegate

- (void)startRefreshData:(UIScrollView *)scrollView {
    [self fetchDataMore:NO];
}

- (void)endRefreshData:(UIScrollView *)scrollView {
    
}

- (void)startLoadMoreData:(UIScrollView *)scrollView {
    [self fetchDataMore:YES];
}

- (void)endLoadMoreData:(UIScrollView *)scrollView {
    
}

#pragma mark - Action
- (void)pushToTaskDetail:(ICRTask *)task {
    ICRTaskDetailViewController *dVC =
    [[ICRTaskDetailViewController alloc] initWithTaskData:task];
    [self PushViewController:dVC animated:YES];
}

- (void)pushToTaskReault:(ICRTask *)task {
    ICRTaskResultViewController *rVC =
    [[ICRTaskResultViewController alloc] initWithTaskData:task];
    [self PushViewController:rVC animated:YES];
}


#pragma mark - Data Update

- (void)fetchTaskList {
    ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
        NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ ORDER BY %@ DESC", [ICRTask TableName], @"requireDate"];
        return [db executeQuery:sql];
    };
    
    __weak typeof(self)weakSelf = self;
    ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.m_arrAllData = fetchedObjects;
    };
    
    ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
    [dbCtrl runFetchForClass:[ICRTask class]
                  fetchBlock:fetchBlk
           fetchResultsBlock:fetchResultsBlk];
}

- (void)updateDisplayDataWithType:(TaskSegmentsType)eType {
    
    NSString *filter = nil;
    NSArray *args = nil;
    
    switch (eType) {
        case kTaskSegUnHandled:
        {
            filter = @"(%K = %@)";
            args = @[ @"state", TaskState[kICRTaskLocalCreated]];
        }
            break;
        case kTaskSegHandled:
        {
            filter = @"(%K = %@)";
            args = @[ @"state", TaskState[kICRTaskLocalFinished]]
                      ;
        }
            break;
        case kTaskSegMine:
        {
            filter = @"(%K == %@)";
            args =@[ @"state", TaskState[kICRTaskLocalCreated]];//创建人是自己的任务
        }
            break;
            
        default:
            break;
    }
    if (filter) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:filter argumentArray:args];
        
        self.m_arrData = [_m_arrAllData filteredArrayUsingPredicate:predicate];
    }
    else {
        self.m_arrData = nil;
    }
    
    [self.m_tableView reloadData];
}

#pragma mark - Get Data

- (void)fetchDataMore:(BOOL)more {
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf fetchTaskList];
        if (!more) {
            [_m_tableView endRefreshWithState:kRefreshStateFinished];
        }
        else {
            [_m_tableView endLoadMoreWithState:kLoadStateFinished];
        }

    };
    
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView showTips:data];
        if (!more) {
            [_m_tableView endRefreshWithState:kRefreshStateFailed];
        }
        else {
            [_m_tableView endLoadMoreWithState:kLoadStateFailed];
        }

    };
    
    ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
    
    ICRTask *data = [_m_arrData lastObject];
    
    [httpCtrl doGetTaskListFromUpdateTime:more ? data.processDate : nil position:0 size:100
                                  success:succ failure:fail];
    
}

@end