From 5bb492ce336117a43c821768defa7494d52c4948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20=E2=80=A2=20Developer?= Date: Sun, 16 Jul 2023 14:28:21 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20ambigous=20statement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/lib/common/settings.dart | 1 - client/lib/common/splash.dart | 20 +++++++---- client/lib/state/app.state.dart | 10 +++++- client/lib/state/auth.state.dart | 58 +++++++++++++++++++++++++------- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/client/lib/common/settings.dart b/client/lib/common/settings.dart index 0e0db2a..fd3951c 100644 --- a/client/lib/common/settings.dart +++ b/client/lib/common/settings.dart @@ -220,7 +220,6 @@ class _SettingsPageState extends State { onTap: () { state.logoutCallback(); Navigator.pop(context); - Navigator.pop(context); }, child: ClipRRect( borderRadius: BorderRadius.circular(10), diff --git a/client/lib/common/splash.dart b/client/lib/common/splash.dart index da27ffd..72761b1 100644 --- a/client/lib/common/splash.dart +++ b/client/lib/common/splash.dart @@ -2,14 +2,14 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:threads/auth/signup/name.dart'; import 'package:threads/helper/enum.dart'; -import 'package:threads/state/auth.state.dart'; import 'package:threads/pages/home.dart'; +import 'package:threads/state/auth.state.dart'; class SplashPage extends StatefulWidget { - const SplashPage({super.key}); + const SplashPage({Key? key}) : super(key: key); @override - State createState() => _SplashPageState(); + _SplashPageState createState() => _SplashPageState(); } class _SplashPageState extends State { @@ -23,6 +23,8 @@ class _SplashPageState extends State { bool isAppUpdated = true; + /// Check if current app is updated app or not + /// If app is not updated then redirect user to update app screen void timer() async { if (isAppUpdated) { Future.delayed(const Duration(seconds: 1)).then((_) { @@ -32,14 +34,20 @@ class _SplashPageState extends State { } } + Widget _body() { + return Container(); + } + @override Widget build(BuildContext context) { var state = Provider.of(context); return Scaffold( backgroundColor: Colors.black, - body: state.authStatus == AuthStatus.NOT_LOGGED_IN - ? const NamePage() - : HomePage(), + body: state.authStatus == AuthStatus.NOT_DETERMINED + ? _body() + : state.authStatus == AuthStatus.NOT_LOGGED_IN + ? const NamePage() + : const HomePage(), ); } } diff --git a/client/lib/state/app.state.dart b/client/lib/state/app.state.dart index 4ebaae9..c7d43a0 100644 --- a/client/lib/state/app.state.dart +++ b/client/lib/state/app.state.dart @@ -1,5 +1,13 @@ import 'package:flutter/material.dart'; class AppStates extends ChangeNotifier { - notifyListeners(); + bool _isBusy = false; + bool get isbusy => _isBusy; + + set isBusy(bool value) { + if (value != _isBusy) { + _isBusy = value; + notifyListeners(); + } + } } diff --git a/client/lib/state/auth.state.dart b/client/lib/state/auth.state.dart index 2a951bf..5ac097b 100644 --- a/client/lib/state/auth.state.dart +++ b/client/lib/state/auth.state.dart @@ -10,7 +10,7 @@ import 'package:threads/helper/shared_prefrence_helper.dart'; import 'package:threads/helper/utility.dart'; import 'package:threads/model/user.module.dart'; import 'package:threads/state/app.state.dart'; -import '../common/locator.dart'; +import 'package:threads/common/locator.dart'; import 'package:path/path.dart' as path; class AuthState extends AppStates { @@ -35,9 +35,6 @@ class AuthState extends AppStates { user = null; _profileQuery!.onValue.drain(); _profileQuery = null; - if (isSignInWithGoogle) { - isSignInWithGoogle = false; - } _firebaseAuth.signOut(); notifyListeners(); await getIt().clearPreferenceValues(); @@ -47,10 +44,35 @@ class AuthState extends AppStates { try { if (_profileQuery == null) { _profileQuery = kDatabase.child("profile").child(user!.uid); + _profileQuery!.onValue.listen(_onProfileChanged); _profileQuery!.onChildChanged.listen(_onProfileUpdated); } + } catch (error) {} + } + + Future signIn(String email, String password, BuildContext context, + {required GlobalKey scaffoldKey}) async { + try { + isBusy = true; + var result = await _firebaseAuth.signInWithEmailAndPassword( + email: email, password: password); + user = result.user; + userId = user!.uid; + return user!.uid; + } on FirebaseException catch (error) { + if (error.code == 'Email Adress Not found') { + Utility.customSnackBar(scaffoldKey, 'User not found', context); + } else { + Utility.customSnackBar( + scaffoldKey, error.message ?? 'Something went wrong', context); + } + return null; } catch (error) { - print(error); + Utility.customSnackBar(scaffoldKey, error.toString(), context); + + return null; + } finally { + isBusy = false; } } @@ -58,6 +80,7 @@ class AuthState extends AppStates { {required GlobalKey scaffoldKey, required String password}) async { try { + isBusy = true; var result = await _firebaseAuth.createUserWithEmailAndPassword( email: userModel.email!, password: password, @@ -76,6 +99,7 @@ class AuthState extends AppStates { createUser(_userModel!, newUser: true); return user!.uid; } catch (error) { + isBusy = false; Utility.customSnackBar(scaffoldKey, error.toString(), context); return null; } @@ -86,16 +110,16 @@ class AuthState extends AppStates { user.userName = Utility.getUserName(id: user.userId!, name: user.displayName!); kAnalytics.logEvent(name: 'create_newUser'); - - user.createAt = DateTime.now().toUtc().toString(); } kDatabase.child('profile').child(user.userId!).set(user.toJson()); _userModel = user; + isBusy = false; } Future getCurrentUser() async { try { + isBusy = true; user = _firebaseAuth.currentUser; if (user != null) { await getProfileUser(); @@ -104,8 +128,10 @@ class AuthState extends AppStates { } else { authStatus = AuthStatus.NOT_LOGGED_IN; } + isBusy = false; return user; } catch (error) { + isBusy = false; authStatus = AuthStatus.NOT_LOGGED_IN; return null; } @@ -131,15 +157,12 @@ class AuthState extends AppStates { createUser(_userModel!); } } - } catch (error) { - print(error); - } + } catch (error) {} } Future _uploadFileToStorage(File file, path) async { var task = _firebaseStorage.ref().child(path); var status = await task.putFile(file); - print(status.state.name); return await task.getDownloadURL(); } @@ -177,9 +200,20 @@ class AuthState extends AppStates { } } } + isBusy = false; }); } catch (error) { - print(error); + isBusy = false; + } + } + + void _onProfileChanged(DatabaseEvent event) { + final val = event.snapshot.value; + if (val is Map) { + final updatedUser = UserModel.fromJson(val); + _userModel = updatedUser; + getIt().saveUserProfile(_userModel!); + notifyListeners(); } }