Skip to content

Commit

Permalink
Merge pull request #614 from hpi-dhc/issue/462-load-active-medications
Browse files Browse the repository at this point in the history
Issue/462 load active medications
  • Loading branch information
tamslo committed Jun 19, 2023
2 parents 5370aa5 + 4f9754b commit 8295c3a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.10.0
COCOAPODS: 1.12.1
13 changes: 13 additions & 0 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart';

import '../../../search/module.dart';
import '../../utilities/hive_utils.dart';
Expand Down Expand Up @@ -83,3 +86,13 @@ Future<void> initUserData() async {
final userData = Hive.box<UserData>(_boxName);
UserData._instance = userData.get('data') ?? UserData();
}

// assumes http reponse from lab server
List<String> activeDrugsFromHTTPResponse(Response resp) {
var activeDrugs = <String>[];
final json = jsonDecode(resp.body) as Map<String, dynamic>;
if (json.containsKey('medications')) {
activeDrugs = List<String>.from(json['medications']);
}
return activeDrugs;
}
9 changes: 6 additions & 3 deletions app/lib/common/utilities/genome_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import '../constants.dart';
import '../models/drug/cached_drugs.dart';
import '../models/module.dart';

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

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

UserData.instance.diplotypes = {for (var d in diplotypes) d.gene: d};
UserData.instance.activeDrugNames = activeDrugs;
await UserData.save();
// invalidate cached drugs because lookups may have changed and we need to
// refilter the matching guidelines
Expand Down
3 changes: 2 additions & 1 deletion app/lib/login/pages/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class LoginPageCubit extends Cubit<LoginPageState> {

try {
// get data
await fetchAndSaveDiplotypes(token, lab.starAllelesUrl.toString());
await fetchAndSaveDiplotypesAndActiveDrugs(
token, lab.starAllelesUrl.toString());
await fetchAndSaveLookups();

// login + fetching of data successful
Expand Down

0 comments on commit 8295c3a

Please sign in to comment.