Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ServingsPickerView.m
- // KetoDiet
- //
- // Created by Антон Рыскалев on 09.04.2018.
- // Copyright © 2018 LAPPIR. All rights reserved.
- //
- #import "ServingsPickerView.h"
- #import "ExerciseCellViewModel.h"
- #import "KetoAppExercise.h"
- #import "KetoAppMeal.h"
- #import "KetoFood.h"
- #import "FoodCellViewModel.h"
- #import "KetoExercise+CoreDataClass.h"
- #import "KetoExerciseSession+CoreDataClass.h"
- #import "KetoMeal.h"
- @interface ServingsPickerView () <UIPickerViewDataSource, UIPickerViewDelegate>
- @property (strong, nonatomic) UIColor *selectorColor;
- @property (nonatomic,weak) id model;
- @property (nonatomic,assign) PickerInputType pickerType;
- @property (nonatomic,weak) NSNumber *quantityFirstValue;
- @property (nonatomic,weak) NSNumber *quantitySecondValue;
- @end
- static NSString *const unicodeVulgarFractionQuarter = @"\u00BC";
- static NSString *const unicodeVulgarFractionOneThirds = @"\u2153";
- static NSString *const unicodeVulgarFractionOneHalf = @"\u00BD";
- static NSString *const unicodeVulgarFractionTwoThirds = @"\u2154";
- static NSString *const unicodeVulgarFractionThreeQuarters = @"\u00BE";
- @implementation ServingsPickerView {
- NSArray *firstComponent;
- NSArray *secondComponent;
- NSArray *thirdComponent;
- //TimePicker
- float selectedTimeSeconds;
- float selectedTimeMinutes;
- float selectedTimeHours;
- //Activity/Water Picker
- NSInteger selectedUnitValue;
- NSString *selectedUnit;
- //FoodPicker
- float quantityServing;
- NSString *quantityDecimalServing;
- KetoServing *choosenServing;
- }
- -(instancetype)initWithPickerType:(PickerInputType)pickerType andModel:(id)model quantityFirstValue:(NSNumber*)quantityFirstValue quantitySecondValue:(NSNumber*)quantitySecondValue delegate:(id)pickerDelegate {
- self = [super init];//WithFrame:CGRectMake(0, self.frame.origin.y, 375, 333)];
- // self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- if (self) {
- _pickerType = pickerType;
- _model = model;
- _pickerDelegate = pickerDelegate;
- _quantityFirstValue = quantityFirstValue;
- _quantitySecondValue = quantitySecondValue;
- self.dataSource = self;
- self.delegate = self;
- self.selectorColor = [UIColor ketoBlueColor];
- [self setupData];
- }
- return self;
- }
- -(void)setupData {
- self.backgroundColor = [UIColor ketoWhiteColor];
- // [self setFrame:CGRectMake(0, self.frame.origin.y, self.frame.size.width, 333)];
- NSInteger selectedRowFirstComponent = 0;
- NSInteger selectedRowSecondComponent = 0;
- NSInteger selectedRowThirdComponent = 0;
- switch (_pickerType) {
- case TimePicker: {
- UILabel *hourLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2.f - self.frame.size.width / 4.f, self.frame.size.height / 2 - 15, 35, 30)];
- hourLabel.text = LocalizedString(@"hour", nil);
- hourLabel.textColor = [UIColor ketoBlueColor];
- hourLabel.font = [UIFont ketoSemiboldFontWithSize:15];
- hourLabel.alpha = 0.7;
- hourLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- [self addSubview:hourLabel];
- UILabel *minsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2.f + 5, self.frame.size.height / 2 - 15, 35, 30)];
- minsLabel.text = LocalizedString(@"min", nil);
- minsLabel.textColor = [UIColor ketoBlueColor];
- minsLabel.font = [UIFont ketoSemiboldFontWithSize:15];
- minsLabel.alpha = 0.7;
- minsLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- [self addSubview:minsLabel];
- UILabel *secsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2.f + self.frame.size.width / 4.f + 5, self.frame.size.height / 2 - 15, 35, 30)];
- secsLabel.text = LocalizedString(@"sec", nil);
- secsLabel.textColor = [UIColor ketoBlueColor];
- secsLabel.font = [UIFont ketoSemiboldFontWithSize:15];
- secsLabel.alpha = 0.7;
- secsLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
- [self addSubview:secsLabel];
- if ([_model isKindOfClass: [ExerciseCellViewModel class]]) {
- NSNumber *time = [[(ExerciseCellViewModel*)_model exercise] time];
- selectedRowSecondComponent = time.integerValue;
- selectedRowThirdComponent = @((time.floatValue - time.integerValue) * 60).integerValue;
- }
- else if ([_model isKindOfClass: [KetoExercise class]]) {
- if (_quantityFirstValue) {
- NSNumber *time = _quantityFirstValue;
- selectedRowSecondComponent = time.integerValue;
- selectedRowThirdComponent = @((time.floatValue - time.integerValue) * 60).integerValue;
- }
- else {
- NSNumber *time = [[(KetoExercise *)_model session] time];
- selectedRowSecondComponent = time.integerValue;
- selectedRowThirdComponent = @((time.floatValue - time.integerValue) * 60).integerValue;
- }
- }
- }
- break;
- case ActivityUnitValuePicker: {
- NSString *unit;
- NSString *choosenUnit;
- NSNumber *unitValue;
- if ([_model isKindOfClass:[KetoExercise class]]) {
- KetoExercise *exercise = _model;
- unit = exercise.unit;
- choosenUnit = exercise.session.choosenUnit;
- unitValue = exercise.session.unitValue;
- } else {
- KetoAppExercise *exercise = [(ExerciseCellViewModel*)_model exercise];
- unit = exercise.unit;
- choosenUnit = exercise.choosenUnit;
- unitValue = exercise.unitValue;
- }
- if (_quantitySecondValue)
- unitValue = _quantitySecondValue;
- if ([unit isEqual: @"mph"]) {
- secondComponent = [NSArray arrayWithObjects:LocalizedString(@"miles", nil),LocalizedString(@"kms", nil), nil];
- if ([choosenUnit isEqual: LocalizedString(@"mile", nil)])
- selectedRowSecondComponent = 0;
- else
- selectedRowSecondComponent = 1;
- }
- else if ([unit isEqual: LocalizedString(@"watt", nil)])
- secondComponent = [NSArray arrayWithObjects:LocalizedString(@"watt", nil), nil];
- else
- secondComponent = nil;
- thirdComponent = nil;
- selectedRowFirstComponent = unitValue.integerValue;
- }
- break;
- case WaterUnitValuePicker: {
- secondComponent = [NSArray arrayWithObjects:LocalizedString(@"ml", nil), LocalizedString(@"oz", nil), LocalizedString(@"cupShort", nil), nil];
- NSString *selectedUnit = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedWaterUnit"];
- if ([selectedUnit isEqualToString:LocalizedString(@"ml", nil)])
- selectedRowSecondComponent = 0;
- else if ([selectedUnit isEqualToString:LocalizedString(@"oz", nil)])
- selectedRowSecondComponent = 1;
- else
- selectedRowSecondComponent = 2;
- if (_quantityFirstValue)
- selectedRowFirstComponent = _quantityFirstValue.integerValue;
- thirdComponent = nil;
- }
- break;
- case FoodServingsPicker: {
- NSNumber *servingQuantity;
- NSArray <KetoServing*>* servings;
- KetoServing *choosenServing;
- KetoServing *defaultServing;
- if ([_model isKindOfClass:[KetoMeal class]]) {
- KetoMeal *meal = _model;
- servingQuantity = meal.servingQuantity;
- servings = [meal.food.servings allObjects];
- choosenServing = meal.choosenServing;
- defaultServing = meal.food.defaultServing;
- } else {
- KetoAppMeal *appMeal = [(FoodCellViewModel*)_model meal];
- servingQuantity = appMeal.servingQuantity;
- servings = [appMeal.food.servings allObjects];
- choosenServing = appMeal.choosenServing;
- defaultServing = appMeal.food.defaultServing;
- }
- secondComponent = [NSArray arrayWithObjects:@"", unicodeVulgarFractionQuarter, unicodeVulgarFractionOneThirds, unicodeVulgarFractionOneHalf, unicodeVulgarFractionTwoThirds, unicodeVulgarFractionThreeQuarters, nil];
- thirdComponent = [NSArray arrayWithArray:servings];
- if (_quantityFirstValue)
- servingQuantity = _quantityFirstValue;
- selectedRowFirstComponent = servingQuantity.integerValue;
- if (choosenServing)
- selectedRowThirdComponent = [thirdComponent indexOfObject:choosenServing];
- else
- selectedRowThirdComponent = [thirdComponent indexOfObject:defaultServing];
- float fractionPartQuantity = servingQuantity.floatValue - (float)servingQuantity.intValue;
- NSArray *fragnanceDecimalsArray = [NSArray arrayWithObjects:@(0),@(1.f/4.f),@(1.f/3.f),@(1.f/2.f),@(2.f/3.f),@(3.f/4.f), nil];
- NSNumber * index = @(fragnanceDecimalsArray.count - 1);
- for (NSNumber* decimal in fragnanceDecimalsArray) {
- if (decimal.floatValue > fractionPartQuantity) {
- index = @([fragnanceDecimalsArray indexOfObject:decimal]);
- break;
- }
- }
- if (index) {
- NSNumber *topValue = [fragnanceDecimalsArray objectAtIndex:index.integerValue];
- NSNumber *underValue = @(0);
- if (![index isEqual:@(0)])
- underValue = [fragnanceDecimalsArray objectAtIndex:index.integerValue - 1];
- NSNumber *n1 = @(fabsf(topValue.floatValue - fractionPartQuantity));
- NSNumber *n2 = @(fabsf(underValue.floatValue - fractionPartQuantity));
- if ([n1 compare:n2] == NSOrderedAscending)
- selectedRowSecondComponent = index.integerValue;
- else
- selectedRowSecondComponent = index.integerValue - 1;
- }
- }
- break;
- default:
- break;
- }
- [self selectRow:selectedRowFirstComponent inComponent:0 animated:NO];
- [self selectRow:selectedRowSecondComponent inComponent:1 animated:NO];
- if (thirdComponent || _pickerType == TimePicker)
- [self selectRow:selectedRowThirdComponent inComponent:2 animated:NO];
- [self pickerView:self didSelectRow:selectedRowFirstComponent inComponent:0];
- [self pickerView:self didSelectRow:selectedRowSecondComponent inComponent:1];
- if (thirdComponent || _pickerType == TimePicker)
- [self pickerView:self didSelectRow:selectedRowThirdComponent inComponent:2];
- [self reloadAllComponents];
- }
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- if (!thirdComponent && _pickerType != TimePicker)
- return 2;
- return 3;
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- if (_pickerType == TimePicker) {
- if (component == 0)
- return 24;
- else if (component == 1)
- return 1000;
- else if (component == 2)
- return 60;
- }
- if (_pickerType == ActivityUnitValuePicker) {
- if (component == 0)
- return 9999;
- else if (component == 1)
- return secondComponent.count;
- }
- if (_pickerType == WaterUnitValuePicker) {
- if (component == 0)
- return 2000;
- else if (component == 1)
- return secondComponent.count;
- }
- if (_pickerType == FoodServingsPicker) {
- if (component == 0)
- return 9999;
- else if (component == 1)
- return secondComponent.count;
- else if (component == 2)
- return thirdComponent.count;
- }
- return 60;
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
- {
- return 35;
- }
- - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- {
- // [[UILabel alloc] initWithFrame:CGRectMake(35, 0, self.frame.size.width/2 - 35, 42)];
- UILabel *columnView = _pickerType == TimePicker ? [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/3.f - self.frame.size.width/5.f, 52)] : [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/8.f , 52)];
- columnView.textAlignment = NSTextAlignmentLeft;
- columnView.font = [UIFont ketoDefaultFontWithSize:15];
- columnView.textColor = [UIColor ketoBlueColor];
- if (_pickerType == TimePicker || component == 0) {
- columnView.text = [NSString stringWithFormat:@"%lu", (long) row];
- if (_pickerType == TimePicker)
- columnView.textAlignment = NSTextAlignmentLeft;
- } else if (component == 1) {
- columnView.text = [NSString stringWithFormat:@"%@", secondComponent[row]];
- if (_pickerType == ActivityUnitValuePicker)
- columnView.frame = CGRectMake(0, 0, self.frame.size.width/4.f , 52);
- } else if (component == 2) {
- columnView.frame = CGRectMake(0, 0, self.frame.size.width/2.3f , 52);
- columnView.numberOfLines = 0;
- KetoServing* serving = thirdComponent[row];
- columnView.text = [NSString stringWithFormat:@"%@",serving.servingName];
- }
- return columnView;
- }
- -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- switch (_pickerType) {
- case TimePicker: {
- switch (component) {
- case 0:
- selectedTimeHours = (float)row;
- break;
- case 1:
- selectedTimeMinutes = (float)row;
- break;
- case 2:
- selectedTimeSeconds = (float)row;
- break;
- default:
- break;
- }
- NSNumber *timeValueInMinutes = @(selectedTimeMinutes + selectedTimeSeconds / 60 + selectedTimeHours * 60);
- [_pickerDelegate newTimeMinutesValue:timeValueInMinutes];
- }
- break;
- case ActivityUnitValuePicker: {
- switch (component) {
- case 0:
- selectedUnitValue = row;
- break;
- case 1:
- selectedUnit = secondComponent[row];
- break;
- default:
- break;
- }
- [_pickerDelegate newChoosenUnit:selectedUnit andValue:@(selectedUnitValue)];
- }
- break;
- case WaterUnitValuePicker: {
- switch (component) {
- case 0:
- selectedUnitValue = row;
- break;
- case 1:
- selectedUnit = secondComponent[row];
- break;
- default:
- break;
- }
- [_pickerDelegate newWaterValue:@(selectedUnitValue) withUnit:selectedUnit];
- }
- break;
- case FoodServingsPicker: {
- switch (component) {
- case 0:
- quantityServing = (float)row;
- break;
- case 1:
- quantityDecimalServing = secondComponent[row];
- break;
- case 2: {
- choosenServing = thirdComponent[row];
- if ([choosenServing.servingName isEqualToString:@"g"]) {
- [self selectRow:100 inComponent:0 animated:YES];
- [self pickerView:self didSelectRow:100 inComponent:0];
- }
- }
- break;
- default:
- break;
- }
- NSNumber *decimalQuantityServing;
- if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionQuarter])
- decimalQuantityServing = @(1.f/4.f);
- else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionOneThirds])
- decimalQuantityServing = @(1.f/3.f);
- else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionOneHalf])
- decimalQuantityServing = @(1.f/2.f);
- else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionTwoThirds])
- decimalQuantityServing = @(2.f/3.f);
- else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionThreeQuarters])
- decimalQuantityServing = @(3.f/4.f);
- else
- decimalQuantityServing = @(0);
- [_pickerDelegate newServing:choosenServing andQuantity:@(quantityServing + decimalQuantityServing.floatValue) andAdditionalDecimalQuantity:quantityDecimalServing];
- }
- break;
- default:
- break;
- }
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
- CGFloat width = self.frame.size.width;
- if (_pickerType == TimePicker) {
- return width / 4.f;
- } else if (_pickerType == FoodServingsPicker) {
- switch (component){
- case 0:
- return width / 9.f;//3.f - 80.f;
- case 1:
- return width / 9.f;//3.f - 80.f;
- case 2:
- return width / 2.3f;
- }
- }
- return width / 3.f;
- }
- -(void)didAddSubview:(UIView *)subview {
- [super didAddSubview:subview];
- if (self.selectorColor) {
- if (subview.bounds.size.height <= 1.0) {
- subview.backgroundColor = self.selectorColor;
- subview.alpha = 0.8f;
- }
- }
- }
- -(void)didMoveToWindow {
- [super didMoveToWindow];
- if (self.selectorColor) {
- for (UIView *subview in self.subviews) {
- if (subview.bounds.size.height <= 1.0) {
- subview.backgroundColor = self.selectorColor;
- subview.alpha = 0.8f;
- }
- }
- }
- }
- @end
- ////////////////////
- //
- // ServingsPickerView.h
- // KetoDiet
- //
- // Created by Антон Рыскалев on 09.04.2018.
- // Copyright © 2018 LAPPIR. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "KetoServing.h"
- typedef enum PickerInputType
- {
- TimePicker,
- ActivityUnitValuePicker,
- WaterUnitValuePicker,
- FoodServingsPicker
- } PickerInputType;
- @protocol ServingsPickerDelegate <NSObject>
- @optional
- -(void)newTimeMinutesValue:(NSNumber*)minutesValue;
- -(void)newChoosenUnit:(NSString*)unit andValue:(NSNumber*)unitValue;
- -(void)newServing:(KetoServing*)serving andQuantity:(NSNumber*)servingQuantity andAdditionalDecimalQuantity:(NSString*)quantityDecimalString;
- -(void)newWaterValue:(NSNumber*)waterValue withUnit:(NSString*)unitString;
- @end
- @interface ServingsPickerView : UIPickerView
- -(instancetype)initWithPickerType:(PickerInputType)pickerType andModel:(id)model quantityFirstValue:(NSNumber*)quantityFirstValue quantitySecondValue:(NSNumber*)quantitySecondValue delegate:(id)pickerDelegate;
- @property (nonatomic, weak) id<ServingsPickerDelegate> pickerDelegate;
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement