Move reusable code from mobile to common

This will be useful for a future desktop application.
This commit is contained in:
Elias Projahn 2020-05-04 21:49:44 +02:00
parent 6e1255f26e
commit 711b19c998
40 changed files with 813 additions and 581 deletions

View file

@ -1,102 +1,30 @@
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:rxdart/rxdart.dart';
import 'package:musicus_common/musicus_common.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// Settings concerning the Musicus server to connect to.
///
/// We don't support setting a scheme here, because there may be password being
/// submitted in the future, so we default to HTTPS.
class ServerSettings {
static const defaultHost = 'musicus.johrpan.de';
static const defaultPort = 1833;
static const defaultBasePath = '/api';
class SettingsStorage extends MusicusSettingsStorage {
SharedPreferences _pref;
/// Host to connect to, e.g. 'musicus.johrpan.de';
final String host;
/// Port to connect to.
final int port;
/// Path to the API.
///
/// This should be null, if the API is at the root of the host.
final String basePath;
ServerSettings({
@required this.host,
@required this.port,
@required this.basePath,
});
}
/// Manager for all settings that are persisted.
class Settings {
static const defaultHost = 'musicus.johrpan.de';
static const defaultPort = 443;
static const defaultBasePath = '/api';
static const _platform = MethodChannel('de.johrpan.musicus/platform');
/// The tree storage access framework tree URI of the music library.
final musicLibraryUri = BehaviorSubject<String>();
/// Musicus server to connect to.
final server = BehaviorSubject<ServerSettings>();
SharedPreferences _shPref;
/// Initialize the settings.
Future<void> load() async {
_shPref = await SharedPreferences.getInstance();
final uri = _shPref.getString('musicLibraryUri');
if (uri != null) {
musicLibraryUri.add(uri);
}
final host = _shPref.getString('serverHost') ?? defaultHost;
final port = _shPref.getInt('serverPort') ?? defaultPort;
final basePath = _shPref.getString('serverBasePath') ?? defaultBasePath;
server.add(ServerSettings(
host: host,
port: port,
basePath: basePath,
));
_pref = await SharedPreferences.getInstance();
}
/// Open the system picker to select a new music library URI.
Future<void> chooseMusicLibraryUri() async {
final uri = await _platform.invokeMethod<String>('openTree');
if (uri != null) {
musicLibraryUri.add(uri);
await _shPref.setString('musicLibraryUri', uri);
}
@override
Future<int> getInt(String key) {
return Future.value(_pref.getInt(key));
}
/// Change the Musicus server settings.
Future<void> setServerSettings(ServerSettings settings) async {
await _shPref.setString('serverHost', settings.host);
await _shPref.setInt('serverPort', settings.port);
await _shPref.setString('serverBasePath', settings.basePath);
server.add(settings);
@override
Future<String> getString(String key) {
return Future.value(_pref.getString(key));
}
/// Reset the server settings to their defaults.
Future<void> resetServerSettings() async {
await setServerSettings(ServerSettings(
host: defaultHost,
port: defaultPort,
basePath: defaultBasePath,
));
@override
Future<void> setInt(String key, int value) async {
await _pref.setInt(key, value);
}
/// Tidy up.
void dispose() {
musicLibraryUri.close();
server.close();
@override
Future<void> setString(String key, String value) async {
await _pref.setString(key, value);
}
}