From d2a8363f6f3b20256c588189103017d2ee6f7678 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sat, 25 Apr 2020 15:02:11 +0200 Subject: [PATCH] mobile: Add Musicus client The settings screen now contains a tile to change the URL of the Musicus server. --- mobile/lib/backend.dart | 28 +++++++++++++++++++ mobile/lib/screens/settings.dart | 46 ++++++++++++++++++++++++++++++++ mobile/pubspec.yaml | 2 ++ 3 files changed, 76 insertions(+) diff --git a/mobile/lib/backend.dart b/mobile/lib/backend.dart index cbd5793..e51d5c2 100644 --- a/mobile/lib/backend.dart +++ b/mobile/lib/backend.dart @@ -6,9 +6,11 @@ import 'package:flutter/widgets.dart'; import 'package:moor/isolate.dart'; import 'package:moor/moor.dart'; import 'package:moor_ffi/moor_ffi.dart'; +import 'package:musicus_client/musicus_client.dart'; import 'package:musicus_database/musicus_database.dart'; import 'package:path/path.dart' as p; import 'package:path_provider/path_provider.dart' as pp; +import 'package:rxdart/rxdart.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'music_library.dart'; @@ -86,6 +88,10 @@ class BackendState extends State { BackendStatus status = BackendStatus.loading; Database db; + + final musicusServerUrl = BehaviorSubject(); + MusicusClient client; + String musicLibraryUri; MusicLibrary ml; @@ -113,6 +119,12 @@ class BackendState extends State { db = Database.connect(dbConnection); _shPref = await SharedPreferences.getInstance(); + final url = _shPref.getString('musicusServerUrl'); + musicusServerUrl.add(url); + if (url != null) { + client = MusicusClient(url); + } + musicLibraryUri = _shPref.getString('musicLibraryUri'); _loadMusicLibrary(); @@ -145,9 +157,25 @@ class BackendState extends State { } } + Future setMusicusServer(String serverUrl) async { + final url = serverUrl.isNotEmpty ? serverUrl : null; + await _shPref.setString('musicusServerUrl', url); + + if (client != null) { + client.dispose(); + } + + if (url != null) { + client = MusicusClient(url); + } + + musicusServerUrl.add(url); + } + @override void dispose() { super.dispose(); + client.dispose(); _moorIsolate.shutdownAll(); } } diff --git a/mobile/lib/screens/settings.dart b/mobile/lib/screens/settings.dart index 4afcb5d..956be15 100644 --- a/mobile/lib/screens/settings.dart +++ b/mobile/lib/screens/settings.dart @@ -21,6 +21,52 @@ class SettingsScreen extends StatelessWidget { backend.chooseMusicLibraryUri(); }, ), + StreamBuilder( + stream: backend.musicusServerUrl, + builder: (context, snapshot) { + return ListTile( + leading: Icon(Icons.router), + title: Text('Musicus server'), + subtitle: Text(snapshot.data ?? 'Set server URL'), + onTap: () { + showDialog( + context: context, + builder: (context) { + final controller = TextEditingController(); + + if (snapshot.data != null) { + controller.text = snapshot.data; + } + + return AlertDialog( + title: Text('Musicus server'), + content: TextField( + controller: controller, + decoration: InputDecoration( + labelText: 'Server URL', + ), + ), + actions: [ + FlatButton( + onPressed: () { + backend.setMusicusServer(controller.text); + Navigator.pop(context); + }, + child: Text('SET'), + ), + FlatButton( + onPressed: () { + Navigator.pop(context); + }, + child: Text('CANCEL'), + ), + ], + ); + }); + }, + ); + } + ), ], ), ); diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yaml index 4a67da8..6edaa7d 100644 --- a/mobile/pubspec.yaml +++ b/mobile/pubspec.yaml @@ -14,6 +14,8 @@ dependencies: sdk: flutter moor: moor_ffi: + musicus_client: + path: ../client musicus_database: path: ../database musicus_player: