Skip to content

Commit

Permalink
Minor code refactoring (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
abedelaziz-deriv authored Aug 5, 2024
1 parent 78da39e commit ae8dd81
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 55 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.5

* Minor code refactoring.
* Fixed a bug in iOS when passing null customParams.

## 1.0.4

* Updated README
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Step 2: To avoid issues with proguard enabled, include the following to proguard
### Web
Inside index.html file, add this line inside `<head></head>` tag:
```html
<script src="./packages/live_chat_plus/src/js/live_chat.js" defer></script>
<script src="./assets/packages/live_chat_plus/assets/live_chat.js" defer></script>
```

## Usage
Expand Down
84 changes: 84 additions & 0 deletions assets/live_chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
function startLiveChat(license, userName, email) {
window.__lc = window.__lc || {};
window.__lc.license = license;;
window.__lc.asyncInit = true;
(function(n, t, c) {
function i(n) {
return e._h ? e._h.apply(null, n) : e._q.push(n)
}
var e = {
_q: [],
_h: null,
_v: "2.0",
on: function() {
i(["on", c.call(arguments)])
},
once: function() {
i(["once", c.call(arguments)])
},
off: function() {
i(["off", c.call(arguments)])
},
get: function() {
if (!e._h) throw new Error("[LiveChatWidget] You can't use getters before load.");
return i(["get", c.call(arguments)])
},
call: function() {
i(["call", c.call(arguments)])
},
init: function() {
var n = t.createElement("script");
n.async = !0, n.type = "text/javascript", n.src = "https://cdn.livechatinc.com/tracking.js", t.head.appendChild(n)
}
};
!n.__lc.asyncInit && e.init(), n.LiveChatWidget = n.LiveChatWidget || e
}(window, document, [].slice));
window.LiveChatWidget.init();
window.LiveChatWidget.on('ready', function() {
window.eventProducer.emit('event', 'chatOpen');
if (window.LiveChatWidget && typeof window.LiveChatWidget.call === 'function') {
window.LiveChatWidget.call('set_customer_email', email);
window.LiveChatWidget.call('set_customer_name', userName);
}
});

if (window.LiveChatWidget && typeof window.LiveChatWidget.call === 'function') {
window.LiveChatWidget.call('maximize');
}

window.LiveChatWidget.on('new_event', onNewEvent);
}

function onNewEvent(event) {
window.eventProducer.emit('event', event.type);
}

function hideWindow() {
LiveChatWidget.call("hide");
}

function destroyWindow() {
LiveChatWidget.call("destroy");
}


class EventProducer {
constructor() {
this.listeners = {};
}

on(event, listener) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(listener);
}

emit(event, data) {
if (this.listeners[event]) {
this.listeners[event].forEach(listener => listener(data));
}
}
}

window.eventProducer = new EventProducer();
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ packages:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
url: "https://pub.dev"
source: hosted
version: "0.6.7"
version: "0.7.1"
lints:
dependency: transitive
description:
Expand All @@ -65,7 +65,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.3"
version: "1.0.5"
material_color_utilities:
dependency: transitive
description:
Expand Down
84 changes: 84 additions & 0 deletions example/web/assets/packages/live_chat_plus/assets/live_chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
function startLiveChat(license, userName, email) {
window.__lc = window.__lc || {};
window.__lc.license = license;;
window.__lc.asyncInit = true;
(function(n, t, c) {
function i(n) {
return e._h ? e._h.apply(null, n) : e._q.push(n)
}
var e = {
_q: [],
_h: null,
_v: "2.0",
on: function() {
i(["on", c.call(arguments)])
},
once: function() {
i(["once", c.call(arguments)])
},
off: function() {
i(["off", c.call(arguments)])
},
get: function() {
if (!e._h) throw new Error("[LiveChatWidget] You can't use getters before load.");
return i(["get", c.call(arguments)])
},
call: function() {
i(["call", c.call(arguments)])
},
init: function() {
var n = t.createElement("script");
n.async = !0, n.type = "text/javascript", n.src = "https://cdn.livechatinc.com/tracking.js", t.head.appendChild(n)
}
};
!n.__lc.asyncInit && e.init(), n.LiveChatWidget = n.LiveChatWidget || e
}(window, document, [].slice));
window.LiveChatWidget.init();
window.LiveChatWidget.on('ready', function() {
window.eventProducer.emit('event', 'chatOpen');
if (window.LiveChatWidget && typeof window.LiveChatWidget.call === 'function') {
window.LiveChatWidget.call('set_customer_email', email);
window.LiveChatWidget.call('set_customer_name', userName);
}
});

if (window.LiveChatWidget && typeof window.LiveChatWidget.call === 'function') {
window.LiveChatWidget.call('maximize');
}

window.LiveChatWidget.on('new_event', onNewEvent);
}

function onNewEvent(event) {
window.eventProducer.emit('event', event.type);
}

function hideWindow() {
LiveChatWidget.call("hide");
}

function destroyWindow() {
LiveChatWidget.call("destroy");
}


class EventProducer {
constructor() {
this.listeners = {};
}

on(event, listener) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(listener);
}

emit(event, data) {
if (this.listeners[event]) {
this.listeners[event].forEach(listener => listener(data));
}
}
}

