From da1d4830e298449e3023d7ad75aafc9bc0ba21a3 Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Wed, 26 Aug 2020 05:56:07 +0530 Subject: [PATCH] Move the logic of deeplink initialization to the home widget --- lib/main.dart | 32 --------------------------- lib/ui/home_widget.dart | 43 +++++++++++++++++++++++++++++++++++++ lib/user_authenticator.dart | 6 ++++-- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 611872547..cad4fd588 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 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 diff --git a/lib/ui/home_widget.dart b/lib/ui/home_widget.dart index d9d4cd048..3cb0ef950 100644 --- a/lib/ui/home_widget.dart +++ b/lib/ui/home_widget.dart @@ -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 { Bus.instance.on().listen((event) { setState(() {}); }); + _initDeepLinks(); super.initState(); } @@ -100,6 +105,44 @@ class _HomeWidgetState extends State { ); } + Future _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) { diff --git a/lib/user_authenticator.dart b/lib/user_authenticator.dart index 6977e163a..5d72dcb36 100644 --- a/lib/user_authenticator.dart +++ b/lib/user_authenticator.dart @@ -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 login(String username, String password) { return _dio.post( Configuration.instance.getHttpEndpoint() + "/users/authenticate", @@ -91,6 +92,7 @@ class UserAuthenticator { }); } + @deprecated Future create(String username, String password) { return _dio .post(Configuration.instance.getHttpEndpoint() + "/users", data: {