ExternHeatPopupView.m 8.4 KB

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