TimePickerPopupView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. //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)setRefConditions:(NSMutableArray<ItemModel> *)refConditions {
  88. _refConditions = refConditions;
  89. [TimePickerPopupView setRefConditions:_refConditions chkDays:_chkDays];
  90. }
  91. + (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions chkDays:(NSArray *)chkDays {
  92. NSArray<ItemSubModel> *subConditions = (NSArray<ItemSubModel> *)[refConditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"];
  93. ItemSubModel *daysCondition = subConditions.firstObject;
  94. NSArray *days = [daysCondition.cmdclsValue componentsSeparatedByString:@"," ];
  95. for (NSString *day in days) {
  96. for (CustomCheckBox *chk in chkDays) {
  97. if ([chk.value isEqualToString:day]) {
  98. chk.checked = YES;
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. - (void)didMoveToSuperview {
  105. }
  106. #pragma mark - Main Logic
  107. #pragma mark - UI Events
  108. - (IBAction)btnConfirmTouched:(id)sender {
  109. //validate
  110. if (![TimePickerPopupView validateCondition:_chkDays]) {
  111. return;
  112. }
  113. ItemSubModel *subItem = nil;
  114. if (_timeTrigger.subItems && _timeTrigger.subItems.count) {
  115. subItem = _timeTrigger.subItems[0];
  116. } else {
  117. subItem = [[ItemSubModel alloc] init];
  118. _timeTrigger.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  119. [_timeTrigger.timers addObject:subItem];
  120. }
  121. //time
  122. NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date timezone:[NSTimeZone systemTimeZone]];
  123. subItem.hour = [NSString stringWithFormat:@"%zd", ds.hour];
  124. subItem.minute = ds.minute < 10 ? [NSString stringWithFormat:@"0%zd", ds.minute] : [NSString stringWithFormat:@"%zd", ds.minute];
  125. //set Days condition
  126. [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  127. [super btnConfirmTouched:sender];
  128. }
  129. #pragma mark - Class Methods
  130. + (NSString *)stringOfDay:(NSInteger)index {
  131. NSString *day = nil;
  132. switch (index) {
  133. case 0:
  134. day = NSLocalizedString(@"월", @"월");
  135. break;
  136. case 1:
  137. day = NSLocalizedString(@"화", @"화");
  138. break;
  139. case 2:
  140. day = NSLocalizedString(@"수", @"수");
  141. break;
  142. case 3:
  143. day = NSLocalizedString(@"목", @"목");
  144. break;
  145. case 4:
  146. day = NSLocalizedString(@"금", @"금");
  147. break;
  148. case 5:
  149. day = NSLocalizedString(@"토", @"토");
  150. break;
  151. case 6:
  152. day = NSLocalizedString(@"일", @"일");
  153. break;
  154. }
  155. return day;
  156. }
  157. + (NSString *)daysOfWeek:(NSArray *)chkDays {
  158. //dayOfWeek
  159. NSMutableString *days = [[NSMutableString alloc] init];
  160. NSInteger i = 0;
  161. for (CustomCheckBox *chk in chkDays) {
  162. if (chk.checked) {
  163. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  164. [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]];
  165. }
  166. i++;
  167. }
  168. return days;
  169. }
  170. + (NSString *)daysOfWeekValue:(NSArray *)chkDays {
  171. NSMutableString *days = [[NSMutableString alloc] init];
  172. for (CustomCheckBox *chk in chkDays) {
  173. if (chk.checked) {
  174. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  175. [days appendFormat:@"%@%@", prefix, chk.value];
  176. }
  177. }
  178. return days;
  179. }
  180. + (BOOL)validateCondition:(NSArray *)chkDays {
  181. //1.validate
  182. NSInteger count = 0;
  183. for (CustomCheckBox *chk in chkDays) {
  184. if (chk.checked) {
  185. count++;
  186. }
  187. }
  188. //1. validate
  189. if (count == 0) {
  190. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  191. }
  192. return count;
  193. }
  194. + (void)setDaysCondition:(NSMutableArray<ItemModel> *)conditions chkDays:(NSArray *)chkDays {
  195. [conditions enumerateObjectsUsingBlock:^(ItemModel *pCondition, NSUInteger idx, BOOL * _Nonnull stop) {
  196. if ([pCondition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  197. [conditions removeObject:pCondition];
  198. *stop = YES;
  199. }
  200. }];
  201. //set condition
  202. ItemModel *condition = [[ItemModel alloc] init];
  203. condition.itemName = @"해당 요일에만";
  204. condition.itemSubTypeCode = ksConditionSubTypeCodeDaysOfWeek;
  205. [conditions addObject:condition];
  206. //set subitems
  207. condition.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  208. ItemSubModel *subCondition = [[ItemSubModel alloc] init];
  209. subCondition.conditionTypeCode = @"09";
  210. subCondition.cmdclsValue = [TimePickerPopupView daysOfWeekValue:chkDays];
  211. subCondition.daysOfWeek = [TimePickerPopupView daysOfWeek:chkDays];
  212. [condition.subItems addObject:subCondition];
  213. }
  214. @end