window.eventProducer = new EventProducer();
2 changes: 1 addition & 1 deletion example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<title>live_chat_example</title>
<link rel="manifest" href="manifest.json">

<script src="./packages/live_chat_plus/src/js/live_chat.js" defer></script>
<script src="./assets/packages/live_chat_plus/assets/live_chat.js" defer></script>
</head>
<body>
<script src="flutter_bootstrap.js" async></script>
Expand Down
8 changes: 5 additions & 3 deletions ios/Classes/LiveChatPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class LiveChatPlugin: NSObject, FlutterPlugin, LiveChatDelegate, FlutterS
let visitorName = (arguments["visitorName"] as? String)
let visitorEmail = (arguments["visitorEmail"] as? String)
let groupId = (arguments["groupId"] as? String)
let customParams = (arguments["customParams"] as! [String: String])
let customParams = (arguments["customParams"] as? [String: String])

if licenseId == "" {
result(FlutterError(code: "", message: "LICENSE NUMBER EMPTY", details: nil))
Expand All @@ -40,8 +40,10 @@ public class LiveChatPlugin: NSObject, FlutterPlugin, LiveChatDelegate, FlutterS
LiveChat.email = visitorEmail
LiveChat.groupId = groupId

for (key, value) in customParams {
LiveChat.setVariable(withKey: key, value: value)
if(customParams != nil) {
for (key, value) in customParams! {
LiveChat.setVariable(withKey: key, value: value)
}
}

LiveChat.delegate = self
Expand Down
6 changes: 4 additions & 2 deletions lib/_internal/live_chat_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import 'package:js/js_util.dart' as js;

/// A web implementation of the LiveChatPlatform of the LiveChat plugin.
class LiveChatWeb extends LiveChatPlatform {
/// Constructs LiveChatWeb
LiveChatWeb();

/// Register livechat instance when web.
static void registerWith(Registrar registrar) {
LiveChatPlatform.instance = LiveChatWeb();
}
Expand All @@ -30,8 +32,8 @@ class LiveChatWeb extends LiveChatPlatform {
}

@override
Stream? getLiveChatEventsStream() {
eventProducer.on('event', js.allowInterop((data) {
Stream<dynamic>? getLiveChatEventsStream() {
eventProducer.on('event', js.allowInterop((dynamic data) {
_streamController.add(data);
}));
return _streamController.stream;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/js/js_helper_web.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import 'js_library.dart';

/// Helper class that handles calling external functions triggered with
/// javascript.
class JSHelper {
/// Calls to open live chat window.
void callStartLiveChat(String licence, String userName, String email) {
startLiveChat(licence, userName, email);
}

/// Calls to close live chat window.
void callHideWindow() {
hideWindow();
}

/// Call to destroy live chat.
void callDestroyWindow() {
destroyWindow();
}
Expand Down
7 changes: 7 additions & 0 deletions lib/src/js/js_library.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import 'package:js/js.dart';

/// Producer class that will be called in javascript.
@JS()
class EventProducer {
/// Triggers whenever a new event is emit in javascript. It should be listened
/// to in dart.
external void on(String event, Function listener);
}

/// Exposes [eventProducer].
@JS('window.eventProducer')
external EventProducer get eventProducer;

/// Triggers opening web live chat.
@JS()
external void startLiveChat(String licence, String userName, String email);

/// Triggers hiding web live chat.
@JS()
external void hideWindow();

/// Triggers destroying web live chat.
@JS()
external void destroyWindow();
45 changes: 25 additions & 20 deletions lib/src/live_chat.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import 'package:live_chat_plus/src/live_chat_platform_interface.dart';

/// Live Chat Plus plugin class.
class LiveChat {
static final LiveChat _singleton = LiveChat._internal();

factory LiveChat() {
return _singleton;
}
/// Constructs Livechat.
factory LiveChat() => _singleton;

LiveChat._internal();

static final LiveChat _singleton = LiveChat._internal();

/// Call to open the live chat window. It requires [licenseId], [username],
/// and [email].
/// In addition, optional [groupId] and [customParameters] can be passed if
/// needed.
Future<void> openChatWindow({
required String licenseId,
required String username,
required String email,
String? groupId,
Map<String, String>? customParameters,
}) async {
return LiveChatPlatform.instance.openChatWindow(
licenseId: licenseId,
username: username,
email: email,
groupId: groupId,
customParameters: customParameters,
);
}
}) async =>
LiveChatPlatform.instance.openChatWindow(
licenseId: licenseId,
username: username,
email: email,
groupId: groupId,
customParameters: customParameters,
);

Future<void> closeChatWindow() async {
return LiveChatPlatform.instance.closeChatWindow();
}
/// Call to close/hide the chat window.
Future<void> closeChatWindow() async =>
LiveChatPlatform.instance.closeChatWindow();

Future<void> clearChatSession() async {
return LiveChatPlatform.instance.clearChatSession();
}
/// Call to clear the chat session, for example, after logout,
Future<void> clearChatSession() async =>
LiveChatPlatform.instance.clearChatSession();

/// Subscribe to events stream coming from live chat windows such as:
/// open, close, message, actual message text, etc.
Stream<dynamic>? get onLiveChatEventReceived =>
LiveChatPlatform.instance.getLiveChatEventsStream();
}
Loading

0 comments on commit ae8dd81

Please sign in to comment.