Today's Menu  Portugal
journal and plan nutrition
V7GoogCalendarViewController.m
Go to the documentation of this file.
1 //
2 // V7GoogleCalendarViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 4/25/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 
10 #import "Z5DataController.h"
11 // #import "Z5GoogleCalendarController.h"
12 // #import "Z5GoogleLoginController.h"
13 
14 @interface V7GoogCalendarViewController ()
15 
16 @end
17 
18 @implementation V7GoogCalendarViewController
19 
20 - (void)viewDidLoad {
21  [super viewDidLoad];
22 
23  self.title = @"Google Calendar";
24  // Initialize the service object.
25  self.service = [[GTLRCalendarService alloc] init];
26  [[self view] setBackgroundColor:[UIColor colorWithHue:0.32
27  saturation:0.8
28  brightness:0.5
29  alpha:1.0]];
30 
31  CGRect totalFrame = self.view.frame;
32 
33  Z5GoogleLoginController *loginControl = [[[Z5DataController sharedInstance] remoteDataController] loginController];
34 
35  Z5GoogleCalendarDataController *calendarControl = [[[Z5DataController sharedInstance] remoteDataController] calendarController];
36 
37  if ([[loginControl signedIn] boolValue]) {
38  [calendarControl signedIn:[loginControl signedInUser]];
39  [calendarControl addCalendarListener:self];
40 
41  CGRect listFilesButtonFrame = CGRectMake(totalFrame.size.width * 0.30,
42  totalFrame.size.height * 0.2,
43  totalFrame.size.width * 0.4,
44  totalFrame.size.height * 0.1);
45 
46  self.listEventsButton = [[UIButton alloc] initWithFrame:listFilesButtonFrame];
47  [self.listEventsButton setTitle:@"list events" forState:UIControlStateNormal];
48  [self.listEventsButton addTarget:self action:@selector(fetchEvents) forControlEvents:UIControlEventTouchUpInside];
49  [self.view addSubview:self.listEventsButton];
50 
51  self.service = [calendarControl service];
52 
53  } else {
54  // Add the sign-in button.
55  CGRect signInButtonFrame = CGRectMake(totalFrame.size.width * 0.45,
56  totalFrame.size.height * 0.1,
57  totalFrame.size.width * 0.1,
58  totalFrame.size.height * 0.05);
59 
60 
61  [loginControl configureGoogleSignIn];
63  signIn.delegate = loginControl;
64  signIn.uiDelegate = self;
65 
66  self.signInButton = [[GIDSignInButton alloc] init];
67  [self.signInButton setFrame:signInButtonFrame];
68  _signInButton.layer.cornerRadius = 8;
69  _signInButton.layer.borderColor = [[UIColor blackColor] CGColor];
70  _signInButton.layer.borderWidth = 3.0;
71  _signInButton .clipsToBounds = YES;
72  [self.view addSubview:self.signInButton];
73 
74  }
75 }
76 
77 #pragma mark - Z5GoogleLoginListener
78 - (void) signedIn: (GIDGoogleUser *)user
79 {
80  NSLog(@"GoogCalendarViewController.signedIn: user-auth: %@", user.authentication.fetcherAuthorizer);
81 
82  Z5GoogleCalendarDataController *calendarControl = [[[Z5DataController sharedInstance] remoteDataController] calendarController];
83  self.service = [calendarControl service];
84 }
85 - (void) signedOut:(GIDGoogleUser *)user
86 {
87  self.service = nil;
88 }
89 
90 // Construct a query and get a list of upcoming events from the user calendar. Display the
91 // start dates and event summaries in the UITextView.
92 - (void)fetchEvents {
93  NSLog(@"GoogCalendarViewController.fetchEvents");
94 
95  GTLRCalendarQuery_EventsList *query =
96  [GTLRCalendarQuery_EventsList queryWithCalendarId:@"primary"];
97  query.maxResults = 10;
98  query.timeMin = [GTLRDateTime dateTimeWithDate:[NSDate date]];
99  query.singleEvents = YES;
100  query.orderBy = kGTLRCalendarOrderByStartTime;
101 
102  [self.service executeQuery:query
103  delegate:self
104  didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)];
105 }
106 
107 - (void)displayResultWithTicket:(GTLRServiceTicket *)ticket
108  finishedWithObject:(GTLRCalendar_Events *)events
109  error:(NSError *)error {
110  NSLog(@"GoogCalendarViewController.displayResult");
111  if (error == nil) {
112  NSMutableString *output = [[NSMutableString alloc] init];
113  if (events.items.count > 0) {
114  [output appendString:@"Upcoming events:\n"];
115  for (GTLRCalendar_Event *event in events) {
116  GTLRDateTime *start = event.start.dateTime ?: event.start.date;
117  NSString *startString =
118  [NSDateFormatter localizedStringFromDate:[start date]
119  dateStyle:NSDateFormatterShortStyle
120  timeStyle:NSDateFormatterShortStyle];
121  [output appendFormat:@"%@ - %@\n", startString, event.summary];
122  }
123  } else {
124  [output appendString:@"No upcoming events found."];
125  }
126  NSLog(@"GoogCalendarViewController.displayResult:\n%@", output);
127  // self.output.text = output;
128  } else {
129  // [self showAlert:@"Error" message:error.localizedDescription];
130  NSLog(@"GoogCalendarViewController.displayResult:\nerror: %@", error.localizedDescription);
131  }
132 }
133 
135  [super didReceiveMemoryWarning];
136  // Dispose of any resources that can be recreated.
137 }
138 
139 /*
140 #pragma mark - Navigation
141 
142 // In a storyboard-based application, you will often want to do a little preparation before navigation
143 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
144  // Get the new view controller using [segue destinationViewController].
145  // Pass the selected object to the new view controller.
146 }
147 */
148 
149 @end
id< GIDSignInDelegate > delegate
Definition: GIDSignIn.h:103
instancetype sharedInstance()
id< GTMFetcherAuthorizationProtocol > fetcherAuthorizer()
id< GIDSignInUIDelegate > uiDelegate
Definition: GIDSignIn.h:106
Singleton interface to both core and remote data sources.
void addCalendarListener:(id< Z5GoogleCalendarListener > ear)
GIDSignIn * sharedInstance()
GIDAuthentication * authentication
Definition: GIDGoogleUser.h:27