style: refactored searching files into Simfile class

This commit is contained in:
Orangerot 2025-01-07 06:38:34 +01:00
parent 685323adea
commit 9c229b94d0
4 changed files with 36 additions and 37 deletions

View file

@ -50,8 +50,8 @@ class GameOverStats extends StatelessWidget {
]), ]),
TextButton( TextButton(
onPressed: () { onPressed: () {
Route route = MaterialPageRoute( Route route =
builder: (context) => Level(stepmaniaFolderPath: simfile.directoryPath)); MaterialPageRoute(builder: (context) => Level(simfile));
Navigator.pushReplacement(context, route); Navigator.pushReplacement(context, route);
}, },
child: Text('Retry')) child: Text('Retry'))

View file

@ -9,8 +9,8 @@ import 'package:sense_the_rhythm/game_over_stats.dart';
import 'package:sense_the_rhythm/simfile.dart'; import 'package:sense_the_rhythm/simfile.dart';
class Level extends StatefulWidget { class Level extends StatefulWidget {
const Level({super.key, required this.stepmaniaFolderPath}); const Level(this.simfile, {super.key});
final String stepmaniaFolderPath; final Simfile simfile;
@override @override
State<Level> createState() => _LevelState(); State<Level> createState() => _LevelState();
@ -25,7 +25,6 @@ class InputDirection {
class _LevelState extends State<Level> { class _LevelState extends State<Level> {
final player = AudioPlayer(); final player = AudioPlayer();
Simfile? simfile;
bool _isPlaying = true; bool _isPlaying = true;
Duration? _duration; Duration? _duration;
Duration? _position; Duration? _position;
@ -77,7 +76,7 @@ class _LevelState extends State<Level> {
player.onPlayerComplete.listen((void _) { player.onPlayerComplete.listen((void _) {
Route route = MaterialPageRoute( Route route = MaterialPageRoute(
builder: (context) => GameOverStats( builder: (context) => GameOverStats(
simfile: simfile!, simfile: widget.simfile,
notes: notes, notes: notes,
)); ));
Navigator.pushReplacement(context, route); Navigator.pushReplacement(context, route);
@ -131,10 +130,7 @@ class _LevelState extends State<Level> {
} }
}); });
simfile = Simfile(widget.stepmaniaFolderPath); widget.simfile.chartSimplest!.beats.forEach((time, noteData) {
simfile!.load();
simfile!.chartSimplest!.beats.forEach((time, noteData) {
int arrowIndex = noteData.indexOf('1'); int arrowIndex = noteData.indexOf('1');
if (arrowIndex < 0 || arrowIndex > 3) { if (arrowIndex < 0 || arrowIndex > 3) {
return; return;
@ -142,7 +138,7 @@ class _LevelState extends State<Level> {
notes.add(Note(time: time, direction: ArrowDirection.values[arrowIndex])); notes.add(Note(time: time, direction: ArrowDirection.values[arrowIndex]));
}); });
player.play(DeviceFileSource(simfile!.audioPath!)); player.play(DeviceFileSource(widget.simfile.audioPath!));
} }
@override @override
@ -192,7 +188,7 @@ class _LevelState extends State<Level> {
} }
}, },
), ),
title: Text(widget.stepmaniaFolderPath.split('/').last), title: Text(widget.simfile.tags['TITLE']!),
actions: [ actions: [
IconButton( IconButton(
icon: Icon(Icons.close), icon: Icon(Icons.close),

View file

@ -5,6 +5,7 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:sense_the_rhythm/esense_connect_dialog.dart'; import 'package:sense_the_rhythm/esense_connect_dialog.dart';
import 'package:sense_the_rhythm/simfile.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'level.dart'; import 'level.dart';
@ -18,7 +19,7 @@ class LevelSelection extends StatefulWidget {
class _LevelSelectionState extends State<LevelSelection> { class _LevelSelectionState extends State<LevelSelection> {
String? stepmaniaCoursesPath; String? stepmaniaCoursesPath;
List<FileSystemEntity> stepmaniaCoursesFolders = []; List<Simfile> stepmaniaCoursesFolders = [];
String eSenseDeviceName = ''; String eSenseDeviceName = '';
ESenseManager? eSenseManager; ESenseManager? eSenseManager;
@ -109,8 +110,9 @@ class _LevelSelectionState extends State<LevelSelection> {
prefs.getString('stepmania_courses'); prefs.getString('stepmania_courses');
if (stepmaniaCoursesPathSetting == null) return; if (stepmaniaCoursesPathSetting == null) return;
List<FileSystemEntity> stepmaniaCoursesFoldersFuture = await listFilesAndFolders(stepmaniaCoursesPathSetting); List<Simfile> stepmaniaCoursesFoldersFuture =
await listFilesAndFolders(stepmaniaCoursesPathSetting);
setState(() { setState(() {
stepmaniaCoursesPath = stepmaniaCoursesPathSetting; stepmaniaCoursesPath = stepmaniaCoursesPathSetting;
stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture; stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture;
@ -129,15 +131,18 @@ class _LevelSelectionState extends State<LevelSelection> {
} }
} }
Future<List<FileSystemEntity>> listFilesAndFolders( Future<List<Simfile>> listFilesAndFolders(String directoryPath) async {
String directoryPath) async {
final directory = Directory(directoryPath); final directory = Directory(directoryPath);
try { try {
// List all files and folders in the directory // List all files and folders in the directory
return directory return directory
.listSync() .listSync()
.where((entity) => FileSystemEntity.isDirectorySync(entity.path)) .where((entity) => FileSystemEntity.isDirectorySync(entity.path))
.toList(); .map((entity) {
Simfile simfile = Simfile(entity.path);
simfile.load();
return simfile;
}).toList();
} catch (e) { } catch (e) {
print("Error reading directory: $e"); print("Error reading directory: $e");
return []; return [];
@ -155,10 +160,10 @@ class _LevelSelectionState extends State<LevelSelection> {
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return ESenseConnectDialog( return ESenseConnectDialog(
deviceStatus: _deviceStatus, deviceStatus: _deviceStatus,
connect: (String name) { connect: (String name) {
_connectToESense(name); _connectToESense(name);
}); });
}, },
), ),
icon: const Icon(Icons.bluetooth)) icon: const Icon(Icons.bluetooth))
@ -176,26 +181,17 @@ class _LevelSelectionState extends State<LevelSelection> {
separatorBuilder: (BuildContext context, int index) => separatorBuilder: (BuildContext context, int index) =>
const Divider(), const Divider(),
itemBuilder: (context, index) { itemBuilder: (context, index) {
String thumbnailPath = Directory(
stepmaniaCoursesFolders[index].path)
.listSync()
.firstWhere(
(file) => file.path.toLowerCase().endsWith('banner.png'),
orElse: () => File(''))
.path;
return ListTile( return ListTile(
leading: Image.file(File(thumbnailPath)), leading: Image.file(
File(stepmaniaCoursesFolders[index].bannerPath!)),
trailing: Icon(Icons.play_arrow), trailing: Icon(Icons.play_arrow),
title: title: Text(stepmaniaCoursesFolders[index].tags["TITLE"]!),
Text(stepmaniaCoursesFolders[index].path.split('/').last),
subtitle: Text('3:45'), subtitle: Text('3:45'),
onTap: () => Navigator.push( onTap: () => Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (BuildContext context) => Level( builder: (BuildContext context) =>
stepmaniaFolderPath: Level(stepmaniaCoursesFolders[index]))),
stepmaniaCoursesFolders[index].path,
))),
); );
}, },
); );

View file

@ -124,7 +124,8 @@ class Simfile {
simfilePath = Directory(directoryPath) simfilePath = Directory(directoryPath)
.listSync() .listSync()
.firstWhere((entity) => entity.path.endsWith('.sm'), .firstWhere((entity) => entity.path.endsWith('.sm'),
orElse: () => File('')).path; orElse: () => File(''))
.path;
audioPath = Directory(directoryPath) audioPath = Directory(directoryPath)
.listSync() .listSync()
@ -132,6 +133,12 @@ class Simfile {
orElse: () => File('')) orElse: () => File(''))
.path; .path;
bannerPath = Directory(directoryPath)
.listSync()
.firstWhere((file) => file.path.toLowerCase().endsWith('banner.png'),
orElse: () => File(''))
.path;
lines = File(simfilePath!).readAsStringSync(); lines = File(simfilePath!).readAsStringSync();
RegExp commentsRegExp = RegExp(r'//.*$'); RegExp commentsRegExp = RegExp(r'//.*$');