2024-12-23 02:08:45 +01:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
2024-12-20 22:46:19 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2025-01-08 12:13:05 +01:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2025-01-07 03:35:33 +01:00
|
|
|
import 'package:sense_the_rhythm/esense_connect_dialog.dart';
|
2025-01-08 06:19:58 +01:00
|
|
|
import 'package:sense_the_rhythm/esense_input.dart';
|
2025-01-07 06:38:34 +01:00
|
|
|
import 'package:sense_the_rhythm/simfile.dart';
|
2024-12-23 02:08:45 +01:00
|
|
|
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';
|
|
|
|
|
2024-12-23 02:08:45 +01:00
|
|
|
class LevelSelection extends StatefulWidget {
|
|
|
|
const LevelSelection({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<LevelSelection> createState() => _LevelSelectionState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LevelSelectionState extends State<LevelSelection> {
|
|
|
|
String? stepmaniaCoursesPath;
|
2025-01-07 06:38:34 +01:00
|
|
|
List<Simfile> stepmaniaCoursesFolders = [];
|
2024-12-23 02:08:45 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
loadFolderPath();
|
|
|
|
}
|
|
|
|
|
2025-01-06 19:21:36 +01:00
|
|
|
|
2024-12-23 02:08:45 +01:00
|
|
|
Future<void> loadFolderPath() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
final String? stepmaniaCoursesPathSetting =
|
|
|
|
prefs.getString('stepmania_courses');
|
|
|
|
|
|
|
|
if (stepmaniaCoursesPathSetting == null) return;
|
2025-01-07 06:38:34 +01:00
|
|
|
List<Simfile> stepmaniaCoursesFoldersFuture =
|
|
|
|
await listFilesAndFolders(stepmaniaCoursesPathSetting);
|
|
|
|
|
2024-12-23 02:08:45 +01:00
|
|
|
setState(() {
|
|
|
|
stepmaniaCoursesPath = stepmaniaCoursesPathSetting;
|
2025-01-07 06:05:28 +01:00
|
|
|
stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture;
|
2024-12-23 02:08:45 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> selectFolder() async {
|
2025-01-08 12:13:05 +01:00
|
|
|
await Permission.manageExternalStorage.request();
|
2024-12-23 02:08:45 +01:00
|
|
|
String? selectedFolder = await FilePicker.platform.getDirectoryPath();
|
2024-12-20 22:46:19 +01:00
|
|
|
|
2024-12-23 02:08:45 +01:00
|
|
|
if (selectedFolder != null) {
|
|
|
|
// Save the selected folder path
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.setString('stepmania_courses', selectedFolder);
|
|
|
|
|
|
|
|
loadFolderPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-07 06:38:34 +01:00
|
|
|
Future<List<Simfile>> listFilesAndFolders(String directoryPath) async {
|
2024-12-23 02:08:45 +01:00
|
|
|
final directory = Directory(directoryPath);
|
|
|
|
try {
|
|
|
|
// List all files and folders in the directory
|
2025-01-08 03:14:51 +01:00
|
|
|
List<Simfile> simfiles = directory
|
2024-12-23 02:08:45 +01:00
|
|
|
.listSync()
|
|
|
|
.where((entity) => FileSystemEntity.isDirectorySync(entity.path))
|
2025-01-07 06:38:34 +01:00
|
|
|
.map((entity) {
|
|
|
|
Simfile simfile = Simfile(entity.path);
|
|
|
|
simfile.load();
|
|
|
|
return simfile;
|
|
|
|
}).toList();
|
2025-01-08 03:14:51 +01:00
|
|
|
simfiles.sort((a,b) => a.tags['TITLE']!.compareTo(b.tags['TITLE']!));
|
|
|
|
|
|
|
|
return simfiles;
|
2024-12-23 02:08:45 +01:00
|
|
|
} catch (e) {
|
|
|
|
print("Error reading directory: $e");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2024-12-20 22:46:19 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2025-01-06 19:21:36 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Sense the Rhythm'),
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
2025-01-07 03:35:33 +01:00
|
|
|
onPressed: () => showDialog(
|
2025-01-06 19:21:36 +01:00
|
|
|
context: context,
|
|
|
|
builder: (BuildContext context) {
|
2025-01-07 03:35:33 +01:00
|
|
|
return ESenseConnectDialog(
|
2025-01-08 06:19:58 +01:00
|
|
|
deviceStatus: ESenseInput.instance.deviceStatus,
|
2025-01-07 06:38:34 +01:00
|
|
|
connect: (String name) {
|
2025-01-08 06:19:58 +01:00
|
|
|
ESenseInput.instance.connectToESense(name);
|
2025-01-07 06:38:34 +01:00
|
|
|
});
|
2025-01-06 19:21:36 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
icon: const Icon(Icons.bluetooth))
|
|
|
|
],
|
|
|
|
),
|
2024-12-23 02:08:45 +01:00
|
|
|
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 {
|
|
|
|
return ListView.separated(
|
|
|
|
itemCount: stepmaniaCoursesFolders.length,
|
|
|
|
separatorBuilder: (BuildContext context, int index) =>
|
|
|
|
const Divider(),
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return ListTile(
|
2025-01-07 06:38:34 +01:00
|
|
|
leading: Image.file(
|
|
|
|
File(stepmaniaCoursesFolders[index].bannerPath!)),
|
2024-12-23 02:08:45 +01:00
|
|
|
trailing: Icon(Icons.play_arrow),
|
2025-01-07 06:38:34 +01:00
|
|
|
title: Text(stepmaniaCoursesFolders[index].tags["TITLE"]!),
|
2024-12-23 02:08:45 +01:00
|
|
|
subtitle: Text('3:45'),
|
|
|
|
onTap: () => Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
2025-01-07 06:38:34 +01:00
|
|
|
builder: (BuildContext context) =>
|
|
|
|
Level(stepmaniaCoursesFolders[index]))),
|
2024-12-23 02:08:45 +01:00
|
|
|
);
|
|
|
|
},
|
2024-12-20 22:46:19 +01:00
|
|
|
);
|
2024-12-23 02:08:45 +01:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: () => {selectFolder()}, child: Icon(Icons.add)),
|
2024-12-20 22:46:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|