Skip to content

Commit

Permalink
Add OpenURL()
Browse files Browse the repository at this point in the history
  • Loading branch information
overcyn committed Sep 27, 2017
1 parent cea694a commit 12c7c8e
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 33 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
* MatchaPager should send events back to android.
* Verify segmentedview works.

High:
* Add openURL function.
* Non-image resources.
* Assets directory that gets merged from any folder containing /assets and importing "gomatcha.io/matcha". What to do about images? _1x.png _2x.png
* Android double tap.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.SpannableString;
import android.util.DisplayMetrics;
import android.util.Log;
Expand Down Expand Up @@ -148,7 +150,13 @@ public GoValue getPropertiesForResource(String path) {

return new GoValue(builder.build().toByteArray());
}


public boolean openURL(String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
context.startActivity(browserIntent);
return true;
}

public void displayAlert(byte[] protobuf) {
try {
final PbAlert.Alert alert = PbAlert.Alert.parseFrom(protobuf);
Expand Down
20 changes: 20 additions & 0 deletions application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,27 @@ image names or folders and this restriction carries over to Matcha as well.
*/
package application

import (
"errors"
"runtime"

"gomatcha.io/matcha/bridge"
)

// // AssetsDir returns the path to the app's assets directory. `NSBundle.mainBundle.resourcePath`
// func AssetsDir() (string, error) {
// return bridge.Bridge("").Call("assetsDir").ToString(), nil
// }

func OpenURL(url string) error {
success := true
if runtime.GOOS == "android" {
success = bridge.Bridge("").Call("openURL", bridge.String(url)).ToBool()
} else {
success = bridge.Bridge("").Call("openURL:", bridge.String(url)).ToBool()
}
if !success {
return errors.New("Unable to open URL")
}
return nil
}
166 changes: 136 additions & 30 deletions bridge/matchaforeign-objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,80 +220,186 @@ ObjcRef MatchaObjcCall(ObjcRef v, CGoBuffer cstr, ObjcRef arguments) {
NSNumber *num = (NSNumber *)argObj;
const char *type = [sig getArgumentTypeAtIndex:i+2];

if (strcmp(type, "c") == 0) {
switch (type[0]) {
case 'c': {
char arg = num.charValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "i") == 0) {
break;
}
case 'i': {
int arg = num.intValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "s") == 0) {
break;
}
case 's': {
short arg = num.shortValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "l") == 0) {
break;
}
case 'l': {
long arg = num.longValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "q") == 0) {
break;
}
case 'q': {
long long arg = num.longLongValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "C") == 0) {
break;
}
case 'C': {
unsigned char arg = num.unsignedCharValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "I") == 0) {
break;
}
case 'I': {
unsigned int arg = num.unsignedIntValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "S") == 0) {
break;
}
case 'S': {
unsigned short arg = num.unsignedShortValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "L") == 0) {
break;
}
case 'L': {
unsigned long arg = num.unsignedLongValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "Q") == 0) {
break;
}
case 'Q': {
unsigned long long arg = num.unsignedLongLongValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "f") == 0) {
break;
}
case 'f': {
float arg = num.floatValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "d") == 0) {
break;
}
case 'd': {
double arg = num.doubleValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "B") == 0) {
break;
}
case 'B': {
bool arg = num.boolValue;
[inv setArgument:&arg atIndex:i+2];
} else if (strcmp(type, "@") == 0) {
break;
}
case '@': {
if ([argObj isKindOfClass:[_MatchaObjcCallSentinel class]]) {
id nilObject = nil;
[inv setArgument:&nilObject atIndex:i+2];
} else {
[inv setArgument:&argObj atIndex:i+2];
}
} else {
break;
}
default: {
@throw @"MatchaObjcCall: Unsupported argument type";
}
}
}

// Invoke.
[inv invoke];

// Get return value.
const char *type = [sig methodReturnType];
id ret = nil;
if (strcmp(type, "c") == 0 || strcmp(type, "i") == 0 || strcmp(type, "s") == 0 || strcmp(type, "l") == 0 || strcmp(type, "q") == 0
|| strcmp(type, "C") == 0 || strcmp(type, "I") == 0 || strcmp(type, "S") == 0 || strcmp(type, "L") == 0 || strcmp(type, "Q") == 0
|| strcmp(type, "f") == 0 || strcmp(type, "d") == 0 || strcmp(type, "B") == 0) {

void *buf = malloc(sig.methodReturnLength);
[inv getReturnValue:&buf];
ret = [[NSNumber alloc] initWithBytes:buf objCType:type];
free(buf);
} else if (strcmp(type, "v") == 0) {
switch (type[0]) {
case 'c': {
char v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'C': {
unsigned char v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'i': {
int v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'I': {
unsigned int v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 's': {
short v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'S': {
unsigned short v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'l': {
long v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'L': {
unsigned long v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'q': {
long long v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'Q': {
unsigned long long v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'f': {
float v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'd': {
double v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'B': {
bool v;
[inv getReturnValue:&v];
ret = @(v);
break;
}
case 'v': {
ret = nil;
} else if (strcmp(type, "@") == 0) {
void *retValue = nil;
[inv getReturnValue:&retValue];
ret = (__bridge id)retValue;
} else {
break;
}
case '@': {
void *v = nil;
[inv getReturnValue:&v];
ret = (__bridge id)v;
break;
}
default: {
@throw @"MatchaObjcCall: Unsupported return type";
}
}
return MatchaTrackObjc(ret);
}

Expand Down
1 change: 1 addition & 0 deletions ios/Matcha/Matcha/MatchaObjcBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- (MatchaGoValue *)imageForResource:(NSString *)path;
- (MatchaGoValue *)propertiesForResource:(NSString *)path;
- (void)displayAlert:(NSData *)protobuf;
- (BOOL)openURL:(NSString *)url;
@end
7 changes: 7 additions & 0 deletions ios/Matcha/Matcha/MatchaObjcBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,11 @@ - (void)displayAlert:(NSData *)protobuf {
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}

- (BOOL)openURL:(NSString *)url {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
#pragma GCC diagnostic pop
}

@end

0 comments on commit 12c7c8e

Please sign in to comment.