@@ -50,75 +50,78 @@ class LoginScreenState extends State<LoginScreen> {
50
50
}
51
51
52
52
void isSignedIn () async {
53
- isLoggedIn = await googleSignIn.isSignedIn ();
53
+ this .setState (() {
54
+ isLoading = true ;
55
+ });
56
+
54
57
prefs = await SharedPreferences .getInstance ();
58
+
59
+ isLoggedIn = await googleSignIn.isSignedIn ();
55
60
if (isLoggedIn) {
56
61
Navigator .push (
57
62
context,
58
63
MaterialPageRoute (builder: (context) => MainScreen (currentUserId: prefs.getString ('id' ))),
59
64
);
60
65
}
66
+
67
+ this .setState (() {
68
+ isLoading = false ;
69
+ });
61
70
}
62
71
63
72
Future <Null > handleSignIn () async {
64
73
prefs = await SharedPreferences .getInstance ();
65
74
66
- if (prefs.getString ('id' ) != null ) {
75
+ this .setState (() {
76
+ isLoading = true ;
77
+ });
78
+
79
+ GoogleSignInAccount googleUser = await googleSignIn.signIn ();
80
+ GoogleSignInAuthentication googleAuth = await googleUser.authentication;
81
+ FirebaseUser firebaseUser = await firebaseAuth.signInWithGoogle (
82
+ accessToken: googleAuth.accessToken,
83
+ idToken: googleAuth.idToken,
84
+ );
85
+ if (firebaseUser != null ) {
86
+ // Check is already sign up
87
+ final QuerySnapshot result =
88
+ await Firestore .instance.collection ('users' ).where ('id' , isEqualTo: firebaseUser.uid).getDocuments ();
89
+ final List <DocumentSnapshot > documents = result.documents;
90
+ if (documents.length == 0 ) {
91
+ // Update data to server if new user
92
+ Firestore .instance.collection ('users' ).document (firebaseUser.uid).setData (
93
+ {'nickname' : firebaseUser.displayName, 'photoUrl' : firebaseUser.photoUrl, 'id' : firebaseUser.uid});
94
+
95
+ // Write data to local
96
+ currentUser = firebaseUser;
97
+ await prefs.setString ('id' , currentUser.uid);
98
+ await prefs.setString ('nickname' , currentUser.displayName);
99
+ await prefs.setString ('photoUrl' , currentUser.photoUrl);
100
+ } else {
101
+ // Write data to local
102
+ await prefs.setString ('id' , documents[0 ]['id' ]);
103
+ await prefs.setString ('nickname' , documents[0 ]['nickname' ]);
104
+ await prefs.setString ('photoUrl' , documents[0 ]['photoUrl' ]);
105
+ await prefs.setString ('aboutMe' , documents[0 ]['aboutMe' ]);
106
+ }
107
+ Fluttertoast .showToast (msg: "Sign in success" );
108
+ this .setState (() {
109
+ isLoading = false ;
110
+ });
111
+
67
112
Navigator .push (
68
113
context,
69
- MaterialPageRoute (builder: (context) => MainScreen (currentUserId: prefs.getString ('id' ))),
114
+ MaterialPageRoute (
115
+ builder: (context) =>
116
+ MainScreen (
117
+ currentUserId: firebaseUser.uid,
118
+ )),
70
119
);
71
120
} else {
121
+ Fluttertoast .showToast (msg: "Sign in fail" );
72
122
this .setState (() {
73
- isLoading = true ;
123
+ isLoading = false ;
74
124
});
75
-
76
- GoogleSignInAccount googleUser = await googleSignIn.signIn ();
77
- GoogleSignInAuthentication googleAuth = await googleUser.authentication;
78
- FirebaseUser firebaseUser = await firebaseAuth.signInWithGoogle (
79
- accessToken: googleAuth.accessToken,
80
- idToken: googleAuth.idToken,
81
- );
82
- if (firebaseUser != null ) {
83
- // Check is already sign up
84
- final QuerySnapshot result =
85
- await Firestore .instance.collection ('users' ).where ('id' , isEqualTo: firebaseUser.uid).getDocuments ();
86
- final List <DocumentSnapshot > documents = result.documents;
87
- if (documents.length == 0 ) {
88
- // Update data to server if new user
89
- Firestore .instance.collection ('users' ).document (firebaseUser.uid).setData (
90
- {'nickname' : firebaseUser.displayName, 'photoUrl' : firebaseUser.photoUrl, 'id' : firebaseUser.uid});
91
-
92
- // Write data to local
93
- currentUser = firebaseUser;
94
- await prefs.setString ('id' , currentUser.uid);
95
- await prefs.setString ('nickname' , currentUser.displayName);
96
- await prefs.setString ('photoUrl' , currentUser.photoUrl);
97
- } else {
98
- // Write data to local
99
- await prefs.setString ('id' , documents[0 ]['id' ]);
100
- await prefs.setString ('nickname' , documents[0 ]['nickname' ]);
101
- await prefs.setString ('photoUrl' , documents[0 ]['photoUrl' ]);
102
- await prefs.setString ('aboutMe' , documents[0 ]['aboutMe' ]);
103
- }
104
- Fluttertoast .showToast (msg: "Sign in success" );
105
- this .setState (() {
106
- isLoading = false ;
107
- });
108
-
109
- Navigator .push (
110
- context,
111
- MaterialPageRoute (
112
- builder: (context) => MainScreen (
113
- currentUserId: firebaseUser.uid,
114
- )),
115
- );
116
- } else {
117
- Fluttertoast .showToast (msg: "Sign in fail" );
118
- this .setState (() {
119
- isLoading = false ;
120
- });
121
- }
122
125
}
123
126
}
124
127
@@ -152,13 +155,13 @@ class LoginScreenState extends State<LoginScreen> {
152
155
Positioned (
153
156
child: isLoading
154
157
? Container (
155
- child: Center (
156
- child: CircularProgressIndicator (
157
- valueColor: AlwaysStoppedAnimation <Color >(themeColor),
158
- ),
159
- ),
160
- color: Colors .white.withOpacity (0.8 ),
161
- )
158
+ child: Center (
159
+ child: CircularProgressIndicator (
160
+ valueColor: AlwaysStoppedAnimation <Color >(themeColor),
161
+ ),
162
+ ),
163
+ color: Colors .white.withOpacity (0.8 ),
164
+ )
162
165
: Container (),
163
166
),
164
167
],
0 commit comments