TimePickerPopupView.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. - (NSString *)stringOfDay:(NSInteger)index {
  91. NSString *day = nil;
  92. switch (index) {
  93. case 0:
  94. day = NSLocalizedString(@"월", @"월");
  95. break;
  96. case 1:
  97. day = NSLocalizedString(@"화", @"화");
  98. break;
  99. case 2:
  100. day = NSLocalizedString(@"수", @"수");
  101. break;
  102. case 3:
  103. day = NSLocalizedString(@"목", @"목");
  104. break;
  105. case 4:
  106. day = NSLocalizedString(@"금", @"금");
  107. break;
  108. case 5:
  109. day = NSLocalizedString(@"토", @"토");
  110. break;
  111. case 6:
  112. day = NSLocalizedString(@"일", @"일");
  113. break;
  114. }
  115. return day;
  116. }
  117. - (NSString *)daysOfWeek {
  118. //dayOfWeek
  119. NSMutableString *days = [[NSMutableString alloc] init];
  120. NSInteger i = 0;
  121. for (CustomCheckBox *chk in _chkDays) {
  122. if (chk.checked) {
  123. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  124. [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]];
  125. }
  126. i++;
  127. }
  128. return days;
  129. }
  130. - (NSString *)cmdClsValue {
  131. NSMutableString *days = [[NSMutableString alloc] init];
  132. for (CustomCheckBox *chk in _chkDays) {
  133. if (chk.checked) {
  134. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  135. [days appendFormat:@"%@%@", prefix, chk.value];
  136. }
  137. }
  138. return days;
  139. }
  140. #pragma mark - UI Events
  141. - (BOOL)validateCondition {
  142. //1.validate
  143. NSInteger count = 0;
  144. for (CustomCheckBox *chk in _chkDays) {
  145. if (chk.checked) {
  146. count++;
  147. }
  148. }
  149. //1. validate
  150. if (count == 0) {
  151. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  152. }
  153. return count;
  154. }
  155. - (IBAction)btnConfirmTouched:(id)sender {
  156. //validate
  157. if (![self validateCondition]) {
  158. return;
  159. }
  160. ItemSubModel *subItem = nil;
  161. if (_timeTrigger.subItems && _timeTrigger.subItems.count) {
  162. subItem = _timeTrigger.subItems[0];
  163. } else {
  164. subItem = [[ItemSubModel alloc] init];
  165. _timeTrigger.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  166. [_timeTrigger.timers addObject:subItem];
  167. }
  168. //time
  169. NSDateComponents *ds = [CommonUtil dateComponents:_timePicker.date timezone:[NSTimeZone systemTimeZone]];
  170. subItem.hour = [NSString stringWithFormat:@"%zd", ds.hour];
  171. subItem.minute = [NSString stringWithFormat:@"%zd", ds.minute];
  172. subItem.cmdclsValue = [self cmdClsValue];
  173. subItem.daysOfWeek = [self daysOfWeek];
  174. [super btnConfirmTouched:sender];
  175. }
  176. @end