Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ServingsPickerView.m
  3. //  KetoDiet
  4. //
  5. //  Created by Антон Рыскалев on 09.04.2018.
  6. //  Copyright © 2018 LAPPIR. All rights reserved.
  7. //
  8.  
  9. #import "ServingsPickerView.h"
  10. #import "ExerciseCellViewModel.h"
  11. #import "KetoAppExercise.h"
  12. #import "KetoAppMeal.h"
  13. #import "KetoFood.h"
  14. #import "FoodCellViewModel.h"
  15. #import "KetoExercise+CoreDataClass.h"
  16. #import "KetoExerciseSession+CoreDataClass.h"
  17. #import "KetoMeal.h"
  18.  
  19. @interface ServingsPickerView () <UIPickerViewDataSource, UIPickerViewDelegate>
  20.  
  21. @property (strong, nonatomic) UIColor *selectorColor;
  22.  
  23. @property (nonatomic,weak) id model;
  24. @property (nonatomic,assign) PickerInputType pickerType;
  25. @property (nonatomic,weak) NSNumber *quantityFirstValue;
  26. @property (nonatomic,weak) NSNumber *quantitySecondValue;
  27.  
  28. @end
  29.  
  30. static NSString *const unicodeVulgarFractionQuarter = @"\u00BC";
  31. static NSString *const unicodeVulgarFractionOneThirds = @"\u2153";
  32. static NSString *const unicodeVulgarFractionOneHalf = @"\u00BD";
  33. static NSString *const unicodeVulgarFractionTwoThirds = @"\u2154";
  34. static NSString *const unicodeVulgarFractionThreeQuarters = @"\u00BE";
  35.  
  36. @implementation ServingsPickerView {
  37.     NSArray *firstComponent;
  38.     NSArray *secondComponent;
  39.     NSArray *thirdComponent;
  40.    
  41.     //TimePicker
  42.     float selectedTimeSeconds;
  43.     float selectedTimeMinutes;
  44.     float selectedTimeHours;
  45.    
  46.     //Activity/Water Picker
  47.     NSInteger selectedUnitValue;
  48.     NSString *selectedUnit;
  49.    
  50.     //FoodPicker
  51.     float quantityServing;
  52.     NSString *quantityDecimalServing;
  53.     KetoServing *choosenServing;
  54. }
  55.  
  56. -(instancetype)initWithPickerType:(PickerInputType)pickerType andModel:(id)model quantityFirstValue:(NSNumber*)quantityFirstValue quantitySecondValue:(NSNumber*)quantitySecondValue delegate:(id)pickerDelegate {
  57.     self = [super init];//WithFrame:CGRectMake(0, self.frame.origin.y, 375, 333)];
  58. //    self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  59.     if (self) {
  60.         _pickerType = pickerType;
  61.         _model = model;
  62.         _pickerDelegate = pickerDelegate;
  63.         _quantityFirstValue = quantityFirstValue;
  64.         _quantitySecondValue = quantitySecondValue;
  65.         self.dataSource = self;
  66.         self.delegate = self;
  67.         self.selectorColor = [UIColor ketoBlueColor];
  68.         [self setupData];
  69.     }
  70.     return self;
  71. }
  72.  
  73. -(void)setupData {
  74.     self.backgroundColor = [UIColor ketoWhiteColor];
  75. //    [self setFrame:CGRectMake(0, self.frame.origin.y, self.frame.size.width, 333)];
  76.    
  77.     NSInteger selectedRowFirstComponent = 0;
  78.     NSInteger selectedRowSecondComponent = 0;
  79.     NSInteger selectedRowThirdComponent = 0;
  80.    
  81.     switch (_pickerType) {
  82.         case TimePicker: {
  83.             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)];
  84.             hourLabel.text = LocalizedString(@"hour", nil);
  85.             hourLabel.textColor = [UIColor ketoBlueColor];
  86.             hourLabel.font = [UIFont ketoSemiboldFontWithSize:15];
  87.             hourLabel.alpha = 0.7;
  88.             hourLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  89.             [self addSubview:hourLabel];
  90.            
  91.             UILabel *minsLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2.f + 5, self.frame.size.height / 2 - 15, 35, 30)];
  92.             minsLabel.text = LocalizedString(@"min", nil);
  93.             minsLabel.textColor = [UIColor ketoBlueColor];
  94.             minsLabel.font = [UIFont ketoSemiboldFontWithSize:15];
  95.             minsLabel.alpha = 0.7;
  96.             minsLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  97.             [self addSubview:minsLabel];
  98.            
  99.             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)];
  100.             secsLabel.text = LocalizedString(@"sec", nil);
  101.             secsLabel.textColor = [UIColor ketoBlueColor];
  102.             secsLabel.font = [UIFont ketoSemiboldFontWithSize:15];
  103.             secsLabel.alpha = 0.7;
  104.             secsLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  105.             [self addSubview:secsLabel];
  106.            
  107.             if ([_model isKindOfClass: [ExerciseCellViewModel class]]) {
  108.                 NSNumber *time = [[(ExerciseCellViewModel*)_model exercise] time];
  109.                 selectedRowSecondComponent = time.integerValue;
  110.                 selectedRowThirdComponent = @((time.floatValue - time.integerValue) * 60).integerValue;
  111.             }
  112.             else if ([_model isKindOfClass: [KetoExercise class]]) {
  113.                 if (_quantityFirstValue) {
  114.                     NSNumber *time = _quantityFirstValue;
  115.                     selectedRowSecondComponent = time.integerValue;
  116.                     selectedRowThirdComponent = @((time.floatValue - time.integerValue) * 60).integerValue;
  117.                 }
  118.                 else {
  119.                     NSNumber *time = [[(KetoExercise *)_model session] time];
  120.                     selectedRowSecondComponent = time.integerValue;
  121.                     selectedRowThirdComponent = @((time.floatValue - time.integerValue) * 60).integerValue;
  122.                 }
  123.             }
  124.         }
  125.             break;
  126.         case ActivityUnitValuePicker: {
  127.             NSString *unit;
  128.             NSString *choosenUnit;
  129.             NSNumber *unitValue;
  130.             if ([_model isKindOfClass:[KetoExercise class]]) {
  131.                 KetoExercise *exercise = _model;
  132.                 unit = exercise.unit;
  133.                 choosenUnit = exercise.session.choosenUnit;
  134.                 unitValue = exercise.session.unitValue;
  135.             } else {
  136.                 KetoAppExercise *exercise = [(ExerciseCellViewModel*)_model exercise];
  137.                 unit = exercise.unit;
  138.                 choosenUnit = exercise.choosenUnit;
  139.                 unitValue = exercise.unitValue;
  140.             }
  141.            
  142.             if (_quantitySecondValue)
  143.                 unitValue = _quantitySecondValue;
  144.            
  145.             if ([unit  isEqual: @"mph"]) {
  146.                 secondComponent = [NSArray arrayWithObjects:LocalizedString(@"miles", nil),LocalizedString(@"kms", nil), nil];
  147.                 if ([choosenUnit  isEqual: LocalizedString(@"mile", nil)])
  148.                     selectedRowSecondComponent = 0;
  149.                 else
  150.                     selectedRowSecondComponent = 1;
  151.             }
  152.             else if ([unit  isEqual: LocalizedString(@"watt", nil)])
  153.                 secondComponent = [NSArray arrayWithObjects:LocalizedString(@"watt", nil), nil];
  154.             else
  155.                 secondComponent = nil;
  156.             thirdComponent = nil;
  157.            
  158.             selectedRowFirstComponent = unitValue.integerValue;
  159.            
  160.         }
  161.             break;
  162.         case WaterUnitValuePicker: {
  163.             secondComponent = [NSArray arrayWithObjects:LocalizedString(@"ml", nil), LocalizedString(@"oz", nil), LocalizedString(@"cupShort", nil), nil];
  164.             NSString *selectedUnit = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedWaterUnit"];
  165.             if ([selectedUnit isEqualToString:LocalizedString(@"ml", nil)])
  166.                 selectedRowSecondComponent = 0;
  167.             else if ([selectedUnit isEqualToString:LocalizedString(@"oz", nil)])
  168.                 selectedRowSecondComponent = 1;
  169.             else
  170.                 selectedRowSecondComponent = 2;
  171.            
  172.             if (_quantityFirstValue)
  173.                 selectedRowFirstComponent = _quantityFirstValue.integerValue;
  174.             thirdComponent = nil;
  175.         }
  176.             break;
  177.         case FoodServingsPicker: {
  178.             NSNumber *servingQuantity;
  179.             NSArray <KetoServing*>* servings;
  180.             KetoServing *choosenServing;
  181.             KetoServing *defaultServing;
  182.            
  183.             if ([_model isKindOfClass:[KetoMeal class]]) {
  184.                 KetoMeal *meal = _model;
  185.                 servingQuantity = meal.servingQuantity;
  186.                 servings = [meal.food.servings allObjects];
  187.                 choosenServing = meal.choosenServing;
  188.                 defaultServing = meal.food.defaultServing;
  189.             } else {
  190.                 KetoAppMeal *appMeal = [(FoodCellViewModel*)_model meal];
  191.                 servingQuantity = appMeal.servingQuantity;
  192.                 servings = [appMeal.food.servings allObjects];
  193.                 choosenServing = appMeal.choosenServing;
  194.                 defaultServing = appMeal.food.defaultServing;
  195.             }
  196.            
  197.             secondComponent = [NSArray arrayWithObjects:@"", unicodeVulgarFractionQuarter, unicodeVulgarFractionOneThirds, unicodeVulgarFractionOneHalf, unicodeVulgarFractionTwoThirds, unicodeVulgarFractionThreeQuarters, nil];
  198.             thirdComponent = [NSArray arrayWithArray:servings];
  199.  
  200.             if (_quantityFirstValue)
  201.                 servingQuantity = _quantityFirstValue;
  202.            
  203.             selectedRowFirstComponent = servingQuantity.integerValue;
  204.             if (choosenServing)
  205.                 selectedRowThirdComponent = [thirdComponent indexOfObject:choosenServing];
  206.             else
  207.                 selectedRowThirdComponent = [thirdComponent indexOfObject:defaultServing];
  208.            
  209.             float fractionPartQuantity = servingQuantity.floatValue - (float)servingQuantity.intValue;
  210.            
  211.             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];
  212.            
  213.             NSNumber * index = @(fragnanceDecimalsArray.count - 1);
  214.            
  215.             for (NSNumber* decimal in fragnanceDecimalsArray) {
  216.                 if (decimal.floatValue > fractionPartQuantity) {
  217.                     index = @([fragnanceDecimalsArray indexOfObject:decimal]);
  218.                     break;
  219.                 }
  220.             }
  221.            
  222.             if (index) {
  223.                 NSNumber *topValue = [fragnanceDecimalsArray objectAtIndex:index.integerValue];
  224.                 NSNumber *underValue = @(0);
  225.                 if (![index isEqual:@(0)])
  226.                     underValue = [fragnanceDecimalsArray objectAtIndex:index.integerValue - 1];
  227.                
  228.                 NSNumber *n1 = @(fabsf(topValue.floatValue - fractionPartQuantity));
  229.                 NSNumber *n2 = @(fabsf(underValue.floatValue - fractionPartQuantity));
  230.                
  231.                 if ([n1 compare:n2] == NSOrderedAscending)
  232.                     selectedRowSecondComponent = index.integerValue;
  233.                 else
  234.                     selectedRowSecondComponent = index.integerValue - 1;
  235.             }
  236.            
  237.         }
  238.             break;
  239.         default:
  240.             break;
  241.     }
  242.    
  243.     [self selectRow:selectedRowFirstComponent inComponent:0 animated:NO];
  244.     [self selectRow:selectedRowSecondComponent inComponent:1 animated:NO];
  245.     if (thirdComponent || _pickerType == TimePicker)
  246.         [self selectRow:selectedRowThirdComponent inComponent:2 animated:NO];
  247.  
  248.    
  249.     [self pickerView:self didSelectRow:selectedRowFirstComponent inComponent:0];
  250.     [self pickerView:self didSelectRow:selectedRowSecondComponent inComponent:1];
  251.     if (thirdComponent || _pickerType == TimePicker)
  252.         [self pickerView:self didSelectRow:selectedRowThirdComponent inComponent:2];
  253.    
  254.     [self reloadAllComponents];
  255. }
  256.  
  257. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  258. {
  259.    
  260.     if (!thirdComponent && _pickerType != TimePicker)
  261.         return 2;
  262.    
  263.     return 3;
  264. }
  265.  
  266. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  267. {
  268.     if (_pickerType == TimePicker) {
  269.         if (component == 0)
  270.             return 24;
  271.         else if (component == 1)
  272.             return 1000;
  273.         else if (component == 2)
  274.             return 60;
  275.     }
  276.    
  277.     if (_pickerType == ActivityUnitValuePicker) {
  278.         if (component == 0)
  279.             return 9999;
  280.         else if (component == 1)
  281.             return secondComponent.count;
  282.     }
  283.    
  284.     if (_pickerType == WaterUnitValuePicker) {
  285.         if (component == 0)
  286.             return 2000;
  287.         else if (component == 1)
  288.             return secondComponent.count;
  289.     }
  290.    
  291.     if (_pickerType == FoodServingsPicker) {
  292.         if (component == 0)
  293.             return 9999;
  294.         else if (component == 1)
  295.             return secondComponent.count;
  296.         else if (component == 2)
  297.             return thirdComponent.count;
  298.     }
  299.        
  300.     return 60;
  301. }
  302.  
  303. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
  304. {
  305.     return 35;
  306. }
  307.  
  308. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
  309. {
  310.    
  311. //    [[UILabel alloc] initWithFrame:CGRectMake(35, 0, self.frame.size.width/2 - 35, 42)];
  312.     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)];
  313.     columnView.textAlignment = NSTextAlignmentLeft;
  314.     columnView.font = [UIFont ketoDefaultFontWithSize:15];
  315.     columnView.textColor = [UIColor ketoBlueColor];
  316.     if (_pickerType == TimePicker || component == 0) {
  317.         columnView.text = [NSString stringWithFormat:@"%lu", (long) row];
  318.         if (_pickerType == TimePicker)
  319.             columnView.textAlignment = NSTextAlignmentLeft;
  320.     } else if (component == 1) {
  321.         columnView.text = [NSString stringWithFormat:@"%@", secondComponent[row]];
  322.         if (_pickerType == ActivityUnitValuePicker)
  323.             columnView.frame = CGRectMake(0, 0, self.frame.size.width/4.f , 52);
  324.     } else if (component == 2) {
  325.         columnView.frame = CGRectMake(0, 0, self.frame.size.width/2.3f , 52);
  326.         columnView.numberOfLines = 0;
  327.         KetoServing* serving = thirdComponent[row];
  328.         columnView.text = [NSString stringWithFormat:@"%@",serving.servingName];
  329.     }
  330.     return columnView;
  331. }
  332.  
  333. -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  334.     switch (_pickerType) {
  335.         case TimePicker: {
  336.             switch (component) {
  337.                 case 0:
  338.                     selectedTimeHours = (float)row;
  339.                     break;
  340.                 case 1:
  341.                     selectedTimeMinutes = (float)row;
  342.                     break;
  343.                 case 2:
  344.                     selectedTimeSeconds = (float)row;
  345.                     break;
  346.                    
  347.                 default:
  348.                     break;
  349.             }
  350.            
  351.             NSNumber *timeValueInMinutes = @(selectedTimeMinutes + selectedTimeSeconds / 60 + selectedTimeHours * 60);
  352.             [_pickerDelegate newTimeMinutesValue:timeValueInMinutes];
  353.         }
  354.             break;
  355.         case ActivityUnitValuePicker: {
  356.             switch (component) {
  357.                 case 0:
  358.                     selectedUnitValue = row;
  359.                     break;
  360.                 case 1:
  361.                     selectedUnit = secondComponent[row];
  362.                     break;
  363.                 default:
  364.                     break;
  365.             }
  366.             [_pickerDelegate newChoosenUnit:selectedUnit andValue:@(selectedUnitValue)];
  367.         }
  368.             break;
  369.         case WaterUnitValuePicker: {
  370.             switch (component) {
  371.                 case 0:
  372.                     selectedUnitValue = row;
  373.                     break;
  374.                 case 1:
  375.                     selectedUnit = secondComponent[row];
  376.                     break;
  377.                 default:
  378.                     break;
  379.             }
  380.             [_pickerDelegate newWaterValue:@(selectedUnitValue) withUnit:selectedUnit];
  381.         }
  382.             break;
  383.         case FoodServingsPicker: {
  384.             switch (component) {
  385.                 case 0:
  386.                     quantityServing = (float)row;
  387.                     break;
  388.                 case 1:
  389.                     quantityDecimalServing = secondComponent[row];
  390.                     break;
  391.                 case 2: {
  392.                     choosenServing = thirdComponent[row];
  393.                     if ([choosenServing.servingName isEqualToString:@"g"]) {
  394.                         [self selectRow:100 inComponent:0 animated:YES];
  395.                         [self pickerView:self didSelectRow:100 inComponent:0];
  396.                     }
  397.                 }
  398.                     break;
  399.                 default:
  400.                     break;
  401.             }
  402.             NSNumber *decimalQuantityServing;
  403.            
  404.             if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionQuarter])
  405.                 decimalQuantityServing = @(1.f/4.f);
  406.             else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionOneThirds])
  407.                 decimalQuantityServing = @(1.f/3.f);
  408.             else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionOneHalf])
  409.                 decimalQuantityServing = @(1.f/2.f);
  410.             else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionTwoThirds])
  411.                 decimalQuantityServing = @(2.f/3.f);
  412.             else if ([quantityDecimalServing isEqualToString:unicodeVulgarFractionThreeQuarters])
  413.                 decimalQuantityServing = @(3.f/4.f);
  414.             else
  415.                 decimalQuantityServing = @(0);
  416.            
  417.                
  418.             [_pickerDelegate newServing:choosenServing andQuantity:@(quantityServing + decimalQuantityServing.floatValue) andAdditionalDecimalQuantity:quantityDecimalServing];
  419.         }
  420.             break;
  421.            
  422.         default:
  423.             break;
  424.     }
  425. }
  426.  
  427. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
  428.     CGFloat width = self.frame.size.width;
  429.     if (_pickerType == TimePicker) {
  430.         return width / 4.f;
  431.     } else if (_pickerType == FoodServingsPicker) {
  432.         switch (component){
  433.             case 0:
  434.                 return width / 9.f;//3.f - 80.f;
  435.             case 1:
  436.                 return width / 9.f;//3.f - 80.f;
  437.             case 2:
  438.                 return width / 2.3f;
  439.         }
  440.     }
  441.    
  442.     return width / 3.f;
  443. }
  444.  
  445. -(void)didAddSubview:(UIView *)subview {
  446.     [super didAddSubview:subview];
  447.     if (self.selectorColor) {
  448.         if (subview.bounds.size.height <= 1.0) {
  449.             subview.backgroundColor = self.selectorColor;
  450.             subview.alpha = 0.8f;
  451.         }
  452.     }
  453. }
  454.  
  455. -(void)didMoveToWindow {
  456.     [super didMoveToWindow];
  457.     if (self.selectorColor) {
  458.         for (UIView *subview in self.subviews) {
  459.             if (subview.bounds.size.height <= 1.0) {
  460.                 subview.backgroundColor = self.selectorColor;
  461.                 subview.alpha = 0.8f;
  462.             }
  463.         }
  464.     }
  465. }
  466.  
  467. @end
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474. ////////////////////
  475.  
  476.  
  477.  
  478.  
  479. //
  480. //  ServingsPickerView.h
  481. //  KetoDiet
  482. //
  483. //  Created by Антон Рыскалев on 09.04.2018.
  484. //  Copyright © 2018 LAPPIR. All rights reserved.
  485. //
  486.  
  487. #import <UIKit/UIKit.h>
  488. #import "KetoServing.h"
  489.  
  490. typedef enum PickerInputType
  491. {
  492.     TimePicker,
  493.     ActivityUnitValuePicker,
  494.     WaterUnitValuePicker,
  495.     FoodServingsPicker
  496. } PickerInputType;
  497.  
  498. @protocol ServingsPickerDelegate <NSObject>
  499. @optional
  500.  
  501. -(void)newTimeMinutesValue:(NSNumber*)minutesValue;
  502. -(void)newChoosenUnit:(NSString*)unit andValue:(NSNumber*)unitValue;
  503. -(void)newServing:(KetoServing*)serving andQuantity:(NSNumber*)servingQuantity andAdditionalDecimalQuantity:(NSString*)quantityDecimalString;
  504. -(void)newWaterValue:(NSNumber*)waterValue withUnit:(NSString*)unitString;
  505. @end
  506.  
  507. @interface ServingsPickerView : UIPickerView
  508.  
  509. -(instancetype)initWithPickerType:(PickerInputType)pickerType andModel:(id)model quantityFirstValue:(NSNumber*)quantityFirstValue quantitySecondValue:(NSNumber*)quantitySecondValue delegate:(id)pickerDelegate;
  510. @property (nonatomic, weak) id<ServingsPickerDelegate> pickerDelegate;
  511.  
  512.  
  513. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement