ExternHeatPopupView.m 8.4 KB

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