Skip to content

Commit

Permalink
Revert: "[ohos,dart]native侧取消业务自定义实现RouterOptions,优化页面返回传参接口易用性,修复nat…
Browse files Browse the repository at this point in the history
…ive页面返回flutter页面时传参可能失败的问题"

Reason: 该修改有bread change,暂时回滚

This reverts commit 8412c44.
  • Loading branch information
0xZOne committed Jul 8, 2024
1 parent 85d82f8 commit 04f44f6
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
5. [dart]优化PlatformView示例代码
6. [dart]增加隐藏platformview的示例代码
7. [dart]补充完善图片格式测试案例
8. Revert: "[ohos,dart]native侧取消业务自定义实现RouterOptions,优化页面返回传参接口易用性..."

## 4.5.11
1. [dart]添加`SystemChrome.setPreferredOrientations`测试案例
Expand Down
11 changes: 8 additions & 3 deletions example/ohos/entry/src/main/ets/entryability/EntryAbility.ets
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import { FlutterManager} from '@ohos/flutter_ohos';
import ApplicationInfoLoader from '@ohos/flutter_ohos/src/main/ets/embedding/engine/loader/ApplicationInfoLoader';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';
import { RouterModule } from '../router/RouterModule';
import { RouterModule, RouterOptions } from '../router/RouterModule';
import { RouterNameConstants } from '../constants/RouterConstants';
import { SimpleTextViewFactory } from '../platform_view/SimpleTextViewFactory';

Expand All @@ -44,7 +44,10 @@ export default class EntryAbility extends UIAbility implements FlutterBoostDeleg

// 打开原生页面
pushNativeRoute(options: FlutterBoostRouteOptions, onPageResult?: (pageName: string, result: Record<string, Object>) => void) {
RouterModule.push(options.getPageName(), options.getArguments(), onPageResult);
RouterModule.push(RouterNameConstants.NATIVE_PAGE, new RouterOptions(
/*"native"*/options.getPageName(),
options.getArguments(),
/*null*/options.getUniqueId()), onPageResult);
}

// 打开flutter页面
Expand All @@ -54,7 +57,9 @@ export default class EntryAbility extends UIAbility implements FlutterBoostDeleg
routerName = RouterNameConstants.FLUTTER_DIALOG
}

RouterModule.push(routerName, options, onPageResult);
RouterModule.push(routerName, new RouterOptions(options.getPageName(),
options.getArguments(),
options.getUniqueId()), onPageResult);
}

// 关闭flutter页面
Expand Down
30 changes: 11 additions & 19 deletions example/ohos/entry/src/main/ets/pages/EntryPage.ets
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@

import hilog from '@ohos.hilog';
import promptAction from '@ohos.promptAction'
import {
FlutterBoost,
FlutterBoostRouteOptions,
FlutterBoostRouteOptionsBuilder } from 'flutter_boost';
import { FlutterBoost } from 'flutter_boost';
import { FlutterUIComponent } from './FlutterUIComponent';
import { FlutterUIPage } from './FlutterUIPage';
import { FlutterUIDialog } from './FlutterUIDialog';
import { NativePage } from './NativePage';
import { RouterModule } from '../router/RouterModule';
import { RouterModule, RouterOptions } from '../router/RouterModule';
import { RouterNameConstants } from '../constants/RouterConstants';

const TAG: string = "EntryPage";
Expand Down Expand Up @@ -64,15 +61,13 @@ struct EntryPage {
}.width('100%')
}

