Sense_the_Rhythm/lib/level_selection.dart

159 lines
5.4 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:file_picker/file_picker.dart';
2024-12-20 22:46:19 +01:00
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:sense_the_rhythm/esense_connect_dialog.dart';
import 'package:sense_the_rhythm/esense_input.dart';
import 'package:sense_the_rhythm/simfile.dart';
import 'package:shared_preferences/shared_preferences.dart';
2024-12-20 22:46:19 +01:00
2024-12-22 17:41:14 +01:00
import 'level.dart';
class LevelSelection extends StatefulWidget {
const LevelSelection({super.key});
@override
State<LevelSelection> createState() => _LevelSelectionState();
}
class _LevelSelectionState extends State<LevelSelection> {
String? stepmaniaCoursesPath;
List<Simfile> stepmaniaCoursesFolders = [];
2025-01-10 20:55:03 +01:00
List<Simfile> stepmaniaCoursesFoldersFiltered = [];
String searchString = '';
@override
void initState() {
super.initState();
loadFolderPath();
}
Future<void> loadFolderPath() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final String? stepmaniaCoursesPathSetting =
prefs.getString('stepmania_courses');
if (stepmaniaCoursesPathSetting == null) return;
List<Simfile> stepmaniaCoursesFoldersFuture =
await listFilesAndFolders(stepmaniaCoursesPathSetting);
setState(() {
stepmaniaCoursesPath = stepmaniaCoursesPathSetting;
stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture;
2025-01-10 20:55:03 +01:00
stepmaniaCoursesFoldersFiltered = stepmaniaCoursesFoldersFuture;
});
}
Future<void> selectFolder() async {
await Permission.manageExternalStorage.request();
String? selectedFolder = await FilePicker.platform.getDirectoryPath();
2024-12-20 22:46:19 +01:00
if (selectedFolder != null) {
// Save the selected folder path
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('stepmania_courses', selectedFolder);
loadFolderPath();
}
}
Future<List<Simfile>> listFilesAndFolders(String directoryPath) async {
final directory = Directory(directoryPath);
try {
// List all files and folders in the directory
List<Simfile> simfiles = directory
.listSync()
.where((entity) => FileSystemEntity.isDirectorySync(entity.path))
.map((entity) {
Simfile simfile = Simfile(entity.path);
simfile.load();
return simfile;
}).toList();
2025-01-10 20:55:03 +01:00
simfiles.sort((a, b) => a.tags['TITLE']!.compareTo(b.tags['TITLE']!));
return simfiles;
} catch (e) {
print("Error reading directory: $e");
return [];
}
}
2024-12-20 22:46:19 +01:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sense the Rhythm'),
actions: [
IconButton(
onPressed: () => showDialog(
context: context,
builder: (BuildContext context) {
return ESenseConnectDialog(
deviceStatus: ESenseInput.instance.deviceStatus,
connect: (String name) {
ESenseInput.instance.connectToESense(name);
});
},
),
icon: const Icon(Icons.bluetooth))
],
),
body: Builder(builder: (context) {
if (stepmaniaCoursesPath == null) {
return Text('Add a Directory with Stepmania Songs on \'+\'');
} else if (stepmaniaCoursesFolders.isEmpty) {
return Text(
'Folder empty. Add Stepmania Songs to Folder or select a different folder on \'+\'');
} else {
2025-01-10 20:55:03 +01:00
return Column(
children: [
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0),
child: TextField(
onChanged: (input) {
setState(() {
stepmaniaCoursesFoldersFiltered = stepmaniaCoursesFolders
.where((simfile) => simfile.tags["TITLE"]!
.toLowerCase()
.contains(input.toLowerCase()))
.toList();
});
},
decoration: InputDecoration(
// icon: Icon(Icons.search),
hintText: 'Search'),
),
),
Expanded(
child: ListView.separated(
itemCount: stepmaniaCoursesFoldersFiltered.length,
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
itemBuilder: (context, index) {
Simfile simfile = stepmaniaCoursesFoldersFiltered[index];
return ListTile(
leading: Image.file(File(simfile.bannerPath!)),
trailing: Icon(Icons.play_arrow),
title: Text(simfile.tags["TITLE"]!),
subtitle: Text('3:45'),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
Level(simfile))),
);
},
),
),
],
2024-12-20 22:46:19 +01:00
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () => {selectFolder()}, child: Icon(Icons.add)),
2024-12-20 22:46:19 +01:00
);
}
}