DaylightPopupView.m 8.7 KB

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