@Builder routerMap(builderName: string, options: ESObject) {
@Builder routerMap(builderName: string, routerOptions: RouterOptions) {
if (builderName == RouterNameConstants.FLUTTER_PAGE) {
FlutterUIPage({ routerOptions: (options as FlutterBoostRouteOptions) })
FlutterUIPage({ routerOptions: routerOptions })
} else if (builderName == RouterNameConstants.FLUTTER_DIALOG) {
FlutterUIDialog({ routerOptions: (options as FlutterBoostRouteOptions) })
FlutterUIDialog({ routerOptions: routerOptions })
} else {
//无论是从dart侧打开native页面还是从native侧打开native页面,都需要在此对builderName(也就是pageName)再次进行分支判断然后选择相应的native页面,
//由于example中只有一个额外的native页面,所以在此不过多展开示范
NativePage({ params: options ? (options as Record<string, Object>) : {} })
NativePage({ params: routerOptions ? routerOptions.params : {} })
}
}

Expand All @@ -99,9 +94,10 @@ struct EntryPage {
.margin({ bottom: 20 })
.onClick(() => {
try {
RouterModule.push(RouterNameConstants.NATIVE_PAGE,
RouterModule.push(RouterNameConstants.NATIVE_PAGE, new RouterOptions(
"native",
{ "message": "A Message From Native" },
(pageName: string, result: Record<string, Object>) => {
null), (pageName: string, result: Record<string, Object>) => {
this.onPageResult(pageName, result)
});
} catch (err) {
Expand Down Expand Up @@ -137,15 +133,11 @@ struct EntryPage {
}.tabBar(this.tabBuilder(0, 'Home'))
// 2. Index
TabContent() {
FlutterUIComponent({ routerOptions: new FlutterBoostRouteOptionsBuilder() //请勿在此对uniqueId进行赋值
.setPageName('flutterPage')
.build() })
FlutterUIComponent({ uri: 'flutterPage' })
}.tabBar(this.tabBuilder(1, 'Index'))
// 3. MediaQuery
TabContent() {
FlutterUIComponent({ routerOptions: new FlutterBoostRouteOptionsBuilder() //请勿在此对uniqueId进行赋值
.setPageName('mediaquery')
.build() })
FlutterUIComponent({ uri: 'mediaquery' })
}.tabBar(this.tabBuilder(2, 'MediaQuery'))
// 4. ArkUI
TabContent() {
Expand Down
22 changes: 13 additions & 9 deletions example/ohos/entry/src/main/ets/pages/FlutterUIComponent.ets
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Curves from '@ohos.curves'
import hilog from '@ohos.hilog';
import { FlutterBoostEntry, FlutterBoostRouteOptions } from 'flutter_boost';
import { FlutterBoostEntry } from 'flutter_boost';
import { FlutterPage, FlutterView } from '@ohos/flutter_ohos';

const TAG: string = "FlutterUIComponent";
@Component
export struct FlutterUIComponent {
@Prop routerOptions: FlutterBoostRouteOptions;
@Prop uri: string;
@Prop params: Record<string, Object> = {};
@Prop xComponentType: XComponentType = XComponentType.SURFACE;

private flutterEntry: FlutterBoostEntry | null = null;
Expand All @@ -16,28 +17,31 @@ export struct FlutterUIComponent {
.combine(TransitionEffect.move(TransitionEdge.END)).animation({curve: Curves.springMotion()});

async aboutToAppear() {
hilog.info(0x0000, TAG, 'Component(#%{public}s) aboutToAppear===', this.routerOptions!.getPageName());
this.flutterEntry = new FlutterBoostEntry(getContext(this), this.routerOptions, false, () => {
// Do nothing: The top-level tab page does not respond to the back gesture.
});
hilog.info(0x0000, TAG, 'Component(#%{public}s) aboutToAppear===', this.uri);
this.flutterEntry = new FlutterBoostEntry(getContext(this),
{ uri: this.uri, params: this.params },
false,
() => {
// Do nothing: The top-level tab page does not respond to the back gesture.
});
await this.flutterEntry.aboutToAppear();
// The |aboutToAppear| method is called first,
// followed by the |getFlutterView| method.
this.flutterView = this.flutterEntry.getFlutterView();
}

aboutToDisappear() {
hilog.info(0x0000, TAG, 'Component(#%{public}s) aboutToDisappear===', this.routerOptions!.getPageName());
hilog.info(0x0000, TAG, 'Component(#%{public}s) aboutToDisappear===', this.uri);
this.flutterEntry?.aboutToDisappear()
}

onPageShow() {
hilog.info(0x0000, TAG, 'Component(#%{public}s) onPageShow===', this.routerOptions!.getPageName());
hilog.info(0x0000, TAG, 'Component(#%{public}s) onPageShow===', this.uri);
this.flutterEntry?.onPageShow()
}

onPageHide() {
hilog.info(0x0000, TAG, 'Component(#%{public}s) onPageHide===', this.routerOptions!.getPageName());
hilog.info(0x0000, TAG, 'Component(#%{public}s) onPageHide===', this.uri);
this.flutterEntry?.onPageHide()
}

Expand Down
5 changes: 3 additions & 2 deletions example/ohos/entry/src/main/ets/pages/FlutterUIDialog.ets
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@

import Curves from '@ohos.curves'
import hilog from '@ohos.hilog';
import { FlutterBoostEntry, FlutterBoostRouteOptions } from 'flutter_boost';
import { FlutterBoostEntry } from 'flutter_boost';
import { FlutterPage, FlutterView } from '@ohos/flutter_ohos';
import { RouterOptions } from '../router/RouterModule';

const TAG: string = "FlutterUIDialog";
@Component
export struct FlutterUIDialog {
private flutterEntry: FlutterBoostEntry | null = null;
private flutterView: FlutterView | null = null;
@Prop xComponentType: XComponentType = XComponentType.SURFACE;
@Prop routerOptions: FlutterBoostRouteOptions;
@Prop routerOptions: RouterOptions;
private effect: object =
TransitionEffect.OPACITY
.combine(TransitionEffect.move(TransitionEdge.BOTTOM)).animation({curve: Curves.springMotion()});
Expand Down
5 changes: 3 additions & 2 deletions example/ohos/entry/src/main/ets/pages/FlutterUIPage.ets
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
*/

import hilog from '@ohos.hilog';
import { FlutterBoostEntry, FlutterBoostRouteOptions } from 'flutter_boost';
import { FlutterBoostEntry } from 'flutter_boost';
import { FlutterPage, FlutterView } from '@ohos/flutter_ohos';
import { RouterOptions } from '../router/RouterModule';

const TAG: string = "FlutterUIPage";
@Component
Expand All @@ -33,7 +34,7 @@ export struct FlutterUIPage {
private flutterView: FlutterView | null = null;
private routeStack: NavPathStack | null = null;
@Prop xComponentType: XComponentType = XComponentType.SURFACE;
@Prop routerOptions: FlutterBoostRouteOptions;
@Prop routerOptions: RouterOptions;

onPopCallback(result: Record<string, Object>): void {
hilog.info(0x0000, TAG, "onPopCallback===");
Expand Down
20 changes: 9 additions & 11 deletions example/ohos/entry/src/main/ets/pages/NativePage.ets
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
* THE SOFTWARE.
*/
import hilog from '@ohos.hilog';
import { FlutterBoostRouteResult } from 'flutter_boost';

import promptAction from '@ohos.promptAction'
import { FlutterBoost, FlutterBoostRouteOptionsBuilder } from 'flutter_boost';
import { FlutterBoost } from 'flutter_boost';
import { FlutterUIComponent } from './FlutterUIComponent';
import { RouterModule } from '../router/RouterModule';
import { RouterModule, RouterOptions } from '../router/RouterModule';
import { RouterNameConstants } from '../constants/RouterConstants';

const TAG: string = "NativePage";
Expand Down Expand Up @@ -86,9 +87,10 @@ export struct NativePage {
.margin({ bottom: 20 })
.onClick(() => {
try {
RouterModule.push(RouterNameConstants.NATIVE_PAGE,
RouterModule.push(RouterNameConstants.NATIVE_PAGE, new RouterOptions(
"native",
{ "message": "A Message From Native" },
(pageName: string, result: Record<string, Object>) => {
null), (pageName: string, result: Record<string, Object>) => {
this.onPageResult(pageName, result)
});
} catch (err) {
Expand Down Expand Up @@ -119,15 +121,11 @@ export struct NativePage {
}.tabBar(this.tabBuilder(0, 'Home'))
// 2. Index
TabContent() {
FlutterUIComponent({ routerOptions: new FlutterBoostRouteOptionsBuilder() //请勿在此对uniqueId进行赋值
.setPageName('flutterPage')
.build() })
FlutterUIComponent({ uri: 'flutterPage' })
}.tabBar(this.tabBuilder(1, 'Index'))
// 3. MediaQuery
TabContent() {
FlutterUIComponent({ routerOptions: new FlutterBoostRouteOptionsBuilder() //请勿在此对uniqueId进行赋值
.setPageName('mediaquery')
.build() })
FlutterUIComponent({ uri: 'mediaquery' })
}.tabBar(this.tabBuilder(2, 'MediaQuery'))
// 4. ArkUI
TabContent() {
Expand All @@ -153,7 +151,7 @@ export struct NativePage {
.hideTitleBar(true)
.onBackPressed(() => {
hilog.info(0x0000, TAG, "onBackPressed===");
RouterModule.pop({ "message": "pop from a native page" });
this.routeStack?.pop(new FlutterBoostRouteResult({ "message": "pop from a native page" }));
return true
})
.onAppear(() => {
Expand Down
24 changes: 17 additions & 7 deletions example/ohos/entry/src/main/ets/router/RouterModule.ets
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { FlutterBoostRouteOptions } from 'flutter_boost';
import { FlutterBoostRouteResult } from 'flutter_boost';

export class RouterOptions {
constructor(uri: string, params: Record<string, Object>, uniqueId: string | null) {
this.uri = uri;
this.params = params;
this.uniqueId = uniqueId;
}
uri: string;
params: Record<string, Object>;
uniqueId: string | null = null; //业务自定义路由参数中需要包含uniqueId,避免丢失dart侧传过来的uniqueId
}

export class RouterModule {
static routeStack: NavPathStack | null = null;
Expand All @@ -7,13 +18,12 @@ export class RouterModule {
RouterModule.routeStack = stack;
}

public static push(name: string, options: ESObject = {}, onPageResult?: (pageName: string, result: Record<string, Object>) => void): void {
public static push(name: string, routerOptions?: RouterOptions, onPageResult?: (pageName: string, result: Record<string, Object>) => void): void {
if (RouterModule.routeStack != null) {
RouterModule.routeStack.pushPath({name: name, param: options, onPop: (popInfo: PopInfo) => {
RouterModule.routeStack.pushPath({name: name, param: routerOptions, onPop: (popInfo: PopInfo) => {
if (onPageResult) {
//如果前一页面是flutter页面,请务必将popInfo.result的类型转换成Record<string, Object> !!!
onPageResult((options instanceof FlutterBoostRouteOptions) ? (options as FlutterBoostRouteOptions).getPageName() : name,
(popInfo && popInfo.result) ? (popInfo.result as Record<string, Object>) : ({} as Record<string, Object>));
onPageResult(routerOptions ? routerOptions.uri : "native",
(popInfo && popInfo.result) ? ((popInfo.result as FlutterBoostRouteResult).result) : ({} as Record<string, Object>));
}
}});
} else {
Expand All @@ -23,7 +33,7 @@ export class RouterModule {

public static pop(result: Record<string, Object> = {}): void {
if (RouterModule.routeStack != null) {
RouterModule.routeStack.pop(result);
RouterModule.routeStack.pop(new FlutterBoostRouteResult(result));
} else {
throw new Error('routeStack is null!');
}
Expand Down
13 changes: 2 additions & 11 deletions lib/src/flutter_boost_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {

late VoidCallback _lifecycleStateListenerRemover;

/// A list of native page's 'Key' who are opened by dart side
final List<String> _nativePageKeys = <String>[];

/// To get the last one in the _nativePageKeys list
String get _topNativePage => _nativePageKeys.last;

@override
void initState() {
assert(
Expand Down Expand Up @@ -615,16 +609,13 @@ class FlutterBoostAppState extends State<FlutterBoostApp> {
final initiatorPage = topContainer?.topPage.pageInfo.uniqueId;
final key = '$initiatorPage#$pageName';
_pendingResult[key] = completer;
_nativePageKeys.add(key);
Logger.log('pendNativeResult, key:$key, size:${_pendingResult.length}');
return completer.future;
}

/// In boost's native side, should avoid calling this method when an outer_route's flutter page
/// pops back to previous outer_route's flutter page.
void onNativeResult(CommonParams params) {
final key = _topNativePage;
_nativePageKeys.remove(key);
final initiatorPage = topContainer?.topPage.pageInfo.uniqueId;
final key = '$initiatorPage#${params.pageName}';
if (_pendingResult.containsKey(key)) {
_pendingResult[key]!.complete(params.arguments);
_pendingResult.remove(key);
Expand Down
5 changes: 4 additions & 1 deletion ohos/index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import FlutterBoostRouteOptions, {
} from './src/main/ets/components/FlutterBoostRouteOptions';

import FlutterBoostAbility from './src/main/ets/components/containers/FlutterBoostAbility';
import FlutterBoostEntry from './src/main/ets/components/containers/FlutterBoostEntry';
import FlutterBoostEntry, {
FlutterBoostRouteResult
} from './src/main/ets/components/containers/FlutterBoostEntry';
import { FlutterBoostPage } from './src/main/ets/components/containers/FlutterBoostPage';
import CommonParams from './src/main/ets/components/messages/CommonParams';

Expand All @@ -47,6 +49,7 @@ export {
FlutterBoostSetupOptionsBuilder,
FlutterBoostRouteOptions,
FlutterBoostRouteOptionsBuilder,
FlutterBoostRouteResult,
FlutterBoostEntry,
FlutterBoostPage,
}
Expand Down
Loading

0 comments on commit 04f44f6

Please sign in to comment.