[LLC] Remove notification handling logics

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-28 16:42:51 +05:30
parent c87c1d5368
commit a2a40b5f86
4 changed files with 0 additions and 116 deletions
-15
View File
@@ -6,7 +6,6 @@ import 'package:dio/dio.dart';
import 'package:meta/meta.dart';
import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stream_chat/src/api/retry_policy.dart';
import 'package:stream_chat/src/event_type.dart';
import 'package:stream_chat/src/models/channel_model.dart';
@@ -37,15 +36,6 @@ typedef DecoderFunction<T> = T Function(Map<String, dynamic>);
/// own backend server. Function requires a single [userId].
typedef TokenProvider = Future<String> Function(String userId);
/// The key used to save the userId to sharedPreferences
const String KEY_USER_ID = 'KEY_USER_ID';
/// The key used to save the token to sharedPreferences
const String KEY_TOKEN = 'KEY_TOKEN';
/// The key used to save the apiKey to sharedPreferences
const String KEY_API_KEY = 'KEY_API_KEY';
/// Provider used to send push notifications.
enum PushProvider {
/// Send notifications using Google's Firebase Cloud Messaging
@@ -378,11 +368,6 @@ class Client {
this.token = token;
_anonymous = false;
final sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setString(KEY_USER_ID, user.id);
await sharedPreferences.setString(KEY_TOKEN, token);
await sharedPreferences.setString(KEY_API_KEY, apiKey);
return connect().then((event) {
_connectCompleter.complete(event);
return event;
@@ -1,99 +0,0 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:stream_chat/src/api/responses.dart';
import 'package:stream_chat/src/models/channel_model.dart';
import 'package:stream_chat/src/models/channel_state.dart';
import 'package:stream_chat/src/models/message.dart';
import 'package:meta/meta.dart';
import 'client.dart';
import 'models/own_user.dart';
/// Utility class to handle and show notifications
class NotificationService {
static Future<void> _handleNotification(
Message message,
ChannelModel channelModel,
Client client,
) async {
if (message != null && client.persistenceEnabled) {
if (client?.state?.channels == null) {
final chatPersistenceClient = client.chatPersistenceClient;
await chatPersistenceClient.updateChannelState(
ChannelState(
channel: channelModel,
messages: [message],
),
);
} else {
final channel = client.state.channels[channelModel.cid];
channel.state.updateChannelState(
ChannelState(
channel: channelModel,
messages: [message],
),
);
}
}
}
static SharedPreferences _sharedPreferences;
static Future<SharedPreferences> _getSharedPreferences() async {
_sharedPreferences ??= await SharedPreferences.getInstance();
return _sharedPreferences;
}
/// Gets the message using the client without storing it in the offline storage
/// It returns an object containing the information about the message and the channel
static Future<GetMessageResponse> _getMessage(String messageId) async {
final sharedPreferences = await _getSharedPreferences();
final apiKey = sharedPreferences.getString(KEY_API_KEY);
final client = Client(apiKey);
final userId = sharedPreferences.getString(KEY_USER_ID);
final token = sharedPreferences.getString(KEY_TOKEN);
client.state.user = OwnUser(id: userId);
client.token = token;
final res = await client.getMessage(messageId);
return res;
}
/// Gets the message using the client and calls a [storeMessageHandler] callback
/// with the message if the user wants to save the message in db
///
/// It returns an object containing the information about the message and the channel
static Future<GetMessageResponse> getAndStoreMessage({
@required String messageId,
@required Future<void> Function(GetMessageResponse) storeMessageHandler,
}) async {
final getMessageResponse = await _getMessage(messageId);
await storeMessageHandler(getMessageResponse);
return getMessageResponse;
}
/// Handles the ios message queue generated by the Notification Service Extension
static Future<void> handleIosMessageQueue(Client client) async {
final sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.reload();
final messageQueue = sharedPreferences.getStringList('messageQueue');
if (messageQueue != null) {
messageQueue.forEach((m) {
final jsonMessage = jsonDecode(m);
final message = Message.fromJson(jsonMessage);
final channelModel = ChannelModel.fromJson(jsonMessage['channel']);
_handleNotification(
message,
channelModel,
client,
);
});
await sharedPreferences.remove('messageQueue');
}
}
}
@@ -26,5 +26,4 @@ export './src/models/own_user.dart';
export './src/models/reaction.dart';
export './src/models/read.dart';
export './src/models/user.dart';
export './src/notifications.dart';
export './src/db/chat_persistence_client.dart';
-1
View File
@@ -10,7 +10,6 @@ environment:
dependencies:
json_annotation: ^3.0.1
shared_preferences: ^0.5.7+3
logging: ^0.11.4
dio: ^3.0.10
web_socket_channel: ^1.1.0