feat: enhance plugin navigator,add popSelf and popToRoot

This commit is contained in:
pengfei.zhou 2021-06-08 11:55:21 +08:00 committed by osborn
parent 864ffa0de7
commit 4da1f3be9d
15 changed files with 162 additions and 8 deletions

View File

@ -16,6 +16,7 @@
package pub.doric.plugin;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import com.github.pengfeizhou.jscore.ArchiveException;
@ -114,4 +115,24 @@ public class NavigatorPlugin extends DoricJavaPlugin {
promise.reject(new JavaValue(e.getLocalizedMessage()));
}
}
@DoricMethod(thread = ThreadMode.UI)
public void popSelf(DoricPromise promise) {
IDoricNavigator navigator = getDoricContext().getDoricNavigator();
if (navigator != null) {
navigator.pop();
promise.resolve();
} else {
promise.reject(new JavaValue("Navigator not implemented"));
}
}
@DoricMethod(thread = ThreadMode.UI)
public void popToRoot(DoricPromise promise) {
PackageManager pm = getDoricContext().getContext().getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(getDoricContext().getContext().getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getDoricContext().getContext().startActivity(intent);
promise.resolve();
}
}

View File

@ -1,4 +1,4 @@
import { Panel, scroller, vlayout, text, layoutConfig, LayoutSpec, Color, gravity, Group, navigator, modal } from "doric";
import { Panel, scroller, vlayout, text, layoutConfig, LayoutSpec, Color, gravity, Group, navigator, modal, notification } from "doric";
import { colors, label } from "./utils";
@Entry
class NaivgatorDemo extends Panel {
@ -64,6 +64,46 @@ class NaivgatorDemo extends Panel {
navigator(context).pop()
},
}),
label('Register Pop Self').apply({
width: 200,
height: 50,
backgroundColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().just(),
onClick: () => {
notification(context).subscribe({
name: "PopSelf",
callback: () => {
navigator(context).popSelf()
}
})
},
}),
label('Send Pop Self').apply({
width: 200,
height: 50,
backgroundColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().just(),
onClick: () => {
notification(context).publish({
name: "PopSelf"
})
},
}),
label('Back to Root').apply({
width: 200,
height: 50,
backgroundColor: colors[0],
textSize: 30,
textColor: Color.WHITE,
layoutConfig: layoutConfig().just(),
onClick: () => {
navigator(context).popToRoot()
},
}),
label('OpenURL').apply({
width: 200,
height: 50,

View File

@ -26,6 +26,10 @@ extern NSString *const DORIC_MASK_RETRY;
@interface DoricViewController : UIViewController <DoricNavigatorDelegate, DoricNavBarDelegate>
@property(nonatomic, copy) NSString *source;
@property(nonatomic, copy) NSString *alias;
@property(nonatomic, copy) NSString *extra;
@property(nonatomic, strong) DoricPanel *doricPanel;
@property(nonatomic) BOOL statusBarHidden;
@property(nonatomic) int statusBarMode;

View File

@ -29,9 +29,6 @@ @interface DoricViewController ()
@property(nonatomic) BOOL navBarHidden;
@property(nonatomic, strong) UIImage *navBarImage;
@property(nonatomic, strong) UIView *maskView;
@property(nonatomic, copy) NSString *source;
@property(nonatomic, copy) NSString *alias;
@property(nonatomic, copy) NSString *extra;
@end
@implementation DoricViewController
@ -105,9 +102,33 @@ - (void)doric_navigator_push:(NSString *)source alias:(NSString *)alias animated
}
- (void)doric_navigator_pop:(BOOL)animated {
if (self.presentingViewController) {
[self dismissViewControllerAnimated:animated completion:nil];
} else {
[self.navigationController popViewControllerAnimated:animated];
}
}
- (void)doric_navigator_popSelf:(BOOL)animated {
if (self.presentingViewController) {
[self dismissViewControllerAnimated:animated completion:nil];
} else {
NSMutableArray *tempStack = [self.navigationController.viewControllers mutableCopy];
for (UIViewController *vc in self.navigationController.viewControllers) {
if (vc == self) {
[tempStack removeObject:vc];
break;
}
}
[self.navigationController setViewControllers:[tempStack copy] animated:animated];
}
}
- (void)doric_navigator_popToRoot:(BOOL)animated {
[self.navigationController popToRootViewControllerAnimated:animated];
}
- (BOOL)doric_navBar_isHidden {
return self.navigationController.navigationBarHidden;
}

View File

@ -23,4 +23,8 @@
- (void)doric_navigator_push:(NSString *)source alias:(NSString *)alias animated:(BOOL)animated extra:(NSString *)extra;
- (void)doric_navigator_pop:(BOOL)animated;
- (void)doric_navigator_popSelf:(BOOL)animated;
- (void)doric_navigator_popToRoot:(BOOL)animated;
@end

View File

@ -22,7 +22,7 @@
#import "DoricExtensions.h"
@implementation DoricNavigatorPlugin
- (void)push:(NSDictionary *)params {
- (void)push:(NSDictionary *)params withPromise:(DoricPromise *)promise {
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
@ -35,15 +35,37 @@ - (void)push:(NSDictionary *)params {
alias = [config optString:@"alias" defaultValue:source];
}
[self.doricContext.navigator doric_navigator_push:source alias:alias animated:animated extra:config[@"extra"]];
[promise resolve:nil];
}];
}
- (void)pop:(NSDictionary *)params {
- (void)pop:(NSDictionary *)params withPromise:(DoricPromise *)promise {
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
BOOL animated = [params optBool:@"animated" defaultValue:YES];
[self.doricContext.navigator doric_navigator_pop:animated];
[promise resolve:nil];
}];
}
- (void)popSelf:(NSDictionary *)params withPromise:(DoricPromise *)promise {
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
BOOL animated = [params optBool:@"animated" defaultValue:YES];
[self.doricContext.navigator doric_navigator_popSelf:animated];
[promise resolve:nil];
}];
}
- (void)popToRoot:(NSDictionary *)params withPromise:(DoricPromise *)promise {
__weak typeof(self) _self = self;
[self.doricContext dispatchToMainQueue:^{
__strong typeof(_self) self = _self;
BOOL animated = [params optBool:@"animated" defaultValue:YES];
[self.doricContext.navigator doric_navigator_popToRoot:animated];
[promise resolve:nil];
}];
}

