ExternHeatPopupView.m 9.4 KB

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