Skip to content

Commit 7591dc3

Browse files
author
LiYuLong
committed
Improve Code generator
1 parent c572679 commit 7591dc3

8 files changed

Lines changed: 154 additions & 100 deletions

File tree

.github/workflows/publish.yml

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,34 @@ on:
66
tags:
77
- 'v[0-9]+.[0-9]+.[0-9]+' # tag pattern on pub.dev: 'v{{version}'
88

9-
# Publish using custom workflow
109
jobs:
11-
publish:
12-
permissions:
13-
id-token: write # Required for authentication using OIDC
14-
runs-on: ubuntu-latest
10+
publish:
11+
permissions:
12+
id-token: write # Required for authentication using OIDC
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
os: [ubuntu-latest]
17+
sdk: [3.7.0]
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
- uses: dart-lang/setup-dart@v1
22+
with:
23+
sdk: ${{ matrix.sdk }}
1524

16-
steps:
17-
- name: Checkout
18-
uses: actions/checkout@v2
19-
- name: Setup Flutter
20-
uses: subosito/flutter-action@v2
21-
with:
22-
flutter-version: '3.7.0'
23-
24-
- name: Validate package
25-
run: flutter pub publish --dry-run
26-
27-
- name: Publish to pub.dev
28-
run: flutter pub publish --force
29-
env:
30-
PUB_HOSTED_URL: https://pub.dev
25+
- name: Setup Flutter
26+
uses: subosito/flutter-action@v2
27+
with:
28+
flutter-version: '1.17.0'
29+
30+
- name: Validate package
31+
run: flutter pub publish --dry-run
32+
- name: format dart files
33+
run: dart format .
34+
35+
- name: Publish to pub.dev
36+
run: flutter pub publish --force
37+
env:
38+
PUB_HOSTED_URL: https://pub.dev
39+

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@
55
## 0.0.2
66

