ExternHeatPopupView.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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:@"05"];
  80. _btnCold.selected = !_btnHot.selected;
  81. } else {//is new
  82. _externHeatTrigger.itemName = @"더울때 / 추울때";
  83. _externHeatTrigger.itemSubTypeCode = ksItemSubTypeCodeHeat;
  84. _btnHot.selected = YES;
  85. _temperature = @"0";
  86. _lblHeat.text = @"0℃ 보다";
  87. [_lblHeat setUnderLine:@"0℃"];
  88. for (CustomCheckBox *chk in _chkDays) {
  89. chk.checked = YES;
  90. }
  91. }
  92. }
  93. - (void)didMoveToSuperview {
  94. [self requestLocationCode];
  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. for (CommonCode *code in _locations) {
  115. if ([code.commonCodeName isEqualToString:@"서울"]) {
  116. _weatherLocation = code;
  117. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  118. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  119. break;
  120. }
  121. }
  122. }
  123. #pragma mark - UI Events
  124. - (IBAction)btnHotTouched:(id)sender {
  125. _btnHot.selected = YES;
  126. _btnCold.selected = NO;
  127. }
  128. - (IBAction)btnColdTouched:(id)sender {
  129. _btnHot.selected = NO;
  130. _btnCold.selected = YES;
  131. }
  132. - (void)lblCityTouched:(id)sender {
  133. if (!_cpopup) {
  134. _cpopup = [[WeatherLocationPopupView alloc] initFromNib];
  135. _cpopup.locations = _locations;
  136. }
  137. [_cpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  138. if (buttonIndex == 0) {
  139. _weatherLocation = _cpopup.weatherLocation;
  140. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  141. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  142. }
  143. }];
  144. }
  145. - (void)lblHeatTouched:(id)sender {
  146. if (!_tpopup) {
  147. _tpopup = [[TemperaturePopupView alloc] initFromNib];
  148. }
  149. [_tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  150. if (buttonIndex == 0) {//OK
  151. _temperature = _tpopup.temperature;
  152. NSString *ftemp = [NSString stringWithFormat:@"%@℃", _temperature];
  153. _lblHeat.text = [NSString stringWithFormat:@"%@ 보다", ftemp];
  154. [_lblHeat setUnderLine:ftemp];
  155. }
  156. }];
  157. }
  158. - (IBAction)btnConfirmTouched:(id)sender {
  159. //validate
  160. if (![TimePickerPopupView validateCondition:_chkDays]) {
  161. return;
  162. }
  163. ItemSubModel *subItem = nil;
  164. if (_externHeatTrigger.subItems && _externHeatTrigger.subItems.count) {
  165. subItem = _externHeatTrigger.subItems[0];
  166. } else {
  167. subItem = [[ItemSubModel alloc] init];
  168. _externHeatTrigger.heats = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  169. [_externHeatTrigger.heats addObject:subItem];
  170. }
  171. //externHeats
  172. subItem.sourceId = _weatherLocation.commonCode;
  173. subItem.sourceName = _weatherLocation.commonCodeName;
  174. subItem.conditionTypeCode = _btnHot.selected ? @"05" : @"03";
  175. subItem.cmdclsValue = _temperature;
  176. //set Days condition
  177. [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  178. [super btnConfirmTouched:sender];
  179. }
  180. @end