summaryrefslogtreecommitdiff
path: root/lib/level_selection.dart
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2024-12-23 02:08:45 +0100
committerOrangerot <purple@orangerot.dev>2024-12-23 02:08:45 +0100
commitd397b62b5b6f6db45a2cc0ae6ca4e03ff04c54b2 (patch)
tree4874d7b28bdc051a83af24cfa4a9ed947b147213 /lib/level_selection.dart
parent9aaf00e6275616f4ff3086b0fb4ae98c2036c4fb (diff)
feat: import song directory and display them with banner
Diffstat (limited to 'lib/level_selection.dart')
-rw-r--r--lib/level_selection.dart121
1 files changed, 95 insertions, 26 deletions
diff --git a/lib/level_selection.dart b/lib/level_selection.dart
index a5a9c32..cc79911 100644
--- a/lib/level_selection.dart
+++ b/lib/level_selection.dart
@@ -1,40 +1,109 @@
+import 'dart:io';
+
+import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
+import 'package:shared_preferences/shared_preferences.dart';
import 'level.dart';
-class LevelSelection extends StatelessWidget {
- final List<String> _musicList = [
- "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
- "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3",
- "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3",
- ];
+class LevelSelection extends StatefulWidget {
+ const LevelSelection({super.key});
+
+ @override
+ State<LevelSelection> createState() => _LevelSelectionState();
+}
+
+class _LevelSelectionState extends State<LevelSelection> {
+ String? stepmaniaCoursesPath;
+ List<FileSystemEntity> stepmaniaCoursesFolders = [];
+
+ @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;
+ setState(() {
+ stepmaniaCoursesPath = stepmaniaCoursesPathSetting;
+ });
+ setState(() async {
+ stepmaniaCoursesFolders =
+ await listFilesAndFolders(stepmaniaCoursesPathSetting);
+ });
+ }
+
+ Future<void> selectFolder() async {
+ String? selectedFolder = await FilePicker.platform.getDirectoryPath();
- final List<String> _coverArtList = [
- "https://cdn.pixabay.com/photo/2020/05/19/13/48/cartoon-5190942_960_720.jpg",
- "https://cdn.pixabay.com/photo/2020/05/19/13/35/cartoon-5190860_960_720.jpg",
- "https://cdn.pixabay.com/photo/2020/10/23/17/52/fox-5679446_960_720.png",
- ];
+ if (selectedFolder != null) {
+ // Save the selected folder path
+ SharedPreferences prefs = await SharedPreferences.getInstance();
+ await prefs.setString('stepmania_courses', selectedFolder);
+
+ loadFolderPath();
+ }
+ }
+
+ Future<List<FileSystemEntity>> listFilesAndFolders(
+ String directoryPath) async {
+ final directory = Directory(directoryPath);
+ try {
+ // List all files and folders in the directory
+ return directory
+ .listSync()
+ .where((entity) => FileSystemEntity.isDirectorySync(entity.path))
+ .toList();
+ } catch (e) {
+ print("Error reading directory: $e");
+ return [];
+ }
+ }
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Sense the Rhythm')),
- body: ListView.separated(
- itemCount: _musicList.length,
- separatorBuilder: (BuildContext context, int index) => const Divider(),
- itemBuilder: (context, index) {
- return ListTile(
- leading: Image.network(_coverArtList[index]),
- trailing: Icon(Icons.play_arrow),
- title: Text('Track ${index + 1}'),
- subtitle: Text('3:45'),
- onTap: () => Navigator.push(context,
- MaterialPageRoute(builder: (BuildContext context) => Level())),
+ 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) {
+ 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)),
+ trailing: Icon(Icons.play_arrow),
+ title:
+ Text(stepmaniaCoursesFolders[index].path.split('/').last),
+ subtitle: Text('3:45'),
+ onTap: () => Navigator.push(
+ context,
+ MaterialPageRoute(
+ builder: (BuildContext context) => Level())),
+ );
+ },
);
- },
- ),
- floatingActionButton:
- FloatingActionButton(onPressed: () => {}, child: Icon(Icons.add)),
+ }
+ }),
+ floatingActionButton: FloatingActionButton(
+ onPressed: () => {selectFolder()}, child: Icon(Icons.add)),
);
}
}