ExternHeatPopupView.m 9.7 KB

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