mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-29 04:49:51 +00:00
feat: Add app-version to Chatwoot Contact Attributes (#2049)
This commit is contained in:
parent
d1de481558
commit
465ff7e78c
4 changed files with 50 additions and 26 deletions
|
@ -1256,7 +1256,7 @@ Future<void> setup({
|
|||
getIt.registerFactoryParam<OrderDetailsPage, Order, void>(
|
||||
(Order order, _) => OrderDetailsPage(getIt.get<OrderDetailsViewModel>(param1: order)));
|
||||
|
||||
getIt.registerFactory(() => SupportViewModel());
|
||||
getIt.registerFactory(() => SupportViewModel(getIt.get<SettingsStore>()));
|
||||
|
||||
getIt.registerFactory(() => SupportPage(getIt.get<SupportViewModel>()));
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||
import 'package:cake_wallet/generated/i18n.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/view_model/settings/link_list_item.dart';
|
||||
import 'package:cake_wallet/view_model/settings/settings_list_item.dart';
|
||||
import 'package:cake_wallet/wallet_type_utils.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||
|
||||
part 'support_view_model.g.dart';
|
||||
|
||||
class SupportViewModel = SupportViewModelBase with _$SupportViewModel;
|
||||
|
||||
abstract class SupportViewModelBase with Store {
|
||||
SupportViewModelBase()
|
||||
final SettingsStore settingsStore;
|
||||
|
||||
SupportViewModelBase(this.settingsStore)
|
||||
: items = [
|
||||
LinkListItem(
|
||||
title: 'Email',
|
||||
|
@ -116,5 +119,8 @@ abstract class SupportViewModelBase with Store {
|
|||
return supportUrl;
|
||||
}
|
||||
|
||||
String get appVersion =>
|
||||
"${isMoneroOnly ? "Monero.com" : "Cake Wallet"} - ${settingsStore.appVersion}";
|
||||
|
||||
List<SettingsListItem> items;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue