Skip to content

Commit

Permalink
updated Login and added platformVersion in Flutter Web
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhijeetRanjan308 committed Apr 15, 2024
1 parent e03d4da commit 0d1c9ce
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/web/kommunicate_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:js' as js;
import 'dart:js_util' as js_util;
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'dart:html' as html;

class KommunicateFlutterPluginWeb {
static void registerWith(Registrar registrar) {
Expand All @@ -21,6 +22,8 @@ class KommunicateFlutterPluginWeb {

Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'getPlatformVersion':
return platformVersion();
case 'login':
return login(call.arguments);
case 'logout':
Expand Down Expand Up @@ -53,29 +56,42 @@ class KommunicateFlutterPluginWeb {
Map<String, dynamic> user = jsonDecode(kmUser);
String appId = user["appId"];
String userId = user['userId'];
Map<String, dynamic> registerUserObjc = {
"appId": "$appId",
"automaticChatOpenOnNavigation": true,
"popupWidget": true,
"userId": "$userId"
};
if (user['password'] != null) {
registerUserObjc['password'] = user['password'];
}
if (user['email'] != null) {
registerUserObjc['email'] = user['email'];
}
if (user['authenticationTypeId'] != null){
registerUserObjc['authenticationTypeId'] = user['authenticationTypeId'];
}
if (!appId.isEmpty && !userId.isEmpty) {
String jsCode = '''
(function(d, m){
var kommunicateSettings = {
"appId": "$appId",
"automaticChatOpenOnNavigation": true,
"popupWidget": true,
"userId": "$userId"
};
var s = document.createElement("script");
s.type = "text/javascript";
s.async = true;
s.src = "https://widget.kommunicate.io/v2/kommunicate.app";
var h = document.getElementsByTagName("head")[0];
h.appendChild(s);
window.kommunicate = m;
m._globals = kommunicateSettings;
m._globals = ${jsonEncode(registerUserObjc)};
})(document, window.kommunicate || {});
''';
await js.context.callMethod('eval', [jsCode]);
}
}

Future<dynamic> platformVersion() async {
return 'Flutter Web : ' + html.window.navigator.userAgent;
}

Future<dynamic> logout() async {
await js.context.callMethod('eval', ['Kommunicate.logout()']);
}
Expand Down

0 comments on commit 0d1c9ce

Please sign in to comment.