Make stream_chat_persistence a add on dependency instead of shipping it with the llc

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-27 12:52:37 +05:30
parent f48b7c8882
commit 1521558304
16 changed files with 126 additions and 187 deletions
@@ -11,7 +11,7 @@ class SharedDB {
static constructOfflineStorage(
String dbName, {
logStatements = false,
bool logStatements = false,
}) {
throw 'Unsupported Platform';
}
@@ -1,5 +1,4 @@
import 'package:stream_chat/stream_chat.dart';
import 'user_mapper.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
///
@@ -3,18 +3,30 @@ import 'package:stream_chat/stream_chat.dart';
import 'db/moor_chat_database.dart';
import 'db/shared/shared_db.dart';
/// Various connection modes on which [StreamChatPersistenceClient] can work
enum ConnectionMode {
/// Connects the [StreamChatPersistenceClient] on a regular/default isolate
regular,
/// Connects the [StreamChatPersistenceClient] on a background isolate
background,
}
///
class StreamChatPersistenceImpl extends StreamChatPersistence {
class StreamChatPersistenceClient extends ChatPersistenceClient {
///
StreamChatPersistenceImpl(
this._userId, {
Logger logger,
}) : _logger = logger,
assert(_userId != null);
StreamChatPersistenceClient({
/// Connection mode on which the client will work
ConnectionMode connectionMode = ConnectionMode.regular,
Level logLevel = Level.WARNING,
}) : assert(connectionMode != null),
assert(logLevel != null),
_connectionMode = connectionMode,
_logger = Logger.detached('💽')..level = logLevel;
final String _userId;
final Logger _logger;
MoorChatDatabase _db;
final Logger _logger;
final ConnectionMode _connectionMode;
bool get _debugAssertConnected {
assert(() {
@@ -30,27 +42,22 @@ class StreamChatPersistenceImpl extends StreamChatPersistence {
}
@override
Future<void> connect({
bool connectBackground = false,
bool logStatements = false,
}) async {
Future<void> connect(String name) async {
if (_db != null) {
throw Exception(
'An instance of StreamChatDatabase is already connected.\n'
'disconnect the previous instance before connecting again.',
);
}
final dbName = 'db_$_userId';
if (connectBackground) {
_logger?.info('Connecting on background isolate');
_db = await SharedDB.constructOfflineStorage(
dbName,
logStatements: logStatements,
);
} else {
_logger?.info('Connecting on a regular isolate');
_db = MoorChatDatabase(dbName, logStatements: logStatements);
switch (_connectionMode) {
case ConnectionMode.regular:
_logger.info('Connecting on a regular isolate');
_db = MoorChatDatabase(name);
return;
case ConnectionMode.background:
_logger.info('Connecting on background isolate');
_db = await SharedDB.constructOfflineStorage(name);
return;
}
}
@@ -202,9 +209,9 @@ class StreamChatPersistenceImpl extends StreamChatPersistence {
@override
Future<void> disconnect({bool flush = false}) async {
if (_db != null) {
_logger?.info('Disconnecting');
_logger.info('Disconnecting');
if (flush) {
_logger?.info('Flushing');
_logger.info('Flushing');
await _db.batch((batch) {
_db.allTables.forEach((table) {
_db.delete(table).go();
@@ -1,3 +1,3 @@
library stream_chat_persistence;
export 'src/stream_chat_persistence_impl.dart';
export 'src/stream_chat_persistence_client.dart';
@@ -11,6 +11,7 @@ dependencies:
moor: ^3.4.0
path: ^1.7.0
path_provider: ^1.6.27
sqlite3_flutter_libs: ^0.3.0
stream_chat:
path: ../dart_client