Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderZsq committed Feb 27, 2019
1 parent 9af3696 commit 6b7abc4
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ - (void)dealloc {
- (void)viewDidLoad {
[super viewDidLoad];
if (self.appeared == NO) {
[self setupData];
if ([self.eventHandler respondsToSelector:@selector(handleViewReady)]) {
[self.eventHandler handleViewReady];
}
[self setupData];
[self setupUI];
self.appeared = YES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @interface SQTrainingCapacityDataManager ()

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) SQTrainingCapacityDataBase * dataBase;
@property (nonatomic, assign) NSInteger action;

@end

Expand Down Expand Up @@ -73,6 +74,7 @@ - (void)fetchDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscl
}
p.model.rows = rows;
p.model.capacity = [d[@"capacity"] integerValue];
p.model.action = [NSString stringWithFormat:@"%ld", ++self.action];
[dataSource addObject:p];
}
self.data = dataSource;
Expand Down Expand Up @@ -108,6 +110,7 @@ - (void)storeDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscl
- (void)addTrainingAction {
SQTrainingCapacityCellPresenter * p = [SQTrainingCapacityCellPresenter new];
p.model = [SQTrainingCapacityModel new];
p.model.action = [NSString stringWithFormat:@"%ld", ++self.action];
[self.data addObject:p];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ - (void)handleViewReady {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[self.interactor loadDataSourceWithTitle:self.view.title type:self.view.type];
[self.view fetchDataSource];
}

- (void)handleViewWillAppear:(BOOL)animated {
Expand Down Expand Up @@ -59,18 +60,20 @@ - (void)handleCommitEditingAtIndexPath:(NSIndexPath *)indexPath {
[self.interactor storeDataSourceWithTitle:self.view.title type:self.view.type dataSource:tempDataSource];
[self.view fetchDataSource];
[self.view.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
self.view.footerView.totalCapacityLabel.text = self.totalCapacity;
}

- (NSString *)totalCapacity {
return self.interactor.totalCapacity;
}

- (NSArray *)fetchDataSourceFromDB {
return [self.interactor fetchDataSource];
return self.interactor.fetchDataSource;
}

- (void)didTouchNavigationBarAddButton {
[self.interactor addTrainingAction];
[self.view fetchDataSource];
[self.view.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.fetchDataSourceFromDB.count - 1 inSection:0]] withRowAnimation:(UITableViewRowAnimationLeft)];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#import "SQTrainingCapacityCell.h"
#import "SQTrainingCapacityHeaderView.h"
#import "SQTrainingCapacityFooterView.h"
#import "SQTrainingCapacityModel.h"
#import "SQTrainingCapacityCellPresenter.h"
#import "SQTrainingCapacityDataSource.h"
#import "SQTrainingCapacityViewEventHandler.h"
Expand Down Expand Up @@ -51,16 +50,13 @@ - (void)setupTableViewContent {
}

- (void)setupTableView {
[self fetchDataSource];
__weak typeof (self) _self = self;
[self setupDataSource:self.dataSource loadCell:^UITableViewCell *(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) {
return [tableView dequeueReusableCellWithIdentifier:@"TrainingCapacity" forIndexPath:indexPath];
} loadCellHeight:^CGFloat(id _Nonnull model) {
return 160;
} bind:^(UITableViewCell * _Nonnull cell, id _Nonnull model) {
SQTrainingCapacityCell * c = (SQTrainingCapacityCell *)cell;
SQTrainingCapacityCellPresenter * p = (SQTrainingCapacityCellPresenter * )model;
p.model.action = [NSString stringWithFormat:@"%ld", [_self.dataSource indexOfObject:model] + 1];
[p bindToCell:c];
}];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (NSArray *)dataSource {
- (void)fetchDataSourceWithType:(SQTrainingCapacityMuscleType)type completion:(void (^)(NSArray * _Nonnull))completion {
NSAssert([NSThread isMainThread], @"main thread only, otherwise use lock to make thread safety");
SQTrainingDateListDataBase * dataBase = [SQSqliteModelTool queryModels:self.dataBase.class columnName:@"type" relation:(ColumnNameToValueRelationTypeEqual) value:@(type) uid:nil].firstObject;
[self.data removeAllObjects];
[self.data addObjectsFromArray:dataBase.dateList];
if (completion) {
completion(self.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ - (void)handleViewReady {
NSAssert([self.interactor conformsToProtocol:@protocol(SQViperInteractor)], @"Interactor should be initlized when view is ready.");
NSLog(@"%s", __func__);
[self.interactor loadDataSourceWithType:self.view.type];
[self.view fetchDataSource];
}

- (void)handleViewWillAppear:(BOOL)animated {
Expand Down Expand Up @@ -68,7 +69,8 @@ - (void)didTouchNavigationBarAddButton {
__weak typeof(self) _self = self;
[self.interactor storeDataSourceWithType:self.view.type dataSource:dataSource completion:^{
[_self.interactor loadDataSourceWithType:self.view.type];
[_self.view setupTableView];
[_self.view fetchDataSource];
[_self.view.tableView reloadData];
}];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@

@interface SQTrainingDateListViewController ()

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation SQTrainingDateListViewController

- (void)setupData {
_dataSource = [NSMutableArray array];
}

- (void)setupUI {
self.title = @"Training Date";
[self setupTableView];
}

- (void)setupTableView {
NSArray *dataSource = [(id<SQTrainingDateListDataSource>)self.viewDataSource fetchDataSourceFromDB];
[self setupDataSource:dataSource loadCell:^UITableViewCell *(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) {
[self setupDataSource:self.dataSource loadCell:^UITableViewCell *(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) {
return [tableView dequeueReusableCellWithIdentifier:@"TrainingDate" forIndexPath:indexPath];
} loadCellHeight:^CGFloat(id _Nonnull model) {
return 44;
Expand All @@ -32,6 +37,12 @@ - (void)setupTableView {
}];
}

- (void)fetchDataSource {
NSArray *dataSource = [(id<SQTrainingDateListDataSource>)self.viewDataSource fetchDataSourceFromDB];
[self.dataSource removeAllObjects];
[self.dataSource addObjectsFromArray:dataSource];
}

- (IBAction)addTraningDate:(UIBarButtonItem *)sender {
[(id<SQTrainingDateListViewEventHandler>)self.eventHandler didTouchNavigationBarAddButton];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign) SQTrainingCapacityMuscleType type;

- (void)setupTableView;
@property (nonatomic, weak) UITableView *tableView;

- (void)fetchDataSource;

@end

Expand Down

0 comments on commit 6b7abc4

Please sign in to comment.