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 d9b5039 commit 7d24dd9
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 151 deletions.
4 changes: 4 additions & 0 deletions SQFitnessProgram/SQFitnessProgram.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@
1496CBF9222635B500AE6A3F /* SQTrainingCapacityDataManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SQTrainingCapacityDataManager.m; sourceTree = "<group>"; };
1496CBFC222635CD00AE6A3F /* SQTrainingCapacityDataService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQTrainingCapacityDataService.h; sourceTree = "<group>"; };
1496CBFD22263BC800AE6A3F /* SQTrainingCapacityViewProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQTrainingCapacityViewProtocol.h; sourceTree = "<group>"; };
1496CBFE2226510D00AE6A3F /* SQTrainingCapacityInteractorDataSource.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQTrainingCapacityInteractorDataSource.h; sourceTree = "<group>"; };
1496CBFF2226559B00AE6A3F /* SQTrainingCapacityInteractorEventHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQTrainingCapacityInteractorEventHandler.h; sourceTree = "<group>"; };
14A8CF1022239C5B000A397B /* SQTrainingMusclesDataManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQTrainingMusclesDataManager.h; sourceTree = "<group>"; };
14BDFFE521DC5A5400C52CEF /* SQTrainingCapacityModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SQTrainingCapacityModel.h; sourceTree = "<group>"; };
14BDFFE621DC5A5400C52CEF /* SQTrainingCapacityModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SQTrainingCapacityModel.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -715,6 +717,8 @@
14F93ACA222571E9007297D2 /* Interactor */ = {
isa = PBXGroup;
children = (
1496CBFF2226559B00AE6A3F /* SQTrainingCapacityInteractorEventHandler.h */,
1496CBFE2226510D00AE6A3F /* SQTrainingCapacityInteractorDataSource.h */,
1496CBF62226321600AE6A3F /* SQTrainingCapacityInteractorInput.h */,
14F93AC7222571E5007297D2 /* SQTrainingCapacityInteractor.h */,
14F93AC8222571E5007297D2 /* SQTrainingCapacityInteractor.m */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "SQTrainingCapacityDataManager.h"
#import "SQTrainingCapacityDataBase.h"
#import "SQTrainingCapacityCellPresenter.h"
#import "SQTrainingCapacityModel.h"
#import "SQSqliteModelTool.h"

@interface SQTrainingCapacityDataManager ()
Expand Down Expand Up @@ -37,13 +39,78 @@ - (instancetype)init {
return self;
}

- (NSString *)totalCapacity {
NSInteger totalCapacity = 0;
for (SQTrainingCapacityCellPresenter * p in self.dataSource) {
totalCapacity += p.model.capacity;
}
return [NSString stringWithFormat:@"%ld", totalCapacity];
}

- (NSArray *)dataSource {
return self.data.copy;
}

- (void)fetchDataSourceWithType:(SQTrainingCapacityMuscleType)type completion:(nonnull void (^)(NSArray * _Nonnull))completion {
- (void)fetchDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type completion:(nonnull void (^)(NSArray * _Nonnull))completion {
NSAssert([NSThread isMainThread], @"main thread only, otherwise use lock to make thread safety");

SQTrainingCapacityDataBase * dataBase = [SQSqliteModelTool queryModels:self.dataBase.class columnName:@"key" relation:(ColumnNameToValueRelationTypeEqual) value:[NSString stringWithFormat:@"%ld-%@", type, title] uid:nil].firstObject;
if (!dataBase) {
[self addTrainingActionWithCompletion:nil];
return;
}
NSMutableArray * dataSource = [NSMutableArray array];
for (NSDictionary * d in dataBase.dataSource) {
SQTrainingCapacityCellPresenter * p = [SQTrainingCapacityCellPresenter new];
SQTrainingCapacityModel * m = [SQTrainingCapacityModel new];
p.model = m;
NSMutableArray * rows = [NSMutableArray array];
for (NSDictionary * dr in d[@"rows"]) {
SQTrainingCapacityRowModel * rm = [SQTrainingCapacityRowModel new];
rm.groups = [dr[@"groups"] integerValue];
rm.times = [dr[@"times"] integerValue];
rm.weight = [dr[@"weight"] integerValue];
[rows addObject:rm];
}
p.model.rows = rows;
p.model.capacity = [d[@"capacity"] integerValue];
[dataSource addObject:p];
}
self.data = dataSource;
if (completion) {
completion(self.data);
}
}

- (void)storeDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type dataSource:(NSArray *)dataSource completion:(void (^)(void))completion {
self.dataBase.key = [NSString stringWithFormat:@"%ld-%@", type, title];
self.dataBase.type = type;
self.dataBase.date = title;
NSMutableArray * tempDataSource = [NSMutableArray array];
for (SQTrainingCapacityCellPresenter * p in dataSource) {
NSMutableDictionary * md = [NSMutableDictionary dictionary];
md[@"capacity"] = @(p.model.capacity);
NSMutableArray * ma = [NSMutableArray array];
for (SQTrainingCapacityRowModel * rm in p.model.rows) {
NSMutableDictionary * mdr = [NSMutableDictionary dictionary];
mdr[@"groups"] = @(rm.groups);
mdr[@"times"] = @(rm.times);
mdr[@"weight"] = @(rm.weight);
[ma addObject:mdr];
}
md[@"rows"] = ma;
[tempDataSource addObject:md];
}
self.dataBase.dataSource = tempDataSource;
[SQSqliteModelTool saveOrUpdateModel:self.dataBase uid:nil];
}

- (void)addTrainingActionWithCompletion:(void(^)(void))completion {
SQTrainingCapacityCellPresenter * p = [SQTrainingCapacityCellPresenter new];
p.model = [SQTrainingCapacityModel new];
[self.data addObject:p];
if (completion) {
completion();
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#import <Foundation/Foundation.h>
#import "SQTrainingCapacityMuscleType.h"

NS_ASSUME_NONNULL_BEGIN

@protocol SQTrainingCapacityDataService <NSObject>

@property (nonatomic, readonly, copy) NSString *totalCapacity;
@property (nonatomic, readonly, strong) NSArray *dataSource;

- (void)fetchDataSourceWithType:(SQTrainingCapacityMuscleType)type completion:(void(^)(NSArray *dataSource))completion;
- (void)fetchDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type completion:(void(^)(NSArray *dataSource))completion;

@end
- (void)storeDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type dataSource:(nonnull NSArray *)dataSource completion:(nonnull void (^)(void))completion;

NS_ASSUME_NONNULL_END
- (void)addTrainingActionWithCompletion:(void(^)(void))completion;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "SQTrainingCapacityPresenter.h"
#import "SQTrainingCapacityInteractor.h"
#import "SQTrainingCapacityWireframe.h"
#import "SQTrainingCapacityDataManager.h"
#import "NSObject+SQViperAssembly.h"

@implementation SQTrainingCapacityBuilder
Expand All @@ -21,12 +22,13 @@ + (UIViewController *)viewControllerForTrainingCapacityWithTitle:(NSString *)tit
NSAssert([view isKindOfClass:[SQTrainingCapacityViewController class]], nil);
view.title = title;
view.type = type;
[self buildView:(id<SQViperViewPrivate>)view trainingDateListDataService:[SQTrainingCapacityDataManager new] router:router];
return view;
}

+ (void)buildView:(id<SQViperViewPrivate>)view router:(id<SQViperRouter>)router {
+ (void)buildView:(id<SQViperViewPrivate>)view trainingDateListDataService:(id<SQTrainingCapacityDataService>)service router:(id<SQViperRouter>)router {
id<SQViperPresenterPrivate> presenter = (id)[SQTrainingCapacityPresenter new];
id<SQViperInteractorPrivate> interactor = (id)[SQTrainingCapacityInteractor new];
id<SQViperInteractorPrivate> interactor = [(id)[SQTrainingCapacityInteractor alloc]initWithTrainingCapacityDataService:service];
id<SQViperWireframePrivate> wireframe = (id)[SQTrainingCapacityWireframe new];
[self assembleViperForView:view
presenter:presenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@

NS_ASSUME_NONNULL_BEGIN

@protocol SQTrainingCapacityInteractorDataSource;
@protocol SQTrainingCapacityInteractorEventHandler;
@protocol SQTrainingCapacityDataService;

@interface SQTrainingCapacityInteractor : NSObject<SQTrainingCapacityInteractorInput>

@property (nonatomic, weak) id dataSource;
@property (nonatomic, weak) id eventHandler;
@property (nonatomic, weak) id<SQTrainingCapacityInteractorDataSource> dataSource;
@property (nonatomic, weak) id<SQTrainingCapacityInteractorEventHandler> eventHandler;

- (instancetype)initWithTrainingCapacityDataService:(id<SQTrainingCapacityDataService>)service NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,50 @@
//

#import "SQTrainingCapacityInteractor.h"
#import "SQTrainingCapacityDataService.h"
#import "SQTrainingCapacityInteractorDataSource.h"

@interface SQTrainingCapacityInteractor ()

@property (nonatomic, strong) id<SQTrainingCapacityDataService> trainingCapacityDataService;

@end

@implementation SQTrainingCapacityInteractor

- (void)loadDataSourceWithType:(SQTrainingCapacityMuscleType)type {

- (instancetype)initWithTrainingCapacityDataService:(id<SQTrainingCapacityDataService>)service {
if (self = [super init]) {
_trainingCapacityDataService = service;
}
return self;
}

- (NSArray *)fetchDataSource {
return self.trainingCapacityDataService.dataSource;
}

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

- (void)loadDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type {
[self.trainingCapacityDataService fetchDataSourceWithTitle:title type:type completion:^(NSArray * _Nonnull dataSource) {

}];
}

- (void)storeDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type dataSource:(NSArray *)dataSource {
[self.trainingCapacityDataService storeDataSourceWithTitle:title type:type dataSource:dataSource completion:^{

}];
}

- (void)addTrainingActionWithCompletion:(void (^)(void))completion {
[self.trainingCapacityDataService addTrainingActionWithCompletion:^{
if (completion) {
completion();
}
}];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// SQTrainingCapacityInteractorDataSource.h
// SQFitnessProgram_Viper
//
// Created by 朱双泉 on 2019/2/27.
// Copyright © 2019 Castie!. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@protocol SQTrainingCapacityInteractorDataSource <NSObject>

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SQTrainingCapacityInteractorEventHandler.h
// SQFitnessProgram_Viper
//
// Created by 朱双泉 on 2019/2/27.
// Copyright © 2019 Castie!. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@protocol SQTrainingCapacityInteractorEventHandler <NSObject>

- (void)addTrainingAction;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ NS_ASSUME_NONNULL_BEGIN

@protocol SQTrainingCapacityInteractorInput <SQViperInteractor>

- (void)loadDataSourceWithType:(SQTrainingCapacityMuscleType)type;
@property (nonatomic, readonly, copy) NSString *totalCapacity;

- (void)loadDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type;

- (void)storeDataSourceWithTitle:(NSString *)title type:(SQTrainingCapacityMuscleType)type dataSource:(nonnull NSArray *)dataSource;

- (void)addTrainingActionWithCompletion:(void(^)(void))completion;

- (NSArray *)fetchDataSource;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
//

#import "SQTrainingCapacityPresenter.h"
#import "SQViperView.h"
#import "SQViperInteractor.h"
#import "SQViperWireframe.h"
#import "SQTrainingCapacityViewProtocol.h"
#import "SQTrainingCapacityInteractorInput.h"
#import "SQTrainingCapacityWireframeInput.h"
#import "SQTrainingCapacityNotification.h"

@interface SQTrainingCapacityPresenter ()

@property (nonatomic, weak) id<SQViperView> view;
@property (nonatomic, strong) id<SQViperInteractor> interactor;
@property (nonatomic, strong) id<SQViperWireframe> wireframe;
@property (nonatomic, weak) id<SQTrainingCapacityViewProtocol> view;
@property (nonatomic, strong) id<SQTrainingCapacityInteractorInput> interactor;
@property (nonatomic, strong) id<SQTrainingCapacityWireframeInput> wireframe;

@end

Expand All @@ -25,6 +26,9 @@ - (void)handleViewReady {
NSAssert(self.wireframe, @"Router should be initlized when view is ready.");
NSAssert([self.view conformsToProtocol:@protocol(SQViperView)], @"Presenter should be attach to a view");
NSAssert([self.interactor conformsToProtocol:@protocol(SQViperInteractor)], @"Interactor should be initlized when view is ready.");
[[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];
}

- (void)handleViewWillAppear:(BOOL)animated {
Expand All @@ -45,6 +49,41 @@ - (void)handleViewDidDisappear:(BOOL)animated {

- (void)handleViewRemoved {
NSLog(@"%s", __func__);
[self.interactor storeDataSourceWithTitle:self.view.title type:self.view.type dataSource:self.fetchDataSourceFromDB];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

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

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

- (void)didTouchNavigationBarAddButton {
__weak typeof(self) _self = self;
[self.interactor addTrainingActionWithCompletion:^{
[_self.view setupTableView];
}];
}

- (void)keyboardWillShow:(NSNotification *)sender {
[self.view setRightBarButtonItem:(UIBarButtonSystemItemDone) target:self action:@selector(doneAction)];
}

- (void)keyboardWillHide:(NSNotification *)sender {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
[self.view setRightBarButtonItem:(UIBarButtonSystemItemAdd) target:self.view action:@selector(addTraningAction)];
#pragma clang diagnostic pop
}

- (void)doneAction {
[self.view.tableView endEditing:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:SQTrainingCapacityBindToModelNotification object:nil];
self.view.footerView.totalCapacityLabel.text = self.totalCapacity;
[self.view.tableView reloadData];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN

@protocol SQTrainingCapacityDataSource <NSObject>

@property (nonatomic, copy) NSString *totalCapacity;
@property (nonatomic, readonly, copy) NSString *totalCapacity;

- (NSArray *)fetchDataSourceFromDB;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign) SQTrainingCapacityMuscleType type;

@property (nonatomic, weak) SQTrainingCapacityFooterView * footerView;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 7d24dd9

Please sign in to comment.