Replace "Mealie" with "Mealient" everywhere
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package gq.kirmanak.mealient.ui.auth
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import timber.log.Timber
|
||||
|
||||
@AndroidEntryPoint
|
||||
class AuthenticationFragment : Fragment() {
|
||||
private var _binding: FragmentAuthenticationBinding? = null
|
||||
private val binding: FragmentAuthenticationBinding
|
||||
get() = checkNotNull(_binding) { "Binding requested when fragment is off screen" }
|
||||
private val viewModel by viewModels<AuthenticationViewModel>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
Timber.v("onCreate() called with: savedInstanceState = $savedInstanceState")
|
||||
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(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
Timber.v("onCreateView() called with: inflater = $inflater, container = $container, savedInstanceState = $savedInstanceState")
|
||||
_binding = FragmentAuthenticationBinding.inflate(inflater, container, false)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
Timber.v("onViewCreated() called with: view = $view, savedInstanceState = $savedInstanceState")
|
||||
binding.button.setOnClickListener { onLoginClicked() }
|
||||
}
|
||||
|
||||
private fun navigateToRecipes() {
|
||||
Timber.v("navigateToRecipes() called")
|
||||
findNavController().navigate(AuthenticationFragmentDirections.actionAuthenticationFragmentToRecipesFragment())
|
||||
}
|
||||
|
||||
private fun onLoginClicked() {
|
||||
Timber.v("onLoginClicked() called")
|
||||
val email: String
|
||||
val pass: String
|
||||
val url: String
|
||||
with(binding) {
|
||||
email = checkIfInputIsEmpty(emailInput, emailInputLayout) {
|
||||
"Email is empty"
|
||||
} ?: return
|
||||
pass = checkIfInputIsEmpty(passwordInput, passwordInputLayout) {
|
||||
"Pass is empty"
|
||||
} ?: return
|
||||
url = checkIfInputIsEmpty(urlInput, urlInputLayout) {
|
||||
"URL is empty"
|
||||
} ?: return
|
||||
}
|
||||
viewLifecycleOwner.lifecycleScope.launchWhenResumed {
|
||||
runCatching {
|
||||
viewModel.authenticate(email, pass, url)
|
||||
}.onFailure {
|
||||
Timber.e(it, "Can't authenticate")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkIfInputIsEmpty(
|
||||
input: EditText,
|
||||
inputLayout: TextInputLayout,
|
||||
errorText: () -> String
|
||||
): String? {
|
||||
Timber.v("checkIfInputIsEmpty() called with: input = $input, inputLayout = $inputLayout, errorText = $errorText")
|
||||
val text = input.text?.toString()
|
||||
Timber.d("Input text is \"$text\"")
|
||||
if (text.isNullOrBlank()) {
|
||||
inputLayout.error = errorText()
|
||||
return null
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
Timber.v("onDestroyView() called")
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package gq.kirmanak.mealient.ui.auth
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class AuthenticationViewModel @Inject constructor(
|
||||
private val authRepo: AuthRepo,
|
||||
private val recipeRepo: RecipeRepo
|
||||
) : ViewModel() {
|
||||
init {
|
||||
Timber.v("constructor called")
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
fun logout() {
|
||||
Timber.v("logout() called")
|
||||
authRepo.logout()
|
||||
viewModelScope.launch { recipeRepo.clearLocalData() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user