refactor
Some checks are pending
BlockStorage/repertory/pipeline/head Build started...

This commit is contained in:
Scott E. Graves 2025-03-16 06:27:37 -05:00
parent d7a0fd9d8c
commit c720181208
5 changed files with 30 additions and 27 deletions

View File

@ -96,7 +96,7 @@ Map<String, dynamic> createDefaultSettings(String mountType) {
return {}; return {};
} }
void displayErrorMessage(context, text) { void displayErrorMessage(context, String text) {
if (!context.mounted) { if (!context.mounted) {
return; return;
} }

View File

@ -62,7 +62,7 @@ class _MyAppState extends State<MyApp> {
return EditMountScreen( return EditMountScreen(
mount: mount, mount: mount,
title: title:
'${initialCaps(mount.type)} [${formatMountName(mount.type, mount.name)}] Settings', '${mount.provider} [${formatMountName(mount.type, mount.name)}] Settings',
); );
}, },
); );

View File

@ -18,9 +18,10 @@ class Mount with ChangeNotifier {
String? get bucket => mountConfig.bucket; String? get bucket => mountConfig.bucket;
String get id => '${type}_$name'; String get id => '${type}_$name';
bool? get mounted => mountConfig.mounted;
String get name => mountConfig.name; String get name => mountConfig.name;
String get path => mountConfig.path; String get path => mountConfig.path;
IconData? get state => mountConfig.state; String get provider => mountConfig.provider;
String get type => mountConfig.type; String get type => mountConfig.type;
Future<void> _fetch() async { Future<void> _fetch() async {
@ -76,7 +77,7 @@ class Mount with ChangeNotifier {
Future<bool> mount(bool unmount, {String? location}) async { Future<bool> mount(bool unmount, {String? location}) async {
try { try {
mountConfig.state = null; mountConfig.mounted = null;
notifyListeners(); notifyListeners();
final response = await http.post( final response = await http.post(

View File

@ -1,12 +1,11 @@
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:repertory/helpers.dart' show initialCaps; import 'package:repertory/helpers.dart' show initialCaps;
class MountConfig { class MountConfig {
bool? mounted;
final String _name; final String _name;
String _path = ''; String _path = '';
Map<String, dynamic> _settings = {}; Map<String, dynamic> _settings = {};
IconData? _state;
final String _type; final String _type;
MountConfig({required name, required type, Map<String, dynamic>? settings}) MountConfig({required name, required type, Map<String, dynamic>? settings})
: _name = name, : _name = name,
@ -16,14 +15,12 @@ class MountConfig {
} }
} }
String? get bucket => String? get bucket => _settings['${provider}Config']?["Bucket"] as String;
_settings['${initialCaps(_type)}Config']?["Bucket"] as String;
String get name => _name; String get name => _name;
String get path => _path; String get path => _path;
String get provider => initialCaps(_type);
UnmodifiableMapView<String, dynamic> get settings => UnmodifiableMapView<String, dynamic> get settings =>
UnmodifiableMapView<String, dynamic>(_settings); UnmodifiableMapView<String, dynamic>(_settings);
IconData? get state => _state;
set state(value) => _state = value;
String get type => _type; String get type => _type;
void updateSettings(Map<String, dynamic> settings) { void updateSettings(Map<String, dynamic> settings) {
@ -32,6 +29,6 @@ class MountConfig {
void updateStatus(Map<String, dynamic> status) { void updateStatus(Map<String, dynamic> status) {
_path = status['Location'] as String; _path = status['Location'] as String;
_state = status['Active'] as bool ? Icons.toggle_on : Icons.toggle_off; mounted = status['Active'] as bool;
} }
} }

View File

@ -24,14 +24,13 @@ class _MountWidgetState extends State<MountWidget> {
return Card( return Card(
margin: const EdgeInsets.all(0.0), margin: const EdgeInsets.all(0.0),
child: Consumer<Mount>( child: Consumer<Mount>(
builder: (context, mount, _) { builder: (context, Mount mount, _) {
final textColor = Theme.of(context).colorScheme.onSurface; final textColor = Theme.of(context).colorScheme.onSurface;
final subTextColor = final subTextColor =
Theme.of(context).brightness == Brightness.dark Theme.of(context).brightness == Brightness.dark
? Colors.white38 ? Colors.white38
: Colors.black87; : Colors.black87;
final isMounted = mount.state == Icons.toggle_on;
final nameText = SelectableText( final nameText = SelectableText(
formatMountName(mount.type, mount.name), formatMountName(mount.type, mount.name),
style: TextStyle(color: subTextColor), style: TextStyle(color: subTextColor),
@ -49,7 +48,7 @@ class _MountWidgetState extends State<MountWidget> {
children: [ children: [
nameText, nameText,
SelectableText( SelectableText(
mount.path.isEmpty && mount.state == null mount.path.isEmpty && mount.mounted == null
? 'loading...' ? 'loading...'
: mount.path.isEmpty : mount.path.isEmpty
? '<mount location not set>' ? '<mount location not set>'
@ -59,16 +58,22 @@ class _MountWidgetState extends State<MountWidget> {
], ],
), ),
title: SelectableText( title: SelectableText(
initialCaps(mount.type), mount.provider,
style: TextStyle(color: textColor, fontWeight: FontWeight.bold), style: TextStyle(color: textColor, fontWeight: FontWeight.bold),
), ),
trailing: IconButton( trailing: IconButton(
icon: Icon( icon: Icon(
mount.state ?? Icons.hourglass_top, mount.mounted == null
? Icons.hourglass_top
: mount.mounted!
? Icons.toggle_on
: Icons.toggle_off,
color: color:
isMounted ? Color.fromARGB(255, 163, 96, 76) : subTextColor, mount.mounted ?? false
? Color.fromARGB(255, 163, 96, 76)
: subTextColor,
), ),
onPressed: _createMountHandler(context, isMounted, mount), onPressed: _createMountHandler(context, mount),
), ),
); );
}, },
@ -76,14 +81,14 @@ class _MountWidgetState extends State<MountWidget> {
); );
} }
VoidCallback? _createMountHandler(context, isMounted, mount) { VoidCallback? _createMountHandler(context, Mount mount) {
return _enabled && mount.state != null return _enabled && mount.mounted != null
? () async { ? () async {
setState(() { setState(() {
_enabled = false; _enabled = false;
}); });
final location = await _getMountLocation(context, mount, isMounted); final location = await _getMountLocation(context, mount);
cleanup() { cleanup() {
setState(() { setState(() {
@ -91,15 +96,15 @@ class _MountWidgetState extends State<MountWidget> {
}); });
} }
if (!isMounted && location == null) { if (!mount.mounted! && location == null) {
displayErrorMessage(context, "Mount location is not set"); displayErrorMessage(context, "Mount location is not set");
return cleanup(); return cleanup();
} }
final success = await mount.mount(isMounted, location: location); final success = await mount.mount(mount.mounted!, location: location);
if (success || if (success ||
isMounted || mount.mounted! ||
constants.navigatorKey.currentContext == null || constants.navigatorKey.currentContext == null ||
!constants.navigatorKey.currentContext!.mounted) { !constants.navigatorKey.currentContext!.mounted) {
return cleanup(); return cleanup();
@ -118,12 +123,12 @@ class _MountWidgetState extends State<MountWidget> {
super.dispose(); super.dispose();
} }
Future<String?> _getMountLocation(context, mount, isMounted) async { Future<String?> _getMountLocation(context, Mount mount) async {
if (isMounted) { if (mount.mounted ?? false) {
return null; return null;
} }
if (!mount.path.isEmpty) { if (mount.path.isNotEmpty) {
return mount.path; return mount.path;
} }