From d4457922c26d5a02e6bb6ab8a70c98459721d123 Mon Sep 17 00:00:00 2001 From: kohlsadrian Date: Fri, 10 Sep 2021 17:10:04 -0300 Subject: [PATCH 1/3] reafactor: stream reuse/clear/reinitialize --- example/lib/zendesk_chat.dart | 10 ++--- lib/answer/zendesk_answer.dart | 29 ++++++++----- lib/chat2/zendesk_chat2.dart | 74 ++++++++++++++++------------------ 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/example/lib/zendesk_chat.dart b/example/lib/zendesk_chat.dart index 167a56b..88610da 100644 --- a/example/lib/zendesk_chat.dart +++ b/example/lib/zendesk_chat.dart @@ -43,8 +43,7 @@ class _ZendeskChat extends State { WidgetsBinding.instance?.addPostFrameCallback((_) async { await _z.connect(); - _subscriptionProvidersStream = - _z.providersStream?.listen((providerModel) { + _subscriptionProvidersStream = _z.providersStream.listen((providerModel) { _providerModel = providerModel; print('ProviderModel: $_providerModel'); setState(() {}); @@ -56,7 +55,7 @@ class _ZendeskChat extends State { setState(() {}); }); _subscriptionConnetionStatusStream = - _z.connectionStatusStream?.listen((connectionStatus) { + _z.connectionStatusStream.listen((connectionStatus) { _connectionStatus = connectionStatus; print('Connection Status: $_connectionStatus'); setState(() {}); @@ -168,9 +167,8 @@ class _ZendeskChat extends State { allowedExtensions: compatibleExt?.toList() ?? [], ); if (result != null) { - final file = result is FilePickerResult - ? result.files.single - : (result as XFile); + final file = + result is FilePickerResult ? result.files.single : (result as XFile); final path = file is PlatformFile ? file.path : (file as XFile).path; diff --git a/lib/answer/zendesk_answer.dart b/lib/answer/zendesk_answer.dart index a68b98d..8ebe427 100644 --- a/lib/answer/zendesk_answer.dart +++ b/lib/answer/zendesk_answer.dart @@ -35,11 +35,10 @@ class ZendeskAnswer { static final _channel = Zendesk.instance.channel; StreamController _providersDeflection = - StreamController(); + StreamController(); StreamController _providersResolveArticleDeflection = - StreamController(); - StreamController _providersRejectArticleDeflection = - StreamController(); + StreamController(); + StreamController _providersRejectArticleDeflection = StreamController(); Stream get providersDeflection => _providersDeflection.stream.asBroadcastStream(); @@ -50,7 +49,15 @@ class ZendeskAnswer { Stream get providersRejectArticleDeflection => _providersRejectArticleDeflection.stream.asBroadcastStream(); + bool _isStreaming = true; + Future query(String query) async { + if (!_isStreaming) { + _providersDeflection = StreamController(); + _providersResolveArticleDeflection = StreamController(); + _providersRejectArticleDeflection = StreamController(); + } + try { final arguments = { 'query': query, @@ -98,13 +105,15 @@ class ZendeskAnswer { } Future dispose() async { - _providersDeflection.sink.close(); - _providersDeflection.close(); + await _providersDeflection.sink.close(); + await _providersDeflection.close(); + + await _providersResolveArticleDeflection.sink.close(); + await _providersResolveArticleDeflection.close(); - _providersResolveArticleDeflection.sink.close(); - _providersResolveArticleDeflection.close(); + await _providersRejectArticleDeflection.sink.close(); + await _providersRejectArticleDeflection.close(); - _providersRejectArticleDeflection.sink.close(); - _providersRejectArticleDeflection.close(); + _isStreaming = false; } } diff --git a/lib/chat2/zendesk_chat2.dart b/lib/chat2/zendesk_chat2.dart index f0fab36..9480c1b 100644 --- a/lib/chat2/zendesk_chat2.dart +++ b/lib/chat2/zendesk_chat2.dart @@ -11,7 +11,7 @@ class Zendesk2Chat { switch (call.method) { case 'sendChatProvidersResult': final providerModel = ChatProviderModel.fromJson(arguments); - _providersStream?.sink.add(providerModel); + _providersStream.sink.add(providerModel); break; case 'sendChatConnectionStatusResult': CONNECTION_STATUS connectionStatus = CONNECTION_STATUS.CONNECTING; @@ -37,16 +37,16 @@ class Zendesk2Chat { connectionStatus = CONNECTION_STATUS.UNREACHABLE; break; } - _connectionStatusStream?.sink.add(connectionStatus); + _connectionStatusStream.sink.add(connectionStatus); break; case 'sendChatSettingsResult': ChatSettingsModel? chatSettingsModel = ChatSettingsModel.fromJson(arguments); - _chatSettingsStream?.sink.add(chatSettingsModel); + _chatSettingsStream.sink.add(chatSettingsModel); break; case 'sendChatIsOnlineResult': final chatAccountModel = ChatAccountModel.fromJson(arguments); - _chatAccountStream?.sink.add(chatAccountModel); + _chatAccountStream.sink.add(chatAccountModel); break; } } catch (e) { @@ -63,38 +63,39 @@ class Zendesk2Chat { /// added ignore so the source won't have warnings /// but don't forget to close or .dispose() when needed!!! /// ignore: close_sinks - StreamController? _providersStream; - StreamController? _connectionStatusStream; - StreamController? _chatSettingsStream; - StreamController? _chatAccountStream; + StreamController _providersStream = StreamController(); + StreamController _connectionStatusStream = + StreamController(); + StreamController _chatSettingsStream = StreamController(); + StreamController _chatAccountStream = StreamController(); bool _isStreaming = false; /// Stream is triggered when socket receive new values /// /// Please see ```ChatProviderModel``` - Stream? get providersStream => - _providersStream?.stream.asBroadcastStream(); + Stream get providersStream => + _providersStream.stream.asBroadcastStream(); /// Stream is triggered when socket receive new values /// /// ```CONNECTION_STATUS```: /// CONNECTED | CONNECTING | DISCONNECTED | /// FAILED | RECONNECTING | UNREACHABLE | UNKNOWN - Stream? get connectionStatusStream => - _connectionStatusStream?.stream.asBroadcastStream(); + Stream get connectionStatusStream => + _connectionStatusStream.stream.asBroadcastStream(); /// Stream is triggered when socket receive new values /// /// Please see ```ChatSettingsModel``` Stream? get chatSettingsStream => - _chatSettingsStream?.stream.asBroadcastStream(); + _chatSettingsStream.stream.asBroadcastStream(); /// Stream is triggered when socket receive new values /// /// Please see ```ChatAccountModel ``` Stream? get chatIsOnlineStream => - _chatAccountStream?.stream.asBroadcastStream(); + _chatAccountStream.stream.asBroadcastStream(); /// Set on Native/Custom chat user information /// @@ -131,22 +132,24 @@ class Zendesk2Chat { /// Start chat providers for custom UI handling /// /// ```periodicRetrieve``` periodic time to update the ```providersStream``` - /// + /// /// ```autoConnect``` Determines if you also want to connect to the chat socket - /// + /// /// The user will not receive push notifications while connected Future startChatProviders({bool autoConnect = true}) async { try { - if (!_isStreaming) { - _providersStream = StreamController(); - _connectionStatusStream = StreamController(); - _chatSettingsStream = StreamController(); - _chatAccountStream = StreamController(); - _isStreaming = true; + if (_isStreaming) { + await dispose(); + + _providersStream = StreamController(); + _connectionStatusStream = StreamController(); + _chatSettingsStream = StreamController(); + _chatAccountStream = StreamController(); } - await _channel.invokeMethod('startChatProviders'); + _isStreaming = true; + await _channel.invokeMethod('startChatProviders'); if (autoConnect) { await connect(); } @@ -242,26 +245,19 @@ class Zendesk2Chat { /// Release and close streams Future dispose() async { try { - _providersStream?.sink.close(); - _providersStream?.close(); - - _connectionStatusStream?.sink.close(); - _connectionStatusStream?.close(); - - _chatSettingsStream?.sink.close(); - _chatSettingsStream?.close(); + await _channel.invokeMethod('chat_dispose'); - _chatAccountStream?.sink.close(); - _chatAccountStream?.close(); + await _providersStream.sink.close(); + await _connectionStatusStream.sink.close(); + await _chatSettingsStream.sink.close(); + await _chatAccountStream.sink.close(); - _providersStream = null; - _connectionStatusStream = null; - _chatSettingsStream = null; - _chatAccountStream = null; + await _providersStream.close(); + await _connectionStatusStream.close(); + await _chatSettingsStream.close(); + await _chatAccountStream.close(); _isStreaming = false; - - await _channel.invokeMethod('chat_dispose'); } catch (e) { print(e); } From 04792287896f6fc9221ba11e1693a4be6e322738 Mon Sep 17 00:00:00 2001 From: kohlsadrian Date: Fri, 10 Sep 2021 17:16:06 -0300 Subject: [PATCH 2/3] docs: answer sdk dart --- lib/answer/zendesk_answer.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/answer/zendesk_answer.dart b/lib/answer/zendesk_answer.dart index 8ebe427..3913782 100644 --- a/lib/answer/zendesk_answer.dart +++ b/lib/answer/zendesk_answer.dart @@ -40,17 +40,23 @@ class ZendeskAnswer { StreamController(); StreamController _providersRejectArticleDeflection = StreamController(); + /// Retrieves `articles` and `interactionAccessToken` to resolve ou reject articles Stream get providersDeflection => _providersDeflection.stream.asBroadcastStream(); + /// Retrieves last `article` resolved success Stream get providersResolveArticleDeflection => _providersResolveArticleDeflection.stream.asBroadcastStream(); + /// Retrieves last `article` rejected success Stream get providersRejectArticleDeflection => _providersRejectArticleDeflection.stream.asBroadcastStream(); bool _isStreaming = true; + /// `query` key word or phrase to retrieve articles related + /// + /// result streams in `providersDeflection` Future query(String query) async { if (!_isStreaming) { _providersDeflection = StreamController(); @@ -68,6 +74,7 @@ class ZendeskAnswer { } } + /// User resolves article as helpful Future resolveArticle( String deflectionArticleId, String articleId, @@ -85,6 +92,9 @@ class ZendeskAnswer { } } + /// User resolves article as unhelpful + /// + /// `reason`: optional reason to rejection Future rejectArticle( String deflectionArticleId, String articleId, @@ -104,6 +114,7 @@ class ZendeskAnswer { } } + /// Release stream resources Future dispose() async { await _providersDeflection.sink.close(); await _providersDeflection.close(); From b08534c39d2a5075254d9bdf3a3a7f1ad8f52099 Mon Sep 17 00:00:00 2001 From: kohlsadrian Date: Fri, 10 Sep 2021 17:17:51 -0300 Subject: [PATCH 3/3] version bump and readme --- CHANGELOG.md | 10 ++++++++++ pubspec.yaml | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65ab9d7..ef4e13d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 2.0.1+0 + +### [DOCS] + +* Dart: Answer SDK + +### [OPTIMISATION] + +* Release and recreate streams on Chat SDK and Answer SDK + # 2.0.0+0 ## Huge update 🎉 diff --git a/pubspec.yaml b/pubspec.yaml index 70e8e8e..e194c10 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: zendesk2 description: Zendesk Android and iOS SDK port for Flutter, easily open native chat or manipulate providers for custom UI -version: 2.0.0+0 +version: 2.0.1+0 homepage: https://github.com/KohlsAdrian/zendesk2 environment: