DaylightPopupView.m 8.0 KB

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