mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-28 06:28:41 +01:00
Re add chatwoot (#1044)
* Revert "Revert "Cw 397 chatwoot live support (#1011)"" This reverts commit ecdc7baa2e3c8f46c3c5013f56c8b864f088ec9f. * Re-add chatwoot Change chatwoot base url
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
|
||||
class SupportChatPage extends BasePage {
|
||||
SupportChatPage(this.supportViewModel, {required this.secureStorage});
|
||||
|
||||
final SupportViewModel supportViewModel;
|
||||
final FlutterSecureStorage secureStorage;
|
||||
|
||||
@override
|
||||
String get title => S.current.settings_support;
|
||||
|
||||
@override
|
||||
AppBarStyle get appBarStyle => AppBarStyle.regular;
|
||||
|
||||
@override
|
||||
Widget body(BuildContext context) => FutureBuilder<String>(
|
||||
future: getCookie(),
|
||||
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||||
print(snapshot.data);
|
||||
if (snapshot.hasData)
|
||||
return ChatwootWidget(
|
||||
secureStorage,
|
||||
supportUrl: supportViewModel.fetchUrl(authToken: snapshot.data!)
|
||||
);
|
||||
return Container();
|
||||
},
|
||||
);
|
||||
|
||||
Future<String> getCookie() async {
|
||||
return await secureStorage.read(key: COOKIE_KEY) ?? "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
const COOKIE_KEY = 'chatwootCookie';
|
||||
|
||||
class ChatwootWidget extends StatefulWidget {
|
||||
ChatwootWidget(this.secureStorage, {required this.supportUrl});
|
||||
|
||||
final FlutterSecureStorage secureStorage;
|
||||
final String supportUrl;
|
||||
|
||||
@override
|
||||
ChatwootWidgetState createState() => ChatwootWidgetState();
|
||||
}
|
||||
|
||||
class ChatwootWidgetState extends State<ChatwootWidget> {
|
||||
final GlobalKey _webViewkey = GlobalKey();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => InAppWebView(
|
||||
key: _webViewkey,
|
||||
initialOptions: InAppWebViewGroupOptions(
|
||||
crossPlatform: InAppWebViewOptions(transparentBackground: true),
|
||||
),
|
||||
initialUrlRequest: URLRequest(url: Uri.tryParse(widget.supportUrl)),
|
||||
onWebViewCreated: (InAppWebViewController controller) {
|
||||
controller.addWebMessageListener(
|
||||
WebMessageListener(
|
||||
jsObjectName: 'ReactNativeWebView',
|
||||
onPostMessage: (String? message, Uri? sourceOrigin, bool isMainFrame,
|
||||
JavaScriptReplyProxy replyProxy) {
|
||||
final shortenedMessage = message?.substring(16);
|
||||
if (shortenedMessage != null && isJsonString(shortenedMessage)) {
|
||||
final parsedMessage = jsonDecode(shortenedMessage);
|
||||
final eventType = parsedMessage["event"];
|
||||
if (eventType == 'loaded') {
|
||||
final authToken = parsedMessage["config"]["authToken"];
|
||||
print(authToken);
|
||||
storeCookie(authToken as String);
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
bool isJsonString(String str) {
|
||||
try {
|
||||
jsonDecode(str);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> storeCookie(String value) async =>
|
||||
await widget.secureStorage.write(key: COOKIE_KEY, value: value);
|
||||
}
|
||||
Reference in New Issue
Block a user