implemented chat archive
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart' as path_provider;
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:universal_io/io.dart';
|
||||
import 'package:watcher/watcher.dart';
|
||||
|
||||
part 'chat_archive_file.g.dart';
|
||||
|
||||
@Riverpod()
|
||||
class ChatArchiveFileRepository extends _$ChatArchiveFileRepository {
|
||||
final Future<Directory> _archiveDirectoryFuture;
|
||||
|
||||
ChatArchiveFileRepository()
|
||||
: _archiveDirectoryFuture =
|
||||
path_provider.getApplicationDocumentsDirectory().then(
|
||||
(documentDirectory) => Directory(
|
||||
path.join(documentDirectory.path, 'archive', 'chat'),
|
||||
).create(recursive: true),
|
||||
);
|
||||
|
||||
Future<List<FileSystemEntity>> list() {
|
||||
return _archiveDirectoryFuture.then((value) => value.list().toList());
|
||||
}
|
||||
|
||||
Future<void> write(String fileName, String contents) async {
|
||||
final directory = await _archiveDirectoryFuture;
|
||||
final file = File(path.join(directory.path, fileName));
|
||||
|
||||
await file.writeAsString(contents, flush: true);
|
||||
}
|
||||
|
||||
Future<String?> read(String fileName) async {
|
||||
final directory = await _archiveDirectoryFuture;
|
||||
final file = File(path.join(directory.path, fileName));
|
||||
|
||||
if (!await file.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return file.readAsString();
|
||||
}
|
||||
|
||||
Future<void> delete(String fileName) async {
|
||||
final directory = await _archiveDirectoryFuture;
|
||||
final file = File(path.join(directory.path, fileName));
|
||||
|
||||
if (await file.exists()) {
|
||||
await file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Raw<Stream<WatchEvent>> build() async* {
|
||||
final watcher = DirectoryWatcher(
|
||||
await _archiveDirectoryFuture
|
||||
.then((archiveDirectory) => archiveDirectory.absolute.path),
|
||||
);
|
||||
|
||||
yield* watcher.events;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'chat_archive_file.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$chatArchiveFileRepositoryHash() =>
|
||||
r'50268506cf0c23ba1724ece6c9f44cb87be6299f';
|
||||
|
||||
/// See also [ChatArchiveFileRepository].
|
||||
@ProviderFor(ChatArchiveFileRepository)
|
||||
final chatArchiveFileRepositoryProvider = AutoDisposeNotifierProvider<
|
||||
ChatArchiveFileRepository, Raw<Stream<WatchEvent>>>.internal(
|
||||
ChatArchiveFileRepository.new,
|
||||
name: r'chatArchiveFileRepositoryProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$chatArchiveFileRepositoryHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$ChatArchiveFileRepository
|
||||
= AutoDisposeNotifier<Raw<Stream<WatchEvent>>>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
Reference in New Issue
Block a user