mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-21 06:18:42 +02:00
feat: Add app-version to Chatwoot Contact Attributes (#2049)
This commit is contained in:
@@ -3,10 +3,8 @@ import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/src/screens/base_page.dart';
|
||||
import 'package:cake_wallet/src/screens/support_chat/widgets/chatwoot_widget.dart';
|
||||
import 'package:cake_wallet/view_model/support_view_model.dart';
|
||||
import 'package:cw_core/utils/print_verbose.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
||||
class SupportChatPage extends BasePage {
|
||||
SupportChatPage(this.supportViewModel, {required this.secureStorage});
|
||||
|
||||
@@ -23,17 +21,16 @@ class SupportChatPage extends BasePage {
|
||||
Widget body(BuildContext context) => FutureBuilder<String>(
|
||||
future: getCookie(),
|
||||
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||||
printV(snapshot.data);
|
||||
if (snapshot.hasData)
|
||||
return ChatwootWidget(
|
||||
secureStorage,
|
||||
supportUrl: supportViewModel.fetchUrl(authToken: snapshot.data!)
|
||||
supportUrl: supportViewModel.fetchUrl(authToken: snapshot.data!),
|
||||
appVersion: supportViewModel.appVersion,
|
||||
);
|
||||
return Container();
|
||||
},
|
||||
);
|
||||
|
||||
Future<String> getCookie() async {
|
||||
return await secureStorage.read(key: COOKIE_KEY) ?? "";
|
||||
}
|
||||
Future<String> getCookie() async =>
|
||||
await secureStorage.read(key: COOKIE_KEY) ?? "";
|
||||
}
|
||||
|
||||
@@ -1,46 +1,50 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:cake_wallet/core/secure_storage.dart';
|
||||
import 'package:cw_core/utils/print_verbose.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
|
||||
const COOKIE_KEY = 'chatwootCookie';
|
||||
|
||||
class ChatwootWidget extends StatefulWidget {
|
||||
ChatwootWidget(this.secureStorage, {required this.supportUrl});
|
||||
const ChatwootWidget(
|
||||
this.secureStorage, {
|
||||
required this.supportUrl,
|
||||
required this.appVersion,
|
||||
});
|
||||
|
||||
final SecureStorage secureStorage;
|
||||
final String supportUrl;
|
||||
final String appVersion;
|
||||
|
||||
@override
|
||||
ChatwootWidgetState createState() => ChatwootWidgetState();
|
||||
}
|
||||
|
||||
class ChatwootWidgetState extends State<ChatwootWidget> {
|
||||
final GlobalKey _webViewkey = GlobalKey();
|
||||
final GlobalKey _webViewKey = GlobalKey();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => InAppWebView(
|
||||
key: _webViewkey,
|
||||
initialSettings: InAppWebViewSettings(
|
||||
transparentBackground: true,
|
||||
),
|
||||
key: _webViewKey,
|
||||
initialSettings: InAppWebViewSettings(transparentBackground: true),
|
||||
initialUrlRequest: URLRequest(url: WebUri(widget.supportUrl)),
|
||||
onWebViewCreated: (InAppWebViewController controller) {
|
||||
controller.addWebMessageListener(
|
||||
WebMessageListener(
|
||||
jsObjectName: 'ReactNativeWebView',
|
||||
onPostMessage: (WebMessage? message, WebUri? sourceOrigin, bool isMainFrame,
|
||||
PlatformJavaScriptReplyProxy replyProxy) {
|
||||
onPostMessage: (WebMessage? message, WebUri? sourceOrigin,
|
||||
bool isMainFrame, PlatformJavaScriptReplyProxy replyProxy) {
|
||||
final shortenedMessage = message?.data.toString().substring(16);
|
||||
if (shortenedMessage != null && isJsonString(shortenedMessage)) {
|
||||
if (shortenedMessage != null &&
|
||||
_isJsonString(shortenedMessage)) {
|
||||
final parsedMessage = jsonDecode(shortenedMessage);
|
||||
final eventType = parsedMessage["event"];
|
||||
if (eventType == 'loaded') {
|
||||
final authToken = parsedMessage["config"]["authToken"];
|
||||
printV(authToken);
|
||||
storeCookie(authToken as String);
|
||||
_storeCookie(authToken as String);
|
||||
_setCustomAttributes(
|
||||
controller, {"app_version": widget.appVersion});
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -49,7 +53,7 @@ class ChatwootWidgetState extends State<ChatwootWidget> {
|
||||
},
|
||||
);
|
||||
|
||||
bool isJsonString(String str) {
|
||||
bool _isJsonString(String str) {
|
||||
try {
|
||||
jsonDecode(str);
|
||||
} catch (e) {
|
||||
@@ -58,7 +62,24 @@ class ChatwootWidgetState extends State<ChatwootWidget> {
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> storeCookie(String value) async {
|
||||
await widget.secureStorage.write(key: COOKIE_KEY, value: value);
|
||||
/// Add additional contact attributes to the chatwoot chat.
|
||||
/// IMPORTANT: You have to add the attribute key in the chatwoot settings
|
||||
/// under: settings/custom-attributes
|
||||
Future<void> _setCustomAttributes(
|
||||
InAppWebViewController controller,
|
||||
Map<String, dynamic> customAttributes,
|
||||
) {
|
||||
final attributeObject = {
|
||||
"event": "set-custom-attributes",
|
||||
"customAttributes": customAttributes,
|
||||
};
|
||||
return controller.postWebMessage(
|
||||
message: WebMessage(
|
||||
data: "chatwoot-widget:${jsonEncode(attributeObject)}",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _storeCookie(String value) =>
|
||||
widget.secureStorage.write(key: COOKIE_KEY, value: value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user