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
+212 -126
View File
@@ -22,6 +22,7 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
Map<String, bool> _folderTypes = {}; // true = tournament, false = training
String _folderFilter = 'all';
bool isRenameEnabled = false;
String _sortMode = 'date'; // or 'score'
@override
void initState() {
@@ -172,8 +173,6 @@ Color _getColorFromName(String colorName) {
}
}
// Function to safely get the score from the JSON data
int _getScoreFromJson(Map<String, dynamic> data) {
if (data.containsKey('totalScore') && data['totalScore'] is int) {
@@ -267,17 +266,24 @@ Color _getColorFromName(String colorName) {
}
// 🔽 Sort using `createdAt` from _folderCreationDates (which is now based on metadata.json)
folders.sort((a, b) {
// 🔽 Apply sorting based on selected sort mode
folders.sort((a, b) {
if (_sortMode == 'score') {
final scoreA = _folderScores[a] ?? 0;
final scoreB = _folderScores[b] ?? 0;
return scoreB.compareTo(scoreA); // High score first
} else {
final dateA = _folderCreationDates[a];
final dateB = _folderCreationDates[b];
if (dateA == null || dateB == null) return 0;
return dateB.compareTo(dateA); // Newest first
});
}
});
return folders;
}
Future<void> _renameFolder(String oldFolderName) async {
final newNameController = TextEditingController();
bool isTournament = _folderTypes[oldFolderName] == true;
@@ -564,61 +570,74 @@ Future<void> _renameFolder(String oldFolderName) async {
}
// Build the folder row with folder name, trash, and rename options
// Build the folder row with folder name, trash, rename options, and view all targets icon
Widget _buildFolderRow(String folderName) {
final numberOfTargets = _groupedFiles[folderName]!.length;
final totalScore = _folderScores[folderName];
final folderDate = _folderCreationDates[folderName];
final formattedDate = folderDate != null
? DateFormat('yyyy-MM-dd').format(folderDate)
: 'Unknown Date';
final isExpanded = _expandedFolder == folderName;
return Container(
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 12),
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 16),
decoration: BoxDecoration(
color: Colors.grey[300], // Lighter background for folder
color: isExpanded ? _bulletColor.withOpacity(0.15) : Colors.grey[300],
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isExpanded ? _bulletColor : Colors.transparent,
width: 1.5,
),
boxShadow: isExpanded
? [
BoxShadow(
color: _bulletColor.withOpacity(0.2),
blurRadius: 6,
offset: const Offset(0, 2),
),
]
: [],
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
contentPadding: const EdgeInsets.all(0),
title: Row(
children: [
Text(folderName, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
if (_folderTypes[folderName] == true)
const Padding(
padding: EdgeInsets.only(left: 6),
child: Text('🏆', style: TextStyle(fontSize: 18)),
),
],
dense: true, // Compact layout
contentPadding: EdgeInsets.zero,
title: Text(
_folderTypes[folderName] == true
? '🏆 $folderName'
: folderName,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
overflow: TextOverflow.ellipsis,
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0), // Add padding between title and info
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Icon(Icons.insert_drive_file, color: Colors.black45),
const SizedBox(width: 4), // Small space between the icon and the text
Text(
"$numberOfTargets / 🎯 $totalScore",
style: const TextStyle(fontSize: 16, color: Colors.black),
),
],
padding: const EdgeInsets.only(top: 4.0),
child: Text(
"📝 $numberOfTargets / 🎯 $totalScore / 📅 $formattedDate",
style: const TextStyle(fontSize: 13, color: Colors.black87),
),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
iconSize: 22,
visualDensity: VisualDensity.compact,
icon: const Icon(Icons.my_location, color: Colors.grey),
onPressed: () => _openFolderTargetView(folderName),
),
IconButton(
iconSize: 22,
visualDensity: VisualDensity.compact,
icon: Icon(Icons.edit, color: _bulletColor),
onPressed: () => _renameFolder(folderName),
),
IconButton(
iconSize: 22,
visualDensity: VisualDensity.compact,
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () => _deleteFolder(folderName),
),
@@ -626,13 +645,11 @@ Widget _buildFolderRow(String folderName) {
),
onTap: () {
setState(() {
// If the folder is already expanded, collapse it, otherwise expand it
_expandedFolder = _expandedFolder == folderName ? null : folderName;
_expandedFolder = isExpanded ? null : folderName;
});
},
),
if (_expandedFolder == folderName)
// When expanded, display the file row inside the folder
if (isExpanded)
..._groupedFiles[folderName]!
.asMap()
.entries
@@ -689,51 +706,53 @@ Widget _buildFolderRow(String folderName) {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
// 🔽 Filter dropdown at the top
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
child: GestureDetector(
onTap: () => _showFolderFilterPopup(context),
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300], // Same as folder background
borderRadius: BorderRadius.circular(12),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
return Scaffold(
body: Column(
children: [
Text(
_folderFilter == 'all'
? 'Showing: All folders'
: _folderFilter == 'tournament'
? 'Showing: 🏆 Tournament folders'
: 'Showing: 🎯 Training folders',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
),
const Icon(Icons.filter_list),
],
// 🔽 Filter dropdown at the top
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
child: GestureDetector(
onTap: () => _showFolderFilterPopup(context),
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: Colors.grey[300], // Same as folder background
borderRadius: BorderRadius.circular(12),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
_folderFilter == 'all'
? 'Showing: 📂 All folders'
: _folderFilter == 'tournament'
? 'Showing: 🏆 Tournament folders'
: 'Showing: 🎯 Training folders',
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
),
const Icon(Icons.filter_list),
],
),
),
),
),
),
// 📁 Folder list or empty state
Expanded(
// 📁 Folder list or empty state
Expanded(
child: _groupedFiles.isEmpty
? const Center(child: Text("No saved target data found", style: TextStyle(fontSize: 18)))
: ListView.builder(
itemCount: _filteredFolderNames().length,
itemBuilder: (context, index) {
String folderName = _filteredFolderNames()[index];
return _buildFolderRow(folderName);
},
: Scrollbar(
thumbVisibility: true, // always show scrollbar
thickness: 6, // optional: adjust width
radius: const Radius.circular(8),
child: ListView.builder(
itemCount: _filteredFolderNames().length,
itemBuilder: (context, index) {
String folderName = _filteredFolderNames()[index];
return _buildFolderRow(folderName);
},
),
),
),
],
@@ -745,66 +764,134 @@ void _showFolderFilterPopup(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
'Filter Folders',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
return StatefulBuilder(
builder: (context, setState) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Filter Folders',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 24),
_buildFilterOption('all', '📂 All folders'),
const SizedBox(height: 12),
_buildFilterOption('tournament', '🏆 Tournament'),
const SizedBox(height: 12),
_buildFilterOption('training', '🎯 Training'),
],
),
),
// Folder type filters
_buildStyledFilterButton('all', '📂 All folders', context),
const SizedBox(height: 12),
_buildStyledFilterButton('tournament', '🏆 Tournament', context),
const SizedBox(height: 12),
_buildStyledFilterButton('training', '🎯 Training', context),
const SizedBox(height: 24),
const Text(
'Sort By',
style: TextStyle(color: Colors.white70, fontSize: 16, fontWeight: FontWeight.w500),
),
const SizedBox(height: 12),
Row(
children: [
Expanded(
child: GestureDetector(
onTap: () {
setState(() => _sortMode = 'date');
this.setState(() {}); // 👈 refresh parent widget
},
child: Container(
padding: const EdgeInsets.symmetric(vertical: 12),
margin: const EdgeInsets.only(right: 8),
decoration: BoxDecoration(
color: _sortMode == 'date' ? _bulletColor : Colors.grey[800],
borderRadius: BorderRadius.circular(12),
),
child: Center(
child: Text(
'📅 Sort by Date',
style: TextStyle(
color: _sortMode == 'date' ? Colors.white : Colors.white70,
fontWeight: FontWeight.w500,
fontSize: 15,
),
),
),
),
),
),
Expanded(
child: GestureDetector(
onTap: () {
setState(() => _sortMode = 'score');
this.setState(() {}); // 👈 refresh parent widget
},
child: Container(
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: _sortMode == 'score' ? _bulletColor : Colors.grey[800],
borderRadius: BorderRadius.circular(12),
),
child: Center(
child: Text(
'🏆 Sort by Score',
style: TextStyle(
color: _sortMode == 'score' ? Colors.white : Colors.white70,
fontWeight: FontWeight.w500,
fontSize: 15,
),
),
),
),
),
),
],
),
],
),
),
);
},
);
},
);
}
Widget _buildFilterOption(String value, String label) {
Widget _buildStyledFilterButton(String value, String label, BuildContext context) {
final isSelected = _folderFilter == value;
return GestureDetector(
onTap: () {
setState(() {
_folderFilter = value;
});
Navigator.of(context).pop();
},
child: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
decoration: BoxDecoration(
color: isSelected ? _bulletColor.withOpacity(0.3) : Colors.grey[800],
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: isSelected ? _bulletColor : Colors.transparent,
width: 1.5,
return SizedBox(
width: double.infinity,
child: GestureDetector(
onTap: () {
setState(() {
_folderFilter = value;
});
Navigator.of(context).pop();
},
child: Container(
padding: const EdgeInsets.symmetric(vertical: 14),
decoration: BoxDecoration(
color: isSelected ? _bulletColor : Colors.grey[800],
borderRadius: BorderRadius.circular(12),
),
),
child: Center(
child: Text(
label,
style: const TextStyle(color: Colors.white, fontSize: 16),
child: Center(
child: Text(
label,
style: TextStyle(
color: isSelected ? Colors.white : Colors.white70,
fontSize: 16,
fontWeight: FontWeight.w500,
),
),
),
),
),
@@ -812,5 +899,4 @@ Widget _buildFilterOption(String value, String label) {
}
}