feat(vault): pick a notebook vault folder on first run
Some checks failed
CI / Windows build (push) Has been cancelled
Some checks failed
CI / Windows build (push) Has been cancelled
Phase 0 of the file-based storage plan (docs/plans/ 2026-06-24-file-based-storage.md). Foundation only — no editor or DB change yet. - VaultService (SharedPreferences): stores the vault root path, vaultRootValid() = path set AND directory exists. - VaultSetupScreen: first-run folder picker (file_picker, Windows). - main.dart gates HomeScreen behind a valid vault, re-prompting if the saved folder is gone. - Settings: a Vault section to change the folder. Editors still use SQLite; later phases move annotations into per-file sidecars under the vault. analyze clean, tests green.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
@@ -6,6 +7,7 @@ import '../l10n/app_localizations.dart';
|
||||
import '../models/pen_tool.dart';
|
||||
import '../models/pressure_curve.dart';
|
||||
import '../providers/settings_provider.dart';
|
||||
import '../services/vault_service.dart';
|
||||
import '../utils/stroke_stabilizer.dart';
|
||||
|
||||
/// Material 3 settings screen for BadNote.
|
||||
@@ -282,6 +284,12 @@ class SettingsScreen extends ConsumerWidget {
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
_SectionHeader(title: 'Vault', icon: Icons.folder_special),
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: _VaultSettings(),
|
||||
),
|
||||
const Divider(),
|
||||
_SectionHeader(title: 'About', icon: Icons.info),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
@@ -320,6 +328,93 @@ class SettingsScreen extends ConsumerWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows the current vault folder path and lets the user re-pick it. Re-uses
|
||||
/// the same `getDirectoryPath` flow as the first-run [VaultSetupScreen],
|
||||
/// persisting the choice through [VaultService.setVaultRoot].
|
||||
class _VaultSettings extends StatefulWidget {
|
||||
const _VaultSettings();
|
||||
|
||||
@override
|
||||
State<_VaultSettings> createState() => _VaultSettingsState();
|
||||
}
|
||||
|
||||
class _VaultSettingsState extends State<_VaultSettings> {
|
||||
VaultService? _vault;
|
||||
String? _path;
|
||||
bool _busy = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
final vault = await VaultService.getInstance();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_vault = vault;
|
||||
_path = vault.vaultRoot;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _changeFolder() async {
|
||||
final vault = _vault;
|
||||
if (vault == null) return;
|
||||
final l = AppLocalizations.of(context);
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
final path = await FilePicker.platform.getDirectoryPath(
|
||||
dialogTitle: l.vaultSetupTitle,
|
||||
lockParentWindow: true,
|
||||
);
|
||||
if (path != null) {
|
||||
await vault.setVaultRoot(path);
|
||||
if (!mounted) return;
|
||||
setState(() => _path = path);
|
||||
messenger.showSnackBar(SnackBar(content: Text(l.vaultUpdated)));
|
||||
}
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
messenger.showSnackBar(
|
||||
SnackBar(content: Text(l.vaultPickFailed(e.toString()))),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l = AppLocalizations.of(context);
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
l.vaultFolderLabel,
|
||||
style: const TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
(_path == null || _path!.isEmpty) ? l.vaultNoneSelected : _path!,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton.icon(
|
||||
onPressed: _busy ? null : _changeFolder,
|
||||
icon: const Icon(Icons.drive_folder_upload),
|
||||
label: Text(l.vaultChangeFolder),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SectionHeader extends StatelessWidget {
|
||||
final String title;
|
||||
final IconData icon;
|
||||
|
||||
158
lib/screens/vault_setup_screen.dart
Normal file
158
lib/screens/vault_setup_screen.dart
Normal file
@@ -0,0 +1,158 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../services/vault_service.dart';
|
||||
|
||||
/// First-run (and re-prompt) gate that asks the user to pick a vault root
|
||||
/// folder — the single folder under which all notebooks will live, like an
|
||||
/// Obsidian vault.
|
||||
///
|
||||
/// On a successful, writable selection the chosen path is persisted via
|
||||
/// [VaultService] and [onVaultReady] is invoked so the host can proceed to the
|
||||
/// home screen. When [missing] is true the screen shows a "your vault folder is
|
||||
/// missing" message instead of the first-run copy (the saved folder no longer
|
||||
/// exists).
|
||||
class VaultSetupScreen extends StatefulWidget {
|
||||
const VaultSetupScreen({
|
||||
super.key,
|
||||
required this.vaultService,
|
||||
required this.onVaultReady,
|
||||
this.missing = false,
|
||||
});
|
||||
|
||||
final VaultService vaultService;
|
||||
|
||||
/// Called after a valid, writable vault root has been persisted.
|
||||
final VoidCallback onVaultReady;
|
||||
|
||||
/// Whether a previously-chosen folder went missing (changes the copy).
|
||||
final bool missing;
|
||||
|
||||
@override
|
||||
State<VaultSetupScreen> createState() => _VaultSetupScreenState();
|
||||
}
|
||||
|
||||
class _VaultSetupScreenState extends State<VaultSetupScreen> {
|
||||
bool _busy = false;
|
||||
String? _error;
|
||||
|
||||
Future<void> _pickFolder() async {
|
||||
final l = AppLocalizations.of(context);
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
// getDirectoryPath is Desktop/Windows supported by file_picker.
|
||||
// lockParentWindow makes the native Windows dialog modal.
|
||||
final path = await FilePicker.platform.getDirectoryPath(
|
||||
dialogTitle: l.vaultSetupTitle,
|
||||
lockParentWindow: true,
|
||||
);
|
||||
if (path == null) {
|
||||
// User cancelled the native dialog.
|
||||
if (mounted) setState(() => _busy = false);
|
||||
return;
|
||||
}
|
||||
if (!await _isWritable(path)) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_error = l.vaultNotWritable;
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
await widget.vaultService.setVaultRoot(path);
|
||||
if (mounted) widget.onVaultReady();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_busy = false;
|
||||
_error = l.vaultPickFailed(e.toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Probe writability by creating and deleting a temp file in [path]. The
|
||||
/// native folder dialog can hand back a read-only location on Windows, so we
|
||||
/// validate before committing it as the vault.
|
||||
Future<bool> _isWritable(String path) async {
|
||||
final probe = File(p.join(path, '.badnote-write-probe'));
|
||||
try {
|
||||
await probe.writeAsString('ok', flush: true);
|
||||
await probe.delete();
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l = AppLocalizations.of(context);
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 480),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(
|
||||
widget.missing
|
||||
? Icons.folder_off_outlined
|
||||
: Icons.folder_special_outlined,
|
||||
size: 56,
|
||||
color: colorScheme.primary,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
widget.missing ? l.vaultMissingTitle : l.vaultSetupHeadline,
|
||||
style: textTheme.headlineSmall
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
widget.missing ? l.vaultMissingBody : l.vaultSetupBody,
|
||||
style: textTheme.bodyMedium
|
||||
?.copyWith(color: colorScheme.onSurfaceVariant),
|
||||
),
|
||||
if (_error != null) ...[
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
_error!,
|
||||
style: textTheme.bodyMedium
|
||||
?.copyWith(color: colorScheme.error),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 32),
|
||||
FilledButton.icon(
|
||||
onPressed: _busy ? null : _pickFolder,
|
||||
icon: _busy
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.folder_open),
|
||||
label: Text(l.vaultChooseFolder),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user