Move the logic of deeplink initialization to the home widget

This commit is contained in:
Vishnu Mohandas 2020-08-26 05:56:07 +05:30
parent fd074e96ee
commit da1d4830e2
3 changed files with 47 additions and 34 deletions

View file

@ -2,7 +2,6 @@ import 'dart:async';
import 'package:computer/computer.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photos/core/constants.dart';
import 'package:photos/core/configuration.dart';
@ -11,11 +10,9 @@ import 'package:photos/folder_service.dart';
import 'package:photos/memories_service.dart';
import 'package:photos/photo_sync_manager.dart';
import 'package:photos/ui/home_widget.dart';
import 'package:photos/user_authenticator.dart';
import 'package:sentry/sentry.dart';
import 'package:super_logging/super_logging.dart';
import 'package:logging/logging.dart';
import 'package:uni_links/uni_links.dart';
final _logger = Logger("main");
@ -37,7 +34,6 @@ void _main() async {
await PhotoSyncManager.instance.init();
await MemoriesService.instance.init();
await FavoriteFilesRepository.instance.init();
await initDeepLinks();
_sync();
final SentryClient sentry = new SentryClient(dsn: SENTRY_DSN);
@ -77,34 +73,6 @@ void _sendErrorToSentry(SentryClient sentry, Object error, StackTrace stack) {
}
}
Future<void> initDeepLinks() async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
String initialLink = await getInitialLink();
// Parse the link and warn the user, if it is not correct,
// but keep in mind it could be `null`.
if (initialLink != null) {
_logger.info("Initial link received: " + initialLink);
} else {
_logger.info("No initial link received.");
}
} on PlatformException {
// Handle exception by warning the user their action did not succeed
// return?
_logger.severe("PlatformException thrown while getting initial link");
}
// Attach a listener to the stream
getLinksStream().listen((String link) {
_logger.info("Link received: " + link);
final ott = Uri.parse(link).queryParameters["ott"];
_logger.info("Ott: " + ott);
// UserAuthenticator.instance.getCredentials(context, ott);
}, onError: (err) {
_logger.severe(err);
});
}
class MyApp extends StatelessWidget with WidgetsBindingObserver {
final _title = 'ente';
@override

View file

@ -2,7 +2,9 @@ import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/local_photos_updated_event.dart';
import 'package:photos/models/filters/important_items_filter.dart';
@ -18,9 +20,11 @@ import 'package:photos/ui/loading_widget.dart';
import 'package:photos/ui/memories_widget.dart';
import 'package:photos/ui/remote_folder_gallery_widget.dart';
import 'package:photos/ui/search_page.dart';
import 'package:photos/user_authenticator.dart';
import 'package:photos/utils/logging_util.dart';
import 'package:shake/shake.dart';
import 'package:logging/logging.dart';
import 'package:uni_links/uni_links.dart';
class HomeWidget extends StatefulWidget {
final String title;
@ -56,6 +60,7 @@ class _HomeWidgetState extends State<HomeWidget> {
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
setState(() {});
});
_initDeepLinks();
super.initState();
}
@ -100,6 +105,44 @@ class _HomeWidgetState extends State<HomeWidget> {
);
}
Future<bool> _initDeepLinks() async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
String initialLink = await getInitialLink();
// Parse the link and warn the user, if it is not correct,
// but keep in mind it could be `null`.
if (initialLink != null) {
_logger.info("Initial link received: " + initialLink);
_getCredentials(context, initialLink);
return true;
} else {
_logger.info("No initial link received.");
}
} on PlatformException {
// Handle exception by warning the user their action did not succeed
// return?
_logger.severe("PlatformException thrown while getting initial link");
}
// Attach a listener to the stream
getLinksStream().listen((String link) {
_logger.info("Link received: " + link);
_getCredentials(context, link);
}, onError: (err) {
_logger.severe(err);
});
return false;
}
void _getCredentials(BuildContext context, String link) {
if (Configuration.instance.hasConfiguredAccount()) {
return;
}
final ott = Uri.parse(link).queryParameters["ott"];
_logger.info("Ott: " + ott);
UserAuthenticator.instance.getCredentials(context, ott);
}
Widget _getMainGalleryWidget() {
return FutureBuilder(
future: FileRepository.instance.loadFiles().then((files) {

View file

@ -61,9 +61,9 @@ class UserAuthenticator {
showGenericErrorDialog(context);
}).then((response) async {
await dialog.hide();
if (response.statusCode == 200) {
if (response != null && response.statusCode == 200) {
_saveConfiguration(response);
Navigator.of(context).pop();
Navigator.of(context).popUntil((route) => route.isFirst);
} else {
showErrorDialog(
context, "Oops.", "Verification failed, please try again.");
@ -71,6 +71,7 @@ class UserAuthenticator {
});
}
@deprecated
Future<bool> login(String username, String password) {
return _dio.post(
Configuration.instance.getHttpEndpoint() + "/users/authenticate",
@ -91,6 +92,7 @@ class UserAuthenticator {
});
}
@deprecated
Future<bool> create(String username, String password) {
return _dio
.post(Configuration.instance.getHttpEndpoint() + "/users", data: {