Skip to content
This repository has been archived by the owner on Nov 21, 2021. It is now read-only.

refactor: stream reuse/clear/reinitialize #30

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 2.1.0+0

### [DOCS]

* Dart: Answer SDK

### [OPTIMISATION]

* Release and recreate streams on Chat SDK and Answer SDK

# 2.0.0+1

* fix(jcentral): Remove reference to jCenter
Expand Down
6 changes: 3 additions & 3 deletions example/lib/zendesk_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class _ZendeskChat extends State<ZendeskChat> {

WidgetsBinding.instance?.addPostFrameCallback((_) async {
await _z.connect();
_subscriptionProvidersStream =
_z.providersStream?.listen((providerModel) {
_subscriptionProvidersStream = _z.providersStream.listen((providerModel) {
_providerModel = providerModel;
print('ProviderModel: $_providerModel');
setState(() {});
Expand All @@ -57,7 +56,7 @@ class _ZendeskChat extends State<ZendeskChat> {
setState(() {});
});
_subscriptionConnetionStatusStream =
_z.connectionStatusStream?.listen((connectionStatus) {
_z.connectionStatusStream.listen((connectionStatus) {
_connectionStatus = connectionStatus;
print('Connection Status: $_connectionStatus');
setState(() {});
Expand Down Expand Up @@ -168,6 +167,7 @@ class _ZendeskChat extends State<ZendeskChat> {
type: FileType.custom,
allowedExtensions: compatibleExt?.toList() ?? [],
);

final file =
result is FilePickerResult ? result.files.single : (result as File);

Expand Down
40 changes: 30 additions & 10 deletions lib/answer/zendesk_answer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,35 @@ class ZendeskAnswer {
static final _channel = Zendesk.instance.channel;

StreamController<AnswerProviderModel> _providersDeflection =
StreamController<AnswerProviderModel>();
StreamController();
StreamController<bool> _providersResolveArticleDeflection =
StreamController<bool>();
StreamController<bool> _providersRejectArticleDeflection =
StreamController<bool>();
StreamController();
StreamController<bool> _providersRejectArticleDeflection = StreamController();

/// Retrieves `articles` and `interactionAccessToken` to resolve ou reject articles
Stream<AnswerProviderModel> get providersDeflection =>
_providersDeflection.stream.asBroadcastStream();

/// Retrieves last `article` resolved success
Stream<bool> get providersResolveArticleDeflection =>
_providersResolveArticleDeflection.stream.asBroadcastStream();

/// Retrieves last `article` rejected success
Stream<bool> get providersRejectArticleDeflection =>
_providersRejectArticleDeflection.stream.asBroadcastStream();

bool _isStreaming = true;

/// `query` key word or phrase to retrieve articles related
///
/// result streams in `providersDeflection`
Future<void> query(String query) async {
if (!_isStreaming) {
_providersDeflection = StreamController();
_providersResolveArticleDeflection = StreamController();
_providersRejectArticleDeflection = StreamController();
}

try {
final arguments = {
'query': query,
Expand All @@ -61,6 +74,7 @@ class ZendeskAnswer {
}
}

/// User resolves article as helpful
Future<void> resolveArticle(
String deflectionArticleId,
String articleId,
Expand All @@ -78,6 +92,9 @@ class ZendeskAnswer {
}
}

/// User resolves article as unhelpful
///
/// `reason`: optional reason to rejection
Future<void> rejectArticle(
String deflectionArticleId,
String articleId,
Expand All @@ -97,14 +114,17 @@ class ZendeskAnswer {
}
}

/// Release stream resources
Future<void> 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;
}
}
74 changes: 35 additions & 39 deletions lib/chat2/zendesk_chat2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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<ChatProviderModel>? _providersStream;
StreamController<CONNECTION_STATUS>? _connectionStatusStream;
StreamController<ChatSettingsModel>? _chatSettingsStream;
StreamController<ChatAccountModel>? _chatAccountStream;
StreamController<ChatProviderModel> _providersStream = StreamController();
StreamController<CONNECTION_STATUS> _connectionStatusStream =
StreamController();
StreamController<ChatSettingsModel> _chatSettingsStream = StreamController();
StreamController<ChatAccountModel> _chatAccountStream = StreamController();

bool _isStreaming = false;

/// Stream is triggered when socket receive new values
///
/// Please see ```ChatProviderModel```
Stream<ChatProviderModel>? get providersStream =>
_providersStream?.stream.asBroadcastStream();
Stream<ChatProviderModel> get providersStream =>
_providersStream.stream.asBroadcastStream();

/// Stream is triggered when socket receive new values
///
/// ```CONNECTION_STATUS```:
/// CONNECTED | CONNECTING | DISCONNECTED |
/// FAILED | RECONNECTING | UNREACHABLE | UNKNOWN
Stream<CONNECTION_STATUS>? get connectionStatusStream =>
_connectionStatusStream?.stream.asBroadcastStream();
Stream<CONNECTION_STATUS> get connectionStatusStream =>
_connectionStatusStream.stream.asBroadcastStream();

/// Stream is triggered when socket receive new values
///
/// Please see ```ChatSettingsModel```
Stream<ChatSettingsModel>? get chatSettingsStream =>
_chatSettingsStream?.stream.asBroadcastStream();
_chatSettingsStream.stream.asBroadcastStream();

/// Stream is triggered when socket receive new values
///
/// Please see ```ChatAccountModel ```
Stream<ChatAccountModel>? get chatIsOnlineStream =>
_chatAccountStream?.stream.asBroadcastStream();
_chatAccountStream.stream.asBroadcastStream();

/// Set on Native/Custom chat user information
///
Expand Down Expand Up @@ -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<void> startChatProviders({bool autoConnect = true}) async {
try {
if (!_isStreaming) {
_providersStream = StreamController<ChatProviderModel>();
_connectionStatusStream = StreamController<CONNECTION_STATUS>();
_chatSettingsStream = StreamController<ChatSettingsModel>();
_chatAccountStream = StreamController<ChatAccountModel>();
_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();
}
Expand Down Expand Up @@ -242,26 +245,19 @@ class Zendesk2Chat {
/// Release and close streams
Future<void> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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+1
version: 2.1.0+0
homepage: https://github.com/KohlsAdrian/zendesk2

environment:
Expand Down