From 9af3696e2e9e68a1e9c2aa3e25845c602ec54662 Mon Sep 17 00:00:00 2001 From: coderZsq Date: Wed, 27 Feb 2019 16:30:12 +0800 Subject: [PATCH] add --- .../Service/SQTrainingCapacityDataManager.m | 1 + .../Presenter/SQTrainingCapacityPresenter.m | 10 +++++++- .../View/SQTrainingCapacityViewController.m | 23 ++++++++++++++----- .../View/SQTrainingCapacityViewEventHandler.h | 2 ++ .../View/SQTrainingCapacityViewProtocol.h | 2 +- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m index 1f5c6586..f1216188 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m @@ -101,6 +101,7 @@ - (void)storeDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscl [tempDataSource addObject:md]; } self.dataBase.dataSource = tempDataSource; + self.data = dataSource.mutableCopy; [SQSqliteModelTool saveOrUpdateModel:self.dataBase uid:nil]; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m index 098db687..58edfdd0 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m @@ -53,6 +53,14 @@ - (void)handleViewRemoved { [[NSNotificationCenter defaultCenter] removeObserver:self]; } +- (void)handleCommitEditingAtIndexPath:(NSIndexPath *)indexPath { + NSMutableArray *tempDataSource = self.fetchDataSourceFromDB.mutableCopy; + [tempDataSource removeObjectAtIndex:indexPath.row]; + [self.interactor storeDataSourceWithTitle:self.view.title type:self.view.type dataSource:tempDataSource]; + [self.view fetchDataSource]; + [self.view.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; +} + - (NSString *)totalCapacity { return self.interactor.totalCapacity; } @@ -63,7 +71,7 @@ - (NSArray *)fetchDataSourceFromDB { - (void)didTouchNavigationBarAddButton { [self.interactor addTrainingAction]; - [self.view setupTableView]; + [self.view.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.fetchDataSourceFromDB.count - 1 inSection:0]] withRowAnimation:(UITableViewRowAnimationLeft)]; } - (void)keyboardWillShow:(NSNotification *)sender { diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m index a7c4526b..bfc42fcc 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m @@ -17,10 +17,16 @@ @interface SQTrainingCapacityViewController () +@property (nonatomic, strong) NSMutableArray *dataSource; + @end @implementation SQTrainingCapacityViewController +- (void)setupData { + _dataSource = [NSMutableArray array]; +} + - (void)setupUI { [self setupRightBarButtonItem]; [self setupTableViewContent]; @@ -45,27 +51,32 @@ - (void)setupTableViewContent { } - (void)setupTableView { - NSArray *dataSource = [(id)self.viewDataSource fetchDataSourceFromDB]; - [self setupDataSource:dataSource loadCell:^UITableViewCell *(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) { + [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", [dataSource indexOfObject:model] + 1]; + p.model.action = [NSString stringWithFormat:@"%ld", [_self.dataSource indexOfObject:model] + 1]; [p bindToCell:c]; }]; } +- (void)fetchDataSource { + NSArray *dataSource = [(id)self.viewDataSource fetchDataSourceFromDB]; + [self.dataSource removeAllObjects]; + [self.dataSource addObjectsFromArray:dataSource]; +} + - (void)addTraningAction { [(id)self.eventHandler didTouchNavigationBarAddButton]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { -// [self.dataSource removeObjectAtIndex:indexPath.row]; -// [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; -// [self calculateTotalCapacity]; + [(id)self.eventHandler handleCommitEditingAtIndexPath:indexPath]; } @end diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewEventHandler.h b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewEventHandler.h index 116fe32f..6bb194b1 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewEventHandler.h +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewEventHandler.h @@ -14,6 +14,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)didTouchNavigationBarAddButton; +- (void)handleCommitEditingAtIndexPath:(NSIndexPath *)indexPath; + @end NS_ASSUME_NONNULL_END diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewProtocol.h b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewProtocol.h index c7a64f3c..689fd6a0 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewProtocol.h +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewProtocol.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, weak) SQTrainingCapacityFooterView *footerView; -- (void)setupTableView; +- (void)fetchDataSource; - (void)setRightBarButtonItem:(UIBarButtonSystemItem)item target:(nullable id)target action:(nullable SEL)action;