DaylightPopupView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // DaylightPopupView.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 "DaylightPopupView.h"
  17. #import "RequestHandler.h"
  18. #import "TimePickerPopupView.h"
  19. @interface DaylightPopupView () {
  20. CommonCode *_weatherLocation;
  21. NSArray<CommonCode> *_locations;
  22. WeatherLocationPopupView *_cpopup;
  23. }
  24. @end
  25. @implementation DaylightPopupView
  26. - (id)initFromNib {
  27. for (UIView *view in [CommonUtil nibViews:@"DaylightPopupView"]) {
  28. if ([view isKindOfClass:[DaylightPopupView class]]) {
  29. self = (DaylightPopupView *)view;
  30. ((CustomCheckBox *)_chkDays[0]).value = ksDayOfWeekMON;
  31. ((CustomCheckBox *)_chkDays[1]).value = ksDayOfWeekTUE;
  32. ((CustomCheckBox *)_chkDays[2]).value = ksDayOfWeekWED;
  33. ((CustomCheckBox *)_chkDays[3]).value = ksDayOfWeekTHU;
  34. ((CustomCheckBox *)_chkDays[4]).value = ksDayOfWeekFRI;
  35. ((CustomCheckBox *)_chkDays[5]).value = ksDayOfWeekSAT;
  36. ((CustomCheckBox *)_chkDays[6]).value = ksDayOfWeekSUN;
  37. for (CustomCheckBox *chk in _chkDays) {
  38. [self alignTextAndImageOfButton:chk];
  39. }
  40. if (!_lblCity.touchHandler) {
  41. [_lblCity addTouchEventHandler:^(id label) {
  42. [self lblCityTouched:label];
  43. }];
  44. }
  45. //Localization
  46. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  47. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  48. }
  49. }
  50. return self;
  51. }
  52. - (void)alignTextAndImageOfButton:(UIButton *)button {
  53. // the space between the image and text
  54. CGFloat spacing = 6.0;
  55. float textMargin = 0;
  56. // get the size of the elements here for readability
  57. CGSize imageSize = button.imageView.image.size;
  58. CGSize titleSize = button.titleLabel.frame.size;
  59. CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit
  60. // lower the text and push it left to center it
  61. button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right
  62. // the text width might have changed (in case it was shortened before due to
  63. // lack of space and isn't anymore now), so we get the frame size again
  64. titleSize = button.titleLabel.bounds.size;
  65. button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right
  66. }
  67. - (void)setDaylightTrigger:(ItemModel *)daylightTrigger {
  68. _daylightTrigger = daylightTrigger;
  69. if (_daylightTrigger.itemSubTypeCode && [_daylightTrigger.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//기존 데이터가 있을 경우,
  70. ItemSubModel *subItem = _daylightTrigger.daylights[0];
  71. _btnSunRise.selected = [subItem.sourceSubId isEqualToString:@"sunriseUtcTime"];
  72. _btnSunSet.selected = !_btnSunRise.selected;
  73. } else {//is new
  74. _daylightTrigger.itemName = @"해뜰때 / 질때";
  75. _daylightTrigger.itemSubTypeCode = ksItemSubTypeCodeDaylight;
  76. _btnSunRise.selected = YES;
  77. for (CustomCheckBox *chk in _chkDays) {
  78. chk.checked = YES;
  79. }
  80. }
  81. }
  82. - (void)setRefConditions:(NSMutableArray<ItemModel> *)refConditions {
  83. _refConditions = refConditions;
  84. [TimePickerPopupView setRefConditions:_refConditions chkDays:_chkDays];
  85. }
  86. - (void)didMoveToSuperview {
  87. if (self.superview) {
  88. [self requestLocationCode];
  89. }
  90. }
  91. #pragma mark - Main Logic
  92. - (void)requestLocationCode {
  93. NSString *path = [NSString stringWithFormat:API_GET_LOCATION_CODES];
  94. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[CommonCodeList class] completion:^(id responseObject) {
  95. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  96. return;
  97. }
  98. CommonCodeList *result = (CommonCodeList *) responseObject;
  99. if (result) {//API 성공 ,
  100. _locations = result.list;
  101. [self setDefaultLocation];
  102. }
  103. } failure:^(id errorObject) {
  104. JDErrorModel *error = (JDErrorModel *)errorObject;
  105. [[JDFacade facade] alert:error.errorMessage];
  106. }];
  107. }
  108. - (void)setDefaultLocation {
  109. ItemSubModel *subItem = _daylightTrigger.daylights.firstObject;
  110. NSString *location = subItem && subItem.sourceName ? subItem.sourceName : @"서울";
  111. for (CommonCode *code in _locations) {
  112. if ([code.commonCodeName isEqualToString:location]) {
  113. _weatherLocation = code;
  114. [[JDFacade facade] setRadioButtonStatus:@YES object:code];
  115. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  116. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  117. break;
  118. }
  119. }
  120. }
  121. #pragma mark - UI Events
  122. - (IBAction)btnSunRiseTouched:(id)sender {
  123. _btnSunRise.selected = YES;
  124. _btnSunSet.selected = NO;
  125. }
  126. - (IBAction)btnSunSetTouched:(id)sender {
  127. _btnSunRise.selected = NO;
  128. _btnSunSet.selected = YES;
  129. }
  130. - (void)lblCityTouched:(id)sender {
  131. if (!_cpopup) {
  132. _cpopup = [[WeatherLocationPopupView alloc] initFromNib];
  133. _cpopup.locations = _locations;
  134. }
  135. [_cpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  136. if (buttonIndex == 0) {
  137. _weatherLocation = _cpopup.weatherLocation ? _cpopup.weatherLocation : _weatherLocation;
  138. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  139. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  140. }
  141. }];
  142. }
  143. - (IBAction)btnConfirmTouched:(id)sender {
  144. //validate
  145. if (![TimePickerPopupView validateCondition:_chkDays]) {
  146. return;
  147. }
  148. ItemSubModel *subItem = nil;
  149. if (_daylightTrigger.subItems && _daylightTrigger.subItems.count) {
  150. subItem = _daylightTrigger.subItems[0];
  151. } else {
  152. subItem = [[ItemSubModel alloc] init];
  153. _daylightTrigger.daylights = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  154. [_daylightTrigger.daylights addObject:subItem];
  155. }
  156. //daylights
  157. subItem.sourceId = _weatherLocation.commonCode;
  158. subItem.sourceName = _weatherLocation.commonCodeName;
  159. subItem.sourceSubId = _btnSunRise.selected ? @"sunriseUtcTime" : @"sunsetUtcTime";
  160. //set Days condition
  161. [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  162. [super btnConfirmTouched:sender];
  163. }
  164. @end