refactored chat archive and added search

This commit is contained in:
Fabian Freund
2024-06-16 13:07:16 +02:00
parent 9daca26261
commit d7a65a5770
18 changed files with 1150 additions and 53 deletions
@@ -0,0 +1,72 @@
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 'file.g.dart';
@Riverpod()
class ChatArchiveFileService extends _$ChatArchiveFileService {
final Future<Directory> _archiveDirectoryFuture;
ChatArchiveFileService()
: _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()
.where((file) => path.extension(file.path) == '.md')
.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();
}
}
Stream<WatchEvent> _directoryStream() async* {
final watcher = DirectoryWatcher(
await _archiveDirectoryFuture
.then((archiveDirectory) => archiveDirectory.absolute.path),
);
yield* watcher.events.where((event) => path.extension(event.path) == '.md');
}
@override
Raw<Stream<WatchEvent>> build() {
//We return a Raw stream here and yield* doesnt support broadcast.
//So it is required to use asBroadcastStream here
return _directoryStream().asBroadcastStream();
}
}
@@ -0,0 +1,27 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'file.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$chatArchiveFileServiceHash() =>
r'21a60ae20e01cfd4f25f7f744fb9bc0c5a465e09';
/// See also [ChatArchiveFileService].
@ProviderFor(ChatArchiveFileService)
final chatArchiveFileServiceProvider = AutoDisposeNotifierProvider<
ChatArchiveFileService, Raw<Stream<WatchEvent>>>.internal(
ChatArchiveFileService.new,
name: r'chatArchiveFileServiceProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$chatArchiveFileServiceHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$ChatArchiveFileService = 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