Skip to content

Commit

Permalink
feat(#668): add change notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Nov 21, 2023
1 parent 4f83d2f commit 740bbdd
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 50 deletions.
21 changes: 21 additions & 0 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ class UserData {
}
}

class ActiveDrugs extends ChangeNotifier {
List<String> activeDrugs = [];

Future<void> _preserve() async {
UserData.instance.activeDrugNames = activeDrugs;
await UserData.save();
}

Future<void> add(String drugName) async {
activeDrugs.add(drugName);
await _preserve();
notifyListeners();
}

Future<void> remove(String drugName) async {
activeDrugs = activeDrugs.filter((name) => name != drugName).toList();
await _preserve();
notifyListeners();
}
}

/// Initializes the user's data by registering all necessary adapters and
/// loading pre-existing data from local storage, if it exists.
Future<void> initUserData() async {
Expand Down
22 changes: 8 additions & 14 deletions app/lib/common/pages/drug/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ import '../../utilities/pdf_utils.dart';
part 'cubit.freezed.dart';

class DrugCubit extends Cubit<DrugState> {
DrugCubit() : super(DrugState.loaded());
DrugCubit(this.activeDrugs) : super(DrugState.loaded());

final ActiveDrugs activeDrugs;

// ignore: avoid_positional_boolean_parameters
Future<void> setActivity(Drug drug, bool? value) async {
if (value == null) return;
emit(DrugState.loading());
await setDrugActivity(drug, value);
if (value) {
await activeDrugs.add(drug.name);
} else {
await activeDrugs.remove(drug.name);
}
emit(DrugState.loaded());
}

Expand All @@ -32,18 +38,6 @@ class DrugCubit extends Cubit<DrugState> {
}
}

// ignore: avoid_positional_boolean_parameters
Future<void> setDrugActivity(Drug drug, bool value) async {
final active = (UserData.instance.activeDrugNames ?? [])
.filter((name) => name != drug.name)
.toList();
if (value) {
active.add(drug.name);
}
UserData.instance.activeDrugNames = active;
await UserData.save();
}

@freezed
class DrugState with _$DrugState {
const factory DrugState.loading() = _LoadingState;
Expand Down
24 changes: 14 additions & 10 deletions app/lib/common/pages/drug/drug.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:provider/provider.dart';

import '../../module.dart';
import 'cubit.dart';
import 'widgets/module.dart';
Expand All @@ -13,16 +15,18 @@ class DrugPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => cubit ?? DrugCubit(),
child: BlocBuilder<DrugCubit, DrugState>(
builder: (context, state) {
return state.when(
loaded: () => _buildDrugsPage(context, loading: false),
loading: () => _buildDrugsPage(context, loading: true),
);
},
),
return Consumer<ActiveDrugs>(
builder: (context, activeDrugs, child) => BlocProvider(
create: (context) => cubit ?? DrugCubit(activeDrugs),
child: BlocBuilder<DrugCubit, DrugState>(
builder: (context, state) {
return state.when(
loaded: () => _buildDrugsPage(context, loading: false),
loading: () => _buildDrugsPage(context, loading: true),
);
},
),
)
);
}

Expand Down
12 changes: 9 additions & 3 deletions app/lib/drug_selection/pages/cubit.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../../common/module.dart';
import '../../common/pages/drug/cubit.dart';

part 'cubit.freezed.dart';

class DrugSelectionPageCubit extends Cubit<DrugSelectionPageState> {
DrugSelectionPageCubit() : super(DrugSelectionPageState.stable());
DrugSelectionPageCubit(this.activeDrugs) :
super(DrugSelectionPageState.stable());

final ActiveDrugs activeDrugs;

// ignore: avoid_positional_boolean_parameters
Future<void> updateDrugActivity(Drug drug, bool? value) async {
if (value == null) return;
emit(DrugSelectionPageState.updating());
await setDrugActivity(drug, value);
if (value) {
await activeDrugs.add(drug.name);
} else {
await activeDrugs.remove(drug.name);
}
emit(DrugSelectionPageState.stable());
}
}
Expand Down
42 changes: 23 additions & 19 deletions app/lib/drug_selection/pages/drug_selection.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:provider/provider.dart';

import '../../common/models/drug/cached_drugs.dart';
import '../../common/module.dart' hide MetaData;
import '../../common/widgets/drug_list/drug_items/drug_checkbox_list.dart';
Expand All @@ -15,25 +17,27 @@ class DrugSelectionPage extends HookWidget {

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => cubit ?? DrugSelectionPageCubit(),
child: BlocBuilder<DrugSelectionPageCubit, DrugSelectionPageState>(
builder: (context, state) {
return unscrollablePageScaffold(
title: context.l10n.drug_selection_header,
padding: PharMeTheme.largeSpace,
body: Column(
children: [
_buildDescription(context),
SizedBox(height: PharMeTheme.mediumSpace),
Expanded(child:_buildDrugList(context, state)),
SizedBox(height: PharMeTheme.mediumSpace),
_buildButton(context, state),
],
),
);
}
),
return Consumer<ActiveDrugs>(
builder: (context, activeDrugs, child) => BlocProvider(
create: (context) => cubit ?? DrugSelectionPageCubit(activeDrugs),
child: BlocBuilder<DrugSelectionPageCubit, DrugSelectionPageState>(
builder: (context, state) {
return unscrollablePageScaffold(
title: context.l10n.drug_selection_header,
padding: PharMeTheme.largeSpace,
body: Column(
children: [
_buildDescription(context),
SizedBox(height: PharMeTheme.mediumSpace),
Expanded(child:_buildDrugList(context, state)),
SizedBox(height: PharMeTheme.mediumSpace),
_buildButton(context, state),
],
),
);
}
),
)
);
}

Expand Down
9 changes: 8 additions & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import 'package:provider/provider.dart';

import 'common/module.dart';

Future<void> main() async {
await initServices();
await fetchAndSaveLookups();
runApp(PharMeApp());
runApp(
ChangeNotifierProvider(
create: (context) => ActiveDrugs(),
child: PharMeApp(),
),
);
await cleanupServices();
}
6 changes: 3 additions & 3 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -981,13 +981,13 @@ packages:
source: hosted
version: "4.2.4"
provider:
dependency: transitive
dependency: "direct main"
description:
name: provider
sha256: e1e7413d70444ea3096815a60fe5da1b11bda8a9dc4769252cc82c53536f8bcc
sha256: "9a96a0a19b594dbc5bf0f1f27d2bc67d5f95957359b461cd9feb44ed6ae75096"
url: "https://pub.dev"
source: hosted
version: "6.0.4"
version: "6.1.1"
pub_semver:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
pdf: ^3.8.1
popover: ^0.2.8+1
printing: ^5.9.3
provider: ^6.1.1
shared_preferences: ^2.0.15
url_launcher: ^6.1.4

Expand Down

0 comments on commit 740bbdd

Please sign in to comment.