summaryrefslogtreecommitdiff
path: root/lib/level_selection.dart
blob: e2cdcbe20a36f2a5bc57338996eaa915e37afe91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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 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();

    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: 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(
                              stepmaniaFolderPath:
                                  stepmaniaCoursesFolders[index].path,
                            ))),
              );
            },
          );
        }
      }),
      floatingActionButton: FloatingActionButton(
          onPressed: () => {selectFolder()}, child: Icon(Icons.add)),
    );
  }
}