Today's Menu  Portugal
journal and plan nutrition
SecondViewController.m
Go to the documentation of this file.
1 //
2 // SecondViewController.m
3 // StayWellFed
4 //
5 // Created by Don Zeek on 5/12/15.
6 // Copyright (c) 2015 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
10 #import "Z5DataController.h"
13 
14 @interface SecondViewController ()
15 
16 @end
17 
18 @implementation SecondViewController {
19  float currentRed;
20  BOOL currentIncreasing;
21 }
22 
23 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
24 {
25  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
26  if (self) {
27  // Custom initialization
28  currentRed = 0.0;
29  currentIncreasing = YES;
30  self.title = @"Network";
31  self.tabBarItem.image = [UIImage imageNamed:@"network32"];
32  }
33  return self;
34 }
35 
36 - (void)viewDidLoad {
37  [super viewDidLoad];
38  // Do any additional setup after loading the view, typically from a nib.
39 
40  // Create an OpenGL ES context and assign it to the view loaded from storyboard
41  // NSLog(@"SecondView.viewDidLoad: view class: %@", NSStringFromClass([[self view] class]));
42 
43  CGRect totalFrame = self.view.frame;
44  float leftMargin = 0.15*totalFrame.size.width;
45  float topMargin = 90;
46  float buttonHeight = 50.0;
47  float portalHeight = 280;
48  float controlMargin = 30.0;
49  float controlWidth = 0.7*totalFrame.size.width;
50  CGRect logGoogleFrame = CGRectMake(leftMargin, topMargin, controlWidth, buttonHeight);
51  CGRect portalFrame = CGRectMake(leftMargin, topMargin+buttonHeight+controlMargin, controlWidth, portalHeight);
52  CGRect saveLocallyFrame = CGRectMake(leftMargin, topMargin+buttonHeight+2*controlMargin+portalHeight, controlWidth, buttonHeight);
53  CGRect readLocallyFrame = CGRectMake(leftMargin, topMargin+2*buttonHeight+3*controlMargin+portalHeight-16, controlWidth, buttonHeight);
54 
55 // UIButton *logGoogleButton = [[UIButton alloc] initWithFrame:logGoogleFrame];
56  self.logGoogleButton = [[UIButton alloc] initWithFrame:logGoogleFrame];
57  [self.logGoogleButton setTitle:@"Log-On Google" forState:UIControlStateNormal];
58 
59  /*
60  if (![[[Z5DataController sharedInstance] remoteDataController] isSignedIn]) {
61  NSLog(@"SecondView.viewDidLoad: NOT logged on");
62  // [logGoogleButton setBackgroundColor:[UIColor colorWithHue:0.3 saturation:1.0 brightness:0.4 alpha:1.0]];
63  [logGoogleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
64  } else {
65 
66  NSLog(@"SecondView.viewDidLoad: logged on");
67 
68  [logGoogleButton setTitle:@"LOG-OFF (already on)" forState:UIControlStateNormal];
69 
70  [logGoogleButton setBackgroundColor:[UIColor colorWithHue:0.8 saturation:0.4 brightness:0.4 alpha:1.0]];
71  [logGoogleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
72 
73  }
74  */
75 
76  [self.logGoogleButton addTarget:self action:@selector(logGoogleNow) forControlEvents:UIControlEventTouchUpInside];
77  [self.view addSubview:self.logGoogleButton];
78 
79  EAGLContext *portalContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
80  self.portal = [[GLKView alloc] initWithFrame:portalFrame context:portalContext];
81  self.portal.delegate = self;
82 
83  // Configure renderbuffers created by the view
84  self.portal.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
85  self.portal.drawableDepthFormat = GLKViewDrawableDepthFormat24;
86  self.portal.drawableStencilFormat = GLKViewDrawableStencilFormat8;
87 
88  // Enable multisampling
89  self.portal.drawableMultisample = GLKViewDrawableMultisample4X;
90 
91  [self.view addSubview:self.portal];
92 
93  UIButton *saveLocallyButton = [[UIButton alloc] initWithFrame:saveLocallyFrame];
94  [saveLocallyButton setTitle:@"Save to Device" forState:UIControlStateNormal];
95  [saveLocallyButton addTarget:self action:@selector(saveToUserDefaults) forControlEvents:UIControlEventTouchUpInside];
96  [self.view addSubview:saveLocallyButton];
97 
98  UIButton *readLocallyButton = [[UIButton alloc] initWithFrame:readLocallyFrame];
99  [readLocallyButton setTitle:@"Read User Defaults" forState:UIControlStateNormal];
100  [readLocallyButton addTarget:self action:@selector(readUserDefaults) forControlEvents:UIControlEventTouchUpInside];
101  [self.view addSubview:readLocallyButton];
102 
103 
104  [[self view] setBackgroundColor:[UIColor lightGrayColor]];
105 }
106 
107 - (void) viewWillAppear:(BOOL)animated
108 {
109  // self.portal.enableSetNeedsDisplay = YES;
110 
111  self.portal.enableSetNeedsDisplay = NO;
112  CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];
113  [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
114 
115 
116 }
117 - (void)render:(CADisplayLink*)displayLink {
118  // GLKView * view = [self.window.subviews objectAtIndex:0];
119  [self.portal display];
120 }
121 
122 #pragma mark - Device
123 
124 - (void)saveToUserDefaults {
125 
126  NSLog(@"SecondView.saveToUserDefaults");
127  [[[Z5DataController sharedInstance] localDataController] putTodaysMenuInUserDefaults];
128 }
129 
134 - (void)readUserDefaults {
135  NSLog(@"SecondView.readUserDefaults");
136  Z5LocalDataController *localData = [[Z5DataController sharedInstance] localDataController];
137 
138 
140  for (MenuItem *meItem in [localData todaysMenuItems]) {
141  NSLog(@"SecondView.readUserDefaults: menu-item: %@", meItem);
142  }
143 }
144 
145 #pragma mark - Google
146 
147 - (void)logGoogleNow {
148 
149  NSLog(@"SecondView.logGoogleNow");
150 
151  [[[Z5DataController sharedInstance] remoteDataController] loginDriveWithNavigation:self.navigationController andHandler:^(void) {
152 
153  // finish-handler will not be called if logging off
154  NSLog(@"SecondView.logGoogleNow/finish-handler");
155 
156  [[[Z5DataController sharedInstance] remoteDataController] initializeAvailableWithDelegate:self];
157  }];
158 
159 }
160 
161 #pragma mark - GLKViewDelegate
162 
163 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
164 
165  if ([[[Z5DataController sharedInstance] remoteDataController] isSignedIn]) {
166  [self cycleClearColor];
167  } else {
168  glClearColor(0.3, 0.6, 0.3, 1.0);
169  glClear(GL_COLOR_BUFFER_BIT);
170  }
171 
172 }
173  - (void)cycleClearColor
174 {
175  if (currentIncreasing) {
176  currentRed += 0.002;
177  } else {
178  currentRed -= 0.01;
179  }
180  if (currentRed >= 1.0) {
181  currentRed = 1.0;
182  currentIncreasing = NO;
183  }
184  if (currentRed <= 0.0) {
185  currentRed = 0.0;
186  currentIncreasing = YES;
187  }
188 
189  glClearColor(currentRed*0.4, currentRed*0.7 + 0.2, 0.5, 1.0);
190  glClear(GL_COLOR_BUFFER_BIT);
191 
192 }
193 #pragma mark - Z5RemoteDataFetch protocol
194 
195 - (void) showHomeAsk
196 {
197  NSLog(@"SecondView.showHomeAsk");
198 }
199 - (void) showHomeError:(NSError *)error
200 {
201  NSLog(@"SecondView.showHomeError");
202 }
203 - (BOOL) tellHomeError
204 {
205  NSLog(@"SecondView.tellHomeError");
206  return NO;
207 }
208 // i (void) showHomeResult: (GTLDriveFile *)filen
209 // {
210 // NSLog(@"SecondView.showHomeResult");
211 // }
212 // - (GTLDriveFile *) tellHomeResult
213 // {
214 // NSLog(@"SecondView.tellHomeResult");
215 // return nil;
216 // }
217 - (void) showFetchAsk
218 {
219  NSLog(@"SecondView.showFetchAsk");
220 }
221 - (void) showFetchError
222 {
223  NSLog(@"SecondView.showFetchError");
224 }
225 - (void) showFetchResult: (NSArray *)filen
226 {
227  NSLog(@"SecondView.showFetchResult");
228 }
229 - (void) showPublishAsk
230 {
231  NSLog(@"SecondView.showPublishAsk");
232 }
233 - (void) showPublishError
234 {
235  NSLog(@"SecondView.showPublishError");
236 }
237 - (void) showPublishResult
238 {
239  NSLog(@"SecondView.showPublishResult");
240 }
241 - (void) showDataFetch: (NSData *) data
242 {
243  NSLog(@"SecondView.showDataFetch");
244 }
245 - (void) showDataFetch: (NSData *) data fromFile: (NSString *) title withGoogleId: (NSString *) fileId
246 {
247  NSLog(@"SecondView.showDataFetch");
248 }
249 - (void) showGoogleFileTitle: (NSString *) title andId: (NSString *) fileId
250 {
251  NSLog(@"SecondView.showGoogleFileTitle");
252 }
253 // - (void) showInsertResult: (GTLDriveFile *) newf
254 // {
255 // NSLog(@"SecondView.showInsertResult");
256 // }
257 - (void) updateUI
258 {
259  NSLog(@"SecondView.updateUI");
260  if (![[[Z5DataController sharedInstance] remoteDataController] isSignedIn]) {
261 
262  NSLog(@"SecondView.updateUI: NOT logged on");
263 
264  [self.logGoogleButton setTitle:@"LOG-ON" forState:UIControlStateNormal];
265 
266  [self.logGoogleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
267 
268  } else {
269 
270  NSLog(@"SecondView.updateUI: logged on");
271 
272  [self.logGoogleButton setTitle:@"LOG-OFF (already on)" forState:UIControlStateNormal];
273 
274  [self.logGoogleButton setBackgroundColor:[UIColor colorWithHue:0.48 saturation:0.4 brightness:0.4 alpha:1.0]];
275  [self.logGoogleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
276 
277  }
278 
279 }
280 
281 
282 #pragma mark - focus-in-time
283 
284 - (void)didReceiveMemoryWarning {
285  [super didReceiveMemoryWarning];
286  // Dispose of any resources that can be recreated.
287 }
288 
289 @end
instancetype sharedInstance()
NSMutableArray * createTodaysMenuFromUserDefaults()
Singleton interface to both core and remote data sources.
BOOL currentIncreasing