2
0
mirror of synced 2025-02-23 23:08:14 +00:00

example/ivy/ios: format code according to Google style guide

Change-Id: I4cef2b60756b3318c123b832bcdb719d2d45fadc
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/360607
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
robaho 2021-11-01 22:30:14 -05:00 committed by Hyang-Ah Hana Kim
parent 2ab7fee9df
commit e68c98865f
8 changed files with 334 additions and 389 deletions

View File

@ -3,12 +3,12 @@
// license that can be found in the LICENSE file.
#import <WebKit/WebKit.h>
#import "Suggestion.h"
#import "IvyController.h"
#import "Suggestion.h"
@interface AppDelegate
: UIResponder <UIApplicationDelegate, UITextFieldDelegate, WKUIDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate, WKUIDelegate>
@property(strong, nonatomic) UIWindow *window;
@end

View File

@ -12,29 +12,24 @@
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application
{
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end

View File

@ -8,3 +8,4 @@
@interface DocsController : UIViewController
@end

View File

@ -13,8 +13,7 @@
@implementation DocsController
- (void)viewDidLoad
{
- (void)viewDidLoad {
[super viewDidLoad];
webView = (WKWebView *)[self.view viewWithTag:11];
NSString *helpHTML = MobileHelp();
@ -23,8 +22,10 @@
// With the following meta tag, WKWebView displays the fonts more nicely.
NSString *meta = @"<meta name='viewport' \
content='width=device-width, initial-scale=1.0, maximum-scale=1.0, \
minimum-scale=1.0, user-scalable=no'>";
content='width=device-width, "
@"initial-scale=1.0, maximum-scale=1.0, \
minimum-scale=1.0, "
@"user-scalable=no'>";
html = [html stringByAppendingString:@"<head>"];
html = [html stringByAppendingString:meta];
@ -33,9 +34,9 @@
[webView loadHTMLString:html baseURL:NULL];
}
- (void)didReceiveMemoryWarning
{
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end

View File

@ -2,14 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#import <WebKit/WebKit.h>
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#import "Suggestion.h"
// IvyController displays the main app view.
@interface IvyController
: UIViewController <UITextFieldDelegate, WKUIDelegate,
SuggestionDelegate>
@interface IvyController : UIViewController <UITextFieldDelegate, WKUIDelegate, SuggestionDelegate>
@property(weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@ -24,3 +22,4 @@
- (IBAction)okPressed:(id)sender;
@end

View File

@ -14,8 +14,7 @@
int demo_index;
}
- (void)viewDidLoad
{
- (void)viewDidLoad {
[super viewDidLoad];
self.input.delegate = self;
@ -31,18 +30,15 @@
[self.okButton setTitle:@"" forState:UIControlStateNormal];
[self.okButton setHidden:TRUE];
[[NSNotificationCenter defaultCenter]
addObserver:self
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textDidChange:)
name:UITextFieldTextDidChangeNotification
object:self.input];
[[NSNotificationCenter defaultCenter]
addObserver:self
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
@ -51,8 +47,7 @@
[self clear:NULL];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([textField isEqual:self.input]) {
textField.inputAccessoryView = self.suggestionView;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
@ -61,8 +56,7 @@
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if ([textField isEqual:self.input]) {
textField.inputAccessoryView = nil;
[textField reloadInputViews];
@ -70,25 +64,20 @@
return YES;
}
- (void)textDidChange:(NSNotification *)notif
{
- (void)textDidChange:(NSNotification *)notif {
[self.suggestionView suggestFor:self.input.text];
}
- (void)suggestionReplace:(NSString *)text
{
- (void)suggestionReplace:(NSString *)text {
self.input.text = text;
[self.suggestionView suggestFor:text];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
- (void)keyboardWillShow:(NSNotification *)aNotification {
// Move the input text field up, as the keyboard has taken some of the screen.
NSDictionary *info = [aNotification userInfo];
CGRect kbFrame =
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSNumber *duration =
[info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
UIViewAnimationCurve keyboardTransitionAnimationCurve;
[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey]
@ -108,13 +97,11 @@
}];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
- (void)keyboardWillHide:(NSNotification *)aNotification {
// Move the input text field back down.
NSDictionary *info = [aNotification userInfo];
NSNumber *duration =
[info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
UIViewAnimationCurve keyboardTransitionAnimationCurve;
[[info valueForKey:UIKeyboardAnimationCurveUserInfoKey]
@ -136,8 +123,7 @@
}];
}
- (void)enterPressed
{
- (void)enterPressed {
NSString *text = self.input.text;
if ([text isEqual:@""]) {
if (self->demo_lines == NULL) {
@ -162,14 +148,10 @@
if (err != nil) {
result = err.description;
}
result = [result
stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
result =
[result stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
result =
[result stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
NSMutableArray *lines =
(NSMutableArray *)[result componentsSeparatedByString:@"\n"];
result = [result stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
result = [result stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
result = [result stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
NSMutableArray *lines = (NSMutableArray *)[result componentsSeparatedByString:@"\n"];
for (NSMutableString *line in lines) {
if ([line hasPrefix:@"#"])
[self appendTape:line tag:@"comment"];
@ -181,33 +163,30 @@
[self.input becomeFirstResponder];
}
- (void)scrollTapeToBottom
{
- (void)scrollTapeToBottom {
NSString *scroll = @"window.scrollBy(0, document.body.offsetHeight);";
[self.tape evaluateJavaScript:scroll completionHandler:nil];
}
- (void)appendTape:(NSString *)text tag:(NSString *)tag
{
- (void)appendTape:(NSString *)text tag:(NSString *)tag {
NSString *injectSrc = @"appendDiv('%@','%@');";
NSString *runToInject = [NSString stringWithFormat:injectSrc, text, tag];
[self.tape evaluateJavaScript:runToInject completionHandler:nil];
[self scrollTapeToBottom];
}
- (void)loadDemo
{
- (void)loadDemo {
[self.okButton setHidden:FALSE];
NSString *text = DemoText();
self->demo_lines = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
self->demo_lines =
[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
self->demo_index = 0;
self.input.text = @"";
self.input.enablesReturnKeyAutomatically = TRUE;
[self enterPressed];
}
- (void)unloadDemo
{
- (void)unloadDemo {
[self.okButton setHidden:TRUE];
self.input.enablesReturnKeyAutomatically = FALSE;
self->demo_lines = NULL;
@ -227,8 +206,11 @@
- (IBAction)clear:(id)sender {
[self unloadDemo];
NSString *string = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"tape" ofType:@"html"] encoding:NSUTF8StringEncoding error:NULL];
NSString *string = [NSString
stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tape" ofType:@"html"]
encoding:NSUTF8StringEncoding
error:NULL];
[self.tape loadHTMLString:string baseURL:NULL];
}
@end

View File

@ -19,3 +19,4 @@
@property(weak) id<SuggestionDelegate> delegate;
@end

View File

@ -16,40 +16,21 @@
NSCharacterSet *breakingChars;
}
- (instancetype)init
{
- (instancetype)init {
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
self = [self initWithFrame:CGRectMake(0.0f, 0.0f, screenWidth, 36.0f)];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame inputViewStyle:UIInputViewStyleKeyboard];
if (self) {
possibleSuggestions = @[
@")base ",
@")debug ",
@")format ",
@")maxdigits ",
@")op ",
@")origin ",
@")prec ",
@")prompt ",
@")seed ",
@"cos ",
@"iota ",
@"log ",
@"max ",
@"min ",
@"pi ",
@"rho ",
@"sin ",
@"sqrt ",
@"tan "
@")base ", @")debug ", @")format ", @")maxdigits ", @")op ", @")origin ", @")prec ",
@")prompt ", @")seed ", @"cos ", @"iota ", @"log ", @"max ", @"min ", @"pi ", @"rho ",
@"sin ", @"sqrt ", @"tan "
];
breakingChars =
[NSCharacterSet characterSetWithCharactersInString:@"/+-*,^|= "];
breakingChars = [NSCharacterSet characterSetWithCharactersInString:@"/+-*,^|= "];
options = [[NSMutableOrderedSet alloc] initWithCapacity:maxSuggestions];
buttons = [[NSMutableArray alloc] init];
self.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.05f];
@ -58,17 +39,14 @@
return self;
}
- (void)suggestFor:(NSString *)t
{
- (void)suggestFor:(NSString *)t {
text = t;
range =
[text rangeOfCharacterFromSet:breakingChars options:NSBackwardsSearch];
range = [text rangeOfCharacterFromSet:breakingChars options:NSBackwardsSearch];
if (range.location == NSNotFound) {
range.location = 0;
range.length = text.length;
} else {
if (range.location > 0 &&
[text characterAtIndex:range.location - 1] == ')') {
if (range.location > 0 && [text characterAtIndex:range.location - 1] == ')') {
// Special case for suggestions that start with ") ".
range.location -= 1;
range.length++;
@ -97,12 +75,10 @@
[self setNeedsLayout];
}
- (void)setSuggestions:(NSArray *)suggestions
{
- (void)setSuggestions:(NSArray *)suggestions {
[options removeAllObjects];
if ([suggestions respondsToSelector:
@selector(countByEnumeratingWithState:objects:count:)]) {
if ([suggestions respondsToSelector:@selector(countByEnumeratingWithState:objects:count:)]) {
for (NSString *suggestion in suggestions) {
if (options.count < maxSuggestions) {
[options addObject:suggestion];
@ -113,32 +89,26 @@
}
}
- (void)layoutSubview:(NSString *)t at:(CGFloat)x width:(CGFloat)w
{
UIButton *b = [[UIButton alloc]
initWithFrame:CGRectMake(x, 0.0f, w, self.bounds.size.height)];
- (void)layoutSubview:(NSString *)t at:(CGFloat)x width:(CGFloat)w {
UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(x, 0.0f, w, self.bounds.size.height)];
[b setTitle:t forState:UIControlStateNormal];
b.titleLabel.adjustsFontSizeToFitWidth = YES;
b.titleLabel.textAlignment = NSTextAlignmentCenter;
[b setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[b addTarget:self
action:@selector(buttonTouched:)
forControlEvents:UIControlEventTouchUpInside];
[b addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:b];
if (x > 0) {
UIView *line = [[UIView alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 0.5f, self.bounds.size.height)];
line.backgroundColor =
[UIColor colorWithRed:0.984 green:0.977 blue:0.81 alpha:1.0];
UIView *line =
[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.5f, self.bounds.size.height)];
line.backgroundColor = [UIColor colorWithRed:0.984 green:0.977 blue:0.81 alpha:1.0];
[b addSubview:line];
}
[buttons addObject:b];
}
- (void)layoutSubviews
{
- (void)layoutSubviews {
for (UIView *subview in buttons) {
[subview removeFromSuperview];
}
@ -152,23 +122,19 @@
for (int i = 0; i < options.count; i++) {
NSString *suggestion = options[i];
CGFloat width =
(self.bounds.size.width - (4 * symbolWidth)) / options.count;
CGFloat width = (self.bounds.size.width - (4 * symbolWidth)) / options.count;
CGFloat x = (4 * symbolWidth) + (i * width);
[self layoutSubview:suggestion at:x width:width];
}
}
- (void)buttonTouched:(UIButton *)button
{
- (void)buttonTouched:(UIButton *)button {
NSTimeInterval duration = 0.08f;
[UIView
animateWithDuration:duration
[UIView animateWithDuration:duration
animations:^{
[button setBackgroundColor:[UIColor whiteColor]];
if ([self.delegate
respondsToSelector:@selector(suggestionReplace:)]) {
if ([self.delegate respondsToSelector:@selector(suggestionReplace:)]) {
NSString *t = self->text;
if (t == nil) {
t = @"";
@ -177,8 +143,8 @@
// Special case for +, -, *, /.
t = [t stringByAppendingString:button.currentTitle];
} else {
t = [self->text stringByReplacingCharactersInRange:
self->range withString:button.currentTitle];
t = [self->text stringByReplacingCharactersInRange:self->range
withString:button.currentTitle];
}
[self performSelector:@selector(suggestionReplace:)
withObject:t
@ -190,9 +156,9 @@
}];
}
- (void)suggestionReplace:(NSString *)t
{
- (void)suggestionReplace:(NSString *)t {
[self.delegate performSelector:@selector(suggestionReplace:) withObject:t];
}
@end