From a9ae0e28fd04c1656934aac6995d5ef01da01097 Mon Sep 17 00:00:00 2001 From: Ben Hall Date: Fri, 5 Nov 2021 00:52:58 +0000 Subject: [PATCH] Mixin preferences: Refactored shared preferences functions to be generic setters/getters used as a mixin (#2) * Refactored shared preferences functions to be generic setters/getters using key/values to allow for multiple preferences to be stored. Refactored the shared preferences functions as a mixin for ease of use. Directory change is now remembered across the app pages. * Added initState() to downloader_menu.dart to fetch prefWorkingDirectory Co-authored-by: ElanMan --- lib/main.dart | 3 -- lib/src/app.dart | 9 ++-- lib/src/globals.dart | 4 +- lib/src/mixins/preferences_mixin.dart | 16 ++++++++ lib/src/pages/downloader.dart | 4 +- lib/src/pages/manager.dart | 41 ++++++++----------- .../widgets/home_page/downloader_menu.dart | 23 ++++++++--- 7 files changed, 57 insertions(+), 43 deletions(-) create mode 100644 lib/src/mixins/preferences_mixin.dart diff --git a/lib/main.dart b/lib/main.dart index 2db4f6c..35e8b41 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,6 @@ import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:quickgui/src/app.dart'; -import 'package:quickgui/src/globals.dart'; import 'package:quickgui/src/model/operating_system.dart'; import 'package:quickgui/src/model/option.dart'; import 'package:quickgui/src/model/version.dart'; @@ -44,7 +42,6 @@ Future> loadOperatingSystems(bool showUbuntus) async { void main() async { WidgetsFlutterBinding.ensureInitialized(); - Directory.current = gCurrentDirectoy; setWindowTitle('Quickgui : a flutter frontend for Quickget and Quickemu'); setWindowMinSize(const Size(692, 580)); setWindowMaxSize(const Size(692, 580)); diff --git a/lib/src/app.dart b/lib/src/app.dart index bc99648..83688aa 100644 --- a/lib/src/app.dart +++ b/lib/src/app.dart @@ -7,10 +7,9 @@ class App extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - theme: ThemeData( - primarySwatch: Colors.pink, - ), - home: const MainPage(title: 'Quickgui - A Flutter frontend for Quickget and Quickemu'), - ); + theme: ThemeData( + primarySwatch: Colors.pink, + ), + home: const MainPage(title: 'Quickgui - A Flutter frontend for Quickget and Quickemu')); } } diff --git a/lib/src/globals.dart b/lib/src/globals.dart index d0ffcdc..75b67e0 100644 --- a/lib/src/globals.dart +++ b/lib/src/globals.dart @@ -1,6 +1,4 @@ import 'dart:io'; -import 'package:flutter/material.dart'; - var gIsSnap = Platform.environment['SNAP']?.isNotEmpty ?? false; -var gCurrentDirectoy = Directory(Platform.environment['HOME'] ?? Directory.current.absolute.path); +const String prefWorkingDirectory = 'workingDirectory'; diff --git a/lib/src/mixins/preferences_mixin.dart b/lib/src/mixins/preferences_mixin.dart new file mode 100644 index 0000000..e14e1b8 --- /dev/null +++ b/lib/src/mixins/preferences_mixin.dart @@ -0,0 +1,16 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +mixin PreferencesMixin { + void savePreference(key, value) async { + final prefs = await SharedPreferences.getInstance(); + prefs.setString(key, value); + } + + Future getPreference(key) async { + final prefs = await SharedPreferences.getInstance(); + if (prefs.containsKey(key)) { + final preference = prefs.getString(key); + return preference ?? false; + } + } +} diff --git a/lib/src/pages/downloader.dart b/lib/src/pages/downloader.dart index 154801c..9b3d159 100644 --- a/lib/src/pages/downloader.dart +++ b/lib/src/pages/downloader.dart @@ -3,7 +3,6 @@ import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; -import 'package:quickgui/src/globals.dart'; import 'package:quickgui/src/model/operating_system.dart'; import 'package:quickgui/src/model/option.dart'; import 'package:quickgui/src/model/version.dart'; @@ -37,7 +36,6 @@ class _DownloaderState extends State { @override void initState() { - Directory.current = gCurrentDirectoy; _progressStream = progressStream(); super.initState(); } @@ -122,7 +120,7 @@ class _DownloaderState extends State { ), Padding( padding: const EdgeInsets.only(top: 32), - child: Text("Target folder : $gCurrentDirectoy"), + child: Text("Target folder : ${Directory.current}"), ), ], ); diff --git a/lib/src/pages/manager.dart b/lib/src/pages/manager.dart index bb2cf0d..e9818f2 100644 --- a/lib/src/pages/manager.dart +++ b/lib/src/pages/manager.dart @@ -4,8 +4,9 @@ import 'package:flutter/material.dart'; import 'package:path/path.dart' as path; import 'package:file_picker/file_picker.dart'; import 'dart:io'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:quickgui/src/globals.dart'; import 'package:quickgui/src/model/vminfo.dart'; +import 'package:quickgui/src/mixins/preferences_mixin.dart'; /// VM manager page. /// Displays a list of available VMs, running state and connection info, @@ -17,18 +18,24 @@ class Manager extends StatefulWidget { State createState() => _ManagerState(); } -class _ManagerState extends State { +class _ManagerState extends State with PreferencesMixin { List _currentVms = []; Map _activeVms = {}; final List _spicyVms = []; Timer? refreshTimer; - static const String prefsWorkingDirectory = 'workingDirectory'; @override void initState() { super.initState(); - _getCurrentDirectory(); - Future.delayed(Duration.zero, () => _getVms(context)); // Reload VM list when we enter the page. + getPreference(prefWorkingDirectory).then((pref) { + if (pref is String) { + setState(() { + Directory.current = pref; + }); + Future.delayed(Duration.zero, () => _getVms(context)); // Reload VM list when we enter the page. + } + }); + refreshTimer = Timer.periodic(const Duration(seconds: 5), (Timer t) { _getVms(context); }); // Reload VM list every 5 seconds. @@ -40,23 +47,6 @@ class _ManagerState extends State { super.dispose(); } - void _saveCurrentDirectory() async { - final prefs = await SharedPreferences.getInstance(); - prefs.setString(prefsWorkingDirectory, Directory.current.path); - } - - void _getCurrentDirectory() async { - final prefs = await SharedPreferences.getInstance(); - if (prefs.containsKey(prefsWorkingDirectory)) { - setState(() { - final directory = prefs.getString(prefsWorkingDirectory); - if (directory != null) { - Directory.current = directory; - } - }); - } - } - VmInfo _parseVmInfo(name) { VmInfo info = VmInfo(); List lines = File(name + '/' + name + '.ports').readAsLinesSync(); @@ -109,8 +99,11 @@ class _ManagerState extends State { onPressed: () async { String? result = await FilePicker.platform.getDirectoryPath(); if (result != null) { - Directory.current = result; - _saveCurrentDirectory(); + setState(() { + Directory.current = result; + }); + + savePreference(prefWorkingDirectory, Directory.current.path); _getVms(context); } }, diff --git a/lib/src/widgets/home_page/downloader_menu.dart b/lib/src/widgets/home_page/downloader_menu.dart index de77eab..8bab364 100644 --- a/lib/src/widgets/home_page/downloader_menu.dart +++ b/lib/src/widgets/home_page/downloader_menu.dart @@ -1,8 +1,8 @@ import 'dart:io'; - +import 'package:quickgui/src/globals.dart'; +import 'package:quickgui/src/mixins/preferences_mixin.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; -import 'package:quickgui/src/globals.dart'; import 'package:quickgui/src/widgets/home_page/home_page_button_group.dart'; class DownloaderMenu extends StatefulWidget { @@ -12,7 +12,19 @@ class DownloaderMenu extends StatefulWidget { State createState() => _DownloaderMenuState(); } -class _DownloaderMenuState extends State { +class _DownloaderMenuState extends State with PreferencesMixin { + @override + void initState() { + super.initState(); + getPreference(prefWorkingDirectory).then((pref) { + if (pref is String) { + setState(() { + Directory.current = pref; + }); + } + }); + } + @override Widget build(BuildContext context) { return Expanded( @@ -40,12 +52,13 @@ class _DownloaderMenuState extends State { var folder = await FilePicker.platform.getDirectoryPath(dialogTitle: "Pick a folder"); if (folder != null) { setState(() { - gCurrentDirectoy = Directory(folder); + Directory.current = folder; }); + savePreference(prefWorkingDirectory, Directory.current.path); } }, child: Text( - "Working directory : ${gCurrentDirectoy.path}", + "Working directory : ${Directory.current.path}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Colors.white), ), ),