navigator add openUrl

This commit is contained in:
pengfei.zhou 2020-04-01 15:19:37 +08:00 committed by osborn
parent 9c520e1dc5
commit 78d4f51314
12 changed files with 78 additions and 13 deletions

View File

@ -15,6 +15,9 @@
*/
package pub.doric.plugin;
import android.content.Intent;
import android.net.Uri;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSObject;
@ -84,4 +87,17 @@ public class NavigatorPlugin extends DoricJavaPlugin {
promise.reject(new JavaValue("Navigator not implemented"));
}
}
@DoricMethod(thread = ThreadMode.UI)
public void openUrl(String url, DoricPromise promise) {
try {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
getDoricContext().getContext().startActivity(intent);
promise.resolve();
} catch (Exception e) {
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
}

View File

@ -48,6 +48,17 @@ class NaivgatorDemo extends Panel {
navigator(context).pop()
},
} as IText),
label('OpenURL').apply({
width: 200,
height: 50,
backgroundColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().just(),
onClick: () => {
navigator(context).openUrl("https://doric.pub")
},
} as IText),
]).apply({
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT),
gravity: gravity().center(),

View File

@ -49,4 +49,16 @@ - (void)pop:(NSDictionary *)params {
[self.doricContext.navigator doric_navigator_pop:animated];
});
}
- (void)openUrl:(NSString *)urlString withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
[promise resolve:nil];
} else {
[promise reject:@"Cannot open"];
}
});
}
@end

View File

@ -2662,18 +2662,22 @@ function navbar(context) {
}
function navigator(context) {
var moduleName = "navigator";
return {
push: function (source, config) {
if (config && config.extra) {
config.extra = JSON.stringify(config.extra);
}
return context.callNative('navigator', 'push', {
return context.callNative(moduleName, 'push', {
source: source, config: config
});
},
pop: function (animated) {
if (animated === void 0) { animated = true; }
return context.callNative('navigator', 'pop', { animated: animated });
return context.callNative(moduleName, 'pop', { animated: animated });
},
openUrl: function (url) {
return context.callNative(moduleName, "openUrl", url);
},
};
}

View File

@ -2004,17 +2004,21 @@ function navbar(context) {
}
function navigator(context) {
const moduleName = "navigator";
return {
push: (source, config) => {
if (config && config.extra) {
config.extra = JSON.stringify(config.extra);
}
return context.callNative('navigator', 'push', {
return context.callNative(moduleName, 'push', {
source, config
});
},
pop: (animated = true) => {
return context.callNative('navigator', 'pop', { animated });
return context.callNative(moduleName, 'pop', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},
};
}

View File

@ -3463,17 +3463,21 @@ function navbar(context) {
}
function navigator(context) {
const moduleName = "navigator";
return {
push: (source, config) => {
if (config && config.extra) {
config.extra = JSON.stringify(config.extra);
}
return context.callNative('navigator', 'push', {
return context.callNative(moduleName, 'push', {
source, config
});
},
pop: (animated = true) => {
return context.callNative('navigator', 'pop', { animated });
return context.callNative(moduleName, 'pop', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},
};
}

1
doric-js/index.d.ts vendored
View File

@ -909,6 +909,7 @@ declare module 'doric/lib/src/native/navigator' {
extra?: object | undefined;
} | undefined) => Promise<any>;
pop: (animated?: boolean) => Promise<any>;
openUrl: (url: string) => Promise<any>;
};
}

View File

@ -6,4 +6,5 @@ export declare function navigator(context: BridgeContext): {
extra?: object | undefined;
} | undefined) => Promise<any>;
pop: (animated?: boolean) => Promise<any>;
openUrl: (url: string) => Promise<any>;
};

View File

@ -1,15 +1,19 @@
export function navigator(context) {
const moduleName = "navigator";
return {
push: (source, config) => {
if (config && config.extra) {
config.extra = JSON.stringify(config.extra);
}
return context.callNative('navigator', 'push', {
return context.callNative(moduleName, 'push', {
source, config
});
},
pop: (animated = true) => {
return context.callNative('navigator', 'pop', { animated });
return context.callNative(moduleName, 'pop', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},
};
}

View File

@ -16,6 +16,7 @@
import { BridgeContext } from "../runtime/global"
export function navigator(context: BridgeContext) {
const moduleName = "navigator"
return {
push: (source: string, config?: {
alias?: string,
@ -25,12 +26,15 @@ export function navigator(context: BridgeContext) {
if (config && config.extra) {
(config as any).extra = JSON.stringify(config.extra)
}
return context.callNative('navigator', 'push', {
return context.callNative(moduleName, 'push', {
source, config
})
},
pop: (animated = true) => {
return context.callNative('navigator', 'pop', { animated })
return context.callNative(moduleName, 'pop', { animated })
},
openUrl: (url: string) => {
return context.callNative(moduleName, "openUrl", url)
},
}
}

View File

@ -3521,17 +3521,21 @@ function navbar(context) {
}
function navigator(context) {
const moduleName = "navigator";
return {
push: (source, config) => {
if (config && config.extra) {
config.extra = JSON.stringify(config.extra);
}
return context.callNative('navigator', 'push', {
return context.callNative(moduleName, 'push', {
source, config
});
},
pop: (animated = true) => {
return context.callNative('navigator', 'pop', { animated });
return context.callNative(moduleName, 'pop', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},
};
}

File diff suppressed because one or more lines are too long