1
1
package com.google.samples.quickstart.canonical
2
2
3
- import android.content.Intent
4
3
import androidx.appcompat.app.AppCompatActivity
5
4
import android.os.Bundle
6
5
import android.util.Log
6
+ import androidx.constraintlayout.widget.ConstraintLayout
7
7
import androidx.fragment.app.FragmentTransaction
8
+ import androidx.lifecycle.ViewModelProviders
8
9
import com.google.android.gms.auth.api.signin.GoogleSignIn
9
- import com.google.android.gms.auth.api.signin.GoogleSignInClient
10
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions
11
- import com.google.android.gms.common.SignInButton
12
- import com.google.android.gms.common.api.ApiException
13
10
import com.google.android.material.bottomnavigation.BottomNavigationView
14
- import com.google.firebase.auth.FirebaseAuth
15
- import com.google.firebase.auth.GoogleAuthProvider
16
11
17
12
class MainActivity : AppCompatActivity () {
18
- lateinit var googleSignInClient: GoogleSignInClient
19
- private lateinit var auth: FirebaseAuth
20
13
21
- private fun googleSignInInit () {
22
- val gso = GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
23
- .requestIdToken(getString(R .string.default_web_client_id))
24
- .requestEmail()
25
- .build()
26
-
27
- googleSignInClient = GoogleSignIn .getClient(this , gso)
28
- }
29
-
30
- private fun signIn () {
31
- auth = FirebaseAuth .getInstance()
32
- val signInIntent = googleSignInClient.signInIntent
33
- startActivityForResult(signInIntent, RC_SIGN_IN )
34
- }
35
-
36
-
37
- override fun onActivityResult (requestCode : Int , resultCode : Int , data : Intent ? ) {
38
- super .onActivityResult(requestCode, resultCode, data)
39
-
40
- // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
41
- if (requestCode == RC_SIGN_IN ) {
42
- val task = GoogleSignIn .getSignedInAccountFromIntent(data)
43
- if (task.isSuccessful) {
44
- try {
45
- // Google Sign In was successful, authenticate with Firebase
46
- val account = task.getResult(ApiException ::class .java)!!
47
- Log .d(TAG , " firebaseAuthWithGoogle:" + account.id)
48
- firebaseAuthWithGoogle(account.idToken!! )
49
- setContentView(R .layout.activity_main)
50
- setupNavigationBar()
51
- } catch (e: ApiException ) {
52
- // Google Sign In failed
53
- Log .w(TAG , " Google sign in failed" , e)
54
- }
55
- } else {
56
- Log .w(TAG , " Google sign in unsuccessful" )
57
- }
58
-
59
- }
60
- // No other requestCode, ignore it.
61
- }
62
-
63
-
64
- private fun firebaseAuthWithGoogle (idToken : String ) {
65
- val credential = GoogleAuthProvider .getCredential(idToken, null )
66
- auth.signInWithCredential(credential)
67
- .addOnCompleteListener(this ) { task ->
68
- if (task.isSuccessful) {
69
- // Firebase Sign in success, update UI with the signed-in user's information
70
- Log .d(FIREBASE_TAG , " signInWithCredential:success" )
71
- val user = auth.currentUser
72
- Log .d(FIREBASE_TAG , " signed-in user's Email:" + user!! .email)
73
- } else {
74
- // If sign in fails, log a message to the user.
75
- Log .w(FIREBASE_TAG , " signInWithCredential:failure" , task.exception)
76
- }
77
- }
78
- }
14
+ private lateinit var signInVM : SignInViewModel
79
15
80
16
81
17
private fun setupNavigationBar () {
82
- val runFragment = RunFragment ()
83
18
supportFragmentManager
84
19
.beginTransaction()
85
- .replace(R .id.fragment_container, runFragment )
20
+ .replace(R .id.fragment_container, RunFragment () )
86
21
.setTransition(FragmentTransaction .TRANSIT_FRAGMENT_OPEN )
87
22
.commit()
88
23
@@ -91,30 +26,28 @@ class MainActivity : AppCompatActivity() {
91
26
when (item.itemId) {
92
27
93
28
R .id.bottom_navigation_item_run -> {
94
- val runFragment = RunFragment ()
95
29
supportFragmentManager
96
30
.beginTransaction()
97
- .replace(R .id.fragment_container, runFragment )
31
+ .replace(R .id.fragment_container, RunFragment () )
98
32
.setTransition(FragmentTransaction .TRANSIT_FRAGMENT_OPEN )
99
33
.commit()
100
34
true
101
35
}
102
36
103
37
R .id.bottom_navigation_item_map -> {
104
- val mapsFragment = MapsFragment ()
38
+ // val mapsFragment = MapsFragment()
105
39
supportFragmentManager
106
40
.beginTransaction()
107
- .replace(R .id.fragment_container, mapsFragment )
41
+ .replace(R .id.fragment_container, MapsFragment () )
108
42
.setTransition(FragmentTransaction .TRANSIT_FRAGMENT_OPEN )
109
43
.commit()
110
44
true
111
45
}
112
46
113
47
R .id.bottom_navigation_item_profile -> {
114
- val meFragment = MeFragment ()
115
48
supportFragmentManager
116
49
.beginTransaction()
117
- .replace(R .id.fragment_container, meFragment )
50
+ .replace(R .id.fragment_container, MeFragment () )
118
51
.setTransition(FragmentTransaction .TRANSIT_FRAGMENT_OPEN )
119
52
.commit()
120
53
true
@@ -125,34 +58,37 @@ class MainActivity : AppCompatActivity() {
125
58
}
126
59
}
127
60
128
-
129
61
override fun onCreate (savedInstanceState : Bundle ? ) {
130
62
super .onCreate(savedInstanceState)
131
63
setContentView(R .layout.activity_main)
132
64
133
- googleSignInInit()
134
- setupNavigationBar( )
65
+ signInVM = ViewModelProviders .of( this )[ SignInViewModel :: class .java]
66
+ signInVM.signInInit( this , this )
135
67
68
+ setupNavigationBar()
136
69
}
137
70
138
-
139
71
override fun onStart () {
140
72
super .onStart()
73
+
141
74
val account = GoogleSignIn .getLastSignedInAccount(this )
142
- if (account == null ) {
143
- setContentView(R .layout.login_in_page)
144
- findViewById<SignInButton >(R .id.sign_in_button).setOnClickListener {
145
- signIn()
146
- }
75
+ account?.let {
76
+ Log .d(TAG , " Already login" )
77
+ } ? : run {
78
+ Log .d(TAG , " No login. Update UI" )
79
+ findViewById<ConstraintLayout >(R .id.main_activity_view).removeAllViews()
80
+
81
+ supportFragmentManager
82
+ .beginTransaction()
83
+ .replace(R .id.main_activity_view, LoginFragment ())
84
+ .setTransition(FragmentTransaction .TRANSIT_FRAGMENT_OPEN )
85
+ .commit()
147
86
}
148
87
}
149
88
150
89
151
90
companion object {
152
- private const val RC_SIGN_IN = 0
153
- private const val TAG = " MainActivity-Login"
154
- private const val FIREBASE_TAG = " MainAct-firebase-Login"
91
+ private const val TAG = " MainActivity"
155
92
}
156
93
157
-
158
94
}
0 commit comments