ExternHeatPopupView.m 10 KB

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