feat(import): one Import-file entry + vault notebooks
All checks were successful
CI / Windows build (push) Successful in 14m14s
All checks were successful
CI / Windows build (push) Successful in 14m14s
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.
This commit is contained in:
@@ -1,61 +1,68 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../models/document.dart';
|
||||
import '../services/database_service.dart';
|
||||
import 'note_provider.dart';
|
||||
import '../services/vault_service.dart';
|
||||
|
||||
const _uuid = Uuid();
|
||||
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<DatabaseService> get _db => ref.read(databaseServiceProvider.future);
|
||||
Future<VaultService> get _vault =>
|
||||
ref.read(vaultServiceProvider.future);
|
||||
|
||||
@override
|
||||
Future<List<Document>> build() async {
|
||||
final db = await _db;
|
||||
return db.getAllDocuments();
|
||||
return _scan();
|
||||
}
|
||||
|
||||
/// Reloads documents from the database and publishes the result to [state]
|
||||
/// so the UI rebuilds. Used by pull-to-refresh.
|
||||
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(() async {
|
||||
final db = await _db;
|
||||
return db.getAllDocuments();
|
||||
});
|
||||
}
|
||||
|
||||
Future<Document> addDocument({
|
||||
required String filename,
|
||||
required String docType,
|
||||
required String filePath,
|
||||
int pageCount = 0,
|
||||
}) async {
|
||||
final db = await _db;
|
||||
final now = DateTime.now();
|
||||
final document = Document(
|
||||
id: _uuid.v4(),
|
||||
filename: filename,
|
||||
docType: docType,
|
||||
filePath: filePath,
|
||||
pageCount: pageCount,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
await db.insertDocument(document);
|
||||
state = AsyncData([document, ...state.value ?? []]);
|
||||
return document;
|
||||
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 db = await _db;
|
||||
await db.deleteDocument(id);
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user