TimePickerPopupView.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 CustomDatePicker
  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. //Localization
  46. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  47. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  48. }
  49. }
  50. return self;
  51. }
  52. - (void)alignTextAndImageOfButton:(UIButton *)button {
  53. // the space between the image and text
  54. CGFloat spacing = 6.0;
  55. float textMargin = 0;
  56. // get the size of the elements here for readability
  57. CGSize imageSize = button.imageView.image.size;
  58. CGSize titleSize = button.titleLabel.frame.size;
  59. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit
  60. // lower the text and push it left to center it
  61. button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right
  62. // the text width might have changed (in case it was shortened before due to
  63. // lack of space and isn't anymore now), so we get the frame size again
  64. titleSize = button.titleLabel.bounds.size;
  65. button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right
  66. }
  67. - (void)setTimeTrigger:(ItemModel *)timeTrigger {
  68. _timeTrigger = timeTrigger;
  69. if (_timeTrigger.itemSubTypeCode && [_timeTrigger.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//기존 데이터가 있을 경우,
  70. ItemSubModel *subItem = _timeTrigger.timers[0];
  71. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  72. ds.hour = [subItem.hour integerValue];
  73. ds.minute = [subItem.minute integerValue];
  74. _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  75. } else {//is new
  76. _timeTrigger.itemName = @"이 시간마다";
  77. _timeTrigger.itemSubTypeCode = ksItemSubTypeCodeTimer;
  78. NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date] timezone:[NSTimeZone systemTimeZone]];
  79. ds.hour = 9;
  80. ds.minute = 0;
  81. _timePicker.date = [[NSCalendar currentCalendar] dateFromComponents:ds];
  82. for (CustomCheckBox *chk in _chkDays) {
  83. chk.checked = YES;
  84. }
  85. }
  86. }
  87. - (void)didMoveToSuperview {
  88. }
  89. #pragma mark - Main Logic
  90. #pragma mark - UI Events
  91. - (IBAction)btnConfirmTouched:(id)sender {
  92. //validate
  93. if (![TimePickerPopupView validateCondition:_chkDays]) {
  94. return;
  95. }
  96. ItemSubModel *subItem = nil;
  97. if (_timeTrigger.subItems && _timeTrigger.subItems.count) {
  98. subItem = _timeTrigger.subItems[0];
  99. } else {
  100. subItem = [[ItemSubModel alloc] init];
  101. _timeTrigger.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  102. [_timeTrigger.timers addObject:subItem];
  103. }
  104. //time
  105. NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date timezone:[NSTimeZone systemTimeZone]];
  106. subItem.hour = [NSString stringWithFormat:@"%zd", ds.hour];
  107. subItem.minute = [NSString stringWithFormat:@"%zd", ds.minute];
  108. //set Days condition
  109. [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  110. [super btnConfirmTouched:sender];
  111. }
  112. #pragma mark - Class Methods
  113. + (NSString *)stringOfDay:(NSInteger)index {
  114. NSString *day = nil;
  115. switch (index) {
  116. case 0:
  117. day = NSLocalizedString(@"월", @"월");
  118. break;
  119. case 1:
  120. day = NSLocalizedString(@"화", @"화");
  121. break;
  122. case 2:
  123. day = NSLocalizedString(@"수", @"수");
  124. break;
  125. case 3:
  126. day = NSLocalizedString(@"목", @"목");
  127. break;
  128. case 4:
  129. day = NSLocalizedString(@"금", @"금");
  130. break;
  131. case 5:
  132. day = NSLocalizedString(@"토", @"토");
  133. break;
  134. case 6:
  135. day = NSLocalizedString(@"일", @"일");
  136. break;
  137. }
  138. return day;
  139. }
  140. + (NSString *)daysOfWeek:(NSArray *)chkDays {
  141. //dayOfWeek
  142. NSMutableString *days = [[NSMutableString alloc] init];
  143. NSInteger i = 0;
  144. for (CustomCheckBox *chk in chkDays) {
  145. if (chk.checked) {
  146. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  147. [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]];
  148. }
  149. i++;
  150. }
  151. return days;
  152. }
  153. + (NSString *)daysOfWeekValue:(NSArray *)chkDays {
  154. NSMutableString *days = [[NSMutableString alloc] init];
  155. for (CustomCheckBox *chk in chkDays) {
  156. if (chk.checked) {
  157. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  158. [days appendFormat:@"%@%@", prefix, chk.value];
  159. }
  160. }
  161. return days;
  162. }
  163. + (BOOL)validateCondition:(NSArray *)chkDays {
  164. //1.validate
  165. NSInteger count = 0;
  166. for (CustomCheckBox *chk in chkDays) {
  167. if (chk.checked) {
  168. count++;
  169. }
  170. }
  171. //1. validate
  172. if (count == 0) {
  173. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  174. }
  175. return count;
  176. }
  177. + (void)setDaysCondition:(NSMutableArray<ItemModel> *)conditions chkDays:(NSArray *)chkDays {
  178. [conditions enumerateObjectsUsingBlock:^(ItemModel *pCondition, NSUInteger idx, BOOL * _Nonnull stop) {
  179. if ([pCondition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  180. [conditions removeObject:pCondition];
  181. *stop = YES;
  182. }
  183. }];
  184. //set condition
  185. ItemModel *condition = [[ItemModel alloc] init];
  186. condition.itemName = @"해당 요일에만";
  187. condition.itemSubTypeCode = ksConditionSubTypeCodeDaysOfWeek;
  188. [conditions addObject:condition];
  189. //set subitems
  190. condition.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  191. ItemSubModel *subCondition = [[ItemSubModel alloc] init];
  192. subCondition.conditionTypeCode = @"09";
  193. subCondition.cmdclsValue = [TimePickerPopupView daysOfWeekValue:chkDays];
  194. subCondition.daysOfWeek = [TimePickerPopupView daysOfWeek:chkDays];
  195. [condition.subItems addObject:subCondition];
  196. }
  197. @end