From 6b7abc482ee5f1b94849e3f973467a379a11962f Mon Sep 17 00:00:00 2001 From: coderZsq Date: Wed, 27 Feb 2019 16:54:11 +0800 Subject: [PATCH] add --- .../Classes/Base/SQBaseTableViewController.m | 2 +- .../Service/SQTrainingCapacityDataManager.m | 3 +++ .../Viper/Presenter/SQTrainingCapacityPresenter.m | 5 ++++- .../Viper/View/SQTrainingCapacityViewController.m | 4 ---- .../Service/SQTrainingDateListDataManager.m | 1 + .../Viper/Presenter/SQTrainingDateListPresenter.m | 4 +++- .../Viper/View/SQTrainingDateListViewController.m | 15 +++++++++++++-- .../Viper/View/SQTrainingDateListViewProtocol.h | 4 +++- 8 files changed, 28 insertions(+), 10 deletions(-) diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/Base/SQBaseTableViewController.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/Base/SQBaseTableViewController.m index 5e2affd3..56c88cdd 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/Base/SQBaseTableViewController.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/Base/SQBaseTableViewController.m @@ -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; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m index f1216188..7cd97909 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Service/SQTrainingCapacityDataManager.m @@ -16,6 +16,7 @@ @interface SQTrainingCapacityDataManager () @property (nonatomic, strong) NSMutableArray *data; @property (nonatomic, strong) SQTrainingCapacityDataBase * dataBase; +@property (nonatomic, assign) NSInteger action; @end @@ -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; @@ -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]; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m index 58edfdd0..e08a77f9 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/Presenter/SQTrainingCapacityPresenter.m @@ -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 { @@ -59,6 +60,7 @@ - (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 { @@ -66,11 +68,12 @@ - (NSString *)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)]; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m index bfc42fcc..0fa6ba18 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingCapacity/Viper/View/SQTrainingCapacityViewController.m @@ -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" @@ -51,8 +50,6 @@ - (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) { @@ -60,7 +57,6 @@ - (void)setupTableView { } 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]; }]; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Service/SQTrainingDateListDataManager.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Service/SQTrainingDateListDataManager.m index f0f4c4cd..ad93c42f 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Service/SQTrainingDateListDataManager.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Service/SQTrainingDateListDataManager.m @@ -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); diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/Presenter/SQTrainingDateListPresenter.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/Presenter/SQTrainingDateListPresenter.m index d336076c..eb5300fa 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/Presenter/SQTrainingDateListPresenter.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/Presenter/SQTrainingDateListPresenter.m @@ -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 { @@ -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]; }]; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewController.m b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewController.m index 637cd191..87e91320 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewController.m +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewController.m @@ -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)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; @@ -32,6 +37,12 @@ - (void)setupTableView { }]; } +- (void)fetchDataSource { + NSArray *dataSource = [(id)self.viewDataSource fetchDataSourceFromDB]; + [self.dataSource removeAllObjects]; + [self.dataSource addObjectsFromArray:dataSource]; +} + - (IBAction)addTraningDate:(UIBarButtonItem *)sender { [(id)self.eventHandler didTouchNavigationBarAddButton]; } diff --git a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewProtocol.h b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewProtocol.h index b8c5b1ee..8b7124a5 100644 --- a/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewProtocol.h +++ b/SQFitnessProgram/SQFitnessProgram_Viper/Classes/TrainingDate/Viper/View/SQTrainingDateListViewProtocol.h @@ -15,7 +15,9 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign) SQTrainingCapacityMuscleType type; -- (void)setupTableView; +@property (nonatomic, weak) UITableView *tableView; + +- (void)fetchDataSource; @end