Skip to content

Commit

Permalink
feat(#691): adapt filter menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Feb 20, 2024
1 parent 988185c commit f6ffacb
Show file tree
Hide file tree
Showing 13 changed files with 271 additions and 251 deletions.
23 changes: 22 additions & 1 deletion app/lib/common/models/drug/warning_level.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';

import '../../module.dart';

part 'warning_level.g.dart';

@HiveType(typeId: 11)
Expand All @@ -20,10 +21,18 @@ extension WarningLevelIcon on WarningLevel {
WarningLevel.red.name: Icons.dangerous_rounded,
WarningLevel.yellow.name: Icons.warning_rounded,
WarningLevel.green.name: Icons.check_circle_rounded,
WarningLevel.none.name: Icons.help_rounded,
};

static final _outlinedIconMap = {
WarningLevel.red.name: Icons.dangerous_outlined,
WarningLevel.yellow.name: Icons.warning_amber_rounded,
WarningLevel.green.name: Icons.check_circle_outline_outlined,
WarningLevel.none.name: Icons.help_outline_rounded,
};

IconData get icon => WarningLevelIcon._iconMap[name]!;
IconData get outlinedIcon => WarningLevelIcon._outlinedIconMap[name]!;
}

extension WarningLevelSeverity on WarningLevel {
Expand All @@ -35,3 +44,15 @@ extension WarningLevelSeverity on WarningLevel {
};
int get severity => WarningLevelSeverity._severityMap[name]!;
}

extension WarningLevelColor on WarningLevel {
static final _colorMap = {
WarningLevel.red.name: Color(0xffffafaf),
WarningLevel.yellow.name: Color(0xffffebcc),
WarningLevel.green.name: Color(0xffcfe8cf),
WarningLevel.none.name: Color(0xffcfe8cf),
};

Color get color => WarningLevelColor._colorMap[name]!;
Color get textColor => darkenColor(color, 0.4);
}
11 changes: 0 additions & 11 deletions app/lib/common/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,3 @@ class AppBarTheme {
final elevation = 0.0;
final centerTitle = false;
}

extension WarningLevelColor on WarningLevel {
static final _colorMap = {
WarningLevel.red.name: Color(0xffffafaf),
WarningLevel.yellow.name: Color(0xffffebcc),
WarningLevel.green.name: Color(0xffcfe8cf),
WarningLevel.none.name: Color(0xffcfe8cf),
};

Color get color => WarningLevelColor._colorMap[name]!;
}
2 changes: 1 addition & 1 deletion app/lib/common/utilities/color_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Color darkenColor(Color color, [double amount = 0.1]) {
final hsl = HSLColor.fromColor(color);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
return hslDark.toColor();
}
}
23 changes: 14 additions & 9 deletions app/lib/common/widgets/drug_list/drug_items/drug_cards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ class DrugCard extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Icon(drug.warningLevel.icon),
SizedBox(width: 4),
Text(
drugName,
style: PharMeTheme.textTheme.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
]),
Row(
children: [
Icon(
drug.warningLevel.icon,
color: PharMeTheme.onSurfaceText,
),
SizedBox(width: 4),
Text(
drugName,
style: PharMeTheme.textTheme.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
],
),
SizedBox(height: PharMeTheme.smallSpace / 2),
if (drug.annotations.brandNames.isNotEmpty) ...[
SizedBox(width: PharMeTheme.smallSpace / 2),
Expand Down
165 changes: 0 additions & 165 deletions app/lib/common/widgets/drug_search.dart

This file was deleted.

137 changes: 137 additions & 0 deletions app/lib/common/widgets/drug_search/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

import '../../../../common/module.dart';
import '../../../drug/widgets/tooltip_icon.dart';
import 'filter_menu.dart';

class DrugSearch extends HookWidget {
DrugSearch({
super.key,
required this.showFilter,
required this.buildDrugItems,
required this.showDrugInteractionIndicator,
this.useDrugClass = true,
this.keepPosition = false,
this.drugItemsBuildParams,
DrugListCubit? cubit,
}) : cubit = cubit ?? DrugListCubit();

final bool showFilter;
final bool useDrugClass;
final bool keepPosition;
final List<Widget> Function(
BuildContext context,
List<Drug> drugs,
{
Map? buildParams,
bool showDrugInteractionIndicator,
}
) buildDrugItems;
final bool showDrugInteractionIndicator;
final DrugListCubit cubit;
final Map? drugItemsBuildParams;

@override
Widget build(BuildContext context) {
final searchController = useTextEditingController();
return Consumer<ActiveDrugs>(
builder: (context, activeDrugs, child) => BlocProvider(
create: (context) => cubit,
child: BlocBuilder<DrugListCubit, DrugListState>(
builder: (context, state) {
return Column(
children: [
Padding(
padding: EdgeInsets.only(
left: PharMeTheme.smallSpace,
right: PharMeTheme.smallSpace,
bottom: PharMeTheme.smallSpace,
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
..._buildSearchBarItems(context, searchController),
if (showFilter) FilterMenu(cubit, state),
],
),
),
scrollList(
keepPosition: keepPosition,
buildDrugList(
context,
state,
activeDrugs,
buildDrugItems: buildDrugItems,
noDrugsMessage: context.l10n.search_no_drugs(
showFilter
? context.l10n.search_no_drugs_with_filter_amendment
: ''
),
drugItemsBuildParams: drugItemsBuildParams,
showDrugInteractionIndicator:
showDrugInteractionIndicator,
useDrugClass: useDrugClass,
)
),
_maybeBuildInteractionIndicator(context, state, activeDrugs)
?? SizedBox.shrink(),
],
);
}
)
)
);
}

List<Widget> _buildSearchBarItems(
BuildContext context,
TextEditingController searchController,
) {
return [
Expanded(
child: CupertinoSearchTextField(
controller: searchController,
onChanged: (value) {
context.read<DrugListCubit>().search(
query: value,
);
},
),
),
SizedBox(width: PharMeTheme.smallToMediumSpace),
TooltipIcon(useDrugClass
? context.l10n.search_page_tooltip_search
: context.l10n.search_page_tooltip_search_no_class
),
];
}

Widget? _maybeBuildInteractionIndicator(
BuildContext context,
DrugListState state,
ActiveDrugs activeDrugs,
) {
return state.whenOrNull(
loaded: (drugs, filter) {
if (showDrugInteractionIndicator) {
final filteredDrugs = filter.filter(
drugs,
activeDrugs,
useDrugClass: useDrugClass,
);
if (filteredDrugs.any((drug) => isInhibitor(drug.name))) {
return PageIndicatorExplanation(
context.l10n.search_page_indicator_explanation(
drugInteractionIndicatorName,
drugInteractionIndicator
),
);
}
}
return null;
}
);
}
}
Loading

0 comments on commit f6ffacb

Please sign in to comment.