View File

@ -3120,6 +3120,14 @@ function navigator(context) {
if (animated === void 0) { animated = true; }
return context.callNative(moduleName, 'pop', { animated: animated });
},
popSelf: function (animated) {
if (animated === void 0) { animated = true; }
return context.callNative(moduleName, 'popSelf', { animated: animated });
},
popToRoot: function (animated) {
if (animated === void 0) { animated = true; }
return context.callNative(moduleName, 'popToRoot', { animated: animated });
},
openUrl: function (url) {
return context.callNative(moduleName, "openUrl", url);
},

View File

@ -2419,6 +2419,12 @@ function navigator(context) {
pop: (animated = true) => {
return context.callNative(moduleName, 'pop', { animated });
},
popSelf: (animated = true) => {
return context.callNative(moduleName, 'popSelf', { animated });
},
popToRoot: (animated = true) => {
return context.callNative(moduleName, 'popToRoot', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},

View File

@ -3940,6 +3940,12 @@ function navigator(context) {
pop: (animated = true) => {
return context.callNative(moduleName, 'pop', { animated });
},
popSelf: (animated = true) => {
return context.callNative(moduleName, 'popSelf', { animated });
},
popToRoot: (animated = true) => {
return context.callNative(moduleName, 'popToRoot', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},

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

@ -887,6 +887,8 @@ declare module 'doric/lib/src/native/navigator' {
singlePage?: boolean | undefined;
} | undefined) => Promise<any>;
pop: (animated?: boolean) => Promise<any>;
popSelf: (animated?: boolean) => Promise<any>;
popToRoot: (animated?: boolean) => Promise<any>;
openUrl: (url: string) => Promise<any>;
};
}

View File

@ -10,5 +10,7 @@ export declare function navigator(context: BridgeContext): {
singlePage?: boolean | undefined;
} | undefined) => Promise<any>;
pop: (animated?: boolean) => Promise<any>;
popSelf: (animated?: boolean) => Promise<any>;
popToRoot: (animated?: boolean) => Promise<any>;
openUrl: (url: string) => Promise<any>;
};

View File

@ -18,6 +18,12 @@ export function navigator(context) {
pop: (animated = true) => {
return context.callNative(moduleName, 'pop', { animated });
},
popSelf: (animated = true) => {
return context.callNative(moduleName, 'popSelf', { animated });
},
popToRoot: (animated = true) => {
return context.callNative(moduleName, 'popToRoot', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},

View File

@ -43,6 +43,12 @@ export function navigator(context: BridgeContext) {
pop: (animated = true) => {
return context.callNative(moduleName, 'pop', { animated })
},
popSelf: (animated = true) => {
return context.callNative(moduleName, 'popSelf', { animated })
},
popToRoot: (animated = true) => {
return context.callNative(moduleName, 'popToRoot', { animated })
},
openUrl: (url: string) => {
return context.callNative(moduleName, "openUrl", url)
},

View File

@ -3994,6 +3994,12 @@ function navigator(context) {
pop: (animated = true) => {
return context.callNative(moduleName, 'pop', { animated });
},
popSelf: (animated = true) => {
return context.callNative(moduleName, 'popSelf', { animated });
},
popToRoot: (animated = true) => {
return context.callNative(moduleName, 'popToRoot', { animated });
},
openUrl: (url) => {
return context.callNative(moduleName, "openUrl", url);
},

File diff suppressed because one or more lines are too long