diff --git a/lib/views/pages/auth/check_email_page.dart b/lib/views/pages/auth/check_email_page.dart index b1b6424..2d372f6 100644 --- a/lib/views/pages/auth/check_email_page.dart +++ b/lib/views/pages/auth/check_email_page.dart @@ -1,16 +1,29 @@ import 'package:flutter/material.dart'; +import 'package:stacker_news/data/api.dart'; +import 'package:stacker_news/main.dart'; +import 'package:stacker_news/utils.dart'; +import 'package:stacker_news/views/pages/home_page.dart'; import 'package:stacker_news/views/widgets/generic_page_scaffold.dart'; -class CheckEmailPage extends StatelessWidget { +class CheckEmailPage extends StatefulWidget { static const String id = 'check_email'; const CheckEmailPage({super.key}); + @override + State createState() => _CheckEmailPageState(); +} + +class _CheckEmailPageState extends State { + final _busy = ValueNotifier(false); + final _tokenController = TextEditingController(); + @override Widget build(BuildContext context) { return GenericPageScaffold( body: Column( mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, children: [ const Text( 'Check your email', @@ -20,13 +33,63 @@ class CheckEmailPage extends StatelessWidget { ), const SizedBox(height: 8), const Text( - 'A sign in link has been sent to your email address', - style: TextStyle( - fontSize: 16, - ), + 'A sign in link has been sent to your email address.\nYou can click it to open directly in the app or, if it doesn\'t work, you can copy the link and paste below.', + style: TextStyle(fontSize: 16), + textAlign: TextAlign.center, ), const SizedBox(height: 16), Image.asset('assets/hello.gif'), + TextField( + controller: _tokenController, + decoration: const InputDecoration( + labelText: 'Paste Magic Link Here', + hintText: 'https://stacker.news/api/auth/callback/email/...', + ), + ), + const SizedBox(height: 24), + ElevatedButton( + onPressed: () async { + if (_tokenController.text.isEmpty) { + return; + } + + try { + _busy.value = true; + final session = + await locator().login(_tokenController.text); + + if (session != null) { + if (context.mounted) { + Navigator.pushReplacementNamed(context, HomePage.id); + } else { + Utils.showError( + 'Error going to home page. Context is not mounted', + ); + } + } + } catch (e) { + Utils.showError('Error logging in: ${e.toString()}'); + } finally { + _busy.value = false; + } + }, + child: const Text('Login'), + ), + ValueListenableBuilder( + valueListenable: _busy, + builder: (context, busy, child) { + if (!busy) { + return const SizedBox.shrink(); + } + + return Container( + color: Colors.black.withOpacity(0.5), + child: const Center( + child: CircularProgressIndicator(), + ), + ); + }, + ), ], ), ); diff --git a/lib/views/pages/auth/sign_in_page.dart b/lib/views/pages/auth/sign_in_page.dart index 123f569..b8a0b8a 100644 --- a/lib/views/pages/auth/sign_in_page.dart +++ b/lib/views/pages/auth/sign_in_page.dart @@ -1,11 +1,7 @@ -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:stacker_news/data/api.dart'; import 'package:stacker_news/main.dart'; -import 'package:stacker_news/utils.dart'; import 'package:stacker_news/views/pages/auth/check_email_page.dart'; -import 'package:stacker_news/views/pages/home_page.dart'; import 'package:stacker_news/views/widgets/generic_page_scaffold.dart'; class SignInPage extends StatelessWidget { @@ -76,9 +72,7 @@ class _SignInFormState extends State { .requestMagicLink(_emailController.text); _busy.value = false; - if (ret && - (Platform.isAndroid || Platform.isIOS) && - context.mounted) { + if (ret && context.mounted) { Navigator.pushNamed(context, CheckEmailPage.id); } }, @@ -96,45 +90,6 @@ class _SignInFormState extends State { // icon: const Icon(Icons.qr_code), // label: const Text('Read QRCode'), // ), - if (!Platform.isAndroid && !Platform.isIOS) - TextField( - controller: _tokenController, - decoration: const InputDecoration( - labelText: 'Paste Magic Link', - hintText: 'https://stacker.news/api/auth/callback/email/...', - ), - ), - if (!Platform.isAndroid && !Platform.isIOS) - const SizedBox(height: 24), - if (!Platform.isAndroid && !Platform.isIOS) - ElevatedButton( - onPressed: () async { - if (_tokenController.text.isEmpty) { - return; - } - - try { - _busy.value = true; - final session = - await locator().login(_tokenController.text); - - if (session != null) { - if (context.mounted) { - Navigator.pushReplacementNamed(context, HomePage.id); - } else { - Utils.showError( - 'Error going to home page. Context is not mounted', - ); - } - } - } catch (e) { - Utils.showError('Error logging in: ${e.toString()}'); - } finally { - _busy.value = false; - } - }, - child: const Text('Login'), - ), ], ), ValueListenableBuilder(