DaysPopupView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // DaysPopupView.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 5/12/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "CustomLabel.h"
  10. #import "CustomCheckBox.h"
  11. #import "DaysPopupView.h"
  12. #import "UIDeviceUtil.h"
  13. #import "ItemModel.h"
  14. @interface DaysPopupView () {
  15. ItemSubModel *_subCondition;
  16. }
  17. @end
  18. @implementation DaysPopupView
  19. @synthesize condition = _condition;
  20. - (id)initFromNib {
  21. for (UIView *view in [CommonUtil nibViews:@"DaysPopupView"]) {
  22. if ([view isKindOfClass:[DaysPopupView class]]) {
  23. self = (DaysPopupView *)view;
  24. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  25. self.frame = [UIScreen mainScreen].bounds;
  26. self.lblTitle.text = NSLocalizedString(@"이 요일에만 실행",nil);
  27. ((CustomCheckBox *)_chkDays[0]).value = ksDayOfWeekMON;
  28. ((CustomCheckBox *)_chkDays[1]).value = ksDayOfWeekTUE;
  29. ((CustomCheckBox *)_chkDays[2]).value = ksDayOfWeekWED;
  30. ((CustomCheckBox *)_chkDays[3]).value = ksDayOfWeekTHU;
  31. ((CustomCheckBox *)_chkDays[4]).value = ksDayOfWeekFRI;
  32. ((CustomCheckBox *)_chkDays[5]).value = ksDayOfWeekSAT;
  33. ((CustomCheckBox *)_chkDays[6]).value = ksDayOfWeekSUN;
  34. for (CustomCheckBox *chk in _chkDays) {
  35. [self alignTextAndImageOfButton:chk];
  36. }
  37. //Localization
  38. [self.btnConfirm setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
  39. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  40. }
  41. }
  42. return self;
  43. }
  44. - (void)alignTextAndImageOfButton:(UIButton *)button {
  45. // the space between the image and text
  46. CGFloat spacing = 6.0;
  47. float textMargin = 0;
  48. // get the size of the elements here for readability
  49. CGSize imageSize = button.imageView.image.size;
  50. CGSize titleSize = button.titleLabel.frame.size;
  51. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit
  52. // lower the text and push it left to center it
  53. button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right
  54. // the text width might have changed (in case it was shortened before due to
  55. // lack of space and isn't anymore now), so we get the frame size again
  56. titleSize = button.titleLabel.bounds.size;
  57. button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right
  58. }
  59. - (void)didMoveToSuperview {
  60. }
  61. - (void)setDaysOfWeek:(NSString *)daysOfWeek {
  62. //dayOfWeek
  63. NSArray *days = [daysOfWeek componentsSeparatedByString:@","];
  64. for (NSString *d in days) {
  65. NSInteger idx = [_chkDays indexOfObjectPassingTest:^BOOL(CustomCheckBox *obj, NSUInteger idx, BOOL *stop) {
  66. return [d isEqualToString:obj.value];
  67. }];
  68. if (idx != NSNotFound) {
  69. CustomCheckBox *chk = _chkDays[idx];
  70. chk.checked = YES;
  71. }
  72. }
  73. }
  74. - (ItemModel *)condition {
  75. //validate
  76. if (![self validateCondition]) {
  77. return nil;
  78. }
  79. _subCondition.cmdclsValue = [self commandClassValue];
  80. _subCondition.daysOfWeek = [self daysOfWeek];
  81. return _condition;
  82. }
  83. - (void)setCondition:(ItemModel *)condition {
  84. _condition = condition;
  85. if (!_condition) {
  86. _condition = [[ItemModel alloc] init];
  87. _condition.itemName = NSLocalizedString(@"요일", @"요일");
  88. _condition.itemSubTypeCode = ksConditionSubTypeCodeDaysOfWeek;
  89. }
  90. if (_condition.subItems && _condition.subItems.count) {
  91. _subCondition = _condition.subItems[0];
  92. } else {
  93. _subCondition = [[ItemSubModel alloc] init];
  94. _subCondition.conditionTypeCode = @"09";
  95. _condition.subItems = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithObjects:_subCondition, nil];
  96. }
  97. [self setDaysOfWeek:_subCondition.cmdclsValue];
  98. }
  99. - (NSString *)stringOfDay:(NSInteger)index {
  100. NSString *day = nil;
  101. switch (index) {
  102. case 0:
  103. day = NSLocalizedString(@"월", @"월");
  104. break;
  105. case 1:
  106. day = NSLocalizedString(@"화", @"화");
  107. break;
  108. case 2:
  109. day = NSLocalizedString(@"수", @"수");
  110. break;
  111. case 3:
  112. day = NSLocalizedString(@"목", @"목");
  113. break;
  114. case 4:
  115. day = NSLocalizedString(@"금", @"금");
  116. break;
  117. case 5:
  118. day = NSLocalizedString(@"토", @"토");
  119. break;
  120. case 6:
  121. day = NSLocalizedString(@"일", @"일");
  122. break;
  123. }
  124. return day;
  125. }
  126. - (NSString *)daysOfWeek {
  127. //dayOfWeek
  128. NSMutableString *days = [[NSMutableString alloc] init];
  129. NSInteger i = 0;
  130. for (CustomCheckBox *chk in _chkDays) {
  131. if (chk.checked) {
  132. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  133. [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]];
  134. }
  135. i++;
  136. }
  137. return days;
  138. }
  139. - (NSString *)commandClassValue {
  140. NSMutableString *days = [[NSMutableString alloc] init];
  141. for (CustomCheckBox *chk in _chkDays) {
  142. if (chk.checked) {
  143. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  144. [days appendFormat:@"%@%@", prefix, chk.value];
  145. }
  146. }
  147. return days;
  148. }
  149. - (BOOL)validateCondition {
  150. //1.validate
  151. NSInteger count = 0;
  152. for (CustomCheckBox *chk in _chkDays) {
  153. if (chk.checked) {
  154. count++;
  155. }
  156. }
  157. //1. validate
  158. if (count == 0) {
  159. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  160. }
  161. return count;
  162. }
  163. - (void)btnConfirmTouched:(id)sender {
  164. //validate
  165. if (![self validateCondition]) {
  166. return;
  167. }
  168. [super btnConfirmTouched:sender];
  169. }
  170. @end