DaylightPopupView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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)didMoveToSuperview {
  83. [self requestLocationCode];
  84. }
  85. #pragma mark - Main Logic
  86. - (void)requestLocationCode {
  87. NSString *path = [NSString stringWithFormat:API_GET_LOCATION_CODES];
  88. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[CommonCodeList class] completion:^(id responseObject) {
  89. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  90. return;
  91. }
  92. CommonCodeList *result = (CommonCodeList *) responseObject;
  93. if (result) {//API 성공 ,
  94. _locations = result.list;
  95. [self setDefaultLocation];
  96. }
  97. } failure:^(id errorObject) {
  98. JDErrorModel *error = (JDErrorModel *)errorObject;
  99. [[JDFacade facade] alert:error.errorMessage];
  100. }];
  101. }
  102. - (void)setDefaultLocation {
  103. for (CommonCode *code in _locations) {
  104. if ([code.commonCodeName isEqualToString:@"서울"]) {
  105. _weatherLocation = code;
  106. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  107. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  108. break;
  109. }
  110. }
  111. }
  112. #pragma mark - UI Events
  113. - (IBAction)btnSunRiseTouched:(id)sender {
  114. _btnSunRise.selected = YES;
  115. _btnSunSet.selected = NO;
  116. }
  117. - (IBAction)btnSunSetTouched:(id)sender {
  118. _btnSunRise.selected = NO;
  119. _btnSunSet.selected = YES;
  120. }
  121. - (void)lblCityTouched:(id)sender {
  122. if (!_cpopup) {
  123. _cpopup = [[WeatherLocationPopupView alloc] initFromNib];
  124. _cpopup.locations = _locations;
  125. }
  126. [_cpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  127. if (buttonIndex == 0) {
  128. _weatherLocation = _cpopup.weatherLocation;
  129. _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName];
  130. [_lblCity setUnderLine:_weatherLocation.commonCodeName];
  131. }
  132. }];
  133. }
  134. - (IBAction)btnConfirmTouched:(id)sender {
  135. //validate
  136. if (![TimePickerPopupView validateCondition:_chkDays]) {
  137. return;
  138. }
  139. ItemSubModel *subItem = nil;
  140. if (_daylightTrigger.subItems && _daylightTrigger.subItems.count) {
  141. subItem = _daylightTrigger.subItems[0];
  142. } else {
  143. subItem = [[ItemSubModel alloc] init];
  144. _daylightTrigger.daylights = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] init];
  145. [_daylightTrigger.daylights addObject:subItem];
  146. }
  147. //daylights
  148. subItem.sourceId = _weatherLocation.commonCode;
  149. subItem.sourceName = _weatherLocation.commonCodeName;
  150. subItem.sourceSubId = _btnSunRise.selected ? @"sunriseUtcTime" : @"sunsetUtcTime";
  151. //set Days condition
  152. [TimePickerPopupView setDaysCondition:_refConditions chkDays:_chkDays];
  153. [super btnConfirmTouched:sender];
  154. }
  155. @end