refactor
All checks were successful
BlockStorage/repertory/pipeline/head This commit looks good

This commit is contained in:
Scott E. Graves 2025-02-28 23:14:37 -06:00
parent 343c324050
commit 52f0d755ba
4 changed files with 37 additions and 18 deletions

View File

@ -0,0 +1,10 @@
import 'package:flutter/foundation.dart';
import 'package:repertory/types/mount_config.dart';
class Mount with ChangeNotifier {
final MountConfig mountConfig;
Mount(this.mountConfig);
String get name => mountConfig.name;
String get type => mountConfig.type;
}

View File

@ -15,17 +15,6 @@ class MountList with ChangeNotifier {
UnmodifiableListView get items => UnmodifiableListView(_mountList);
void _sort(list) {
list.sort((a, b) {
final res = a.type.compareTo(b.type);
if (res != 0) {
return res;
}
return a.name.compareTo(b.name);
});
}
Future<void> _fetch() async {
final response = await http.get(
Uri.parse('${Uri.base.origin}/api/v1/mount_list'),
@ -34,8 +23,7 @@ class MountList with ChangeNotifier {
if (response.statusCode == 200) {
List<MountConfig> nextList = [];
var data = jsonDecode(response.body);
data.forEach((key, value) {
jsonDecode(response.body).forEach((key, value) {
nextList.addAll(
value.map((name) => MountConfig.fromJson(key, name)).toList(),
);
@ -48,6 +36,17 @@ class MountList with ChangeNotifier {
}
}
void _sort(list) {
list.sort((a, b) {
final res = a.type.compareTo(b.type);
if (res != 0) {
return res;
}
return a.name.compareTo(b.name);
});
}
void add(MountConfig config) {
var item = _mountList.firstWhereOrNull((cfg) => cfg.name == config.name);
if (item != null) {
@ -62,5 +61,7 @@ class MountList with ChangeNotifier {
void remove(String name) {
_mountList.removeWhere((item) => item.name == name);
notifyListeners();
}
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:repertory/models/mount.dart';
import 'package:repertory/models/mount_list.dart';
import 'package:repertory/widgets/mount_widget.dart';
@ -17,7 +18,10 @@ class _MountListWidgetState extends State<MountListWidget> {
builder: (context, mountList, widget) {
return ListView.builder(
itemBuilder: (context, idx) {
return MountWidget(mountConfig: mountList.items[idx]);
return ChangeNotifierProvider(
create: (context) => Mount(mountList.items[idx]),
child: const MountWidget(),
);
},
itemCount: mountList.items.length,
);

View File

@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
import 'package:repertory/types/mount_config.dart';
import 'package:provider/provider.dart';
import 'package:repertory/models/mount.dart';
class MountWidget extends StatelessWidget {
final MountConfig mountConfig;
const MountWidget({super.key, required this.mountConfig});
const MountWidget({super.key});
@override
Widget build(BuildContext context) {
@ -11,7 +11,11 @@ class MountWidget extends StatelessWidget {
child: Container(
height: 40,
color: Colors.blue,
child: Text('${mountConfig.type} ${mountConfig.name}'),
child: Consumer<Mount>(
builder: (context, mount, widget) {
return Text('${mount.type} ${mount.name}');
},
),
),
);
}