TimePickerPopupView.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // TimePickerPopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/24/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "ItemModel.h"
  10. #import "CustomLabel.h"
  11. #import "CustomButton.h"
  12. #import "CustomCheckBox.h"
  13. #import "CommonUtil.h"
  14. #import "TimePickerPopupView.h"
  15. @implementation CustomTimePicker
  16. - (void)awakeFromNib {
  17. self.layer.backgroundColor = [UIColor clearColor].CGColor;
  18. }
  19. - (void)addSubview:(UIView *)view {
  20. if (!_changed) {
  21. _changed = YES;
  22. [self setValue:kUITextColor02 forKey:@"textColor"];
  23. }
  24. [super addSubview:view];
  25. }
  26. @end
  27. @interface TimePickerPopupView () {
  28. }
  29. @end
  30. @implementation TimePickerPopupView
  31. - (id)initFromNib {
  32. for (UIView *view in [CommonUtil nibViews:@"TimePickerPopupView"]) {
  33. if ([view isKindOfClass:[TimePickerPopupView class]]) {
  34. self = (TimePickerPopupView *)view;
  35. ((CustomCheckBox *)_chkDays[0]).value = ksDayOfWeekMON;
  36. ((CustomCheckBox *)_chkDays[1]).value = ksDayOfWeekTUE;
  37. ((CustomCheckBox *)_chkDays[2]).value = ksDayOfWeekWED;
  38. ((CustomCheckBox *)_chkDays[3]).value = ksDayOfWeekTHU;
  39. ((CustomCheckBox *)_chkDays[4]).value = ksDayOfWeekFRI;
  40. ((CustomCheckBox *)_chkDays[5]).value = ksDayOfWeekSAT;
  41. ((CustomCheckBox *)_chkDays[6]).value = ksDayOfWeekSUN;
  42. for (CustomCheckBox *chk in _chkDays) {
  43. [self alignTextAndImageOfButton:chk];
  44. }
  45. [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  46. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  47. [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  48. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  49. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  50. //Localization
  51. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  52. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  53. }
  54. }
  55. return self;
  56. }
  57. - (void)alignTextAndImageOfButton:(UIButton *)button {
  58. // the space between the image and text
  59. CGFloat spacing = 6.0;
  60. float textMargin = 0;
  61. // get the size of the elements here for readability
  62. CGSize imageSize = button.imageView.image.size;
  63. CGSize titleSize = button.titleLabel.frame.size;
  64. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit
  65. // lower the text and push it left to center it
  66. button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right
  67. // the text width might have changed (in case it was shortened before due to
  68. // lack of space and isn't anymore now), so we get the frame size again
  69. titleSize = button.titleLabel.bounds.size;
  70. button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right
  71. }
  72. - (void)setTimeTrigger:(ItemModel *)timeTrigger {
  73. _timeTrigger = timeTrigger;
  74. if (_timeTrigger.itemSubTypeCode && [_timeTrigger.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//기존 데이터가 있을 경우,
  75. ItemSubModel *subItem = _timeTrigger.timers[0];
  76. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  77. ds.hour = [subItem.hour integerValue];
  78. ds.minute = [subItem.minute integerValue];
  79. _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  80. } else {//is new
  81. _timeTrigger.itemName = @"이 시간마다";
  82. _timeTrigger.itemSubTypeCode = ksItemSubTypeCodeTimer;
  83. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  84. ds.hour = 9;
  85. ds.minute = 0;
  86. _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  87. for (CustomCheckBox *chk in _chkDays) {
  88. chk.checked = YES;
  89. }
  90. }
  91. }
  92. - (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions {
  93. _refConditions = refConditions;
  94. [TimePickerPopupView setRefConditions:_refConditions chkDays:_chkDays];
  95. }
  96. + (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions chkDays:(NSArray *)chkDays {
  97. NSArray<ItemSubModel> *subConditions = (NSArray<ItemSubModel> *)[refConditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"];
  98. ItemSubModel *daysCondition = subConditions.firstObject;
  99. NSArray *days = [daysCondition.cmdclsValue componentsSeparatedByString:@"," ];
  100. for (NSString *day in days) {
  101. for (CustomCheckBox *chk in chkDays) {
  102. if ([chk.value isEqualToString:day]) {
  103. chk.checked = YES;
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. - (void)didMoveToSuperview {
  110. }
  111. #pragma mark - Main Logic
  112. #pragma mark - UI Events
  113. - (IBAction)btnConfirmTouched:(id)sender {
  114. //validate
  115. if (![TimePickerPopupView validateCondition:_chkDays]) {
  116. return;
  117. }
  118. ItemSubModel *subItem = nil;
  119. if (_timeTrigger.subItems && _timeTrigger.subItems.count) {
  120. subItem = _timeTrigger.subItems[0];
  121. } else {
  122. subItem = [[ItemSubModel alloc] init];
  123. _timeTrigger.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  124. [_timeTrigger.timers addObject:subItem];
  125. }
  126. //time
  127. NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date timezone:[NSTimeZone systemTimeZone]];
  128. subItem.hour = [NSString stringWithFormat:@"%zd", ds.hour];
  129. subItem.minute = ds.minute < 10 ? [NSString stringWithFormat:@"0%zd", ds.minute] : [NSString stringWithFormat:@"%zd", ds.minute];
  130. //set Days condition
  131. [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  132. [super btnConfirmTouched:sender];
  133. }
  134. #pragma mark - Class Methods
  135. + (NSString *)stringOfDay:(NSInteger)index {
  136. NSString *day = nil;
  137. switch (index) {
  138. case 0:
  139. day = NSLocalizedString(@"월", @"월");
  140. break;
  141. case 1:
  142. day = NSLocalizedString(@"화", @"화");
  143. break;
  144. case 2:
  145. day = NSLocalizedString(@"수", @"수");
  146. break;
  147. case 3:
  148. day = NSLocalizedString(@"목", @"목");
  149. break;
  150. case 4:
  151. day = NSLocalizedString(@"금", @"금");
  152. break;
  153. case 5:
  154. day = NSLocalizedString(@"토", @"토");
  155. break;
  156. case 6:
  157. day = NSLocalizedString(@"일", @"일");
  158. break;
  159. }
  160. return day;
  161. }
  162. + (NSString *)daysOfWeek:(NSArray *)chkDays {
  163. //dayOfWeek
  164. NSMutableString *days = [[NSMutableString alloc] init];
  165. NSInteger i = 0;
  166. for (CustomCheckBox *chk in chkDays) {
  167. if (chk.checked) {
  168. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  169. [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]];
  170. }
  171. i++;
  172. }
  173. return days;
  174. }
  175. + (NSString *)daysOfWeekValue:(NSArray *)chkDays {
  176. NSMutableString *days = [[NSMutableString alloc] init];
  177. for (CustomCheckBox *chk in chkDays) {
  178. if (chk.checked) {
  179. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  180. [days appendFormat:@"%@%@", prefix, chk.value];
  181. }
  182. }
  183. return days;
  184. }
  185. + (BOOL)validateCondition:(NSArray *)chkDays {
  186. //1.validate
  187. NSInteger count = 0;
  188. for (CustomCheckBox *chk in chkDays) {
  189. if (chk.checked) {
  190. count++;
  191. }
  192. }
  193. //1. validate
  194. if (count == 0) {
  195. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  196. }
  197. return count;
  198. }
  199. + (void)setDaysCondition:(NSMutableArray<ItemModel> *)conditions chkDays:(NSArray *)chkDays {
  200. [conditions enumerateObjectsUsingBlock:^(ItemModel *pCondition, NSUInteger idx, BOOL * _Nonnull stop) {
  201. if ([pCondition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  202. [conditions removeObject:pCondition];
  203. *stop = YES;
  204. }
  205. }];
  206. //set condition
  207. ItemModel *condition = [[ItemModel alloc] init];
  208. condition.itemName = @"해당 요일에만";
  209. condition.itemSubTypeCode = ksConditionSubTypeCodeDaysOfWeek;
  210. [conditions addObject:condition];
  211. //set subitems
  212. condition.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  213. ItemSubModel *subCondition = [[ItemSubModel alloc] init];
  214. subCondition.conditionTypeCode = @"09";
  215. subCondition.cmdclsValue = [TimePickerPopupView daysOfWeekValue:chkDays];
  216. subCondition.daysOfWeek = [TimePickerPopupView daysOfWeek:chkDays];
  217. [condition.subItems addObject:subCondition];
  218. }
  219. @end