From f0644e40585864a67c6f5c9a63ff79af9cf02b7c Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sat, 28 Mar 2020 10:18:52 +0100 Subject: [PATCH] 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. --- lib/app.dart | 37 +++++++++++++++++++++++++++++++++++++ lib/backend.dart | 29 ++++++++++++++++++++++++++++- pubspec.yaml | 1 + 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/lib/app.dart b/lib/app.dart index d6e31fb..c021ce2 100644 --- a/lib/app.dart +++ b/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: [ + 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( + context, + MaterialPageRoute( + builder: (context) => FilesSelector( + mode: FilesSelectorMode.directory, + ), + fullscreenDialog: true, + ), + ); + + if (path != null) { + backend.setMusicLibraryPath(path); + } + }, + ), + ], + ), + ); } else { return Content(); } diff --git a/lib/backend.dart b/lib/backend.dart index 47dcdfb..ff99cdc 100644 --- a/lib/backend.dart +++ b/lib/backend.dart @@ -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 { Database db; BackendStatus status = BackendStatus.loading; + String musicLibraryPath; + + SharedPreferences _shPref; @override void initState() { @@ -49,6 +54,9 @@ class BackendState extends State { } Future _load() async { + _shPref = await SharedPreferences.getInstance(); + musicLibraryPath = _shPref.getString('musicLibraryPath'); + db = Database('musicus.sqlite'); final permissionStatus = @@ -58,6 +66,16 @@ class BackendState extends State { setState(() { status = BackendStatus.needsPermissions; }); + } else { + await _loadMusicLibrary(); + } + } + + Future _loadMusicLibrary() async { + if (musicLibraryPath == null) { + setState(() { + status = BackendStatus.setup; + }); } else { setState(() { status = BackendStatus.ready; @@ -68,7 +86,7 @@ class BackendState extends State { Future requestPermissions() async { final result = await _permissionHandler.requestPermissions([PermissionGroup.storage]); - + if (result[PermissionGroup.storage] == PermissionStatus.granted) { setState(() { status = BackendStatus.ready; @@ -78,6 +96,15 @@ class BackendState extends State { Future openAppSettings() => _permissionHandler.openAppSettings(); + Future setMusicLibraryPath(String path) async { + musicLibraryPath = path; + await _shPref.setString('musicLibraryPath', path); + setState(() { + status = BackendStatus.loading; + }); + await _loadMusicLibrary(); + } + void startPlayer() { playerActive.add(true); } diff --git a/pubspec.yaml b/pubspec.yaml index 28e8da9..37835fa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ dependencies: path: permission_handler: rxdart: + shared_preferences: dev_dependencies: build_runner: