Implement observing authentication statuses
This commit is contained in:
@@ -12,6 +12,7 @@ import androidx.navigation.fragment.findNavController
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealie.databinding.FragmentAuthenticationBinding
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -24,7 +25,17 @@ class AuthenticationFragment : Fragment() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
Timber.v("onCreate() called with: savedInstanceState = $savedInstanceState")
|
||||
checkIfAuthenticatedAlready()
|
||||
listenToAuthenticationStatuses()
|
||||
}
|
||||
|
||||
private fun listenToAuthenticationStatuses() {
|
||||
Timber.d("listenToAuthenticationStatuses() called")
|
||||
lifecycleScope.launchWhenCreated {
|
||||
viewModel.authenticationStatuses().collectLatest {
|
||||
Timber.d("listenToAuthenticationStatuses: new status = $it")
|
||||
if (it) navigateToRecipes()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
@@ -43,13 +54,6 @@ class AuthenticationFragment : Fragment() {
|
||||
binding.button.setOnClickListener { onLoginClicked() }
|
||||
}
|
||||
|
||||
private fun checkIfAuthenticatedAlready() {
|
||||
Timber.v("checkIfAuthenticatedAlready() called")
|
||||
lifecycleScope.launchWhenCreated {
|
||||
if (viewModel.isAuthenticated()) navigateToRecipes()
|
||||
}
|
||||
}
|
||||
|
||||
private fun navigateToRecipes() {
|
||||
findNavController().navigate(AuthenticationFragmentDirections.actionAuthenticationFragmentToRecipesFragment())
|
||||
}
|
||||
@@ -73,8 +77,6 @@ class AuthenticationFragment : Fragment() {
|
||||
lifecycleScope.launchWhenResumed {
|
||||
runCatching {
|
||||
viewModel.authenticate(email, pass, url)
|
||||
}.onSuccess {
|
||||
navigateToRecipes()
|
||||
}.onFailure {
|
||||
Timber.e(it, "Can't authenticate")
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package gq.kirmanak.mealie.ui.auth
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealie.data.auth.AuthRepo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -14,15 +15,13 @@ class AuthenticationViewModel @Inject constructor(
|
||||
Timber.v("constructor called")
|
||||
}
|
||||
|
||||
suspend fun isAuthenticated(): Boolean {
|
||||
Timber.v("isAuthenticated() called")
|
||||
val result = authRepo.isAuthenticated()
|
||||
Timber.d("isAuthenticated() returned: $result")
|
||||
return result
|
||||
}
|
||||
|
||||
suspend fun authenticate(username: String, password: String, baseUrl: String) {
|
||||
Timber.v("authenticate() called with: username = $username, password = $password, baseUrl = $baseUrl")
|
||||
authRepo.authenticate(username, password, baseUrl)
|
||||
}
|
||||
|
||||
fun authenticationStatuses(): Flow<Boolean> {
|
||||
Timber.v("authenticationStatuses() called")
|
||||
return authRepo.authenticationStatuses()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user