From 989e0e81b322a15df8581b1dc7181d5b6e3c4f88 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 3 Mar 2021 21:39:46 +1100 Subject: [PATCH] Add settings to control sounds --- lib/app_settings.dart | 38 +++++++++++++++++++++ lib/barcode.dart | 23 +++++++++---- lib/settings/app_settings.dart | 61 ++++++++++++++++++++++++---------- lib/widget/dialogs.dart | 9 +++-- 4 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 lib/app_settings.dart diff --git a/lib/app_settings.dart b/lib/app_settings.dart new file mode 100644 index 00000000..062c7a9e --- /dev/null +++ b/lib/app_settings.dart @@ -0,0 +1,38 @@ +/* + * Class for managing app-level configuration options + */ + +import 'package:sembast/sembast.dart'; +import 'package:InvenTree/preferences.dart'; + +class InvenTreeSettingsManager { + + final store = StoreRef("settings"); + + Future get _db async => await InvenTreePreferencesDB.instance.database; + + Future getValue(String key, dynamic backup) async { + + final value = await store.record(key).get(await _db); + + if (value == null) { + return backup; + } + + return value; + } + + Future setValue(String key, dynamic value) async { + + await store.record(key).put(await _db, value); + } + + // Ensure we only ever create a single instance of this class + static final InvenTreeSettingsManager _manager = new InvenTreeSettingsManager._internal(); + + factory InvenTreeSettingsManager() { + return _manager; + } + + InvenTreeSettingsManager._internal(); +} \ No newline at end of file diff --git a/lib/barcode.dart b/lib/barcode.dart index f97e2ebd..3997f088 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -1,3 +1,4 @@ +import 'package:InvenTree/app_settings.dart'; import 'package:InvenTree/widget/dialogs.dart'; import 'package:InvenTree/widget/snacks.dart'; import 'package:audioplayers/audio_cache.dart'; @@ -39,14 +40,24 @@ class BarcodeHandler { QRViewController _controller; BuildContext _context; - void successTone() { - AudioCache player = AudioCache(); - player.play("sounds/barcode_scan.mp3"); + void successTone() async { + + final bool en = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; + + if (en) { + AudioCache player = AudioCache(); + player.play("sounds/barcode_scan.mp3"); + } } - void failureTone() { - AudioCache player = AudioCache(); - player.play("sounds/barcode_error.mp3"); + void failureTone() async { + + final bool en = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; + + if (en) { + AudioCache player = AudioCache(); + player.play("sounds/barcode_error.mp3"); + } } Future onBarcodeMatched(Map data) { diff --git a/lib/settings/app_settings.dart b/lib/settings/app_settings.dart index 4fa72d9e..febd7f0f 100644 --- a/lib/settings/app_settings.dart +++ b/lib/settings/app_settings.dart @@ -5,6 +5,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:InvenTree/app_settings.dart'; + class InvenTreeAppSettingsWidget extends StatefulWidget { @override _InvenTreeAppSettingsState createState() => _InvenTreeAppSettingsState(); @@ -15,10 +17,43 @@ class _InvenTreeAppSettingsState extends State { final GlobalKey<_InvenTreeAppSettingsState> _settingsKey = GlobalKey<_InvenTreeAppSettingsState>(); _InvenTreeAppSettingsState() { - } - bool a = false; + bool barcodeSounds = true; + bool serverSounds = true; + + @override + void initState() { + super.initState(); + + loadSettings(); + } + + void loadSettings() async { + barcodeSounds = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; + serverSounds = await InvenTreeSettingsManager().getValue("serverSounds", true) as bool; + + setState(() { + }); + } + + void setBarcodeSounds(bool en) async { + + await InvenTreeSettingsManager().setValue("barcodeSounds", en); + barcodeSounds = await InvenTreeSettingsManager().getValue("barcodeSounds", true); + + setState(() { + }); + } + + void setServerSounds(bool en) async { + + await InvenTreeSettingsManager().setValue("serverSounds", en); + serverSounds = await InvenTreeSettingsManager().getValue("serverSounds", true); + + setState(() { + }); + } @override Widget build(BuildContext context) { @@ -33,35 +68,27 @@ class _InvenTreeAppSettingsState extends State { children: [ ListTile( title: Text( - "Sounds", + I18N.of(context).sounds, style: TextStyle(fontWeight: FontWeight.bold), ), leading: FaIcon(FontAwesomeIcons.volumeUp), ), ListTile( - title: Text("Server Error"), + title: Text(I18N.of(context).serverError), subtitle: Text("Play audible tone on server error"), leading: FaIcon(FontAwesomeIcons.server), trailing: Switch( - value: false, - onChanged: (value) { - setState(() { - // TODO - }); - }, + value: serverSounds, + onChanged: setServerSounds, ), ), ListTile( - title: Text("Barcode Tones"), + title: Text(I18N.of(context).barcodeTones), subtitle: Text("Play audible tones for barcode actions"), leading: FaIcon(FontAwesomeIcons.qrcode), trailing: Switch( - value: a, - onChanged: (value) { - setState(() { - a = value; - }); - }, + value: barcodeSounds, + onChanged: setBarcodeSounds, ), ), Divider(height: 1), diff --git a/lib/widget/dialogs.dart b/lib/widget/dialogs.dart index fcc92345..d22ff0b1 100644 --- a/lib/widget/dialogs.dart +++ b/lib/widget/dialogs.dart @@ -1,4 +1,5 @@ +import 'package:InvenTree/app_settings.dart'; import 'package:InvenTree/widget/snacks.dart'; import 'package:audioplayers/audio_cache.dart'; import 'package:audioplayers/audioplayers.dart'; @@ -116,8 +117,12 @@ Future showServerError(String title, String description) async { } // Play a sound - AudioCache player = AudioCache(); - player.play("sounds/server_error.mp3"); + final bool tones = await InvenTreeSettingsManager().getValue("serverSounds", true) as bool; + + if (tones) { + AudioCache player = AudioCache(); + player.play("sounds/server_error.mp3"); + } showSnackIcon( title,