feat: Refactor Wolf3dApp to manage audio shutdown on dispose and add audio shutdown test
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
library;
|
||||
|
||||
import 'dart:collection';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wolf_3d_flutter/wolf_3d_flutter.dart';
|
||||
|
||||
@@ -21,108 +20,38 @@ class Wolf3dApp extends StatefulWidget {
|
||||
State<Wolf3dApp> createState() => _Wolf3dAppState();
|
||||
}
|
||||
|
||||
class _Wolf3dAppState extends State<Wolf3dApp> {
|
||||
bool _isLoadingGameData = false;
|
||||
String? _pickerError;
|
||||
class _Wolf3dAppState extends State<Wolf3dApp> with WidgetsBindingObserver {
|
||||
Future<void>? _shutdownFuture;
|
||||
|
||||
Future<void> _pickGameDataDirectory() async {
|
||||
setState(() {
|
||||
_isLoadingGameData = true;
|
||||
_pickerError = null;
|
||||
});
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
try {
|
||||
final String? directoryPath = await getDirectoryPath(
|
||||
confirmButtonText: 'Use this folder',
|
||||
);
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
unawaited(_ensureAudioShutdown());
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
if (directoryPath == null || directoryPath.trim().isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
await widget.engine.init(directory: directoryPath.trim());
|
||||
} catch (error) {
|
||||
setState(() {
|
||||
_pickerError = 'Unable to load selected directory: $error';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoadingGameData = false;
|
||||
});
|
||||
}
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.detached) {
|
||||
unawaited(_ensureAudioShutdown());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pickGameDataFiles() async {
|
||||
setState(() {
|
||||
_isLoadingGameData = true;
|
||||
_pickerError = null;
|
||||
});
|
||||
|
||||
try {
|
||||
final List<XFile> selectedFiles = await openFiles();
|
||||
|
||||
if (selectedFiles.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final LinkedHashSet<String> directories = LinkedHashSet<String>();
|
||||
for (final XFile file in selectedFiles) {
|
||||
final String directory = _directoryFromFilePath(file.path);
|
||||
if (directory.isNotEmpty) {
|
||||
directories.add(directory);
|
||||
}
|
||||
}
|
||||
|
||||
if (directories.isEmpty) {
|
||||
setState(() {
|
||||
_pickerError = 'Selected files do not expose local filesystem paths.';
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
final List<String> orderedDirectories = directories.toList(
|
||||
growable: false,
|
||||
);
|
||||
await widget.engine.init(
|
||||
directory: orderedDirectories.first,
|
||||
additionalDirectories: orderedDirectories.skip(1),
|
||||
);
|
||||
} catch (error) {
|
||||
setState(() {
|
||||
_pickerError = 'Unable to load selected files: $error';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoadingGameData = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _directoryFromFilePath(String path) {
|
||||
final String trimmedPath = path.trim();
|
||||
if (trimmedPath.isEmpty) {
|
||||
return '';
|
||||
Future<void> _ensureAudioShutdown() {
|
||||
final Future<void>? existing = _shutdownFuture;
|
||||
if (existing != null) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
final int slashIndex = trimmedPath.lastIndexOf('/');
|
||||
final int backslashIndex = trimmedPath.lastIndexOf(r'\');
|
||||
final int separatorIndex = slashIndex > backslashIndex
|
||||
? slashIndex
|
||||
: backslashIndex;
|
||||
|
||||
if (separatorIndex < 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (separatorIndex == 0) {
|
||||
return trimmedPath[0];
|
||||
}
|
||||
|
||||
return trimmedPath.substring(0, separatorIndex);
|
||||
final Future<void> shutdown = widget.engine.shutdownAudio();
|
||||
_shutdownFuture = shutdown;
|
||||
return shutdown;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -130,10 +59,6 @@ class _Wolf3dAppState extends State<Wolf3dApp> {
|
||||
return widget.engine.availableGames.isEmpty
|
||||
? NoGameDataScreen(
|
||||
configuredDataDirectory: widget.engine.configuredDataDirectory,
|
||||
onPickGameDataDirectory: _pickGameDataDirectory,
|
||||
onPickGameDataFiles: _pickGameDataFiles,
|
||||
isLoadingGameData: _isLoadingGameData,
|
||||
pickerError: _pickerError,
|
||||
)
|
||||
: GameScreen(wolf3d: widget.engine);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user