434 lines
14 KiB
Dart
434 lines
14 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import 'package:path/path.dart' as path;
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'note_editor.dart';
|
|
|
|
class NoteListScreen extends StatefulWidget {
|
|
final void Function(bool) toggleTheme;
|
|
final VoidCallback toggleTags;
|
|
final bool isDarkMode;
|
|
final bool showTags;
|
|
|
|
NoteListScreen({
|
|
required this.toggleTheme,
|
|
required this.toggleTags,
|
|
required this.isDarkMode,
|
|
required this.showTags,
|
|
});
|
|
|
|
@override
|
|
_NoteListScreenState createState() => _NoteListScreenState();
|
|
}
|
|
|
|
class _NoteListScreenState extends State<NoteListScreen> {
|
|
TextEditingController searchController = TextEditingController();
|
|
List<File> notes = [];
|
|
List<File> filteredNotes = [];
|
|
String? notesDirectoryPath;
|
|
String? defaultDir;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initializeApp(); // Call an async method to handle initialization
|
|
searchController.addListener(() {
|
|
final text = searchController.text.trim();
|
|
if (text.startsWith(':')) {
|
|
_handleCommand(text);
|
|
} else {
|
|
_filterNotes();
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _initializeApp() async {
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
setState(() {
|
|
defaultDir = dir.path; // Update state with defaultDir
|
|
});
|
|
await _loadNotes(); // Load notes after setting defaultDir
|
|
}
|
|
|
|
void _showDirectoryChoiceDialog(String defaultPath) {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false, // Force user to make a choice
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text("Choose Notes Directory"),
|
|
content: Text(
|
|
"Your notes will be saved in:\n\n📂 $defaultPath\n\nWould you like to use a different folder?"),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () async {
|
|
// Reset to default directory
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(
|
|
'notes_directory'); // Remove custom directory from preferences
|
|
setState(() {
|
|
notesDirectoryPath = defaultDir; // Reset to default directory
|
|
});
|
|
Navigator.pop(context); // Close dialog
|
|
_loadNotes(); // Reload notes from the default directory
|
|
},
|
|
child: Text("Use Default"),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
Navigator.pop(context); // Close the current dialog
|
|
await _selectNotesDirectory(); // Allow user to pick a folder
|
|
},
|
|
child: Text("Choose Folder"),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> _selectNotesDirectory() async {
|
|
String? selectedDirectory = await FilePicker.platform.getDirectoryPath();
|
|
if (selectedDirectory != null) {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(
|
|
'notes_directory', selectedDirectory); // Save custom directory
|
|
setState(() {
|
|
notesDirectoryPath = selectedDirectory; // Update state
|
|
});
|
|
_loadNotes(); // Reload notes from the new directory
|
|
}
|
|
}
|
|
|
|
Future<void> _loadNotes() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
notesDirectoryPath = prefs.getString('notes_directory') ??
|
|
defaultDir; // Use custom or default directory
|
|
|
|
final notesDir = Directory(notesDirectoryPath!);
|
|
if (!notesDir.existsSync()) {
|
|
notesDir.createSync(recursive: true);
|
|
}
|
|
|
|
final files = notesDir
|
|
.listSync()
|
|
.whereType<File>()
|
|
.where((file) => file.path.endsWith('.md'))
|
|
.toList();
|
|
setState(() {
|
|
notes = files;
|
|
filteredNotes = files;
|
|
});
|
|
}
|
|
|
|
void _filterNotes() {
|
|
final query = searchController.text.toLowerCase();
|
|
setState(() {
|
|
filteredNotes = notes.where((note) {
|
|
final content = note.readAsStringSync().toLowerCase();
|
|
return note.uri.pathSegments.last.toLowerCase().contains(query) ||
|
|
content.contains(query);
|
|
}).toList();
|
|
});
|
|
}
|
|
|
|
List<InlineSpan> _formatTags(String content) {
|
|
final tagPattern = RegExp(r'([#@+][a-zA-Z0-9_]+)');
|
|
final List<InlineSpan> spans = [];
|
|
for (final match in tagPattern.allMatches(content)) {
|
|
final tag = match.group(0)!;
|
|
Color tagColor = Colors.white;
|
|
if (tag.startsWith('#')) tagColor = Colors.pinkAccent;
|
|
if (tag.startsWith('+')) tagColor = Colors.cyan;
|
|
if (tag.startsWith('@')) tagColor = Colors.orange;
|
|
|
|
spans.add(TextSpan(
|
|
text: tag + ' ',
|
|
style: TextStyle(color: tagColor, fontWeight: FontWeight.bold)));
|
|
}
|
|
return spans;
|
|
}
|
|
|
|
void _createNewNote(String title) async {
|
|
try {
|
|
final safeTitle = _sanitizeFilename(title);
|
|
final newNote = File(path.join(notesDirectoryPath!, '$safeTitle.md'));
|
|
|
|
if (!newNote.existsSync()) {
|
|
newNote.writeAsStringSync('# $title\n\n');
|
|
setState(() {
|
|
notes.add(newNote);
|
|
filteredNotes.add(newNote);
|
|
});
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NoteEditorScreen(
|
|
note: newNote,
|
|
isDarkMode: widget.isDarkMode,
|
|
notesDirectoryPath: notesDirectoryPath,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Failed to create note: $e'),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
bool _isNewNote(String title) {
|
|
final safeTitle = _sanitizeFilename(title);
|
|
return !notes.any((note) => note.uri.pathSegments.last == '$safeTitle.md');
|
|
}
|
|
|
|
String _sanitizeFilename(String title) {
|
|
return title
|
|
.replaceAll(RegExp(r'[<>:"/\\|?*]'), '') // Remove invalid characters
|
|
.trim();
|
|
}
|
|
|
|
void _deleteNote(File note) {
|
|
final trashDir = Directory('${note.parent.path}/trash');
|
|
if (!trashDir.existsSync()) {
|
|
trashDir.createSync(recursive: true);
|
|
}
|
|
final trashedNote = File('${trashDir.path}/${note.uri.pathSegments.last}');
|
|
note.renameSync(trashedNote.path);
|
|
|
|
setState(() {
|
|
notes.remove(note);
|
|
filteredNotes.remove(note);
|
|
});
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Note moved to trash'),
|
|
action: SnackBarAction(
|
|
label: 'Undo',
|
|
onPressed: () {
|
|
trashedNote.renameSync(note.path);
|
|
setState(() {
|
|
notes.add(note);
|
|
filteredNotes.add(note);
|
|
});
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _emptyTrash() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final notesPath = prefs.getString('notes_directory') ??
|
|
(await getApplicationDocumentsDirectory()).path;
|
|
final trashDir = Directory('$notesPath/trash');
|
|
if (trashDir.existsSync()) {
|
|
trashDir.deleteSync(recursive: true);
|
|
trashDir.createSync();
|
|
}
|
|
}
|
|
|
|
void _confirmEmptyTrash() {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text("Empty Trash"),
|
|
content: Text(
|
|
"Are you sure you want to permanently delete all trashed notes?"),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: Text("Cancel"),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
_emptyTrash();
|
|
Navigator.pop(context);
|
|
},
|
|
child: Text("Empty Trash"),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
void _handleCommand(String text) {
|
|
switch (text) {
|
|
case ':tags':
|
|
widget.toggleTags();
|
|
break;
|
|
case ':notags':
|
|
widget.toggleTags();
|
|
break;
|
|
case ':day':
|
|
widget.toggleTheme(false);
|
|
break;
|
|
case ':night':
|
|
widget.toggleTheme(true);
|
|
break;
|
|
case ':emptytrash':
|
|
_confirmEmptyTrash();
|
|
break;
|
|
default:
|
|
return; // Do nothing for unrecognized commands
|
|
}
|
|
searchController.clear();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 15),
|
|
child: TextField(
|
|
enableInteractiveSelection: true,
|
|
autocorrect: false,
|
|
textInputAction: TextInputAction.done,
|
|
controller: searchController,
|
|
decoration: InputDecoration(
|
|
hintText: 'Search or enter a command...',
|
|
border: InputBorder.none,
|
|
),
|
|
style: GoogleFonts.ibmPlexMono(color: Colors.grey[300]),
|
|
onSubmitted: (text) {
|
|
final trimmedText = text.trim();
|
|
if (trimmedText.isNotEmpty && _isNewNote(trimmedText)) {
|
|
_createNewNote(trimmedText);
|
|
searchController
|
|
.clear(); // Clears the search field after creation
|
|
}
|
|
},
|
|
),
|
|
),
|
|
actions: [
|
|
Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 15),
|
|
child: PopupMenuButton<String>(
|
|
icon: Icon(Icons.radio_button_checked,
|
|
color: Colors.orange), // Menu button (⋮)
|
|
onSelected: (String value) {
|
|
switch (value) {
|
|
case 'Toggle Tags':
|
|
widget.toggleTags();
|
|
break;
|
|
case 'Toggle Theme':
|
|
setState(() {
|
|
widget.toggleTheme(!widget.isDarkMode);
|
|
});
|
|
break;
|
|
case 'Select Folder':
|
|
_showDirectoryChoiceDialog(
|
|
notesDirectoryPath ?? ''); // Re-trigger popup
|
|
break;
|
|
case 'Empty Trash':
|
|
_confirmEmptyTrash();
|
|
break;
|
|
}
|
|
},
|
|
itemBuilder: (BuildContext context) => [
|
|
PopupMenuItem<String>(
|
|
value: 'Toggle Tags',
|
|
child: ListTile(
|
|
leading:
|
|
Icon(widget.showTags ? Icons.label_off : Icons.label),
|
|
title: Text(widget.showTags ? 'Hide Tags' : 'Show Tags'),
|
|
),
|
|
),
|
|
PopupMenuItem<String>(
|
|
value: 'Toggle Theme',
|
|
child: ListTile(
|
|
leading: Icon(
|
|
Theme.of(context).brightness == Brightness.dark
|
|
? Icons.light_mode
|
|
: Icons.dark_mode,
|
|
),
|
|
title: Text(
|
|
Theme.of(context).brightness == Brightness.dark
|
|
? 'Switch to Light Mode'
|
|
: 'Switch to Dark Mode',
|
|
),
|
|
),
|
|
),
|
|
PopupMenuItem<String>(
|
|
value: 'Select Folder',
|
|
child: ListTile(
|
|
leading: Icon(Icons.folder),
|
|
title: Text('Set Notes Directory'),
|
|
),
|
|
),
|
|
PopupMenuItem<String>(
|
|
value: 'Empty Trash',
|
|
child: ListTile(
|
|
leading: Icon(Icons.delete, color: Colors.redAccent),
|
|
title: Text('Empty Trash',
|
|
style: TextStyle(color: Colors.redAccent)),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 15),
|
|
child: ListView.builder(
|
|
itemCount: filteredNotes.length,
|
|
itemBuilder: (context, index) {
|
|
final noteFile = filteredNotes[index];
|
|
final noteContent = noteFile.readAsStringSync();
|
|
final tagSpans = _formatTags(noteContent).isNotEmpty
|
|
? _formatTags(noteContent)
|
|
: [TextSpan(text: '')];
|
|
|
|
return Dismissible(
|
|
key: Key(noteFile.path),
|
|
direction: DismissDirection.endToStart,
|
|
background: Container(
|
|
color: Colors.redAccent,
|
|
alignment: Alignment.centerRight,
|
|
padding: EdgeInsets.symmetric(horizontal: 20),
|
|
child: Icon(Icons.delete, color: Colors.white),
|
|
),
|
|
onDismissed: (direction) => _deleteNote(noteFile),
|
|
child: ListTile(
|
|
title:
|
|
Text(noteFile.uri.pathSegments.last.replaceAll('.md', '')),
|
|
subtitle: widget.showTags && tagSpans.isNotEmpty
|
|
? RichText(
|
|
text: TextSpan(
|
|
style: DefaultTextStyle.of(context).style,
|
|
children: tagSpans))
|
|
: null,
|
|
onTap: () async {
|
|
await Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NoteEditorScreen(
|
|
note: File(path.join(notesDirectoryPath!,
|
|
noteFile.uri.pathSegments.last)),
|
|
isDarkMode: widget.isDarkMode,
|
|
notesDirectoryPath: notesDirectoryPath,
|
|
),
|
|
),
|
|
);
|
|
_loadNotes(); // Reload notes after returning
|
|
setState(() {}); // Force UI refresh
|
|
},
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|