mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-28 15:09:53 -06:00
50 lines
1.4 KiB
Dart
50 lines
1.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:phylum/libphylum/db/db.dart';
|
|
import 'package:phylum/libphylum/phylum_account.dart';
|
|
|
|
import 'menu_option.dart';
|
|
|
|
typedef MenuOptionGroups = Iterable<Iterable<MenuOption>>;
|
|
|
|
Future<Iterable<PopupMenuEntry<MenuOption>>> buildPopupMenuItems(
|
|
PhylumAccount account,
|
|
MenuOptionGroups options,
|
|
Iterable<Resource> resources, [
|
|
bool hideDisabled = true,
|
|
]) async {
|
|
final availableOptions = await Future.wait(
|
|
options.map(
|
|
(group) async => Future.wait(
|
|
group.map((o) async {
|
|
final enabled = await o.filter(account, resources);
|
|
return hideDisabled && !enabled
|
|
? null
|
|
: PopupMenuItem(
|
|
value: o,
|
|
enabled: enabled,
|
|
child: ListTile(
|
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
|
leading: Icon(o.icon),
|
|
title: Text(o.text),
|
|
),
|
|
);
|
|
}),
|
|
).then((items) => items.whereType<PopupMenuEntry<MenuOption>>()),
|
|
),
|
|
);
|
|
|
|
final m = availableOptions.fold(
|
|
<PopupMenuEntry<MenuOption>>[],
|
|
(acc, e) {
|
|
if (e.isEmpty) {
|
|
return acc;
|
|
}
|
|
if (acc.isEmpty) {
|
|
return e.toList(growable: false);
|
|
}
|
|
return [...acc, PopupMenuDivider(), ...e];
|
|
},
|
|
);
|
|
return m;
|
|
}
|