Saving and sorting - Book update

This commit is contained in:
2025-04-08 17:34:11 +02:00
parent ba75cb70a2
commit 3914be95b1
4 changed files with 350 additions and 292 deletions
+124 -160
View File
@@ -525,199 +525,164 @@ void _moveBullet(Offset offsetDelta) {
Future<String?> _showSaveOptionsDialog() async {
Future<String?> _showSaveOptionsDialog() async {
final directory = await getApplicationDocumentsDirectory();
final saveDirectory = Directory('${directory.path}/saved_images');
// Ensure directory exists
if (!await saveDirectory.exists()) {
await saveDirectory.create(recursive: true);
}
// Load existing folders and their modified time
final folderStats = <Map<String, dynamic>>[];
final folders = saveDirectory.listSync();
for (var folder in folders) {
if (folder is Directory) {
final stat = folder.statSync();
folderStats.add({
'name': folder.path.split('/').last,
'changed': stat.changed,
});
final folders = saveDirectory.listSync().whereType<Directory>().toList();
// Sort to get most recently modified
folders.sort((a, b) => b.statSync().changed.compareTo(a.statSync().changed));
final String? lastFolder = folders.isNotEmpty ? folders.first.path.split('/').last : null;
bool lastIsTournament = false;
if (lastFolder != null) {
final metadataFile = File('${folders.first.path}/metadata.json');
if (await metadataFile.exists()) {
try {
final metadata = jsonDecode(await metadataFile.readAsString());
lastIsTournament = metadata['isTournament'] == true;
} catch (_) {}
}
}
// Sort by modified time, newest first
folderStats.sort((a, b) =>
(b['changed'] as DateTime).compareTo(a['changed'] as DateTime));
String? lastFolderName =
folderStats.isNotEmpty ? folderStats.first['name'] : null;
// Auto-generate next Training and Tournament folder names
int trainingCount = 1;
int tournamentCount = 1;
TextEditingController _controller = TextEditingController();
bool isNewFolderEnabled = folderStats.isEmpty; // pre-enable if none
bool isTournament = false;
for (var folder in folders) {
final name = folder.path.split('/').last;
if (name.startsWith('Training')) {
final num = int.tryParse(name.replaceAll('Training ', ''));
if (num != null && num >= trainingCount) trainingCount = num + 1;
}
if (name.startsWith('Tournament')) {
final num = int.tryParse(name.replaceAll('Tournament ', ''));
if (num != null && num >= tournamentCount) tournamentCount = num + 1;
}
}
return await showDialog<String>(
final String nextTraining = 'Training $trainingCount';
final String nextTournament = 'Tournament $tournamentCount';
return showDialog<String>(
context: context,
builder: (context) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: StatefulBuilder(
builder: (context, setState) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.only(bottom: 16.0),
child: Text(
'Save Target Data',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Save Target Data',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 24),
// 🎯 Training Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.pop(
context,
jsonEncode({'folder': nextTraining, 'isTournament': false}),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: _bulletColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
const Text(
"Select where you'd like to save the data:",
style: TextStyle(color: Colors.white70),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
child: Text('🎯 Save to "$nextTraining"'),
),
),
const SizedBox(height: 12),
// Quick save to newest folder
if (lastFolderName != null) ...[
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.pop(context, lastFolderName);
},
style: ElevatedButton.styleFrom(
backgroundColor: _bulletColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text("Save to '$lastFolderName'"),
),
),
const SizedBox(height: 16),
],
// Always show folder name input
SizedBox(
width: double.infinity,
child: TextField(
controller: _controller,
style: const TextStyle(color: Colors.white),
textAlign: TextAlign.center,
decoration: InputDecoration(
filled: true,
fillColor: Colors.grey[800],
hintText: "Enter a new folder name",
hintStyle: const TextStyle(color: Colors.white54),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 14),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide.none,
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: _bulletColor),
),
),
onChanged: (value) {
setState(() {
isNewFolderEnabled = value.trim().isNotEmpty;
});
},
// 🏆 Tournament Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.pop(
context,
jsonEncode({'folder': nextTournament, 'isTournament': true}),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: _bulletColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
const SizedBox(height: 12),
child: Text('🏆 Save to "$nextTournament"'),
),
),
if (lastFolder != null) ...[
const SizedBox(height: 12),
// Tournament toggle
if (isNewFolderEnabled) ...[
GestureDetector(
onTap: () {
setState(() {
isTournament = !isTournament;
});
},
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: isTournament
? _bulletColor.withOpacity(0.2)
: Colors.grey[850],
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isTournament
? _bulletColor
: Colors.grey[700]!,
width: 1.5,
),
),
padding: const EdgeInsets.symmetric(
horizontal: 16, vertical: 14),
child: Center(
child: Text(
isTournament
? 'Tournament Mode 🏆'
: 'Mark as Tournament 🏆',
style: TextStyle(
color: isTournament
? _bulletColor
: Colors.white70,
fontWeight: FontWeight.w500,
fontSize: 16,
),
),
),
),
// 📁 Last Used Folder Button (highlighted subtly)
SizedBox(
width: double.infinity,
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.white.withOpacity(0.2), width: 1),
borderRadius: BorderRadius.circular(12),
),
const SizedBox(height: 16),
],
// Create folder button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: isNewFolderEnabled
? () => Navigator.pop(
context,
jsonEncode({
'folder': _controller.text.trim(),
'isTournament': isTournament,
}),
)
: null,
onPressed: () {
Navigator.pop(
context,
jsonEncode({
'folder': lastFolder,
'isTournament': lastIsTournament,
}),
);
},
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 14),
backgroundColor:
isNewFolderEnabled ? _bulletColor : Colors.grey[700],
backgroundColor: _bulletColor.withOpacity(0.9),
foregroundColor: Colors.white,
disabledBackgroundColor: Colors.grey[700],
elevation: 0,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: const Text("Create New Folder"),
child: Column(
children: [
Text(
'${lastIsTournament ? "🏆" : "🎯"} Save to "$lastFolder"',
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
const Text(
'Last used folder',
style: TextStyle(fontSize: 12, color: Colors.white70),
),
],
),
),
),
],
);
},
),
],
],
),
),
);
@@ -725,7 +690,6 @@ void _moveBullet(Offset offsetDelta) {
);
}
Map<String, dynamic>? _getRingCenterSize(List<DetectionResult> results) {
try {
final ring = results.firstWhere((r) => r.classId == 2);