ModePopTableView.m 9.7 KB

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