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;
Future<void> _fetch() async {
final response = await http.get(
Uri.parse(
Uri.encodeFull('${getBaseUri()}/api/v1/mount?name=$name&type=$type'),
),
);
try {
final response = await http.get(
Uri.parse(
Uri.encodeFull('${getBaseUri()}/api/v1/mount?name=$name&type=$type'),
),
);
if (response.statusCode != 200) {
return;
if (response.statusCode != 200) {
return;
}
mountConfig.updateSettings(jsonDecode(response.body));
notifyListeners();
} catch (e) {
debugPrint('$e');
}
mountConfig.updateSettings(jsonDecode(response.body));
notifyListeners();
}
Future<void> _fetchStatus() async {
final response = await http.get(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/mount_status?name=$name&type=$type',
try {
final response = await http.get(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/mount_status?name=$name&type=$type',
),
),
),
);
);
if (response.statusCode != 200) {
return;
if (response.statusCode != 200) {
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 {
await http.post(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/mount?unmount=$unmount&name=$name&type=$type&location=$location',
try {
await http.post(
Uri.parse(
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 {
@ -70,30 +82,40 @@ class Mount with ChangeNotifier {
}
Future<void> setValue(String key, String value) async {
await http.put(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/set_value_by_name?name=$name&type=$type&key=$key&value=$value',
try {
await http.put(
Uri.parse(
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 {
final response = await http.get(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/get_mount_location?name=$name&type=$type',
try {
final response = await http.get(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/get_mount_location?name=$name&type=$type',
),
),
),
);
);
if (response.statusCode != 200) {
return null;
if (response.statusCode != 200) {
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);
Future<void> _fetch() async {
final response = await http.get(
Uri.parse('${getBaseUri()}/api/v1/mount_list'),
);
try {
final response = await http.get(
Uri.parse('${getBaseUri()}/api/v1/mount_list'),
);
if (response.statusCode == 200) {
if (response.statusCode != 200) {
return;
}
List<MountConfig> nextList = [];
jsonDecode(response.body).forEach((key, value) {
@ -33,7 +36,8 @@ class MountList with ChangeNotifier {
_mountList = nextList;
notifyListeners();
return;
} catch (e) {
debugPrint('$e');
}
}
@ -50,25 +54,22 @@ class MountList with ChangeNotifier {
Future<void> add(
String type,
String name, {
String? apiPassword,
String? apiPort,
String? bucket,
String? encryptionToken,
String? hostNameOrIp,
String? path,
}) async {
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',
String name,
Map<String, dynamic> mountConfig,
) async {
try {
await http.post(
Uri.parse(
Uri.encodeFull(
'${getBaseUri()}/api/v1/add_mount?name=$name&type=$type&config=${jsonEncode(mountConfig)}',
),
),
),
);
);
return _fetch();
return _fetch();
} catch (e) {
debugPrint('$e');
}
}
void remove(String name) {

View File

@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:repertory/constants.dart';
import 'package:repertory/helpers.dart';
import 'package:repertory/models/mount.dart';
import 'package:repertory/models/mount_list.dart';
import 'package:repertory/types/mount_config.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),
),
],
),
),