This commit is contained in:
Scott E. Graves 2025-03-06 14:04:05 -06:00
parent 6f45848db4
commit 992a02a6ee
3 changed files with 106 additions and 67 deletions

View File

@ -20,48 +20,60 @@ class Mount with ChangeNotifier {
String get type => mountConfig.type; String get type => mountConfig.type;
Future<void> _fetch() async { Future<void> _fetch() async {
final response = await http.get( try {
Uri.parse( final response = await http.get(
Uri.encodeFull('${getBaseUri()}/api/v1/mount?name=$name&type=$type'), Uri.parse(
), Uri.encodeFull('${getBaseUri()}/api/v1/mount?name=$name&type=$type'),
); ),
);
if (response.statusCode != 200) { if (response.statusCode != 200) {
return; return;
}
mountConfig.updateSettings(jsonDecode(response.body));
notifyListeners();
} catch (e) {
debugPrint('$e');
} }
mountConfig.updateSettings(jsonDecode(response.body));
notifyListeners();
} }
Future<void> _fetchStatus() async { Future<void> _fetchStatus() async {
final response = await http.get( try {
Uri.parse( final response = await http.get(
Uri.encodeFull( Uri.parse(
'${getBaseUri()}/api/v1/mount_status?name=$name&type=$type', Uri.encodeFull(
'${getBaseUri()}/api/v1/mount_status?name=$name&type=$type',
),
), ),
), );
);
if (response.statusCode != 200) { if (response.statusCode != 200) {
return; return;
}
mountConfig.updateStatus(jsonDecode(response.body));
notifyListeners();
} catch (e) {
debugPrint('$e');
} }
mountConfig.updateStatus(jsonDecode(response.body));
notifyListeners();
} }
Future<void> mount(bool unmount, {String? location}) async { Future<void> mount(bool unmount, {String? location}) async {
await http.post( try {
Uri.parse( await http.post(
Uri.encodeFull( Uri.parse(
'${getBaseUri()}/api/v1/mount?unmount=$unmount&name=$name&type=$type&location=$location', Uri.encodeFull(
'${getBaseUri()}/api/v1/mount?unmount=$unmount&name=$name&type=$type&location=$location',
),
), ),
), );
);
return refresh(); return refresh();
} catch (e) {
debugPrint('$e');
}
} }
Future<void> refresh() async { Future<void> refresh() async {
@ -70,30 +82,40 @@ class Mount with ChangeNotifier {
} }
Future<void> setValue(String key, String value) async { Future<void> setValue(String key, String value) async {
await http.put( try {
Uri.parse( await http.put(
Uri.encodeFull( Uri.parse(
'${getBaseUri()}/api/v1/set_value_by_name?name=$name&type=$type&key=$key&value=$value', Uri.encodeFull(
'${getBaseUri()}/api/v1/set_value_by_name?name=$name&type=$type&key=$key&value=$value',
),
), ),
), );
);
return refresh(); return refresh();
} catch (e) {
debugPrint('$e');
}
} }
Future<String?> getMountLocation() async { Future<String?> getMountLocation() async {
final response = await http.get( try {
Uri.parse( final response = await http.get(
Uri.encodeFull( Uri.parse(
'${getBaseUri()}/api/v1/get_mount_location?name=$name&type=$type', Uri.encodeFull(
'${getBaseUri()}/api/v1/get_mount_location?name=$name&type=$type',
),
), ),
), );
);
if (response.statusCode != 200) { if (response.statusCode != 200) {
return null; return null;
}
return jsonDecode(response.body)['Location'] as String;
} catch (e) {
debugPrint('$e');
} }
return jsonDecode(response.body)['Location'] as String; return null;
} }
} }

View File

@ -17,11 +17,14 @@ class MountList with ChangeNotifier {
UnmodifiableListView<MountConfig>(_mountList); UnmodifiableListView<MountConfig>(_mountList);
Future<void> _fetch() async { Future<void> _fetch() async {
final response = await http.get( try {
Uri.parse('${getBaseUri()}/api/v1/mount_list'), final response = await http.get(
); Uri.parse('${getBaseUri()}/api/v1/mount_list'),
);
if (response.statusCode == 200) { if (response.statusCode != 200) {
return;
}
List<MountConfig> nextList = []; List<MountConfig> nextList = [];
jsonDecode(response.body).forEach((key, value) { jsonDecode(response.body).forEach((key, value) {
@ -33,7 +36,8 @@ class MountList with ChangeNotifier {
_mountList = nextList; _mountList = nextList;
notifyListeners(); notifyListeners();
return; } catch (e) {
debugPrint('$e');
} }
} }
@ -50,25 +54,22 @@ class MountList with ChangeNotifier {
Future<void> add( Future<void> add(
String type, String type,
String name, { String name,
String? apiPassword, Map<String, dynamic> mountConfig,
String? apiPort, ) async {
String? bucket, try {
String? encryptionToken, await http.post(
String? hostNameOrIp, Uri.parse(
String? path, Uri.encodeFull(
}) async { '${getBaseUri()}/api/v1/add_mount?name=$name&type=$type&config=${jsonEncode(mountConfig)}',
await http.post( ),
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/add_mount?name=$name&type=$type&bucket=$bucket'
'&path=$path&apiPassword=$apiPassword&apiPort=$apiPort&hostNameOrIp=$hostNameOrIp'
'&encryptionToken=$encryptionToken',
), ),
), );
);
return _fetch(); return _fetch();
} catch (e) {
debugPrint('$e');
}
} }
void remove(String name) { void remove(String name) {

View File

@ -1,7 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:repertory/constants.dart'; import 'package:repertory/constants.dart';
import 'package:repertory/helpers.dart'; import 'package:repertory/helpers.dart';
import 'package:repertory/models/mount.dart'; import 'package:repertory/models/mount.dart';
import 'package:repertory/models/mount_list.dart';
import 'package:repertory/types/mount_config.dart'; import 'package:repertory/types/mount_config.dart';
import 'package:repertory/widgets/mount_settings.dart'; import 'package:repertory/widgets/mount_settings.dart';
@ -116,6 +118,20 @@ class _AddMountScreenState extends State<AddMountScreen> {
), ),
), ),
), ),
if (_mount != null)
ElevatedButton.icon(
onPressed: () {
Provider.of<MountList>(context, listen: false).add(
_mountType,
_mountNameController.text,
_settings[_mountType]!,
);
Navigator.pop(context);
},
label: const Text('Add'),
icon: Icon(Icons.add),
),
], ],
), ),
), ),