ModePopTableView.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // ModePopTableView.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/27/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "ModeModel.h"
  10. #import "CustomLabel.h"
  11. #import "CustomImageView.h"
  12. #import "CustomCheckBox.h"
  13. #import "UIImageView+WebCache.h"
  14. #import "ModePopTableView.h"
  15. #import "RequestHandler.h"
  16. #import "ItemModel.h"
  17. #import "ImageUtil.h"
  18. #define kfModePopTableViewCellHeight 100.0f
  19. @interface ModePopTableViewCell : UITableViewCell
  20. @property (strong, nonatomic) CustomImageView *imgvImage;
  21. @property (strong, nonatomic) CustomLabel *lblTitle;
  22. @property (strong, nonatomic) CustomCheckBox *chkSelect;
  23. @end
  24. @implementation ModePopTableViewCell
  25. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  26. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  27. self.imgvImage = [[CustomImageView alloc] initWithFrame:CGRectMake(10, 20, 60, 60)];
  28. _imgvImage.contentMode = UIViewContentModeScaleAspectFit;
  29. CGFloat width = IPHONE_WIDTH - 80 - 25 - 10 - 20 - 20; //margin - margin - radiosize - padding
  30. self.lblTitle = [[CustomLabel alloc] initWithFrame:CGRectMake(80, 20, width, 60)];
  31. _lblTitle.numberOfLines = 0;
  32. _lblTitle.font = [UIFont systemFontOfSize:kUIFontSize02];
  33. _lblTitle.textColor = kUITextColor01;
  34. self.chkSelect = [[CustomCheckBox alloc] initWithFrame:CGRectMake(80 + width + 10, 37.5, 25.0f, 25.0f) normalImage:[UIImage imageNamed:@"common_checkbox_default"] highlightImage:[UIImage imageNamed:@"common_checkbox_checked"]];
  35. [self addSubview:_imgvImage];
  36. [self addSubview:_lblTitle];
  37. [self addSubview:_chkSelect];
  38. self.backgroundColor = [UIColor clearColor];
  39. self.selectionStyle = UITableViewCellSelectionStyleNone;
  40. }
  41. return self;
  42. }
  43. @end
  44. @interface ModePopTableView () <UITableViewDataSource, UITableViewDelegate> {
  45. UIImage *_bgCellImage;
  46. BOOL _isNotFirstLoading;
  47. ItemSubModel *_subCondition;
  48. NSMutableArray<ModeModel> *_modeList;
  49. }
  50. @end
  51. @implementation ModePopTableView
  52. @synthesize condition = _condition;
  53. - (id)initFromNib {
  54. for (UIView *view in [CommonUtil nibViews:@"ModePopTableView"]) {
  55. if ([view isKindOfClass:[ModePopTableView class]]) {
  56. self = (ModePopTableView *)view;
  57. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  58. self.frame = [UIScreen mainScreen].bounds;
  59. self.lblTitle.text = NSLocalizedString(@"이 홈모드에서만 실행",nil);
  60. _tableView.dataSource = self;
  61. _tableView.delegate = self;
  62. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  63. _tableView.backgroundColor = [UIColor clearColor];
  64. _tableView.scrollEnabled = YES;
  65. _tableView.tableFooterView = [[UIView alloc] init];
  66. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  67. _bgCellImage = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  68. [self.btnConfirm setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
  69. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  70. }
  71. }
  72. return self;
  73. }
  74. - (void)didMoveToSuperview {
  75. }
  76. - (void)willMoveToSuperview:(UIView *)newSuperview {
  77. if (!newSuperview)
  78. return;
  79. [self loadData];
  80. }
  81. - (void)loadData {
  82. [self requestModes];
  83. }
  84. - (void)requestModes {
  85. NSString *path = API_GET_DASHBOARD_MODE_LIST;
  86. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ModeListModel class] completion:^(id responseObject) {
  87. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  88. return;
  89. }
  90. ModeListModel *fetchedModeList = (ModeListModel *)responseObject;
  91. if (fetchedModeList && fetchedModeList.list && fetchedModeList.list.count) {//API 성공 ,
  92. _modeList = (NSMutableArray<ModeModel> *)[[NSMutableArray alloc] initWithArray:fetchedModeList.list];
  93. if (_subCondition && _subCondition.cmdclsValue) {//match the previous select
  94. NSArray *tmpList = [_subCondition.cmdclsValue componentsSeparatedByString:@","];
  95. for (NSString *modeId in tmpList) {
  96. NSInteger indx = [_modeList indexOfObjectPassingTest:^BOOL(ModeModel *obj, NSUInteger idx, BOOL *stop) {//일치하는 인덱스를 구함.
  97. return [modeId isEqualToString:obj.modeId];
  98. }];
  99. if (indx != NSNotFound) {
  100. ModeModel *mode = _modeList[indx];
  101. [[JDFacade facade] setCheckBoxStatus:@YES object:mode]; //for check box
  102. }
  103. }
  104. }
  105. [self setModeContents];
  106. }
  107. } failure:^(id errorObject) {
  108. JDErrorModel *error = (JDErrorModel *)errorObject;
  109. [[JDFacade facade] alert:error.errorMessage];
  110. }];
  111. }
  112. - (void)setModeContents {
  113. [_tableView reloadData];
  114. }
  115. #pragma mark - UITableView DataSource & Delegate
  116. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  117. return 1;
  118. }
  119. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  120. return _modeList.count;
  121. }
  122. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  123. static NSString *CellIdentifier = @"CellIdentifier";
  124. ModePopTableViewCell *cell = (ModePopTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  125. if (cell == nil) {
  126. cell = [[ModePopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  127. }
  128. ModeModel *mode = _modeList[indexPath.row];
  129. cell.lblTitle.text = mode.modeName;
  130. cell.chkSelect.value = mode;
  131. [cell.imgvImage sd_setImageWithURL:[NSURL URLWithString:mode.imageFileName] placeholderImage:nil];
  132. cell.chkSelect.checked = [cell.chkSelect getCheckStatusFromValue];
  133. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage];
  134. return cell;
  135. }
  136. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  137. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  138. ModeModel *mode = _modeList[indexPath.row];
  139. ModePopTableViewCell *tcell = (ModePopTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
  140. tcell.chkSelect.value = mode;
  141. [tcell.chkSelect checkBoxClicked];
  142. [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  143. }
  144. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  145. // Remove seperator inset
  146. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  147. [cell setSeparatorInset:UIEdgeInsetsZero];
  148. }
  149. // Prevent the cell from inheriting the Table View's margin settings
  150. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  151. [cell setPreservesSuperviewLayoutMargins:NO];
  152. }
  153. // Explictly set your cell's layout margins
  154. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  155. [cell setLayoutMargins:UIEdgeInsetsZero];
  156. }
  157. }
  158. #pragma mark - UI Events
  159. - (void)didCheckBoxClicked:(id)sender {
  160. // CustomCheckBox *chk = (CustomCheckBox *)sender;
  161. // ModeModel *mode = chk.value;
  162. }
  163. - (NSString *)homeModes {
  164. NSMutableString *modeString = [[NSMutableString alloc] init];
  165. for (ModeModel *mode in _modeList) {
  166. if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) {
  167. NSString *prefix = [modeString isEmptyString] ? ksEmptyString : @", ";
  168. [modeString appendFormat:@"%@%@", prefix, mode.modeName];
  169. }
  170. }
  171. return modeString;
  172. }
  173. - (NSString *)homeModesCommandClassValue {
  174. NSMutableString *modeString = [[NSMutableString alloc] init];
  175. for (ModeModel *mode in _modeList) {
  176. if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) {
  177. NSString *prefix = [modeString isEmptyString] ? ksEmptyString : @",";
  178. [modeString appendFormat:@"%@%@", prefix, mode.modeId];
  179. }
  180. }
  181. return modeString;
  182. }
  183. - (void)setCondition:(ItemModel *)condition {
  184. _condition = condition;
  185. if (!_condition) {
  186. _condition = [[ItemModel alloc] init];
  187. _condition.itemName = NSLocalizedString(@"홈모드", @"홈모드");
  188. _condition.itemSubTypeCode = ksConditionSubTypeCodeHomeMode;
  189. }
  190. if (_condition.subItems && _condition.subItems.count) {
  191. _subCondition = _condition.subItems[0];
  192. } else {
  193. _subCondition = [[ItemSubModel alloc] init];
  194. _subCondition.sourceId = @"VAR";
  195. _subCondition.conditionTypeCode = @"01";
  196. _condition.subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithObjects:_subCondition, nil];
  197. }
  198. }
  199. - (ItemModel *)condition {
  200. //validate
  201. if (![self validateCondition]) {
  202. return nil;
  203. }
  204. _subCondition.cmdclsValue = [self homeModesCommandClassValue];
  205. _subCondition.homeModes = [self homeModes];
  206. return _condition;
  207. }
  208. - (BOOL)validateCondition {
  209. NSInteger count = 0;
  210. for (ModeModel *mode in _modeList) {
  211. if ([[[JDFacade facade] getCheckBoxStatus:mode] boolValue]) {
  212. count ++;
  213. }
  214. }
  215. //1. validate
  216. if (count == 0) {
  217. [[JDFacade facade] alert:NSLocalizedString(@"홈모드를 선택해주세요", @"홈모드를 선택해주세요")];
  218. }
  219. return count;
  220. }
  221. - (void)btnConfirmTouched:(id)sender {
  222. //validate
  223. if (![self validateCondition]) {
  224. return;
  225. }
  226. [super btnConfirmTouched:sender];
  227. }
  228. @end