ExternHeatPopupView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. for (NSInteger i = 0 ; i < WEEKDAYS.count ; i++) {
  105. if ([subItem.dayofweek containsString: [WEEKDAYS objectAtIndex:i]]) {
  106. CustomCheckBox *chk = [_chkDays objectAtIndex:i];
  107. chk.checked = YES;
  108. }
  109. }
  110. } else {//is new
  111. _externHeatTrigger.itemName = @"더울때 / 추울때";
  112. _externHeatTrigger.itemSubTypeCode = ksItemSubTypeCodeHeat;
  113. _btnOver.selected = YES;
  114. _btnZeroDegree.selected = NO;
  115. _temperature = @"0";
  116. _lblHeat.text = @"0℃ 보다";
  117. [_lblHeat setUnderLine:@"0℃"];
  118. // for (CustomCheckBox *chk in _chkDays) {
  119. // chk.checked = YES;
  120. // }
  121. }
  122. }
  123. - (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions {
  124. _refConditions = refConditions;
  125. [TimePickerPopupView setRefConditions:_refConditions chkDays:_chkDays];
  126. }
  127. - (void)didMoveToSuperview {
  128. [self requestLocationCode];
  129. }
  130. #pragma mark - Main Logic
  131. - (void)requestLocationCode {
  132. NSString *path = [NSString stringWithFormat:API_GET_LOCATION_CODES];
  133. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[CommonCodeList class] completion:^(id responseObject) {
  134. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  135. return;
  136. }
  137. CommonCodeList *result = (CommonCodeList *) responseObject;
  138. if (result) {//API 성공 ,
  139. _locations = result.list;
  140. [self setDefaultLocation];
  141. }
  142. } failure:^(id errorObject) {
  143. JDErrorModel *error = (JDErrorModel *)errorObject;
  144. [[JDFacade facade] alert:error.errorMessage];
  145. }];
  146. }
  147. - (void)setDefaultLocation {
  148. ItemSubModel *subItem = _externHeatTrigger.heats.firstObject;
  149. NSString *location = subItem && subItem.sourceName ? subItem.sourceName : @"서울";
  150. for (CommonCode *code in _locations) {
  151. if ([code.commonCodeName isEqualToString:location]) {
  152. _weatherLocation = code;
  153. [[JDFacade facade] setRadioButtonStatus:@YES object:code];
  154. _lblCity.text = [NSString stringWithFormat:@"%@ 지역이", _weatherLocation.commonCodeName];
  155. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  156. break;
  157. }
  158. }
  159. }
  160. #pragma mark - UI Events
  161. - (IBAction)changeTempPlusMinus:(id)sender {
  162. UIButton *btn = sender;
  163. btn.selected = !btn.selected;
  164. }
  165. - (IBAction)btnActionBelowOver:(id)sender {
  166. _btnBelow.selected = NO;
  167. _btnOver.selected = NO;
  168. ((CustomButton *)sender).selected = YES;
  169. }
  170. - (void)lblCityTouched:(id)sender {
  171. if (!_cpopup) {
  172. _cpopup = [[WeatherLocationPopupView alloc] initFromNib];
  173. _cpopup.locations = _locations;
  174. }
  175. [_cpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  176. if (buttonIndex == 0) {
  177. _weatherLocation = _cpopup.weatherLocation ? _cpopup.weatherLocation : _weatherLocation;
  178. _lblCity.text = [NSString stringWithFormat:@"%@ 지역이", _weatherLocation.commonCodeName];
  179. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  180. }
  181. }];
  182. }
  183. - (void)lblHeatTouched:(id)sender {
  184. if (!_tpopup) {
  185. _tpopup = [[TemperaturePopupView alloc] initFromNib];
  186. }
  187. ItemSubModel *subItem = _externHeatTrigger.heats.firstObject;
  188. _tpopup.temperature = subItem && subItem.cmdclsValue ? subItem.cmdclsValue : nil;
  189. [_tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  190. if (buttonIndex == 0) {//OK
  191. _temperature = _tpopup.temperature;
  192. NSString *ftemp = [NSString stringWithFormat:@"%@℃", _temperature];
  193. _lblHeat.text = [NSString stringWithFormat:@"%@ 보다", ftemp];
  194. [_lblHeat setUnderLine:ftemp];
  195. }
  196. }];
  197. }
  198. - (IBAction)btnConfirmTouched:(id)sender {
  199. //validate
  200. if (![TimePickerPopupView validateCondition:_chkDays]) {
  201. return;
  202. }
  203. if (_tfTemper.text.trim.length == 0) {
  204. return;
  205. }
  206. ItemSubModel *subItem = nil;
  207. if (_externHeatTrigger.subItems && _externHeatTrigger.subItems.count) {
  208. subItem = _externHeatTrigger.subItems[0];
  209. }
  210. else {
  211. subItem = [[ItemSubModel alloc] init];
  212. _externHeatTrigger.heats = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  213. [_externHeatTrigger.heats addObject:subItem];
  214. }
  215. _temperature = [NSString stringWithFormat:@"%@%@",
  216. _btnZeroDegree.selected ? @"-" : @"+",
  217. _tfTemper.text];
  218. //externHeats
  219. subItem.sourceId = _weatherLocation.commonCode;
  220. subItem.sourceName = _weatherLocation.commonCodeName;
  221. subItem.conditionTypeCode = _btnOver.selected ? ksConditionTypeCodeGreatOrEqual : ksConditionTypeCodeLessOrEqual;
  222. subItem.cmdclsValue = _temperature;
  223. subItem.dayofweek = [TimePickerPopupView daysOfWeek:_chkDays];
  224. //set Days condition
  225. //[TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  226. [super btnConfirmTouched:sender];
  227. }
  228. @end