// // ExternHeatPopupView.m // kneet2 // // Created by Jason Lee on 11/24/15. // Copyright (c) 2015 ntels. All rights reserved. // #import "JDObject.h" #import "ItemModel.h" #import "JDJSONModel.h" #import "CustomLabel.h" #import "CustomButton.h" #import "CustomCheckBox.h" #import "CommonUtil.h" #import "WeatherLocationPopupView.h" #import "ExternHeatPopupView.h" #import "RequestHandler.h" @interface ExternHeatPopupView () { CommonCode *_weatherLocation; NSArray *_locations; WeatherLocationPopupView *_cpopup; } @end @implementation ExternHeatPopupView - (id)initFromNib { for (UIView *view in [CommonUtil nibViews:@"ExternHeatPopupView"]) { if ([view isKindOfClass:[ExternHeatPopupView class]]) { self = (ExternHeatPopupView *)view; ((CustomCheckBox *)_chkDays[0]).value = ksDayOfWeekMON; ((CustomCheckBox *)_chkDays[1]).value = ksDayOfWeekTUE; ((CustomCheckBox *)_chkDays[2]).value = ksDayOfWeekWED; ((CustomCheckBox *)_chkDays[3]).value = ksDayOfWeekTHU; ((CustomCheckBox *)_chkDays[4]).value = ksDayOfWeekFRI; ((CustomCheckBox *)_chkDays[5]).value = ksDayOfWeekSAT; ((CustomCheckBox *)_chkDays[6]).value = ksDayOfWeekSUN; for (CustomCheckBox *chk in _chkDays) { [self alignTextAndImageOfButton:chk]; } if (!_lblCity.touchHandler) { [_lblCity addTouchEventHandler:^(id label) { [self lblCityTouched:label]; }]; } if (!_lblHeat.touchHandler) { [_lblHeat addTouchEventHandler:^(id label) { [self lblHeatTouched:label]; }]; } //Localization [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal]; [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal]; } } return self; } - (void)alignTextAndImageOfButton:(UIButton *)button { // the space between the image and text CGFloat spacing = 6.0; float textMargin = 0; // get the size of the elements here for readability CGSize imageSize = button.imageView.image.size; CGSize titleSize = button.titleLabel.frame.size; CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // get the height they will take up as a unit // lower the text and push it left to center it button.titleEdgeInsets = UIEdgeInsetsMake( 0.0, -imageSize.width +textMargin, - (totalHeight - titleSize.height), +textMargin ); // top, left, bottom, right // the text width might have changed (in case it was shortened before due to // lack of space and isn't anymore now), so we get the frame size again titleSize = button.titleLabel.bounds.size; button.imageEdgeInsets = UIEdgeInsetsMake(25, 0.0, 0.0, -titleSize.width ); // top, left, bottom, right } - (void)setExternHeatTrigger:(ItemModel *)externHeatTrigger { _externHeatTrigger = externHeatTrigger; if (_externHeatTrigger.itemSubTypeCode && [_externHeatTrigger.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//기존 데이터가 있을 경우, ItemSubModel *subItem = _externHeatTrigger.heats[0]; _btnHot.selected = [subItem.conditionTypeCode isEqualToString:@"05"]; _btnCold.selected = !_btnHot.selected; } else {//is new _externHeatTrigger.itemName = @"더울때 / 추울때"; _externHeatTrigger.itemSubTypeCode = ksItemSubTypeCodeHeat; _btnHot.selected = YES; for (CustomCheckBox *chk in _chkDays) { chk.checked = YES; } } } - (void)didMoveToSuperview { [self requestLocationCode]; } #pragma mark - Main Logic - (void)requestLocationCode { NSString *path = [NSString stringWithFormat:API_GET_LOCATION_CODES]; [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[CommonCodeList class] completion:^(id responseObject) { if (!responseObject) {//응답결과가 잘못되었거나 없을 경우, return; } CommonCodeList *result = (CommonCodeList *) responseObject; if (result) {//API 성공 , _locations = result.list; [self setDefaultLocation]; } } failure:^(id errorObject) { JDErrorModel *error = (JDErrorModel *)errorObject; [[JDFacade facade] alert:error.errorMessage]; }]; } - (void)setDefaultLocation { for (CommonCode *code in _locations) { if ([code.commonCodeName isEqualToString:@"서울"]) { _weatherLocation = code; _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName]; [_lblCity setUnderLine:_weatherLocation.commonCodeName]; break; } } } - (NSString *)stringOfDay:(NSInteger)index { NSString *day = nil; switch (index) { case 0: day = NSLocalizedString(@"월", @"월"); break; case 1: day = NSLocalizedString(@"화", @"화"); break; case 2: day = NSLocalizedString(@"수", @"수"); break; case 3: day = NSLocalizedString(@"목", @"목"); break; case 4: day = NSLocalizedString(@"금", @"금"); break; case 5: day = NSLocalizedString(@"토", @"토"); break; case 6: day = NSLocalizedString(@"일", @"일"); break; } return day; } - (NSString *)daysOfWeek { //dayOfWeek NSMutableString *days = [[NSMutableString alloc] init]; NSInteger i = 0; for (CustomCheckBox *chk in _chkDays) { if (chk.checked) { NSString *prefix = [days isEmptyString] ? ksEmptyString : @", "; [days appendFormat:@"%@%@", prefix, [self stringOfDay:i]]; } i++; } return days; } - (NSString *)cmdClsValue { NSMutableString *days = [[NSMutableString alloc] init]; for (CustomCheckBox *chk in _chkDays) { if (chk.checked) { NSString *prefix = [days isEmptyString] ? ksEmptyString : @","; [days appendFormat:@"%@%@", prefix, chk.value]; } } return days; } #pragma mark - UI Events - (IBAction)btnHotTouched:(id)sender { _btnHot.selected = YES; _btnCold.selected = NO; } - (IBAction)btnColdTouched:(id)sender { _btnHot.selected = NO; _btnCold.selected = YES; } - (void)lblCityTouched:(id)sender { if (!_cpopup) { _cpopup = [[WeatherLocationPopupView alloc] initFromNib]; _cpopup.locations = _locations; } [_cpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 0) { _weatherLocation = _cpopup.weatherLocation; _lblCity.text = [NSString stringWithFormat:@"%@ 지역에", _weatherLocation.commonCodeName]; [_lblCity setUnderLine:_weatherLocation.commonCodeName]; } }]; } - (void)lblHeatTouched:(id)sender { } - (BOOL)validateCondition { //1.validate NSInteger count = 0; for (CustomCheckBox *chk in _chkDays) { if (chk.checked) { count++; } } //1. validate if (count == 0) { [[JDFacade facade] alert:NSLocalizedString(@"요일을 선택해주세요", @"요일을 선택해주세요")]; } return count; } - (IBAction)btnConfirmTouched:(id)sender { //validate if (![self validateCondition]) { return; } ItemSubModel *subItem = nil; if (_externHeatTrigger.subItems && _externHeatTrigger.subItems.count) { subItem = _externHeatTrigger.subItems[0]; } else { subItem = [[ItemSubModel alloc] init]; _externHeatTrigger.heats = (NSMutableArray *)[[NSMutableArray alloc] init]; [_externHeatTrigger.heats addObject:subItem]; } //daylights subItem.sourceId = _weatherLocation.commonCode; subItem.sourceName = _weatherLocation.commonCodeName; subItem.cmdclsValue = [self cmdClsValue]; //온도값. subItem.daysOfWeek = [self daysOfWeek]; [super btnConfirmTouched:sender]; } @end