muzzle-velocity/lib/note_editor.dart
randogoth fc6c69db24 devbox setup, dep upgrades, lint fixes, Android build fixes
- Add devbox.json pinning Flutter 3.35.7, Android SDK (Nix flake),
  openjdk17, just, openssl
- Add android/nix-android-sdk/ Nix flake + flake.lock + Android Studio
  config script
- Add justfile with build/get/upgrade/icons/analyze/rebuild recipes
- flutter pub upgrade --major-versions: file_picker ^11, google_fonts ^8,
  permission_handler ^12, share_plus ^12, flutter_lints ^6 (+71 transitive)
- Fix file_picker v11 API: FilePicker.platform → FilePicker.getDirectoryPath()
- Fix all 37 analyzer issues: deprecated APIs (window, WillPopScope, Share,
  SvgPicture color), async BuildContext guards, key constructors, private
  createState() return types, unused locals, print → debugPrint, etc.
- Hook _handleCommand into onSubmitted for : prefix commands
- Android: Gradle 8.3→8.11.1, AGP 8.2.2→8.9.1, KGP 1.8.22→2.2.20,
  compileSdk/targetSdk 34→36, namespace auto-derivation for library modules,
  kotlin.compiler.execution.strategy=in-process

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 17:08:29 +03:00

338 lines
11 KiB
Dart
Executable file

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_markdown_latex/flutter_markdown_latex.dart';
import 'package:flutter_svg/svg.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:share_plus/share_plus.dart';
import 'themes/day.dart';
import 'themes/night.dart';
import 'themes/markdown.dart';
import 'codefield.dart';
class NoteEditorScreen extends StatefulWidget {
final File note;
final String? notesDirectoryPath; // ✅ Add this
final bool isDarkMode;
const NoteEditorScreen({
super.key,
required this.note,
required this.isDarkMode,
required this.notesDirectoryPath,
});
@override
State<NoteEditorScreen> createState() => _NoteEditorScreenState();
}
class _NoteEditorScreenState extends State<NoteEditorScreen> {
late CodeController _controller;
late String _initialContent;
bool _isPreviewMode = false;
bool _isLoading = true;
bool _autoSaveEnabled = false; // Controlled by user toggle
double _fontSize = 16.0;
@override
void initState() {
super.initState();
_loadPreferences();
_loadNoteContent();
}
Future<void> _loadPreferences() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_autoSaveEnabled = prefs.getBool('auto_save_enabled') ?? false;
});
}
Future<void> _setAutoSaveEnabled(bool value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('auto_save_enabled', value);
setState(() {
_autoSaveEnabled = value;
});
}
Future<void> _loadNoteContent() async {
try {
_initialContent = await widget.note.readAsString();
} catch (e) {
debugPrint("❌ Failed to read file: ${widget.note.path}");
_initialContent = "";
}
setState(() {
_controller = CodeController(
text: _initialContent,
language: markdown,
);
_controller.addListener(_onTextChanged);
_isLoading = false;
});
}
void _onTextChanged() {
if (_autoSaveEnabled && _controller.text != _initialContent) {
_saveNote();
}
}
Future<void> _saveNote() async {
try {
if (_controller.text == _initialContent) return; // No change, skip saving
await widget.note.writeAsString(_controller.text);
_initialContent = _controller.text; // Update initial state
debugPrint("✅ Note saved successfully");
} catch (e) {
debugPrint("❌ Error saving note: ${widget.note.path}");
}
}
void _shareNote() {
if (_controller.text.isNotEmpty) {
SharePlus.instance.share(ShareParams(
text: _controller.text,
subject: widget.note.uri.pathSegments.last,
));
}
}
Future<bool> _onWillPop() async {
if (_controller.text == _initialContent) return true; // No changes, exit
return await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Save Changes?"),
content: Text("You have unsaved changes. Save before exiting?"),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(true), // Discard and exit
child: Text("Discard"),
),
TextButton(
onPressed: () async {
await _saveNote();
if (context.mounted) Navigator.of(context).pop(true);
},
child: Text("Save"),
),
TextButton(
onPressed: () => Navigator.of(context).pop(false), // Stay on page
child: Text("Cancel"),
),
],
),
) ??
false;
}
void _updateFontSize(double newSize) {
setState(() {
_fontSize = newSize;
});
}
@override
void dispose() {
_controller.removeListener(_onTextChanged);
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (bool didPop, Object? result) async {
if (didPop) return;
final bool shouldPop = await _onWillPop();
if (shouldPop && context.mounted) Navigator.of(context).pop();
},
child: Scaffold(
appBar: AppBar(
title: Text(widget.note.uri.pathSegments.last.replaceAll('.md', '')),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () async {
if (await _onWillPop()) {
if (context.mounted) Navigator.pop(context);
}
},
),
actions: [
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: PopupMenuButton<String>(
icon: SvgPicture.asset(
"assets/blaster.svg",
width: 30,
colorFilter: const ColorFilter.mode(Colors.orange, BlendMode.srcIn),
),
onSelected: (String value) {
switch (value) {
case 'Share': // ✅ New case for sharing
_shareNote();
break;
case 'Save':
_saveNote();
break;
case 'Toggle Autosave':
_setAutoSaveEnabled(!_autoSaveEnabled);
break;
case 'Toggle Preview':
setState(() {
_isPreviewMode = !_isPreviewMode;
});
break;
}
},
itemBuilder: (BuildContext context) => [
PopupMenuItem<String>( // ✅ Share button
value: 'Share',
child: ListTile(
leading: Icon(Icons.share_outlined),
title: Text("Share"),
),
),
PopupMenuDivider(),
PopupMenuItem<String>(
value: 'Save',
child: ListTile(
leading: Icon(Icons.save_outlined),
title: Text("Save"),
),
),
PopupMenuItem<String>(
value: 'Toggle Autosave',
child: ListTile(
leading: Icon(_autoSaveEnabled
? Icons.alarm_off
: Icons.alarm_on),
title: Text(_autoSaveEnabled
? "Disable Autosave"
: "Enable Autosave"),
),
),
PopupMenuDivider(),
PopupMenuItem<String>(
enabled: false,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Font Size'),
StatefulBuilder(
builder: (context, setState) {
return Slider(
value: _fontSize,
min: 12.0,
max: 24.0,
divisions: 6,
label: _fontSize.toString(),
onChanged: (newSize) {
setState(() {
_fontSize = newSize;
});
_updateFontSize(newSize);
},
);
},
),
],
),
),
PopupMenuDivider(),
PopupMenuItem<String>(
value: 'Toggle Preview',
child: ListTile(
leading:
Icon(_isPreviewMode ? Icons.edit_note_outlined : Icons.visibility_outlined),
title:
Text(_isPreviewMode ? "Edit Mode" : "Preview Mode"),
),
),
],
),
),
],
),
body: _isLoading
? Center(child: CircularProgressIndicator())
: _isPreviewMode
? Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: MarkdownBody(
builders: {
'latex': LatexElementBuilder(
textStyle: TextStyle(
color: widget.isDarkMode
? Colors.white
: Colors.black)),
},
extensionSet: md.ExtensionSet(
[LatexBlockSyntax()],
[LatexInlineSyntax()],
),
data: _controller.text,
selectable: true,
styleSheet: MarkdownStyleSheet(
p: TextStyle(
fontSize: _fontSize,
color: widget.isDarkMode
? Colors.white
: Colors.black),
h1: TextStyle(
fontSize: _fontSize + 8,
color: widget.isDarkMode
? Colors.white
: Colors.black,
fontWeight: FontWeight.bold),
h2: TextStyle(
fontSize: _fontSize + 6,
color: widget.isDarkMode
? Colors.white
: Colors.black,
fontWeight: FontWeight.bold),
h3: TextStyle(
fontSize: _fontSize + 4,
color: widget.isDarkMode
? Colors.white
: Colors.black,
fontWeight: FontWeight.bold),
code: TextStyle(
fontSize: _fontSize,
backgroundColor: Colors.transparent),
codeblockDecoration: BoxDecoration(
color: Colors.transparent),
),
),
),
)
: Padding(
padding: const EdgeInsets.all(16.0),
child: CodeTheme(
data: CodeThemeData(
styles: widget.isDarkMode ? nightTheme : dayTheme),
child: SingleChildScrollView(
child: WrappedCodeField(
wrap: true,
controller: _controller,
textStyle: TextStyle(
fontSize: _fontSize,
),
),
),
),
),
),
);
}
}