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
//
// ICRTaskHandleViewController.m
// XFFruit
//
// Created by Xummer on 15/5/20.
// Copyright (c) 2015年 Xummer. All rights reserved.
//
#import "ICRTaskHandleViewController.h"
#import "ICRTaskDetailViewController.h"
#import "ICRTaskResultViewController.h"
#import "ICRTaskItemView.h"
#import "ICRTask.h"
#import "ICRStore.h"
static NSString *TaskCellIdentifier = @"TaskCell";
@interface ICRTaskHandleViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (strong, nonatomic) IBTTableView *m_tableView;
@property (strong, nonatomic) NSArray *m_arrData;
@property (strong, nonatomic) ICRStore *m_store;
@end
@implementation ICRTaskHandleViewController
#pragma mark - Life Cycle
- (instancetype)initWithStore:(id)store {
self = [super init];
if (!self) {
return nil;
}
if ([store isKindOfClass:[ICRStore class]]) {
self.m_store = store;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"门店任务";
[self initTableView];
[self fetchTaskList];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self doHttpGetTaskList];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Private Method
- (void)initTableView {
CGRect frame = self.view.bounds;
self.m_tableView =
[[IBTTableView 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;
[self.view addSubview:_m_tableView];
}
#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 ]] ||
[data.state isEqualToString:TaskState[ kICRTaskLocalAlloted ]])
{
[self pushToTaskDetail:data];
}
else if ([data.state isEqualToString:TaskState[ kICRTaskLocalFinished ]])
{
[self pushToTaskReault:data];
}
}
- (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)doHttpGetTaskList {
__weak typeof(self)weakSelf = self;
void(^succ)(id) = ^(id data) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf fetchTaskList];
};
void(^fail)(id) = ^(id data) {
[IBTLoadingView showTips:data];
};
ICRHTTPController *httpCtrl = [ICRHTTPController sharedController];
[httpCtrl doGetTaskListFromUpdateTime:0 position:0 size:100
success:succ failure:fail];
}
- (void)fetchTaskList {
ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE storeCode = ? ORDER BY %@ DESC", [ICRTask TableName], @"requireDate"];
return [db executeQuery:sql, _m_store.code];
};
__weak typeof(self)weakSelf = self;
ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.m_arrData = fetchedObjects;
[strongSelf.m_tableView reloadData];
};
ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
[dbCtrl runFetchForClass:[ICRTask class]
fetchBlock:fetchBlk
fetchResultsBlock:fetchResultsBlk];
}
@end