Skip to content

Commit

Permalink
feat(#668): add change notifier to login
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Nov 22, 2023
1 parent 740bbdd commit 4f4ef7e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 55 deletions.
27 changes: 20 additions & 7 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,34 @@ class UserData {
class ActiveDrugs extends ChangeNotifier {
List<String> activeDrugs = [];

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

Future<void> setList(List<String> drugNames) async {
activeDrugs = drugNames;
await _preserveAndNotify();
}

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

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

// ignore: avoid_positional_boolean_parameters
Future<void> changeActivity(String drugName, bool value) async {
if (value) {
await _add(drugName);
} else {
await _remove(drugName);
}
}
}

Expand Down
6 changes: 1 addition & 5 deletions app/lib/common/pages/drug/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ class DrugCubit extends Cubit<DrugState> {
Future<void> setActivity(Drug drug, bool? value) async {
if (value == null) return;
emit(DrugState.loading());
if (value) {
await activeDrugs.add(drug.name);
} else {
await activeDrugs.remove(drug.name);
}
await activeDrugs.changeActivity(drug.name, value);
emit(DrugState.loaded());
}

Expand Down
13 changes: 8 additions & 5 deletions app/lib/common/utilities/genome_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import '../models/drug/cached_drugs.dart';
import '../models/module.dart';

Future<void> fetchAndSaveDiplotypesAndActiveDrugs(
String token, String url) async {
String token, String url, ActiveDrugs activeDrugs) async {
if (!shouldFetchDiplotypes()) return;
final response = await getDiplotypes(token, url);
if (response.statusCode == 200) {
await _saveDiplotypeAndActiveDrugsResponse(response);
await _saveDiplotypeAndActiveDrugsResponse(response, activeDrugs);
} else {
throw Exception();
}
Expand All @@ -22,15 +22,18 @@ Future<Response> getDiplotypes(String? token, String url) async {
return get(Uri.parse(url), headers: {'Authorization': 'Bearer $token'});
}

Future<void> _saveDiplotypeAndActiveDrugsResponse(Response response) async {
Future<void> _saveDiplotypeAndActiveDrugsResponse(
Response response,
ActiveDrugs activeDrugs,
) async {
// parse response to list of user's diplotypes
final diplotypes =
diplotypesFromHTTPResponse(response).filterValidDiplotypes();
final activeDrugs = activeDrugsFromHTTPResponse(response);
final activeDrugList = activeDrugsFromHTTPResponse(response);

UserData.instance.diplotypes = {for (var d in diplotypes) d.gene: d};
UserData.instance.activeDrugNames = activeDrugs;
await UserData.save();
await activeDrugs.setList(activeDrugList);
// invalidate cached drugs because lookups may have changed and we need to
// refilter the matching guidelines
await CachedDrugs.erase();
Expand Down
8 changes: 2 additions & 6 deletions app/lib/drug_selection/pages/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ part 'cubit.freezed.dart';
class DrugSelectionPageCubit extends Cubit<DrugSelectionPageState> {
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());
if (value) {
await activeDrugs.add(drug.name);
} else {
await activeDrugs.remove(drug.name);
}
await activeDrugs.changeActivity(drug.name, value);
emit(DrugSelectionPageState.stable());
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/lib/login/pages/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import '../models/lab.dart';
part 'cubit.freezed.dart';

class LoginPageCubit extends Cubit<LoginPageState> {
LoginPageCubit() : super(LoginPageState.initial());
LoginPageCubit(this.activeDrugs): super(LoginPageState.initial());

ActiveDrugs activeDrugs;

void revertToInitialState() => emit(LoginPageState.initial());

Expand Down Expand Up @@ -46,7 +48,7 @@ class LoginPageCubit extends Cubit<LoginPageState> {
try {
// get data
await fetchAndSaveDiplotypesAndActiveDrugs(
token, lab.starAllelesUrl.toString());
token, lab.starAllelesUrl.toString(), activeDrugs);
await fetchAndSaveLookups();

await updateCachedDrugs();
Expand Down
63 changes: 33 additions & 30 deletions app/lib/login/pages/login.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:provider/provider.dart';

import '../../../common/module.dart';
import '../../common/widgets/full_width_button.dart';
Expand All @@ -17,40 +18,42 @@ class LoginPage extends HookWidget {
Widget build(BuildContext context) {
final dropdownValue = useState(labs.first.name);

return BlocProvider(
create: (context) => cubit ?? LoginPageCubit(),
child: BlocBuilder<LoginPageCubit, LoginPageState>(
builder: (context, state) {
return unscrollablePageScaffold(
padding: PharMeTheme.largeSpace,
body: Stack(
children: [
Positioned.fill(
child: Align(
alignment: Alignment.center,
child: SvgPicture.asset(
'assets/images/logo.svg',
return Consumer<ActiveDrugs>(
builder: (context, activeDrugs, child) => BlocProvider(
create: (context) => cubit ?? LoginPageCubit(activeDrugs),
child: BlocBuilder<LoginPageCubit, LoginPageState>(
builder: (context, state) {
return unscrollablePageScaffold(
padding: PharMeTheme.largeSpace,
body: Stack(
children: [
Positioned.fill(
child: Align(
alignment: Alignment.center,
child: SvgPicture.asset(
'assets/images/logo.svg',
),
),
),
),
Positioned(
child: Align(
alignment: Alignment.bottomCenter,
child: state.when(
initial: () =>
_buildInitialScreen(context, dropdownValue),
loadingUserData: CircularProgressIndicator.new,
loadedUserData: () => _buildLoadedScreen(context),
error: (message) =>
_buildErrorScreen(context, message),
Positioned(
child: Align(
alignment: Alignment.bottomCenter,
child: state.when(
initial: () =>
_buildInitialScreen(context, dropdownValue),
loadingUserData: CircularProgressIndicator.new,
loadedUserData: () => _buildLoadedScreen(context),
error: (message) =>
_buildErrorScreen(context, message),
),
),
),
),
],
),
);
},
),
],
),
);
},
),
)
);
}

Expand Down

0 comments on commit 4f4ef7e

Please sign in to comment.