77
* improve code generator
8-
* fixe some warnning
8+
* fixe some warnning
9+
10+
## 0.0.3
11+
12+
* improve code generator
13+
* more example case

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,74 @@ class JsApi {
6969
}
7070
```
7171

72+
Inject the JSObject into webView
73+
```dart
74+
final DWebViewController controller =
75+
DWebViewController.fromPlatformCreationParams(params);
76+
// #enddocregion platform_features
77+
78+
controller
79+
..setBackgroundColor(Colors.white)
80+
..loadFlutterAsset('assets/js-call-dart.html')
81+
..addJavaScriptObject(JsApiWrapper())
82+
..addJavaScriptObject(JsEchoApiWrapper(), namespace: 'echo')
83+
..setNavigationDelegate(
84+
NavigationDelegate(
85+
onProgress: (progress) {
86+
print('process=$progress');
87+
},
88+
onPageStarted: (url) {
89+
print('url onPageStarted');
90+
},
91+
onPageFinished: (url) {
92+
print('url onPageFinished');
93+
},
94+
),
95+
);
96+
```
97+
7298
this will export an object as the bridge between JS and Native, then you can get JS callback (sync or async) by this bridge
7399

100+
So you can simple call item as follow case:
101+
```dart
102+
FilledButton(
103+
child: const Text('addValue(3,4)'),
104+
onPressed: () {
105+
_controller.callHandler(
106+
'addValue',
107+
args: [3, 4],
108+
handler: (retValue) {
109+
Fluttertoast.showToast(msg: retValue.toString());
110+
},
111+
);
112+
},
113+
),
114+
FilledButton(
115+
child: const Text("append('I','love','you')"),
116+
onPressed: () {
117+
_controller.callHandler(
118+
'append',
119+
args: ["I", "love", "you"],
120+
handler: (retValue) {
121+
Fluttertoast.showToast(msg: retValue.toString());
122+
},
123+
);
124+
},
125+
),
126+
```
127+
128+
and the equal js as are these:
129+
```javascript
130+
dsBridge.register('addValue', function (r, l) {
131+
return r + l;
132+
})
133+
134+
dsBridge.registerAsyn('append', function (arg1, arg2, arg3, responseCallback) {
135+
responseCallback(arg1 + " " + arg2 + " " + arg3);
136+
})
137+
```
138+
more examples are in the example demo . you can download to enjoy it.
139+
74140
## Finally
75141

76142
If you like DSBridge for Flutter, please click star/like to let more people know it, Thanks!

example/lib/src/page/dart_call_js_page.dart

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ class _DartCallJsPageState extends State<DartCallJsPage> {
4646
Widget build(BuildContext context) {
4747
return Scaffold(
4848
backgroundColor: Colors.white,
49-
appBar: AppBar(
50-
title: const Text('Dart call JavaScript'),
51-
),
49+
appBar: AppBar(title: const Text('Dart call JavaScript')),
5250
body: Padding(
5351
padding: const EdgeInsets.symmetric(horizontal: 20),
5452
child: SingleChildScrollView(
@@ -58,61 +56,82 @@ class _DartCallJsPageState extends State<DartCallJsPage> {
5856
FilledButton(
5957
child: const Text('addValue(3,4)'),
6058
onPressed: () {
61-
_controller.callHandler('addValue', args: [3, 4],
62-
handler: (retValue) {
63-
Fluttertoast.showToast(msg: retValue.toString());
64-
});
59+
_controller.callHandler(
60+
'addValue',
61+
args: [3, 4],
62+
handler: (retValue) {
63+
Fluttertoast.showToast(msg: retValue.toString());
64+
},
65+
);
6566
},
6667
),
6768
FilledButton(
6869
child: const Text("append('I','love','you')"),
6970
onPressed: () {
70-
_controller.callHandler('append', args: ["I", "love", "you"],
71-
handler: (retValue) {
72-
Fluttertoast.showToast(msg: retValue.toString());
73-
});
71+
_controller.callHandler(
72+
'append',
73+
args: ["I", "love", "you"],
74+
handler: (retValue) {
75+
Fluttertoast.showToast(msg: retValue.toString());
76+
},
77+
);
7478
},
7579
),
7680
FilledButton(
7781
child: const Text('startTimer()'),
7882
onPressed: () {
79-
_controller.callHandler('startTimer', handler: (retValue) {
80-
Fluttertoast.showToast(msg: retValue.toString());
81-
});
83+
_controller.callHandler(
84+
'startTimer',
85+
handler: (retValue) {
86+
Fluttertoast.showToast(msg: retValue.toString());
87+
},
88+
);
8289
},
8390
),
8491
FilledButton(
8592
child: const Text('syn.addValue(5,6)'),
8693
onPressed: () {
87-
_controller.callHandler('syn.addValue', args: [5, 6],
88-
handler: (retValue) {
89-
Fluttertoast.showToast(msg: retValue.toString());
90-
});
94+
_controller.callHandler(
95+
'syn.addValue',
96+
args: [5, 6],
97+
handler: (retValue) {
98+
Fluttertoast.showToast(msg: retValue.toString());
99+
},
100+
);
91101
},
92102
),
93103
FilledButton(
94104
child: const Text('syn.getInfo()'),
95105
onPressed: () {
96-
_controller.callHandler('syn.getInfo', handler: (retValue) {
97-
Fluttertoast.showToast(msg: retValue.toString());
98-
});
106+
_controller.callHandler(
107+
'syn.getInfo',
108+
handler: (retValue) {
109+
Fluttertoast.showToast(msg: retValue.toString());
110+
},
111+
);
99112
},
100113
),
101114
FilledButton(
102115
child: const Text('asyn.addValue(5,6)'),
103116
onPressed: () {
104-
_controller.callHandler('asyn.addValue', args: [5, 6],
105-
handler: (retValue) {
106-
Fluttertoast.showToast(msg: retValue.toString());
107-
});
117+
_controller.callHandler(
118+
'asyn.addValue',
119+
args: [5, 6],
120+
handler: (retValue) {
121+
Fluttertoast.showToast(msg: retValue.toString());
122+
},
123+
);
108124
},
109125
),
110126
FilledButton(
111127
child: const Text('asyn.getInfo()'),
112128
onPressed: () {
113-
_controller.callHandler('asyn.getInfo', handler: (retValue) {
114-
Fluttertoast.showToast(msg: retValue.toString());
115-
});
129+
_controller.callHandler(
130+
'asyn.getInfo',
131+
handler: (retValue) {
132+
Fluttertoast.showToast(msg: retValue.toString());
133+
},
134+
);
116135
},
117136
),
118137
FilledButton(
@@ -150,7 +169,7 @@ class _DartCallJsPageState extends State<DartCallJsPage> {
150169
SizedBox(
151170
height: 10,
152171
child: DWebViewWidget(controller: _controller),
153-
)
172+
),
154173
],
155174
),
156175
),

lib/src/annotations/annotations.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ class dsBridge {
1010
final String? as;
1111
const dsBridge({this.async = false, this.as});
1212
}
13-

lib/src/generator/code_info/code_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ abstract class CodeGenerator {
1717
Element element,
1818
ConstantReader annotation,
1919
BuildStep buildStep,
20-
);
20+
) {}
2121
}

lib/src/generator/code_info/method_code_generator.dart

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,7 @@ class MethodCodeGenerator extends CodeGenerator {
1212
Element element,
1313
ConstantReader annotation,
1414
BuildStep buildStep,
15-
) {
16-
// final method = element as MethodElement;
17-
// String methodName = method.name;
18-
// final methodAnnotationObject =
19-
// method.metadata
20-
// .firstWhere(
21-
// (meta) =>
22-
// meta.computeConstantValue()?.type?.getDisplayString() ==
23-
// 'dsBridge',
24-
// )
25-
// .computeConstantValue();
26-
// final isAsync = methodAnnotationObject?.getField('async')!.toBoolValue();
27-
28-
// final asMethodName =
29-
// (methodAnnotationObject?.getField('as')?.toStringValue() ?? methodName)
30-
// .trim()
31-
// .replaceAll(" ", "");
32-
33-
// registerFunction =
34-
// "registerFunction($asMethodName,functionName:'$asMethodName');";
35-
36-
// // 获取方法的参数列表
37-
// final parameters = method.parameters
38-
// .map((param) {
39-
// final paramName = param.name;
40-
// final paramType = param.type.getDisplayString();
41-
42-
// return '$paramType $paramName';
43-
// })
44-
// .join(', ');
45-
46-
// // 获取方法的返回类型
47-
// final returnType = method.returnType.getDisplayString();
48-
// // 生成调用 _wrappedInstance 方法的代码
49-
50-
// code = '''
51-
// @pragma('vm:entry-point')
52-
// $returnType $asMethodName($parameters) {
53-
// debugPrint('Before calling $methodName with annotation: isAsync - $isAsync as - $asMethodName');
54-
// ${returnType == 'void' ? '' : 'final result ='} _wrappedInstance.$methodName(${method.parameters.map((p) => p.name).join(', ')}); // 调用被包装类的方法
55-
// debugPrint('After calling $methodName');
56-
// ${returnType == 'void' ? '' : 'return result;'}
57-
// }
58-
// ''';
59-
}
15+
) {}
6016

6117
String get registerFunction =>
6218
"registerFunction($asMethodName,functionName:'$asMethodName');";

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: flutter_dsbridge_annotation
22
description: "A modern cross-platform JavaScript bridge annotation"
3-
version: 0.0.2
3+
version: 0.0.3
44
homepage: https://github.com/skeyboy/flutter_dsbridge_annotation
55

66
environment:
7-
sdk: ^3.7.0
7+
sdk: ">=3.7.0 <4.0.0"
88
flutter: ">=1.17.0"
99

1010
dependencies:
@@ -26,5 +26,5 @@ topics:
2626
- jsbridge
2727
- javascript
2828
- webview
29-
- html
29+
- wkwebview
3030
- dsbridge

0 commit comments

Comments
 (0)