This repository has been archived on 2026-06-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FabledApp/lib/screens/notes/note_edit_screen.dart
T
bvandeusen fceae5529d Add Projects feature; sync Note/Task models with backend additions
## New: Projects
- Project model, API, repository, Riverpod provider
- ProjectListScreen: active/archived sections, create dialog, long-press status/delete
- ProjectSelector widget: DropdownButtonFormField for note + task editors
- Projects tab added to shell (bottom nav + navigation rail, 4th position)
- /projects route registered in GoRouter ShellRoute

## Updated: Note model
- Added tags: List<String>, projectId: int?, milestoneId: int?
- NotesApi.create/update pass tags and project_id
- NotesRepository and NotesNotifier signatures updated
- NoteEditScreen: chip-based tag input + ProjectSelector

## Updated: Task model
- Added projectId: int?, milestoneId: int?, parentId: int?
- TasksApi.create passes project_id; update payload includes project_id
- TasksRepository and TasksNotifier signatures updated
- TaskEditScreen: ProjectSelector added; project_id sent on save

## Provider fix
- ProjectsNotifier.update renamed to updateProject to avoid conflict
  with AsyncNotifier.update(FutureOr<State> Function(State)) base method

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 20:52:59 -05:00

282 lines
9.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../core/exceptions.dart';
import '../../core/wikilink_syntax.dart';
import '../../providers/api_client_provider.dart';
import '../../providers/notes_provider.dart';
import '../../widgets/project_selector.dart';
class NoteEditScreen extends ConsumerStatefulWidget {
final int? noteId;
const NoteEditScreen({super.key, this.noteId});
@override
ConsumerState<NoteEditScreen> createState() => _NoteEditScreenState();
}
class _NoteEditScreenState extends ConsumerState<NoteEditScreen> {
final _titleController = TextEditingController();
final _contentController = TextEditingController();
final _tagController = TextEditingController();
List<String> _tags = [];
int? _projectId;
bool _preview = false;
bool _saving = false;
late final Future<void> _initFuture;
@override
void initState() {
super.initState();
_initFuture =
widget.noteId != null ? _loadExisting() : Future.value();
}
@override
void dispose() {
_titleController.dispose();
_contentController.dispose();
_tagController.dispose();
super.dispose();
}
Future<void> _loadExisting() async {
final note =
await ref.read(notesRepositoryProvider).getOne(widget.noteId!);
_titleController.text = note.title;
_contentController.text = note.body;
_tags = List<String>.from(note.tags);
_projectId = note.projectId;
}
void _addTag(String raw) {
final tag = raw.trim().replaceAll(',', '').toLowerCase();
if (tag.isEmpty || _tags.contains(tag)) {
_tagController.clear();
return;
}
setState(() {
_tags = [..._tags, tag];
_tagController.clear();
});
}
void _removeTag(String tag) {
setState(() => _tags = _tags.where((t) => t != tag).toList());
}
Future<void> _delete() async {
final confirm = await showDialog<bool>(
context: context,
builder: (dialogContext) => AlertDialog(
title: const Text('Delete note?'),
content: Text(_titleController.text),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext, false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(dialogContext, true),
child: const Text('Delete'),
),
],
),
);
if (confirm == true) {
await ref.read(notesProvider.notifier).delete(widget.noteId!);
if (mounted) context.pop();
}
}
Future<void> _save() async {
final title = _titleController.text.trim();
final body = _contentController.text;
if (title.isEmpty) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text('Title is required.')));
return;
}
setState(() => _saving = true);
try {
if (widget.noteId == null) {
await ref.read(notesProvider.notifier).create(
title,
body,
tags: _tags,
projectId: _projectId,
);
if (mounted) context.pop();
} else {
await ref.read(notesProvider.notifier).updateNote(
widget.noteId!,
title,
body,
tags: _tags,
projectId: _projectId,
clearProject: _projectId == null,
);
if (mounted) context.pop();
}
} on AppException catch (e) {
if (mounted) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(e.message)));
}
} finally {
if (mounted) setState(() => _saving = false);
}
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initFuture,
builder: (context, snapshot) {
return Scaffold(
appBar: AppBar(
title: Text(widget.noteId == null ? 'New Note' : 'Edit Note'),
actions: [
if (widget.noteId != null)
IconButton(
icon: const Icon(Icons.delete_outline),
tooltip: 'Delete',
onPressed: _delete,
),
IconButton(
icon: Icon(_preview ? Icons.edit : Icons.preview),
tooltip: _preview ? 'Edit' : 'Preview',
onPressed: () => setState(() => _preview = !_preview),
),
IconButton(
icon: _saving
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.check),
onPressed: _saving ? null : _save,
),
],
),
body: snapshot.connectionState == ConnectionState.waiting
? const Center(child: CircularProgressIndicator())
: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 0),
child: TextField(
controller: _titleController,
decoration: const InputDecoration(
hintText: 'Title',
border: InputBorder.none,
),
style: Theme.of(context).textTheme.titleLarge,
textInputAction: TextInputAction.next,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: _TagInput(
tags: _tags,
controller: _tagController,
onAdd: _addTag,
onRemove: _removeTag,
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: ProjectSelector(
value: _projectId,
onChanged: (id) => setState(() => _projectId = id),
),
),
const Divider(height: 1),
Expanded(
child: _preview
? Markdown(
data: _contentController.text,
extensionSet: wikilinkExtensionSet,
)
: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16),
child: TextField(
controller: _contentController,
decoration: const InputDecoration(
hintText: 'Write in markdown...',
border: InputBorder.none,
),
maxLines: null,
expands: true,
keyboardType: TextInputType.multiline,
textAlignVertical: TextAlignVertical.top,
),
),
),
],
),
);
},
);
}
}
class _TagInput extends StatelessWidget {
final List<String> tags;
final TextEditingController controller;
final ValueChanged<String> onAdd;
final ValueChanged<String> onRemove;
const _TagInput({
required this.tags,
required this.controller,
required this.onAdd,
required this.onRemove,
});
@override
Widget build(BuildContext context) {
return Wrap(
spacing: 6,
runSpacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
...tags.map(
(tag) => Chip(
label: Text('#$tag', style: const TextStyle(fontSize: 12)),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
padding: const EdgeInsets.symmetric(horizontal: 4),
deleteIcon: const Icon(Icons.close, size: 14),
onDeleted: () => onRemove(tag),
),
),
SizedBox(
width: 120,
child: TextField(
controller: controller,
decoration: const InputDecoration(
hintText: 'Add tag…',
border: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.symmetric(vertical: 4),
),
style: const TextStyle(fontSize: 13),
textInputAction: TextInputAction.done,
onSubmitted: onAdd,
onChanged: (v) {
if (v.endsWith(',') || v.endsWith(' ')) {
onAdd(v.replaceAll(RegExp(r'[, ]+$'), ''));
}
},
),
),
],
);
}
}