mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
Add music library path setting
This introduces a new state of the backend called "setup". If the music library path is not set, the backend goes into that state and the app widget can show the UI to set it up. This also introduces a new dependency on shared_preferences.
This commit is contained in:
parent
723977daec
commit
f0644e4058
3 changed files with 66 additions and 1 deletions
37
lib/app.dart
37
lib/app.dart
|
|
@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
|
|||
|
||||
import 'backend.dart';
|
||||
import 'screens/home.dart';
|
||||
import 'selectors/files.dart';
|
||||
import 'widgets/player_bar.dart';
|
||||
|
||||
class App extends StatelessWidget {
|
||||
|
|
@ -64,6 +65,42 @@ class App extends StatelessWidget {
|
|||
],
|
||||
),
|
||||
);
|
||||
} else if (backend.status == BackendStatus.setup) {
|
||||
return Material(
|
||||
color: Theme.of(context).scaffoldBackgroundColor,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
'Choose the base path for\nyour music library.',
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.headline6,
|
||||
),
|
||||
SizedBox(
|
||||
height: 16.0,
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.folder_open),
|
||||
title: Text('Choose path'),
|
||||
onTap: () async {
|
||||
final path = await Navigator.push<String>(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => FilesSelector(
|
||||
mode: FilesSelectorMode.directory,
|
||||
),
|
||||
fullscreenDialog: true,
|
||||
),
|
||||
);
|
||||
|
||||
if (path != null) {
|
||||
backend.setMusicLibraryPath(path);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return Content();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'database.dart';
|
||||
|
||||
enum BackendStatus {
|
||||
loading,
|
||||
needsPermissions,
|
||||
setup,
|
||||
ready,
|
||||
}
|
||||
|
||||
|
|
@ -33,6 +35,9 @@ class BackendState extends State<Backend> {
|
|||
|
||||
Database db;
|
||||
BackendStatus status = BackendStatus.loading;
|
||||
String musicLibraryPath;
|
||||
|
||||
SharedPreferences _shPref;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
|
@ -49,6 +54,9 @@ class BackendState extends State<Backend> {
|
|||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
_shPref = await SharedPreferences.getInstance();
|
||||
musicLibraryPath = _shPref.getString('musicLibraryPath');
|
||||
|
||||
db = Database('musicus.sqlite');
|
||||
|
||||
final permissionStatus =
|
||||
|
|
@ -58,6 +66,16 @@ class BackendState extends State<Backend> {
|
|||
setState(() {
|
||||
status = BackendStatus.needsPermissions;
|
||||
});
|
||||
} else {
|
||||
await _loadMusicLibrary();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadMusicLibrary() async {
|
||||
if (musicLibraryPath == null) {
|
||||
setState(() {
|
||||
status = BackendStatus.setup;
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
status = BackendStatus.ready;
|
||||
|
|
@ -78,6 +96,15 @@ class BackendState extends State<Backend> {
|
|||
|
||||
Future<void> openAppSettings() => _permissionHandler.openAppSettings();
|
||||
|
||||
Future<void> setMusicLibraryPath(String path) async {
|
||||
musicLibraryPath = path;
|
||||
await _shPref.setString('musicLibraryPath', path);
|
||||
setState(() {
|
||||
status = BackendStatus.loading;
|
||||
});
|
||||
await _loadMusicLibrary();
|
||||
}
|
||||
|
||||
void startPlayer() {
|
||||
playerActive.add(true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ dependencies:
|
|||
path:
|
||||
permission_handler:
|
||||
rxdart:
|
||||
shared_preferences:
|
||||
|
||||
dev_dependencies:
|
||||
build_runner:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue