Skip to content

Commit 0ac0415

Browse files
committed
refactor: 💡 clean code
1 parent d756d6a commit 0ac0415

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

‎lib/src/chative_widget.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,35 @@ class ChativeWidgetController {
7676
/// A StatefulWidget that displays a chat interface using WebView
7777
class ChativeWidget extends StatefulWidget {
7878
final ChativeWidgetController? controller;
79+
80+
/// The channel ID to be used for the chat widget
7981
final String channelId;
82+
83+
/// The user data to be passed to the chat widget
8084
final Map<String, dynamic>? user;
85+
86+
/// The widget to be displayed as the header of the chat widget
8187
final Widget? headerWidget;
88+
89+
/// The decoration to be applied to the chat widget container
8290
final BoxDecoration? containerDecoration;
91+
92+
/// The top inset of the chat widget
8393
final double insetTop;
94+
95+
/// The bottom inset of the chat widget
8496
final double insetBottom;
97+
98+
/// Callback function to be called when the chat widget is closed
8599
final OnClosed? onClosed;
100+
101+
/// Callback function to be called when the chat widget is loaded
86102
final OnLoaded? onLoaded;
103+
104+
/// Callback function to be called when a new message is received
87105
final OnNewMessage? onNewMessage;
106+
107+
/// Callback function to be called when an error occurs
88108
final OnError? onError;
89109

90110
const ChativeWidget({

‎lib/src/utils.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:convert';
22

33
import 'package:chative_sdk/src/constants.dart';
44

5+
/// Generates the JavaScript script to be injected into the WebView
56
String generateScript(Map<String, dynamic> user) {
67
String userString = jsonEncode(user);
78
return '''
@@ -37,6 +38,7 @@ String generateScript(Map<String, dynamic> user) {
3738
''';
3839
}
3940

41+
/// Generates the JavaScript script to be injected into the WebView to get the config
4042
String generateScriptGetError(String channelId) {
4143
return '''
4244
function getTimeZone() {
@@ -69,6 +71,7 @@ String generateScriptGetError(String channelId) {
6971
''';
7072
}
7173

74+
/// Converts a Dart object into a JavaScript object string
7275
String widgetApi(String event, dynamic data) {
7376
String dataString;
7477
if (data is String) {
@@ -118,3 +121,12 @@ bool isScriptSafe(String script) {
118121

119122
return true;
120123
}
124+
125+
/// Safely parses a JSON string into a Map
126+
Map<String, dynamic> parseJson(String message) {
127+
try {
128+
return jsonDecode(message) as Map<String, dynamic>;
129+
} catch (e) {
130+
return {};
131+
}
132+
}

‎lib/src/webview.dart‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dart:convert';
21
import 'dart:io';
32

43
import 'package:file_picker/file_picker.dart';
@@ -16,22 +15,33 @@ typedef OnNewMessage = void Function();
1615
typedef OnError = void Function(String message);
1716

1817
class Webview extends StatefulWidget {
18+
/// The unique identifier of the channel
1919
final String channelId;
20+
21+
/// The user information to be passed to the widget
2022
final Map<String, dynamic>? user;
23+
24+
/// Callback when the widget has loaded
2125
final OnLoaded? onLoaded;
26+
27+
/// Callback when the widget has been closed
2228
final OnClosed? onClosedWidget;
29+
30+
/// Callback when a new message has been received
2331
final OnNewMessage? onNewMessage;
32+
33+
/// Callback when an error has occurred
2434
final OnError? onError;
2535

2636
const Webview({
27-
Key? key,
37+
super.key,
2838
required this.channelId,
2939
this.user,
3040
this.onLoaded,
3141
this.onClosedWidget,
3242
this.onNewMessage,
3343
this.onError,
34-
}) : super(key: key);
44+
});
3545

3646
@override
3747
State<Webview> createState() => WebviewState();
@@ -72,7 +82,7 @@ class WebviewState extends State<Webview> {
7282

7383
/// Handles JavaScript messages received from the WebView
7484
void _handleJavaScriptMessage(JavaScriptMessage message) {
75-
final parsedData = _parseJson(message.message);
85+
final parsedData = parseJson(message.message);
7686

7787
switch (parsedData['event']) {
7888
case 'closed':
@@ -93,15 +103,6 @@ class WebviewState extends State<Webview> {
93103
}
94104
}
95105

96-
/// Safely parses a JSON string into a Map
97-
Map<String, dynamic> _parseJson(String message) {
98-
try {
99-
return jsonDecode(message) as Map<String, dynamic>;
100-
} catch (e) {
101-
return {};
102-
}
103-
}
104-
105106
/// Creates a NavigationDelegate for the WebView
106107
NavigationDelegate _createNavigationDelegate() {
107108
return NavigationDelegate(
@@ -203,6 +204,12 @@ class WebviewState extends State<Webview> {
203204

204205
/// Displays an external WebView when the URL is outside the widgetUrl
205206
void _showExternalWebView(String url) {
207+
Uri uri = Uri.parse(url);
208+
209+
if (uri.scheme != 'https' && uri.scheme != 'http') {
210+
return;
211+
}
212+
206213
_externalController = WebViewController()
207214
..setJavaScriptMode(JavaScriptMode.unrestricted)
208215
..loadRequest(Uri.parse(url));

0 commit comments

Comments
 (0)