@@ -14,10 +14,9 @@ @interface DetailViewController ()
1414
1515@property (strong , nonatomic ) NSDate * oldestDate, * newestDate;
1616@property (assign , nonatomic ) CGFloat smallestValue, biggestValue;
17- @property (nonatomic , assign ) NSInteger numberOfPoints;
18-
1917
2018@property (weak , nonatomic ) IBOutlet UIStepper *graphObjectIncrement;
19+ @property (strong , nonatomic ) IBOutlet UIActivityIndicatorView * activity;
2120
2221@property (strong , nonatomic ) NSMutableArray <NSNumber *> *arrayOfValues;
2322@property (strong , nonatomic ) NSMutableArray <NSDate *> *arrayOfDates;
@@ -41,9 +40,11 @@ @implementation DetailViewController
4140
4241- (void )viewDidLoad {
4342 [super viewDidLoad ];
44- self.navigationItem . leftBarButtonItem = self. splitViewController . displayModeButtonItem ;
43+ self.activity = [[UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray] ;
4544 self.navigationItem .leftItemsSupplementBackButton = YES ;
46-
45+ UIBarButtonItem * activityButton = [[UIBarButtonItem alloc ] initWithCustomView: self .activity];
46+ self.navigationItem .rightBarButtonItems = [self .navigationItem.rightBarButtonItems arrayByAddingObject: activityButton];
47+
4748 self.maxValue = -1.0 ;
4849 self.minValue = -1.0 ;
4950 self.maxXValue = -1.0 ;
@@ -66,7 +67,7 @@ - (void)viewDidLoad {
6667
6768 // Do any additional setup after loading the view.
6869
69- self. graphObjectIncrement . value = 1000 ;
70+ _numberOfPoints = 10 ;
7071
7172 [self hydrateDatasets ];
7273
@@ -75,21 +76,35 @@ - (void)viewDidLoad {
7576
7677#pragma mark Data management
7778
79+ -(void ) setNumberOfPoints : (NSInteger )numberOfPoints {
80+ if (numberOfPoints != _numberOfPoints) {
81+ NSInteger oldNumberOfPoints = _numberOfPoints;
82+
83+ _numberOfPoints = numberOfPoints;
84+
85+ if (numberOfPoints == oldNumberOfPoints + 1 ) {
86+ [self addPointToGraph ];
87+ } else if (numberOfPoints == oldNumberOfPoints - 1 ) {
88+ [self removePointFromGraph ];
89+ } else {
90+ [self hydrateDatasets ];
91+ }
92+ [self .myGraph reloadGraph ];
93+ }
94+ }
95+
7896float randomProbability () {
7997 return (float ) ((double )(arc4random ())) / UINT32_MAX;
8098}
8199
82-
83100- (void )hydrateDatasets {
84101 // Reset the arrays of values (Y-Axis points) and dates (X-Axis points / labels)
85102 if (!self.arrayOfValues ) self.arrayOfValues = [[NSMutableArray alloc ] init ];
86103 if (!self.arrayOfDates ) self.arrayOfDates = [[NSMutableArray alloc ] init ];
87104 [self .arrayOfValues removeAllObjects ];
88105 [self .arrayOfDates removeAllObjects ];
89106
90- self.totalNumber = 0 ;
91107 NSDate *date = [NSDate date ];
92- self.numberOfPoints = self.graphObjectIncrement .value ;
93108 // Add objects to the array based on the stepper value
94109 CGFloat lastValue = 5000 ;
95110 for (int i = 0 ; i < self.numberOfPoints ; i++) {
@@ -104,13 +119,15 @@ - (void)hydrateDatasets {
104119 date = [self dateForGraphAfterDate: date];
105120 }
106121 [self checkMaximums ];
122+ self.graphObjectIncrement .value = self.numberOfPoints ;
107123}
108124
109125-(void ) checkMaximums {
110126 self.oldestDate = [NSDate distantFuture ];
111127 self.newestDate = [NSDate distantPast ];
112128 self.biggestValue = -INFINITY;
113129 self.smallestValue = INFINITY;
130+ self.totalNumber = 0 ;
114131 for (NSInteger i = 0 ; i < self.numberOfPoints ; i++) {
115132 CGFloat value = self.arrayOfValues [(NSUInteger )i].floatValue ;
116133 if (value < BEMNullGraphValue) {
@@ -119,7 +136,7 @@ -(void) checkMaximums {
119136 self.smallestValue = MIN (self.smallestValue ,value );
120137 }
121138 }
122- self.oldestDate = self.arrayOfDates [0 ];
139+ if (self. arrayOfDates . count > 0 ) self.oldestDate = self.arrayOfDates [0 ];
123140 self.newestDate = [self .arrayOfDates lastObject ]; // needs to be last for notification
124141
125142}
@@ -157,14 +174,7 @@ - (float)getRandomFloat {
157174}
158175
159176- (IBAction )addOrRemovePointFromGraph : (id )sender {
160- if (self.graphObjectIncrement .value > self.numberOfPoints ) {
161- [self addPointToGraph ];
162- } else if (self.graphObjectIncrement .value < self.numberOfPoints ) {
163- [self removePointFromGraph ];
164- }
165177 self.numberOfPoints = self.graphObjectIncrement .value ;
166- [self checkMaximums ];
167- [self .myGraph reloadGraph ];
168178}
169179
170180- (void ) addPointToGraph {
@@ -180,6 +190,7 @@ - (void) addPointToGraph {
180190 [self .arrayOfValues addObject: newValue];
181191 NSDate *lastDate = self.arrayOfDates .count > 0 ? [self .arrayOfDates lastObject ]: [NSDate date ];
182192 NSDate *newDate = [self dateForGraphAfterDate: lastDate];
193+ self.oldestDate = newDate;
183194 [self .arrayOfDates addObject: newDate];
184195}
185196
@@ -188,8 +199,10 @@ - (void) removePointFromGraph {
188199 // Remove point
189200 [self .arrayOfValues removeObjectAtIndex: 0 ];
190201 [self .arrayOfDates removeObjectAtIndex: 0 ];
202+ [self checkMaximums ];
191203 }
192204}
205+
193206-(NSString *) formatNumber : (NSNumber *) number {
194207 return [NSNumberFormatter localizedStringFromNumber: number
195208 numberStyle: NSNumberFormatterDecimalStyle];
@@ -287,6 +300,16 @@ -(BOOL) respondsToSelector:(SEL)aSelector {
287300 }
288301}
289302
303+ -(void ) lineGraphDidBeginLoading : (BEMSimpleLineGraphView *)graph {
304+ [self .activity startAnimating ];
305+
306+ }
307+
308+ -(void ) lineGraphDidFinishDrawing : (BEMSimpleLineGraphView *)graph {
309+ [self .activity stopAnimating ];
310+
311+ }
312+
290313
291314- (NSString *)lineGraph : (BEMSimpleLineGraphView *)graph labelOnXAxisForIndex : (NSInteger )index {
292315// return [NSString stringWithFormat:@"%lu", (long)index];
0 commit comments