Files
BadNote/lib/providers/document_provider.dart
Akiba So 2f0fda5f95
All checks were successful
CI / Windows build (push) Successful in 14m14s
feat(import): one Import-file entry + vault notebooks
Phase 3. Import becomes a single top-level action beside "Create
notebook" and the library is vault-backed.

- VaultService.createNotebook copies a picked file into a fresh
  (de-duplicated) notebook folder under the vault; its sidecar lives
  beside it, so annotations travel with the file.
- Home screen: one "Import file" action with a multi-extension picker
  (pdf / docx / pptx); routes to the editor by extension.
- The document list is now a vault scan (folders with a source file),
  not the SQLite documents table — no cache, always correct.
- PPTX soffice detection fix; DOCX convert-on-import is best-effort
  and fails gracefully when LibreOffice is unavailable.

analyze clean, tests green.
2026-06-24 21:12:38 +08:00

70 lines
2.2 KiB
Dart

import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../models/document.dart';
import '../services/vault_service.dart';
final vaultServiceProvider = FutureProvider<VaultService>((ref) async {
return VaultService.getInstance();
});
final documentListProvider =
AsyncNotifierProvider<DocumentListNotifier, List<Document>>(
DocumentListNotifier.new,
);
/// The home-screen document list is now sourced from a VAULT SCAN (folders
/// under the vault root containing a source file + optional sidecar), NOT the
/// SQLite `documents` table. The sidecar that travels with the file is the
/// source of truth; there is no SQLite cache for this list (the scan is cheap —
/// one directory listing — and always correct).
class DocumentListNotifier extends AsyncNotifier<List<Document>> {
Future<VaultService> get _vault =>
ref.read(vaultServiceProvider.future);
@override
Future<List<Document>> build() async {
return _scan();
}
Future<List<Document>> _scan() async {
final vault = await _vault;
final notebooks = await vault.scanNotebooks();
return notebooks.map(_toDocument).toList();
}
/// Adapt a scanned [VaultNotebook] into the [Document] shape the home-screen
/// tiles already render. The notebook folder path doubles as a stable id.
Document _toDocument(VaultNotebook nb) {
return Document(
id: nb.folderPath,
filename: nb.filename,
docType: nb.docType,
filePath: nb.sourceFilePath,
pageCount: 0,
createdAt: nb.modified,
updatedAt: nb.modified,
);
}
/// Re-scan the vault and publish the result. Used by pull-to-refresh and
/// after an import.
Future<void> loadDocuments() async {
state = const AsyncLoading();
state = await AsyncValue.guard(_scan);
}
/// Remove a notebook by deleting its folder (source file + sidecar travel
/// together, so removing the folder removes the whole notebook). [id] is the
/// notebook folder path produced by [_toDocument].
Future<void> removeDocument(String id) async {
final dir = Directory(id);
if (await dir.exists()) {
await dir.delete(recursive: true);
}
final current = state.value ?? [];
state = AsyncData(current.where((d) => d.id != id).toList());
}
}