Today's Menu  Portugal
journal and plan nutrition
W7WebSnippetViewController.m
Go to the documentation of this file.
1 //
2 // W7WebSnippetViewController.m
3 // TodaysMenu
4 //
5 // Created by Don Zeek on 12/1/18.
6 // Copyright © 2018 net.dzeek.y2015.ios.portfolio. All rights reserved.
7 //
8 #import <UIKit/UIKit.h>
9 #import <WebKit/WebKit.h>
10 
11 #import "Z5DataController.h"
15 
16 @interface W7WebSnippetViewController ()
17 
18 
19 @end
20 
21 
22 
23 
24 
25 @implementation W7WebSnippetViewController {
26  // UITextView *snippetTextView;
27  WKWebView *webTitleView;
28  WKWebView *webIngredientsView;
29  WKWebView *webInstructionView;
31  CGSize overallSize;
32 
33  WKWebView *webView;
34  // UIPickerView *detailSpecifyPicker;
35 }
36 
37 - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNilz
38  bundle:(nullable NSBundle *)nibBundleOrNilz
39 {
40  self = [super initWithNibName:nibNameOrNilz bundle:nibBundleOrNilz];
41  if (self) {
42  ;
43  }
44  return self;
45 }
46 - (void)viewDidLoad {
47  [super viewDidLoad];
48  // Do any additional setup after loading the view.
49 
50  self.title = @"Capture Web Snippet";
51 
52  navbarBottomEdge = self.navigationController.navigationBar.frame.origin.y
53  + self.navigationController.navigationBar.frame.size.height;
54 
55  // fetch, scale, and add a subview background image
56  UIImage *backgrdImage = [UIImage imageNamed:@"blueHydrangea.png"];
57  CGSize photoSize = [backgrdImage size];
58  CGRect totalFrame = self.view.frame;
59  overallSize = totalFrame.size;
60  float resizeRatio = photoSize.height / totalFrame.size.height;
61  UIImage *resizedImage = [UIImage imageWithCGImage:[backgrdImage CGImage]
62  scale:backgrdImage.scale * resizeRatio
63  orientation:(backgrdImage.imageOrientation) ];
64  UIImageView *backgrd = [[UIImageView alloc] initWithImage:resizedImage];
65  [self.view addSubview:backgrd];
66  // end - fetch, scale, and add a subview background image
67 
68  float toDriveuttonWidth = 72.0;
69  CGRect toDriveFrame = CGRectMake(overallSize.width - (toDriveuttonWidth+ 6.0), navbarBottomEdge + 10.0, toDriveuttonWidth, toDriveuttonWidth);
70  UIButton *toDriveButton = [[UIButton alloc] initWithFrame:toDriveFrame];
71  [toDriveButton setTitle:@"Drive" forState:UIControlStateNormal];
72  [toDriveButton addTarget:self action:@selector(pushToCreateEntryOnDrive) forControlEvents:UIControlEventTouchUpInside];
73  [toDriveButton addGradientFace];
74  [self.view addSubview:toDriveButton];
75 
76 
77  float snippetTitleFrameTop = navbarBottomEdge + 20.0 + 80.0;
78  float snippetTitleFrameHeight = 48.0;
79  CGRect snippetTitleFrame = CGRectMake(8.0,
80  snippetTitleFrameTop,
81  overallSize.width - 16.0,
82  snippetTitleFrameHeight);
83  webTitleView = [self viewDidLoadSnippet:_showHtmlTitle inFrame:snippetTitleFrame];
84 
85  float snippetIngredientsFrameTop = snippetTitleFrameTop + snippetTitleFrameHeight + 4.0;
86  float remainingHeight = overallSize.height - snippetIngredientsFrameTop;
87  float snippetIngredientsFrameHeight = remainingHeight/2 - 4.0;
88  CGRect snippetIngredientsFrame = CGRectMake(8.0,
89  snippetIngredientsFrameTop,
90  overallSize.width - 16.0,
91  snippetIngredientsFrameHeight);
92  webIngredientsView = [self viewDidLoadSnippet:_showHtmlIngredients inFrame:snippetIngredientsFrame];
93 
94  float snippetInstructionFrameTop = snippetIngredientsFrameTop + snippetIngredientsFrameHeight + 4.0;
95  float snippetInstructionFrameHeight = overallSize.height - snippetInstructionFrameTop - 4.0;
96  CGRect snippetInstructionFrame = CGRectMake(8.0,
97  snippetInstructionFrameTop,
98  overallSize.width - 16.0,
99  snippetInstructionFrameHeight);
100  webInstructionView = [self viewDidLoadSnippet:_showHtmlInstruction inFrame:snippetInstructionFrame];
101 
102 
103  [self.view addSubview:webTitleView];
104  [self.view addSubview:webIngredientsView];
105  [self.view addSubview:webInstructionView];
106 
107 }
108 
109 //- (void) viewDidAppear:(BOOL)animated
110 //{
111 // [super viewDidAppear:animated];
112 //
113 // [detailSpecifyPicker selectRow:0 inComponent:0 animated:NO];
114 //}
115 - (WKWebView *) viewDidLoadSnippet:(NSString *)input inFrame:(CGRect)snippetFrame
116 {
117  WKWebView *webView = [[WKWebView alloc] initWithFrame:snippetFrame];
118  webView.navigationDelegate = self;
119  webView.UIDelegate = nil;
120 
121  // background-color:#e5ceb3;
122  // background-image:url(\"tiffany_gradient.png\");
123  if (nil==input ) {
124  input = @"<body bgcolor=\"orange\"><center><h1><b>Error</b></h1></center></body>";
125  }
126  [webView loadHTMLString:input baseURL:nil];
127  [webView setOpaque:YES];
128  [webView setBackgroundColor:[UIColor whiteColor]];
129  webView.layer.cornerRadius = 8;
130  webView.layer.borderColor = [[UIColor blackColor] CGColor];
131  webView.layer.borderWidth = 3.0;
132  webView.clipsToBounds = YES;
133  return webView;
134 }
135 - (void) loadWebView:(WKWebView *)webview with:(NSString *)htmlload
136 {
137  [webview loadHTMLString:htmlload baseURL:nil];
138 }
139 - (void) pushToCreateEntryOnDrive
140 {
141  V7GoogDriveMenuItemViewController *driveMenuItem =
142  [[V7GoogDriveMenuItemViewController alloc] initWithNibName:nil bundle:nil];
143  driveMenuItem.delegate = self;
144  if (nil != _showTextTitle) {
145  driveMenuItem.titleTextStr = [driveMenuItem.titleTextStr stringByAppendingString:_showTextTitle];
146  }
147 
148  [self.navigationController pushViewController:driveMenuItem animated:YES];
149 }
150 
151 - (void) popPushController:(UIViewController *)rebuildVC
152 {
153  NSString *rebuildClassStr = NSStringFromClass([rebuildVC class]);
154  NSLog(@"V7GoogDriveMenuItemViewController.popPushController: class %@",
155  rebuildClassStr);
156  if ([rebuildClassStr isEqualToString:@"V7GoogDriveMenuItemViewControlle"]) {
157  UIViewController *popped = [self.navigationController popViewControllerAnimated:NO];
158  NSLog(@"CameraSnapViewController.popPushController: %@", NSStringFromClass([popped class]));
159 
160  NSLog(@"W7WebSnippetViewController.popPushController: rebuild-vc");
162  }
163 }
164 
165 - (NSString *) embelishHtmlReturn:(NSString *)htmlbody
166 {
167  NSString *styleMU = @"<style> body {background-color: #BBFFDD; }, li { background-color: #FFFF00; border: none; color: black; padding: 5px 9px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; }, ul { list-style-type: circle; } </style> ";
168  NSString *metaMU = @"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">";
169  NSString *ret = [NSString stringWithFormat:@" %@ %@ %@ ", metaMU, styleMU, htmlbody];
170 
171  return ret;
172 }
173 //#pragma mark - UIPickerViewDelegate
174 //- (void)pickerView:(UIPickerView *)pickerView
175 // didSelectRow:(NSInteger)row
176 // inComponent:(NSInteger)component
177 //{
178 // switch (row) {
179 // case 0:
180 // NSLog(@"W7WebViewSnippetVC.picker-didSelect Name");
181 // if (nil == _showHtmlTitle) {
182 // _showHtmlTitle = [self embelishHtmlReturn:@"<b>no ingredients heading</b>"];
183 // }
184 // [self loadWebView:webTitleView with:_showHtmlTitle];
185 // break;
186 //
187 // case 1:
188 // NSLog(@"W7WebViewSnippetVC.pickier-didSelect Ingredients");
189 // if (nil == _showHtmlIngredients) {
190 // _showHtmlIngredients = [self embelishHtmlReturn:@"<b>no ingredients heading</b>"];
191 // }
192 // [self loadWebView:webIngredientsView with:_showHtmlIngredients];
193 // break;
194 //
195 // case 2:
196 // NSLog(@"W7WebViewSnippetVC.pickier-didSelect Instructions");
197 // if (nil == _showHtmlInstruction) {
198 // _showHtmlInstruction = [self embelishHtmlReturn:@"<b>no ingredients heading</b>"];
199 // }
200 // [self loadWebView:webTitleView with:_showHtmlInstruction];
201 // break;
202 //
203 // default:
204 // NSLog(@"W7WebViewSnippetVC.pickier-didSelect ??error-default??");
205 // break;
206 // }
207 //}
208 //#pragma - UIPickerViewDataSource
209 //
210 //- (NSString *)pickerView:(UIPickerView *)pickerView
211 // titleForRow:(NSInteger)row
212 // forComponent:(NSInteger)component
213 //{
214 // NSString *returnLabel;
215 // switch (row) {
216 // case 0:
217 // returnLabel = @"Name";
218 // break;
219 // case 1:
220 // returnLabel = @"Ingredients";
221 // break;
222 // case 2:
223 // returnLabel = @"Directions";
224 // break;
225 // case 3:
226 // returnLabel = @"Note";
227 // break;
228 // }
229 // return returnLabel;
230 //}
231 //#pragma mark - UIPickerViewDataSource
232 //- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
233 //{
234 // return 1;
235 //}
236 //- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
237 //{
238 // return 3;
239 //}
240 #pragma - mark ?
241 - (void)textViewDidChange:(UITextView *)textView
242 {
243  NSLog(@"W7WebSnippetViewController.textViewDidChange: Called");
244 // CGFloat fixedWidth = textView.frame.size.width;
245 // CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
246 // CGRect newFrame = textView.frame;
247 // newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
248 // textView.frame = newFrame;
249 }
250 /*
251 #pragma mark - Navigation
252 
253 // In a storyboard-based application, you will often want to do a little preparation before navigation
254 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
255  // Get the new view controller using [segue destinationViewController].
256  // Pass the selected object to the new view controller.
257 }
258 */
259 
260 @end
WKWebView * webView
WKWebView * webInstructionView
CGSize overallSize
id< V7GoogDriveMenuViewProtocol > delegate
WKWebView * viewDidLoadSnippet:inFrame:(NSString *input, [inFrame] CGRect snippetFrame)
WKWebView * webIngredientsView
float navbarBottomEdge