DaylightPopupView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // DaylightPopupView.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 "JDJSONModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomButton.h"
  13. #import "CustomCheckBox.h"
  14. #import "CommonUtil.h"
  15. #import "WeatherLocationPopupView.h"
  16. #import "DaylightPopupView.h"
  17. #import "RequestHandler.h"
  18. @interface DaylightPopupView () {
  19. CommonCode *_weatherLocation;
  20. NSArray<CommonCode> *_locations;
  21. WeatherLocationPopupView *_cpopup;
  22. }
  23. @end
  24. @implementation DaylightPopupView
  25. - (id)initFromNib {
  26. for (UIView *view in [CommonUtil nibViews:@"DaylightPopupView"]) {
  27. if ([view isKindOfClass:[DaylightPopupView class]]) {
  28. self = (DaylightPopupView *)view;
  29. ((CustomCheckBox *)_chkDays[0]).value = ksDayOfWeekMON;
  30. ((CustomCheckBox *)_chkDays[1]).value = ksDayOfWeekTUE;
  31. ((CustomCheckBox *)_chkDays[2]).value = ksDayOfWeekWED;
  32. ((CustomCheckBox *)_chkDays[3]).value = ksDayOfWeekTHU;
  33. ((CustomCheckBox *)_chkDays[4]).value = ksDayOfWeekFRI;
  34. ((CustomCheckBox *)_chkDays[5]).value = ksDayOfWeekSAT;
  35. ((CustomCheckBox *)_chkDays[6]).value = ksDayOfWeekSUN;
  36. for (CustomCheckBox *chk in _chkDays) {
  37. [self alignTextAndImageOfButton:chk];
  38. }
  39. if (!_lblCity.touchHandler) {
  40. [_lblCity addTouchEventHandler:^(id label) {
  41. [self lblCityTouched:label];
  42. }];
  43. }
  44. //Localization
  45. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  46. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  47. }
  48. }
  49. return self;
  50. }
  51. - (void)alignTextAndImageOfButton:(UIButton *)button {
  52. // the space between the image and text
  53. CGFloat spacing = 6.0;
  54. float textMargin = 0;
  55. // get the size of the elements here for readability
  56. CGSize imageSize = button.imageView.image.size;
  57. CGSize titleSize = button.titleLabel.frame.size;
  58. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit
  59. // lower the text and push it left to center it
  60. button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right
  61. // the text width might have changed (in case it was shortened before due to
  62. // lack of space and isn't anymore now), so we get the frame size again
  63. titleSize = button.titleLabel.bounds.size;
  64. button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right
  65. }
  66. - (void)setDaylightTrigger:(ItemModel *)daylightTrigger {
  67. _daylightTrigger = daylightTrigger;
  68. if (_daylightTrigger.itemSubTypeCode && [_daylightTrigger.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//기존 데이터가 있을 경우,
  69. ItemSubModel *subItem = _daylightTrigger.daylights[0];
  70. _btnSunRise.selected = [subItem.sourceSubId isEqualToString:@"sunriseUtcTime"];
  71. _btnSunSet.selected = !_btnSunRise.selected;
  72. } else {//is new
  73. _daylightTrigger.itemName = @"해뜰때 / 질때";
  74. _daylightTrigger.itemSubTypeCode = ksItemSubTypeCodeDaylight;
  75. _btnSunRise.selected = YES;
  76. for (CustomCheckBox *chk in _chkDays) {
  77. chk.checked = YES;
  78. }
  79. }
  80. }
  81. - (void)didMoveToSuperview {
  82. [self requestLocationCode];
  83. }
  84. #pragma mark - Main Logic
  85. - (void)requestLocationCode {
  86. NSString *path = [NSString stringWithFormat:API_GET_LOCATION_CODES];
  87. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[CommonCodeList class] completion:^(id responseObject) {
  88. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  89. return;
  90. }
  91. CommonCodeList *result = (CommonCodeList *) responseObject;
  92. if (result) {//API 성공 ,
  93. _locations = result.list;
  94. [self setDefaultLocation];
  95. }
  96. } failure:^(id errorObject) {
  97. JDErrorModel *error = (JDErrorModel *)errorObject;
  98. [[JDFacade facade] alert:error.errorMessage];
  99. }];
  100. }
  101. - (void)setDefaultLocation {
  102. for (CommonCode *code in _locations) {
  103. if ([code.commonCodeName isEqualToString:@"서울"]) {
  104. _weatherLocation = code;
  105. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  106. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  107. break;
  108. }
  109. }
  110. }
  111. - (NSString *)stringOfDay:(NSInteger)index {
  112. NSString *day = nil;
  113. switch (index) {
  114. case 0:
  115. day = NSLocalizedString(@"월", @"월");
  116. break;
  117. case 1:
  118. day = NSLocalizedString(@"화", @"화");
  119. break;
  120. case 2:
  121. day = NSLocalizedString(@"수", @"수");
  122. break;
  123. case 3:
  124. day = NSLocalizedString(@"목", @"목");
  125. break;
  126. case 4:
  127. day = NSLocalizedString(@"금", @"금");
  128. break;
  129. case 5:
  130. day = NSLocalizedString(@"토", @"토");
  131. break;
  132. case 6:
  133. day = NSLocalizedString(@"일", @"일");
  134. break;
  135. }
  136. return day;
  137. }
  138. - (NSString *)daysOfWeek {
  139. //dayOfWeek
  140. NSMutableString *days = [[NSMutableString alloc] init];
  141. NSInteger i = 0;
  142. for (CustomCheckBox *chk in _chkDays) {
  143. if (chk.checked) {
  144. NSString *prefix = [days isEmptyString] ? ksEmptyString : @", ";
  145. [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]];
  146. }
  147. i++;
  148. }
  149. return days;
  150. }
  151. - (NSString *)cmdClsValue {
  152. NSMutableString *days = [[NSMutableString alloc] init];
  153. for (CustomCheckBox *chk in _chkDays) {
  154. if (chk.checked) {
  155. NSString *prefix = [days isEmptyString] ? ksEmptyString : @",";
  156. [days appendFormat:@"%@%@", prefix, chk.value];
  157. }
  158. }
  159. return days;
  160. }
  161. #pragma mark - UI Events
  162. - (IBAction)btnSunRiseTouched:(id)sender {
  163. _btnSunRise.selected = YES;
  164. _btnSunSet.selected = NO;
  165. }
  166. - (IBAction)btnSunSetTouched:(id)sender {
  167. _btnSunRise.selected = NO;
  168. _btnSunSet.selected = YES;
  169. }
  170. - (void)lblCityTouched:(id)sender {
  171. if (!_cpopup) {
  172. _cpopup = [[WeatherLocationPopupView alloc] initFromNib];
  173. _cpopup.locations = _locations;
  174. }
  175. [_cpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  176. if (buttonIndex == 0) {
  177. _weatherLocation = _cpopup.weatherLocation;
  178. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  179. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  180. }
  181. }];
  182. }
  183. - (BOOL)validateCondition {
  184. //1.validate
  185. NSInteger count = 0;
  186. for (CustomCheckBox *chk in _chkDays) {
  187. if (chk.checked) {
  188. count++;
  189. }
  190. }
  191. //1. validate
  192. if (count == 0) {
  193. [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")];
  194. }
  195. return count;
  196. }
  197. - (IBAction)btnConfirmTouched:(id)sender {
  198. //validate
  199. if (![self validateCondition]) {
  200. return;
  201. }
  202. ItemSubModel *subItem = nil;
  203. if (_daylightTrigger.subItems && _daylightTrigger.subItems.count) {
  204. subItem = _daylightTrigger.subItems[0];
  205. } else {
  206. subItem = [[ItemSubModel alloc] init];
  207. _daylightTrigger.daylights = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  208. [_daylightTrigger.daylights addObject:subItem];
  209. }
  210. //daylights
  211. subItem.sourceId = _weatherLocation.commonCode;
  212. subItem.sourceName = _weatherLocation.commonCodeName;
  213. subItem.sourceSubId = _btnSunRise.selected ? @"sunriseUtcTime" : @"sunsetUtcTime";
  214. subItem.cmdclsValue = [self cmdClsValue];
  215. subItem.daysOfWeek = [self daysOfWeek];
  216. [super btnConfirmTouched:sender];
  217. }
  218. @end