Skip to content

Commit

Permalink
refactor(app): rename CachedDrugs to DrugsWithGuidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Sep 23, 2024
1 parent 5070948 commit aaa723d
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 43 deletions.
10 changes: 5 additions & 5 deletions app/integration_test/fixtures/set_app_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ void setUserDataForGuideline(Guideline guideline) {
}
}

void addDrugToCachedDrugs(Drug drug) {
CachedDrugs.instance.drugs = CachedDrugs.instance.drugs ?? [];
final drugIsPresent = CachedDrugs.instance.drugs!.any(
void addDrugToDrugsWithGuidelines(Drug drug) {
DrugsWithGuidelines.instance.drugs = DrugsWithGuidelines.instance.drugs ?? [];
final drugIsPresent = DrugsWithGuidelines.instance.drugs!.any(
(presentDrug) => presentDrug.name == drug.name,
);
if (drugIsPresent) return;
CachedDrugs.instance.drugs!.add(drug);
DrugsWithGuidelines.instance.drugs!.add(drug);
}

void setAppData({required Drug drug, required Guideline guideline}) {
addDrugToCachedDrugs(drug);
addDrugToDrugsWithGuidelines(drug);
initializeGenotypeResultKeys().values.forEach(setGenotypeResult);
setUserDataForGuideline(guideline);
}
4 changes: 2 additions & 2 deletions app/integration_test/main_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ void main() {

UserData.instance.genotypeResults = {};

CachedDrugs.instance.version = 1;
CachedDrugs.instance.drugs = List.empty();
DrugsWithGuidelines.instance.version = 1;
DrugsWithGuidelines.instance.drugs = List.empty();

group('test the main page', () {
testWidgets('test that tabs change pages', (tester) async {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/common/models/drug/drug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ String _getDrugWithBrandNames(
String drugName,
{ required bool capitalize }
) {
final drug = CachedDrugs.instance.drugs?.firstOrNullWhere(
final drug = DrugsWithGuidelines.instance.drugs?.firstOrNullWhere(
(drug) => drug.name == drugName
);
final displayedDrugName = capitalize ? drugName.capitalize() : drugName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import 'package:hive/hive.dart';
import '../../utilities/hive_utils.dart';
import '../module.dart';

part 'cached_drugs.g.dart';
part 'drugs_with_guidelines.g.dart';

const _boxName = 'cachedDrugs';
const _boxName = 'DrugsWithGuidelines';

@HiveType(typeId: 5)
class CachedDrugs {
factory CachedDrugs() => _instance;
class DrugsWithGuidelines {
factory DrugsWithGuidelines() => _instance;

// private constructor
CachedDrugs._();
DrugsWithGuidelines._();

static CachedDrugs _instance = CachedDrugs._();
static CachedDrugs get instance => _instance;
static DrugsWithGuidelines _instance = DrugsWithGuidelines._();
static DrugsWithGuidelines get instance => _instance;

/// Writes the current instance to local storage
static Future<void> save() async =>
Hive.box<CachedDrugs>(_boxName).put('data', _instance);
Hive.box<DrugsWithGuidelines>(_boxName).put('data', _instance);

static Future<void> erase() async {
_instance = CachedDrugs._();
await CachedDrugs.save();
_instance = DrugsWithGuidelines._();
await DrugsWithGuidelines.save();
}

@HiveField(0)
Expand All @@ -33,9 +33,9 @@ class CachedDrugs {
List<Drug>? drugs;
}

Future<void> initCachedDrugs() async {
Future<void> initDrugsWithGuidelines() async {
try {
Hive.registerAdapter(CachedDrugsAdapter());
Hive.registerAdapter(DrugsWithGuidelinesAdapter());
Hive.registerAdapter(DrugAdapter());
Hive.registerAdapter(DrugAnnotationsAdapter());
Hive.registerAdapter(GuidelineAdapter());
Expand All @@ -49,10 +49,11 @@ Future<void> initCachedDrugs() async {
// cached drugs have exactly the matching guidelines saved, i.e. they can be
// used to figure out the user's gene lookupkeys, i.e. we have to encrypt.
final encryptionKey = await retrieveExistingOrGenerateKey();
await Hive.openBox<CachedDrugs>(
await Hive.openBox<DrugsWithGuidelines>(
_boxName,
encryptionCipher: HiveAesCipher(encryptionKey),
);
final cachedDrugs = Hive.box<CachedDrugs>(_boxName);
CachedDrugs._instance = cachedDrugs.get('data') ?? CachedDrugs();
final drugsWithGuidelines = Hive.box<DrugsWithGuidelines>(_boxName);
DrugsWithGuidelines._instance =
drugsWithGuidelines.get('data') ?? DrugsWithGuidelines();
}
2 changes: 1 addition & 1 deletion app/lib/common/models/module.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export 'anni_response.dart';
export 'drug/cached_drugs.dart';
export 'drug/drug.dart';
export 'drug/drug_inhibitors.dart';
export 'drug/drugs_with_guidelines.dart';
export 'drug/guideline.dart';
export 'drug/warning_level.dart';
export 'metadata.dart';
Expand Down
4 changes: 2 additions & 2 deletions app/lib/common/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Future<void> initServices() async {

await initMetaData();
await initUserData();
await initCachedDrugs();
await initDrugsWithGuidelines();
WidgetsFlutterBinding.ensureInitialized();
}

Future<void> cleanupServices() async {
await MetaData.save();
await UserData.save();

await CachedDrugs.save();
await DrugsWithGuidelines.save();
}
14 changes: 7 additions & 7 deletions app/lib/common/utilities/drug_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import 'package:http/http.dart';
import '../../app.dart';
import '../module.dart';

Future<void> updateCachedDrugs() async {
Future<void> updateDrugsWithGuidelines() async {
final isOnline = await hasConnectionTo(anniUrl().host);
if (!isOnline && CachedDrugs.instance.version == null) {
if (!isOnline && DrugsWithGuidelines.instance.version == null) {
throw Exception();
}
final versionResponse = await get(anniUrl('version'));
if (versionResponse.statusCode != 200) throw Exception();
final version = AnniVersionResponse.fromJson(jsonDecode(versionResponse.body))
.data
.version;
if (version == CachedDrugs.instance.version) return;
if (version == DrugsWithGuidelines.instance.version) return;
final dataResponse = await get(anniUrl('data'));
if (dataResponse.statusCode != 200) throw Exception();
final data = AnniDataResponse.fromJson(jsonDecode(dataResponse.body)).data;
final previousVersion = CachedDrugs.instance.version;
CachedDrugs.instance.drugs = data.drugs;
CachedDrugs.instance.version = data.version;
await CachedDrugs.save();
final previousVersion = DrugsWithGuidelines.instance.version;
DrugsWithGuidelines.instance.drugs = data.drugs;
DrugsWithGuidelines.instance.version = data.version;
await DrugsWithGuidelines.save();
await updateGenotypeResults();
if (previousVersion != null) {
final context = PharMeApp.navigatorKey.currentContext;
Expand Down
9 changes: 5 additions & 4 deletions app/lib/common/utilities/genome_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Future<void> _saveDiplotypeAndActiveDrugsResponse(
await activeDrugs.setList(activeDrugList);
// invalidate cached drugs because lookups may have changed and we need to
// refilter the matching guidelines
await CachedDrugs.erase();
await DrugsWithGuidelines.erase();
}

String formatLookupMapKey(String gene, String variant) {
Expand All @@ -44,7 +44,7 @@ String formatLookupMapKey(String gene, String variant) {
@visibleForTesting
Map<String, GenotypeResult> initializeGenotypeResultKeys() {
final emptyGenotypeResults = <String, GenotypeResult>{};
for (final drug in CachedDrugs.instance.drugs ?? <Drug>[]) {
for (final drug in DrugsWithGuidelines.instance.drugs ?? <Drug>[]) {
for (final guideline in drug.guidelines) {
for (final gene in guideline.lookupkey.keys) {
for (final variant in guideline.lookupkey[gene]!) {
Expand Down Expand Up @@ -110,9 +110,10 @@ bool shouldUpdateGenotypeResults() {
DateTime.now().difference(MetaData.instance.lookupsLastFetchDate!) >
cpicMaxCacheTime;
final labDataPresent = UserData.instance.labData?.isNotEmpty ?? false;
final cachedDrugsPresent = CachedDrugs.instance.drugs?.isNotEmpty ?? false;
final drugsWithGuidelinesPresent =
DrugsWithGuidelines.instance.drugs?.isNotEmpty ?? false;
return labDataPresent &&
cachedDrugsPresent &&
drugsWithGuidelinesPresent &&
(genotypeResultsMissing || lookupsAreOutdated);
}

Expand Down
4 changes: 2 additions & 2 deletions app/lib/common/widgets/drug_list/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DrugListCubit extends Cubit<DrugListState> {
FilterState.initial();

if (useCache) {
final drugs = CachedDrugs.instance.drugs;
final drugs = DrugsWithGuidelines.instance.drugs;
if (drugs != null) {
emit(DrugListState.loaded(drugs, filter));
return;
Expand All @@ -62,7 +62,7 @@ class DrugListCubit extends Cubit<DrugListState> {

emit(DrugListState.loading());
try {
await updateCachedDrugs();
await updateDrugsWithGuidelines();
await loadDrugs(updateIfNull: false, filter: filter);
} catch (error) {
emit(DrugListState.error());
Expand Down
2 changes: 1 addition & 1 deletion app/lib/drug_selection/pages/drug_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class DrugSelectionPage extends HookWidget {
}

Widget _buildDrugList(BuildContext context, DrugSelectionState state) {
if (CachedDrugs.instance.drugs!.isEmpty) {
if (DrugsWithGuidelines.instance.drugs!.isEmpty) {
return Column(
children: [
Text(
Expand Down
2 changes: 1 addition & 1 deletion app/lib/login/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class LoginCubit extends Cubit<LoginState> {
// ignore: use_build_context_synchronously
emit(LoginState.loadingUserData(context.l10n.auth_updating_data));
}
await updateCachedDrugs();
await updateDrugsWithGuidelines();

// login + fetching of data successful
MetaData.instance.isLoggedIn = true;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/more/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Future<void> deleteAllAppData() async {
await _deleteAppDir();
await UserData.erase();
await MetaData.erase();
await CachedDrugs.erase();
await DrugsWithGuidelines.erase();
}

// The folders themself cannot be deleted on iOS, therefore delete all content
Expand Down
2 changes: 1 addition & 1 deletion app/lib/report/pages/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ReportPage extends StatelessWidget {
final warningLevelCounts = <String, WarningLevelCounts>{};
for (final genotypeResult in userGenotypes) {
warningLevelCounts[genotypeResult.key.value] = {};
final affectedDrugs = CachedDrugs.instance.drugs?.filter(
final affectedDrugs = DrugsWithGuidelines.instance.drugs?.filter(
(drug) => drug.guidelineGenotypes.contains(genotypeResult.key.value)
) ?? [];
for (final warningLevel in WarningLevel.values) {
Expand Down

0 comments on commit aaa723d

Please sign in to comment.