TimePickerPopupView.m 11 KB

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