@@ -5,31 +5,30 @@ import android.util.Log
5
5
import android.view.LayoutInflater
6
6
import android.view.View
7
7
import android.view.ViewGroup
8
- import android.widget.Toast
9
8
import androidx.fragment.app.Fragment
10
- import com.google.firebase.auth.FirebaseAuth
11
- import com.google.firebase.auth.FirebaseUser
12
- import com.google.firebase.auth.ktx.auth
13
- import com.google.firebase.ktx.Firebase
14
- import com.google.firebase.quickstart.auth.R
9
+ import androidx.fragment.app.viewModels
10
+ import androidx.lifecycle.DefaultLifecycleObserver
11
+ import androidx.lifecycle.Lifecycle
12
+ import androidx.lifecycle.LifecycleOwner
13
+ import androidx.lifecycle.lifecycleScope
14
+ import androidx.lifecycle.repeatOnLifecycle
15
15
import com.google.firebase.quickstart.auth.databinding.FragmentCustomBinding
16
+ import kotlinx.coroutines.launch
16
17
17
18
/* *
18
19
* Demonstrate Firebase Authentication using a custom minted token. For more information, see:
19
20
* https://firebase.google.com/docs/auth/android/custom-auth
20
21
*/
21
22
class CustomAuthFragment : Fragment () {
22
-
23
- private lateinit var auth: FirebaseAuth
24
-
25
23
private var _binding : FragmentCustomBinding ? = null
26
24
private val binding: FragmentCustomBinding
27
25
get() = _binding !!
28
26
29
- private var customToken: String? = null
27
+ private val viewModel by viewModels<CustomAuthViewModel >()
28
+
30
29
private lateinit var tokenReceiver: TokenBroadcastReceiver
31
30
32
- override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
31
+ override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
33
32
_binding = FragmentCustomBinding .inflate(inflater, container, false )
34
33
return binding.root
35
34
}
@@ -38,76 +37,39 @@ class CustomAuthFragment : Fragment() {
38
37
super .onViewCreated(view, savedInstanceState)
39
38
40
39
// Button click listeners
41
- binding.buttonSignIn.setOnClickListener { startSignIn() }
40
+ binding.buttonSignIn.setOnClickListener { viewModel. startSignIn() }
42
41
43
42
// Create token receiver (for demo purposes only)
44
43
tokenReceiver = object : TokenBroadcastReceiver () {
45
44
override fun onNewToken (token : String? ) {
46
45
Log .d(TAG , " onNewToken:$token " )
47
- setCustomToken(token.toString() )
46
+ viewModel. setCustomToken(token)
48
47
}
49
48
}
50
49
51
- // Initialize Firebase Auth
52
- auth = Firebase .auth
53
- }
54
-
55
- override fun onStart () {
56
- super .onStart()
57
- // Check if user is signed in (non-null) and update UI accordingly.
58
- val currentUser = auth.currentUser
59
- updateUI(currentUser)
60
- }
61
-
62
- override fun onResume () {
63
- super .onResume()
64
- requireActivity().registerReceiver(tokenReceiver, TokenBroadcastReceiver .filter)
65
- }
66
-
67
- override fun onPause () {
68
- super .onPause()
69
- requireActivity().unregisterReceiver(tokenReceiver)
70
- }
71
-
72
- private fun startSignIn () {
73
- // Initiate sign in with custom token
74
- customToken?.let {
75
- auth.signInWithCustomToken(it)
76
- .addOnCompleteListener(requireActivity()) { task ->
77
- if (task.isSuccessful) {
78
- // Sign in success, update UI with the signed-in user's information
79
- Log .d(TAG , " signInWithCustomToken:success" )
80
- val user = auth.currentUser
81
- updateUI(user)
82
- } else {
83
- // If sign in fails, display a message to the user.
84
- Log .w(TAG , " signInWithCustomToken:failure" , task.exception)
85
- Toast .makeText(context, " Authentication failed." ,
86
- Toast .LENGTH_SHORT ).show()
87
- updateUI(null )
88
- }
89
- }
90
- }
91
- }
50
+ viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
51
+ override fun onResume (owner : LifecycleOwner ) {
52
+ super .onResume(owner)
53
+ requireActivity().registerReceiver(tokenReceiver, TokenBroadcastReceiver .filter)
54
+ }
92
55
93
- private fun updateUI (user : FirebaseUser ? ) {
94
- if (user != null ) {
95
- binding.textSignInStatus.text = getString(R .string.custom_auth_signin_status_user, user.uid)
96
- } else {
97
- binding.textSignInStatus.text = getString(R .string.custom_auth_signin_status_failed)
56
+ override fun onPause (owner : LifecycleOwner ) {
57
+ super .onPause(owner)
58
+ requireActivity().unregisterReceiver(tokenReceiver)
59
+ }
60
+ })
61
+
62
+ lifecycleScope.launch {
63
+ viewLifecycleOwner.repeatOnLifecycle(Lifecycle .State .STARTED ) {
64
+ viewModel.uiState.collect { uiState ->
65
+ binding.buttonSignIn.isEnabled = uiState.isSignInEnabled
66
+ binding.textSignInStatus.text = uiState.signInStatus
67
+ binding.textTokenStatus.text = uiState.tokenStatus
68
+ }
69
+ }
98
70
}
99
71
}
100
72
101
- private fun setCustomToken (token : String ) {
102
- customToken = token
103
-
104
- val status = " Token:$customToken "
105
-
106
- // Enable/disable sign-in button and show the token
107
- binding.buttonSignIn.isEnabled = true
108
- binding.textTokenStatus.text = status
109
- }
110
-
111
73
override fun onDestroyView () {
112
74
super .onDestroyView()
113
75
_binding = null
0 commit comments