[StreamChatPersistence] Add initial implementation
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import 'package:stream_chat/src/api/requests.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/event.dart';
|
||||
import 'package:stream_chat/src/models/member.dart';
|
||||
import 'package:stream_chat/src/models/message.dart';
|
||||
import 'package:stream_chat/src/models/reaction.dart';
|
||||
import 'package:stream_chat/src/models/read.dart';
|
||||
import 'package:stream_chat/src/models/user.dart';
|
||||
|
||||
///
|
||||
abstract class StreamChatDatabase {
|
||||
/// Creates a new connection to the database
|
||||
Future<void> connect({
|
||||
bool connectBackground = false,
|
||||
bool logStatements = false,
|
||||
});
|
||||
|
||||
/// Closes the database instance
|
||||
/// If [flush] is true, the database data will be deleted
|
||||
Future<void> disconnect({bool flush = false});
|
||||
|
||||
/// Get stored replies by messageId
|
||||
Future<List<Message>> getReplies(
|
||||
String parentId, {
|
||||
String lessThan,
|
||||
});
|
||||
|
||||
/// Get stored connection event
|
||||
Future<Event> getConnectionInfo();
|
||||
|
||||
/// Get stored lastSyncAt
|
||||
Future<DateTime> getLastSyncAt();
|
||||
|
||||
/// Update stored connection event
|
||||
Future<void> updateConnectionInfo(Event event);
|
||||
|
||||
/// Update stored lastSyncAt
|
||||
Future<void> updateLastSyncAt(DateTime lastSyncAt);
|
||||
|
||||
/// Get the channel cids saved in the offline storage
|
||||
Future<List<String>> getChannelCids();
|
||||
|
||||
Future<ChannelModel> getChannelByCid(String cid);
|
||||
|
||||
Future<List<Member>> getMembersByCid(String cid);
|
||||
|
||||
Future<List<Read>> getReadsByCid(String cid);
|
||||
|
||||
Future<List<Message>> getMessagesByCid(
|
||||
String cid, {
|
||||
int limit = 20,
|
||||
String messageLessThan,
|
||||
String messageGreaterThan,
|
||||
});
|
||||
|
||||
/// Get list of channels by filter, sort and paginationParams
|
||||
Future<List<ChannelState>> getChannelStates({
|
||||
Map<String, dynamic> filter,
|
||||
List<SortOption> sort = const [],
|
||||
PaginationParams paginationParams,
|
||||
});
|
||||
|
||||
/// Update list of channel queries
|
||||
/// If [clearQueryCache] is true before the insert
|
||||
/// the list of matching rows will be deleted
|
||||
Future<void> updateChannelQueries(
|
||||
Map<String, dynamic> filter,
|
||||
List<String> cids,
|
||||
bool clearQueryCache,
|
||||
);
|
||||
|
||||
/// Remove a message by message id
|
||||
Future<void> deleteMessageByIds(List<String> messageIds);
|
||||
|
||||
/// Remove a message by message id
|
||||
Future<void> deleteMessageByCids(List<String> cids);
|
||||
|
||||
/// Remove a channel by cid
|
||||
Future<void> deleteChannelByCids(List<String> cids);
|
||||
|
||||
/// Update messages data from a list
|
||||
Future<void> updateMessages(String cid, List<Message> messages);
|
||||
|
||||
/// Get the info about channel threads
|
||||
Future<Map<String, List<Message>>> getChannelThreads(String cid);
|
||||
|
||||
Future<void> updateChannels(List<ChannelModel> channels);
|
||||
|
||||
Future<void> updateMembers(String cid, List<Member> members);
|
||||
|
||||
Future<void> updateReads(String cid, List<Read> reads);
|
||||
|
||||
Future<void> updateUsers(List<User> users);
|
||||
|
||||
Future<void> updateReactions(List<Reaction> reactions);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
import '../exceptions.dart';
|
||||
|
||||
part 'result.freezed.dart';
|
||||
|
||||
///
|
||||
@freezed
|
||||
abstract class Result<T> with _$Result<T> {
|
||||
///
|
||||
const factory Result.success({@required T data}) = Success<T>;
|
||||
|
||||
///
|
||||
const factory Result.error({@required ApiError error}) = Error<T>;
|
||||
}
|
||||
|
||||
///
|
||||
extension ResultX<T> on Result<T> {
|
||||
///
|
||||
bool get isSuccess => this is Success<T>;
|
||||
|
||||
///
|
||||
bool get isError => this is Error<T>;
|
||||
|
||||
///
|
||||
T get data {
|
||||
if (isError) {
|
||||
throw Exception(
|
||||
'Result is not successful. Check result.isSuccess before reading the data.',
|
||||
);
|
||||
}
|
||||
return (this as Success<T>).data;
|
||||
}
|
||||
|
||||
///
|
||||
ApiError get error {
|
||||
if (isSuccess) {
|
||||
throw Exception(
|
||||
'Result is successful, not an error. Check result.isError before reading the error.',
|
||||
);
|
||||
}
|
||||
return (this as Error<T>).error;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ library stream_chat;
|
||||
|
||||
export 'package:dio/src/dio_error.dart';
|
||||
export 'package:dio/src/multipart_file.dart';
|
||||
export 'package:logging/src/level.dart';
|
||||
export 'package:logging/logging.dart' show Logger, Level;
|
||||
|
||||
export './src/api/channel.dart';
|
||||
export './src/api/connection_status.dart';
|
||||
@@ -27,3 +27,5 @@ export './src/models/reaction.dart';
|
||||
export './src/models/read.dart';
|
||||
export './src/models/user.dart';
|
||||
export './src/notifications.dart';
|
||||
export './src/db/stream_chat_database.dart';
|
||||
export './src/utils/result.dart';
|
||||
|
||||
@@ -26,6 +26,9 @@ dependencies:
|
||||
collection: ^1.14.12
|
||||
sqlite3_flutter_libs: ^0.3.0
|
||||
pedantic: ^1.9.2
|
||||
freezed: ^0.12.7
|
||||
stream_chat_persistence:
|
||||
path: ../stream_chat_persistence
|
||||
|
||||
dev_dependencies:
|
||||
build_runner: ^1.10.0
|
||||
|
||||
Reference in New Issue
Block a user