Skip to content

Commit

Permalink
refactor(#661): renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Nov 14, 2024
1 parent 8b869fe commit 160128f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 25 deletions.
22 changes: 12 additions & 10 deletions app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class PharMeApp extends StatelessWidget {

final _appRouter = AppRouter();

Future<void> _setDeepLinkSharePublishUrl(PlatformDeepLink deepLink) async {
final queryParameters = deepLink.uri.queryParameters;
MetaData.instance.deepLinkSharePublishUrl =
queryParameters['provider_url'];
await MetaData.save();
}

@override
Widget build(BuildContext context) {
return ErrorHandler(
Expand All @@ -23,16 +30,11 @@ class PharMeApp extends StatelessWidget {
debugShowCheckedModeBanner: false,
routerConfig: _appRouter.config(
deepLinkBuilder: (deepLink) async {
final queryParameters = deepLink.uri.queryParameters;
final isDeepLinkShareRequest = deepLink.path == '' &&
queryParameters.containsKey('provider_url');
if (isDeepLinkShareRequest) {
MetaData.instance.deepLinkSharePublishUrl =
queryParameters['provider_url'];
await MetaData.save();
if (_appRouter.currentPath != '/') {
return DeepLink.path(_appRouter.currentPath);
}
if (deepLink.path.startsWith('/open_file')) {
await _setDeepLinkSharePublishUrl(deepLink);
}
if (_appRouter.currentPath != '/') {
return DeepLink.path(_appRouter.currentPath);
}
// default route
return getInitialRoute();
Expand Down
3 changes: 3 additions & 0 deletions app/lib/common/models/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class MetaData {

@HiveField(6)
String? deepLinkSharePublishUrl;

@HiveField(7)
bool? awaitingDeepLinkSharePublishUrl;
}

/// Initializes the user's metadata by registering all necessary adapters and
Expand Down
13 changes: 7 additions & 6 deletions app/lib/login/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ class LoginCubit extends Cubit<LoginState> {
// genomic data from it's endpoint.
Future<void> signInAndLoadUserData(BuildContext context, Lab lab) async {
emit(LoginState.loadingUserData(
lab.authLoadingMessage(),
cancelable: lab.cancelAuthInApp,
lab.preparationLoadingMessage(),
cancelable: lab.cancelPreparationInApp,
));
try {
await lab.authenticate();
await lab.prepareDataLoad();
} on LabProcessCanceled {
revertToInitialState();
return;
} on LabAuthenticationError {
} on Exception {
emit(LoginState.error(
// ignore: use_build_context_synchronously
context.l10n.err_could_not_retrieve_access_token,
));
return;
}

if (lab.authenticationWasCanceled) {
lab.authenticationWasCanceled = false;
if (lab.preparationWasCanceled) {
lab.preparationWasCanceled = false;
MetaData.instance.awaitingDeepLinkSharePublishUrl = false;
return;
}

Expand Down
13 changes: 10 additions & 3 deletions app/lib/login/models/deep_link_share_flow_lab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class DeepLinkShareFlowLab extends Lab {

@override
// ignore: overridden_fields
bool cancelAuthInApp = true;
bool cancelPreparationInApp= true;

@override
String? authLoadingMessage() =>
String? preparationLoadingMessage() =>
'Please open the $shareAppName and share your data with PharMe';

Future<void> _waitForDeepLinkSharePublishUrl() async {
Expand All @@ -28,13 +28,20 @@ class DeepLinkShareFlowLab extends Lab {
}
}

Future<void> _setAwaitingDeepLinkSharePublishUrl(bool newValue) async {
MetaData.instance.awaitingDeepLinkSharePublishUrl = newValue;
await MetaData.save();
}

@override
Future<void> authenticate() async {
Future<void> prepareDataLoad() async {
await _setAwaitingDeepLinkSharePublishUrl(true);
await _waitForDeepLinkSharePublishUrl();
}

@override
Future<(List<LabResult>, List<String>)> loadData() async {
await _setAwaitingDeepLinkSharePublishUrl(false);
publishUrl = Uri.parse(
MetaData.instance.deepLinkSharePublishUrl!,
);
Expand Down
43 changes: 39 additions & 4 deletions app/lib/login/models/lab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:http/http.dart' as http;

import '../../app.dart';
import '../../common/module.dart';

class LabProcessCanceled implements Exception {
Expand All @@ -18,12 +19,14 @@ class Lab {
});

String name;
bool cancelAuthInApp = false;
bool authenticationWasCanceled = false;
bool cancelPreparationInApp = false;
bool preparationWasCanceled = false;

String? authLoadingMessage() => null;
String? preparationLoadingMessage() => null;

String? preparationErrorMessage(BuildContext context) => null;

Future<void> authenticate() async {}
Future<void> prepareDataLoad() async {}
Future<(List<LabResult>, List<String>)> loadData() async {
throw UnimplementedError();
}
Expand All @@ -33,6 +36,38 @@ class Lab {
{
Map<String,String>? headers,
}) async {
final awaitingOpenFile =
MetaData.instance.awaitingDeepLinkSharePublishUrl ?? false;
final loggedIn = MetaData.instance.isLoggedIn ?? false;
final needsConfirmation = !awaitingOpenFile || loggedIn;
final context = PharMeApp.navigatorKey.currentContext;
if (context == null && needsConfirmation) throw Exception();
if (needsConfirmation) {
final dialogTitle = loggedIn
? 'Confirm data overwrite'
: 'Received data';
final dialogText = 'PharMe received data from another app. ${
loggedIn
? 'Overwrite existing data?'
: 'Continue if you want to import the data.'
}';
await showAdaptiveDialog(
context: PharMeApp.navigatorKey.currentContext!,
builder: (context) => DialogWrapper(
title: dialogTitle,
content: DialogContentText(dialogText),
actions: [
DialogAction(
onPressed: () => Navigator.pop(context),
text: context.l10n.action_cancel,
),
DialogAction(
onPressed: () => throw LabProcessCanceled(),
text: context.l10n.action_understood,
),
],
),);
}
final response = await http.get(dataUrl, headers: headers);
if (response.statusCode != 200) throw Exception();
final json = jsonDecode(response.body) as Map<String, dynamic>;
Expand Down
6 changes: 5 additions & 1 deletion app/lib/login/models/oauth_authorization_code_flow_lab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class OAuthAuthorizationCodeFlowLab extends Lab {
late String? token;

@override
Future<void> authenticate() async {
String? preparationErrorMessage(BuildContext context) =>
context.l10n.err_fetch_user_data_failed;

@override
Future<void> prepareDataLoad() async {
const clientId = 'pharme-app';
const callbackUrlScheme = 'localhost';
final url = authUrl.replace(queryParameters: {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/login/pages/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class LoginPage extends HookWidget {
context.l10n.action_cancel,
() {
final selectedLab = _getSelectedLab(dropdownValue);
selectedLab.authenticationWasCanceled = true;
selectedLab.preparationWasCanceled = true;
context
.read<LoginCubit>()
.revertToInitialState();
Expand Down

0 comments on commit 160128f

Please sign in to comment.