mirror of
https://github.com/cyberbread-ru/notepad.git
synced 2026-07-14 12:51:01 +03:00
initial vibecoded release
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../models/note.dart';
|
||||
import '../viewmodels/note_view_model.dart';
|
||||
|
||||
class NoteEditorScreen extends StatefulWidget {
|
||||
final String noteId;
|
||||
|
||||
const NoteEditorScreen({super.key, required this.noteId});
|
||||
|
||||
@override
|
||||
State<NoteEditorScreen> createState() => _NoteEditorScreenState();
|
||||
}
|
||||
|
||||
class _NoteEditorScreenState extends State<NoteEditorScreen> {
|
||||
late final TextEditingController _titleController;
|
||||
late final TextEditingController _contentController;
|
||||
late Note _note;
|
||||
bool _isDirty = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final vm = context.read<NoteViewModel>();
|
||||
_note = vm.notes.firstWhere((n) => n.id == widget.noteId);
|
||||
_titleController = TextEditingController(text: _note.title);
|
||||
_contentController = TextEditingController(text: _note.content);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_contentController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
if (!_isDirty) return;
|
||||
final vm = context.read<NoteViewModel>();
|
||||
final updated = _note.copyWith(
|
||||
title: _titleController.text,
|
||||
content: _contentController.text,
|
||||
);
|
||||
await vm.updateNote(updated);
|
||||
setState(() => _isDirty = false);
|
||||
}
|
||||
|
||||
void _onChanged() {
|
||||
if (!_isDirty) setState(() => _isDirty = true);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopScope(
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (didPop) _save();
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
titleSpacing: 0,
|
||||
title: TextField(
|
||||
controller: _titleController,
|
||||
onChanged: (_) => _onChanged(),
|
||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Title',
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
if (_isDirty)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.check),
|
||||
onPressed: _save,
|
||||
tooltip: 'Save',
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
|
||||
child: TextField(
|
||||
controller: _contentController,
|
||||
onChanged: (_) => _onChanged(),
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
keyboardType: TextInputType.multiline,
|
||||
style: const TextStyle(fontSize: 16, height: 1.5),
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Start writing...',
|
||||
border: InputBorder.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../viewmodels/note_view_model.dart';
|
||||
import 'note_editor_screen.dart';
|
||||
import 'settings_screen.dart';
|
||||
|
||||
class NoteListScreen extends StatelessWidget {
|
||||
const NoteListScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final vm = context.watch<NoteViewModel>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Notes'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.more_vert),
|
||||
onPressed: () => _showMenu(context, vm),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _buildBody(context, vm),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => _createNote(context, vm),
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(BuildContext context, NoteViewModel vm) {
|
||||
if (vm.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (vm.error != null) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(vm.error!, style: const TextStyle(color: Colors.red)),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: vm.loadNotes,
|
||||
child: const Text('Retry'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (vm.notes.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'No notes yet.\nTap + to create one.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.grey),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: vm.notes.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final note = vm.notes[index];
|
||||
return ListTile(
|
||||
title: Text(
|
||||
note.title.isEmpty ? 'Untitled' : note.title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
subtitle: Text(
|
||||
note.content.isEmpty ? 'No content' : note.content,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: Text(
|
||||
_formatDate(note.updatedAt),
|
||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
onTap: () => _openNote(context, note.id),
|
||||
onLongPress: () => _confirmDelete(context, vm, note.id),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createNote(BuildContext context, NoteViewModel vm) async {
|
||||
final note = await vm.createNote();
|
||||
if (context.mounted) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => NoteEditorScreen(noteId: note.id)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _openNote(BuildContext context, String noteId) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => NoteEditorScreen(noteId: noteId)),
|
||||
);
|
||||
}
|
||||
|
||||
void _showMenu(BuildContext context, NoteViewModel vm) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (_) => SafeArea(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.settings_outlined),
|
||||
title: const Text('Backup settings'),
|
||||
onTap: () async {
|
||||
Navigator.pop(context);
|
||||
await _openSettings(context);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.cloud_upload_outlined),
|
||||
title: const Text('Backup to server'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
_runBackup(context, vm);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.cloud_download_outlined),
|
||||
title: const Text('Restore from server'),
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
_runRestore(context, vm);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openSettings(BuildContext context) async {
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => const SettingsScreen()),
|
||||
);
|
||||
// Settings saved — user needs to restart the app for sync to activate.
|
||||
// We'll improve this later when we add a proper service locator.
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Restart the app to apply backup settings.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _runBackup(BuildContext context, NoteViewModel vm) async {
|
||||
await vm.backup();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
vm.backupStatus == BackupStatus.success
|
||||
? 'Backup successful'
|
||||
: 'Backup failed: ${vm.error}',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _runRestore(BuildContext context, NoteViewModel vm) async {
|
||||
await vm.restore();
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
vm.backupStatus == BackupStatus.success
|
||||
? 'Restore successful'
|
||||
: 'Restore failed: ${vm.error}',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _confirmDelete(
|
||||
BuildContext context, NoteViewModel vm, String id) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Delete note?'),
|
||||
content: const Text('This cannot be undone.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Delete', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed == true) await vm.deleteNote(id);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime dt) {
|
||||
final now = DateTime.now();
|
||||
if (dt.year == now.year && dt.month == now.month && dt.day == now.day) {
|
||||
return '${dt.hour.toString().padLeft(2, '0')}:${dt.minute.toString().padLeft(2, '0')}';
|
||||
}
|
||||
return '${dt.day}/${dt.month}/${dt.year}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class SettingsScreen extends StatefulWidget {
|
||||
const SettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
State<SettingsScreen> createState() => _SettingsScreenState();
|
||||
}
|
||||
|
||||
class _SettingsScreenState extends State<SettingsScreen> {
|
||||
final _urlController = TextEditingController();
|
||||
final _keyController = TextEditingController();
|
||||
bool _isSaving = false;
|
||||
bool _obscureKey = true;
|
||||
|
||||
static const _urlPrefKey = 'server_url';
|
||||
static const _apiKeyPrefKey = 'api_key';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSettings();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_urlController.dispose();
|
||||
_keyController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _loadSettings() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_urlController.text = prefs.getString(_urlPrefKey) ?? '';
|
||||
_keyController.text = prefs.getString(_apiKeyPrefKey) ?? '';
|
||||
}
|
||||
|
||||
Future<void> _saveSettings() async {
|
||||
setState(() => _isSaving = true);
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_urlPrefKey, _urlController.text.trim());
|
||||
await prefs.setString(_apiKeyPrefKey, _keyController.text.trim());
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Settings saved')),
|
||||
);
|
||||
Navigator.pop(context, true); // true = settings changed
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _isSaving = false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _clearSettings() async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(
|
||||
title: const Text('Clear settings?'),
|
||||
content: const Text('Server URL and API key will be removed.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Clear', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed == true) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_urlPrefKey);
|
||||
await prefs.remove(_apiKeyPrefKey);
|
||||
_urlController.clear();
|
||||
_keyController.clear();
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Settings cleared')),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Backup settings'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
tooltip: 'Clear settings',
|
||||
onPressed: _clearSettings,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.secondaryContainer,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.info_outline,
|
||||
color: Theme.of(context).colorScheme.onSecondaryContainer,
|
||||
size: 20,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Point to your running Python server. '
|
||||
'Leave blank to use local storage only.',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onSecondaryContainer,
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
TextField(
|
||||
controller: _urlController,
|
||||
keyboardType: TextInputType.url,
|
||||
autocorrect: false,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server URL',
|
||||
hintText: 'http://192.168.1.x:8000',
|
||||
prefixIcon: Icon(Icons.dns_outlined),
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextField(
|
||||
controller: _keyController,
|
||||
obscureText: _obscureKey,
|
||||
autocorrect: false,
|
||||
enableSuggestions: false,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'API Key',
|
||||
hintText: 'Your X-API-Key value',
|
||||
prefixIcon: const Icon(Icons.key_outlined),
|
||||
border: const OutlineInputBorder(),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(
|
||||
_obscureKey
|
||||
? Icons.visibility_outlined
|
||||
: Icons.visibility_off_outlined,
|
||||
),
|
||||
onPressed: () => setState(() => _obscureKey = !_obscureKey),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
FilledButton.icon(
|
||||
onPressed: _isSaving ? null : _saveSettings,
|
||||
icon: _isSaving
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.save_outlined),
|
||||
label: Text(_isSaving ? 'Saving...' : 'Save'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AppSettings {
|
||||
static const _urlPrefKey = 'server_url';
|
||||
static const _apiKeyPrefKey = 'api_key';
|
||||
|
||||
static Future<({String url, String apiKey})> load() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return (
|
||||
url: prefs.getString(_urlPrefKey) ?? '',
|
||||
apiKey: prefs.getString(_apiKeyPrefKey) ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
static bool isConfigured({required String url, required String apiKey}) {
|
||||
return url.isNotEmpty && apiKey.isNotEmpty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user