-
Notifications
You must be signed in to change notification settings - Fork 2
/
pinwheel.dart
246 lines (229 loc) · 8.5 KB
/
pinwheel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'models.dart';
import 'serializers.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
typedef PinwheelExitCallback = void Function(PinwheelExitPayload? payload);
typedef PinwheelErrorCallback = void Function(PinwheelError error);
typedef PinwheelEventCallback = void Function(String name, PinwheelEventPayload? payload);
typedef PinwheelSuccessCallback = void Function(PinwheelSuccessPayload payload);
typedef PinwheelLoginCallback = void Function(PinwheelLoginPayload payload);
typedef PinwheelLoginAttemptCallback = void Function(PinwheelLoginAttemptPayload payload);
class Pinwheel {
static const MethodChannel _channel =
const MethodChannel('pinwheel', JSONMethodCodec());
static final _standardSerializers =
(serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
PinwheelErrorCallback? _onError;
PinwheelExitCallback? _onExit;
PinwheelEventCallback? _onEvent;
PinwheelSuccessCallback? _onSuccess;
PinwheelLoginCallback? _onLogin;
PinwheelLoginAttemptCallback? _onLoginAttempt;
Pinwheel(
int id,
PinwheelErrorCallback? onError,
PinwheelExitCallback? onExit,
PinwheelEventCallback? onEvent,
PinwheelSuccessCallback? onSuccess,
PinwheelLoginCallback? onLogin,
PinwheelLoginAttemptCallback? onLoginAttempt
) {
_channel.setMethodCallHandler(_handleMethod);
_onError = onError;
_onExit = onExit;
_onEvent = onEvent;
_onSuccess = onSuccess;
_onLogin = onLogin;
_onLoginAttempt = onLoginAttempt;
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case 'onEvent':
try {
String name;
PinwheelEventPayload? payload;
if (call.arguments != null && call.arguments != "null") {
var data = _standardSerializers.deserializeWith(
PinwheelEventChannelArgument.serializer, json.decode(call.arguments))!;
name = data.name;
if (data.payload != null) {
String payloadString = data.payload!;
switch (name) {
case 'select_employer':
payload = _standardSerializers.deserializeWith(PinwheelSelectedEmployerPayload.serializer, json.decode(payloadString))!;
break;
case 'select_platform':
payload = _standardSerializers.deserializeWith(PinwheelSelectedPlatformPayload.serializer, json.decode(payloadString))!;
break;
case 'login':
payload = _standardSerializers.deserializeWith(PinwheelLoginPayload.serializer, json.decode(payloadString))!;
break;
case 'login_attempt':
payload = _standardSerializers.deserializeWith(PinwheelLoginAttemptPayload.serializer, json.decode(payloadString))!;
break;
case 'input_allocation':
payload = _standardSerializers.deserializeWith(PinwheelInputAllocationPayload.serializer, json.decode(payloadString));
break;
case 'exit':
payload = _standardSerializers.deserializeWith(PinwheelSelectedEmployerPayload.serializer, json.decode(payloadString))!;
break;
case 'success':
payload = _standardSerializers.deserializeWith(PinwheelSuccessPayload.serializer, json.decode(payloadString))!;
break;
case 'error':
payload = _standardSerializers.deserializeWith(PinwheelError.serializer, json.decode(payloadString))!;
break;
case 'dd_form_create':
payload = _standardSerializers.deserializeWith(PinwheelDDFormCreatePayload.serializer, json.decode(payloadString))!;
break;
case 'screen_transition':
payload = _standardSerializers.deserializeWith(PinwheelScreenTransitionPayload.serializer, json.decode(payloadString))!;
break;
}
}
if (_onEvent != null) {
_onEvent!(name, payload);
}
}
} catch(error) {
print(error);
}
break;
case 'onExit':
try {
PinwheelExitPayload? result;
if (call.arguments != null && call.arguments != "null") {
result = _standardSerializers.deserializeWith(PinwheelExitPayload.serializer, json.decode(call.arguments))!;
}
if (_onExit != null) {
_onExit!(result);
}
} catch(error) {
print(error);
}
break;
case 'onError':
try {
var result = _standardSerializers.deserializeWith(PinwheelError.serializer, json.decode(call.arguments))!;
if (_onError != null) {
_onError!(result);
}
} catch(error) {
}
break;
case 'onSuccess':
try {
var result = _standardSerializers.deserializeWith(PinwheelSuccessPayload.serializer, json.decode(call.arguments))!;
if (_onSuccess != null) {
_onSuccess!(result);
}
} catch(error) {
print(error);
}
break;
case 'onLogin':
try {
var result = _standardSerializers.deserializeWith(PinwheelLoginPayload.serializer, json.decode(call.arguments))!;
if (_onLogin != null) {
_onLogin!(result);
}
} catch(error) {
print(error);
}
break;
case 'onLoginAttempt':
try {
var result = _standardSerializers.deserializeWith(PinwheelLoginAttemptPayload.serializer, json.decode(call.arguments))!;
if (_onLoginAttempt != null) {
_onLoginAttempt!(result);
}
} catch(error) {
print(error);
}
break;
}
}
}
class PinwheelLink extends StatefulWidget {
final String token;
final PinwheelExitCallback? onExit;
final PinwheelErrorCallback? onError;
final PinwheelEventCallback? onEvent;
final PinwheelSuccessCallback? onSuccess;
final PinwheelLoginCallback? onLogin;
final PinwheelLoginAttemptCallback? onLoginAttempt;
const PinwheelLink ({
Key? key,
required this.token,
this.onExit,
this.onError,
this.onEvent,
this.onSuccess,
this.onLogin,
this.onLoginAttempt
}): super(key: key);
@override
PinwheelLinkState createState() => PinwheelLinkState();
}
class PinwheelLinkState extends State<PinwheelLink> {
Pinwheel? controller;
Widget build(BuildContext context) {
final String viewType = 'pinwheel-link-view';
final Map<String, String> creationParams = {
"token": widget.token
};
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return PlatformViewLink(
viewType: viewType,
surfaceFactory: (BuildContext context, PlatformViewController controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: JSONMessageCodec(),
)
..addOnPlatformViewCreatedListener((int id) {
params.onPlatformViewCreated(id);
_onPlatformViewCreated(id);
})
..create();
},
);
case TargetPlatform.iOS:
return UiKitView(
viewType: viewType,
onPlatformViewCreated: _onPlatformViewCreated,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: JSONMessageCodec(),
);
default:
throw UnsupportedError("Unsupported platform view");
}
}
void _onPlatformViewCreated(int id) {
controller = Pinwheel(
id,
widget.onError,
widget.onExit,
widget.onEvent,
widget.onSuccess,
widget.onLogin,
widget.onLoginAttempt,
);
}
}