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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:file_picker/file_picker.dart';
import 'package:sense_the_rhythm/utils/esense_input.dart';
import 'package:sense_the_rhythm/utils/simfile.dart';
import 'package:sense_the_rhythm/widgets/connection_status_button.dart';
import 'package:sense_the_rhythm/widgets/level_list_entry.dart';
class LevelSelection extends StatefulWidget {
const LevelSelection({super.key});
@override
State<LevelSelection> createState() => _LevelSelectionState();
}
class _LevelSelectionState extends State<LevelSelection> {
String? _stepmaniaCoursesPath;
List<Simfile> _stepmaniaCoursesFolders = [];
List<Simfile> _stepmaniaCoursesFoldersFiltered = [];
@override
void initState() {
super.initState();
_loadFolderPath();
}
/// gets folder path from persistent storage and updates state with loaded simfiles
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;
_stepmaniaCoursesFolders = stepmaniaCoursesFoldersFuture;
_stepmaniaCoursesFoldersFiltered = stepmaniaCoursesFoldersFuture;
});
}
/// open folder selection dialog and save selected folder in persistent storage
Future<void> _selectFolder() async {
await Permission.manageExternalStorage.request();
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();
}
}
/// load all simfiles from a [directoryPath]
Future<List<Simfile>> _listFilesAndFolders(String directoryPath) async {
final directory = Directory(directoryPath);
try {
// List all files and folders in the directory
List<Simfile> simfiles = directory
.listSync(recursive: true)
.where((entity) => entity.path.endsWith('.sm'))
.map((entity) => Simfile(entity.path))
.toList();
List<bool> successfullLoads =
await Future.wait(simfiles.map((simfile) => simfile.load()));
List<Simfile> simfilesFiltered = [];
for (int i = 0; i < simfiles.length; i++) {
if (successfullLoads[i]) {
simfilesFiltered.add(simfiles[i]);
}
}
simfilesFiltered
.sort((a, b) => a.tags['TITLE']!.compareTo(b.tags['TITLE']!));
return simfilesFiltered;
} catch (e) {
print("Error reading directory: $e");
return [];
}
}
/// filter stepmaniaCoursesFolders based on [input]
void _filterLevels(String input) {
setState(() {
_stepmaniaCoursesFoldersFiltered = _stepmaniaCoursesFolders
.where((simfile) => simfile.tags["TITLE"]!
.toLowerCase()
.contains(input.toLowerCase()))
.toList();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Sense the Rhythm'),
actions: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: ValueListenableBuilder(
valueListenable: ESenseInput.instance.deviceStatus,
builder:
(BuildContext context, String deviceStatus, Widget? child) {
return ConnectionStatusButton(deviceStatus);
},
),
)
],
),
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 Column(
children: [
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0),
child: TextField(
onChanged: _filterLevels,
decoration: InputDecoration(
// icon: Icon(Icons.search),
hintText: 'Search'),
),
),
Expanded(
child: ListView.separated(
itemCount: _stepmaniaCoursesFoldersFiltered.length,
separatorBuilder: (BuildContext context, int index) =>
const Divider(),
itemBuilder: (context, index) {
Simfile simfile = _stepmaniaCoursesFoldersFiltered[index];
return LevelListEntry(simfile: simfile);
},
),
),
],
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
_selectFolder();
},
child: Icon(Icons.add)),
);
}
}
|