diff --git a/lib/core/common/loader.dart b/lib/core/common/loader.dart new file mode 100644 index 0000000..03db677 --- /dev/null +++ b/lib/core/common/loader.dart @@ -0,0 +1,12 @@ +import 'package:flutter/material.dart'; + +class Loader extends StatelessWidget { + const Loader({super.key}); + + @override + Widget build(BuildContext context) { + return const Center( + child: CircularProgressIndicator(), + ); + } +} \ No newline at end of file diff --git a/lib/core/common/sign_in_button.dart b/lib/core/common/sign_in_button.dart index 08dd318..d58bcf8 100644 --- a/lib/core/common/sign_in_button.dart +++ b/lib/core/common/sign_in_button.dart @@ -8,7 +8,7 @@ class SignInButton extends ConsumerWidget { const SignInButton({super.key}); void signInWithGoogle(BuildContext context, WidgetRef ref) { - ref.read(authControllerProvider).signInWithGoogle(context); + ref.read(authControllerProvider.notifier).signInWithGoogle(context); } @override diff --git a/lib/features/auth/controller/auth_controller.dart b/lib/features/auth/controller/auth_controller.dart index ccd9d05..2d33d16 100644 --- a/lib/features/auth/controller/auth_controller.dart +++ b/lib/features/auth/controller/auth_controller.dart @@ -2,18 +2,31 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:reddit_tutorial/core/utils.dart'; +import '../../../models/user_model.dart'; import '../repository/auth_repository.dart'; -final authControllerProvider = Provider( - (ref) => AuthController(authRepository: ref.read(authRepositoryProvider))); +final userProvider = StateProvider((ref) => null); -class AuthController { +final authControllerProvider = StateNotifierProvider( + (ref) => AuthController( + authRepository: ref.watch(authRepositoryProvider), ref: ref)); + +class AuthController extends StateNotifier { final AuthRepository _authRepository; - AuthController({required AuthRepository authRepository}) - : _authRepository = authRepository; + final Ref _ref; + AuthController({required AuthRepository authRepository, required Ref ref}) + : _authRepository = authRepository, + _ref = ref, + super(false); //loading void signInWithGoogle(BuildContext context) async { + state = true; final user = await _authRepository.signInWithGoogle(); - user.fold((l) => showSnackBar(context, l.message), (r) => null); + state = false; + user.fold( + (l) => showSnackBar(context, l.message), + (userModel) => + _ref.read(userProvider.notifier).update((state) => userModel), + ); } } diff --git a/lib/features/auth/screens/login_screen.dart b/lib/features/auth/screens/login_screen.dart index 702d79f..c875054 100644 --- a/lib/features/auth/screens/login_screen.dart +++ b/lib/features/auth/screens/login_screen.dart @@ -1,13 +1,17 @@ import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:reddit_tutorial/core/common/sign_in_button.dart'; +import 'package:reddit_tutorial/features/auth/controller/auth_controller.dart'; +import '../../../core/common/loader.dart'; import '../../../core/constants/constants.dart'; -class LoginScreen extends StatelessWidget { +class LoginScreen extends ConsumerWidget { const LoginScreen({super.key}); @override - Widget build(BuildContext context) { + Widget build(BuildContext context, WidgetRef ref) { + final isLoading = ref.watch(authControllerProvider); return Scaffold( appBar: AppBar( title: Image.asset( @@ -16,26 +20,30 @@ class LoginScreen extends StatelessWidget { ), actions: [TextButton(onPressed: () {}, child: const Text('Skip'))], ), - body: Column( - children: [ - const SizedBox(height: 30), - const Text( - 'Dive into anything', - style: TextStyle( - fontSize: 24, fontWeight: FontWeight.bold, letterSpacing: 0.5), - ), - const SizedBox(height: 30), - Padding( - padding: const EdgeInsets.all(8.0), - child: Image.asset( - Constants.logoEmotePath, - height: 250, + body: isLoading + ? const Loader() + : Column( + children: [ + const SizedBox(height: 30), + const Text( + 'Dive into anything', + style: TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + letterSpacing: 0.5), + ), + const SizedBox(height: 30), + Padding( + padding: const EdgeInsets.all(8.0), + child: Image.asset( + Constants.logoEmotePath, + height: 250, + ), + ), + const SizedBox(height: 30), + const SignInButton(), + ], ), - ), - const SizedBox(height: 30), - const SignInButton(), - ], - ), ); } }