summaryrefslogtreecommitdiff
path: root/lib/level_selection.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/level_selection.dart')
-rw-r--r--lib/level_selection.dart67
1 files changed, 43 insertions, 24 deletions
diff --git a/lib/level_selection.dart b/lib/level_selection.dart
index e2cdcbe..33d8c8b 100644
--- a/lib/level_selection.dart
+++ b/lib/level_selection.dart
@@ -2,6 +2,10 @@ import 'dart:io';
import 'package:file_picker/file_picker.dart';
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';
import 'level.dart';
@@ -15,7 +19,7 @@ class LevelSelection extends StatefulWidget {
class _LevelSelectionState extends State<LevelSelection> {
String? stepmaniaCoursesPath;
- List<FileSystemEntity> stepmaniaCoursesFolders = [];
+ List<Simfile> stepmaniaCoursesFolders = [];
@override
void initState() {
@@ -23,22 +27,24 @@ class _LevelSelectionState extends State<LevelSelection> {
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;
- });
- setState(() async {
- stepmaniaCoursesFolders =
- await listFilesAndFolders(stepmaniaCoursesPathSetting);
+ stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture;
});
}
Future<void> selectFolder() async {
+ await Permission.manageExternalStorage.request();
String? selectedFolder = await FilePicker.platform.getDirectoryPath();
if (selectedFolder != null) {
@@ -50,15 +56,21 @@ class _LevelSelectionState extends State<LevelSelection> {
}
}
- Future<List<FileSystemEntity>> listFilesAndFolders(
- String directoryPath) async {
+ Future<List<Simfile>> listFilesAndFolders(String directoryPath) async {
final directory = Directory(directoryPath);
try {
// List all files and folders in the directory
- return directory
+ List<Simfile> simfiles = directory
.listSync()
.where((entity) => FileSystemEntity.isDirectorySync(entity.path))
- .toList();
+ .map((entity) {
+ Simfile simfile = Simfile(entity.path);
+ simfile.load();
+ return simfile;
+ }).toList();
+ simfiles.sort((a,b) => a.tags['TITLE']!.compareTo(b.tags['TITLE']!));
+
+ return simfiles;
} catch (e) {
print("Error reading directory: $e");
return [];
@@ -68,7 +80,23 @@ class _LevelSelectionState extends State<LevelSelection> {
@override
Widget build(BuildContext context) {
return Scaffold(
- appBar: AppBar(title: const Text('Sense the Rhythm')),
+ 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 \'+\'');
@@ -81,26 +109,17 @@ class _LevelSelectionState extends State<LevelSelection> {
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
itemBuilder: (context, index) {
- String thumbnailPath = Directory(
- stepmaniaCoursesFolders[index].path)
- .listSync()
- .firstWhere(
- (file) => file.path.toLowerCase().endsWith('banner.png'),
- orElse: () => File(''))
- .path;
return ListTile(
- leading: Image.file(File(thumbnailPath)),
+ leading: Image.file(
+ File(stepmaniaCoursesFolders[index].bannerPath!)),
trailing: Icon(Icons.play_arrow),
- title:
- Text(stepmaniaCoursesFolders[index].path.split('/').last),
+ title: Text(stepmaniaCoursesFolders[index].tags["TITLE"]!),
subtitle: Text('3:45'),
onTap: () => Navigator.push(
context,
MaterialPageRoute(
- builder: (BuildContext context) => Level(
- stepmaniaFolderPath:
- stepmaniaCoursesFolders[index].path,
- ))),
+ builder: (BuildContext context) =>
+ Level(stepmaniaCoursesFolders[index]))),
);
},
);