diff --git a/app/src/main/java/com/miguelrr/capsshop/domain/usecases/LogInUseCase.kt b/app/src/main/java/com/miguelrr/capsshop/domain/usecases/LogInUseCase.kt index 38c8770..9d73854 100644 --- a/app/src/main/java/com/miguelrr/capsshop/domain/usecases/LogInUseCase.kt +++ b/app/src/main/java/com/miguelrr/capsshop/domain/usecases/LogInUseCase.kt @@ -1,4 +1,16 @@ package com.miguelrr.capsshop.domain.usecases -class LogInUseCase { +import com.miguelrr.capsshop.data.UserRepository +import com.miguelrr.capsshop.domain.model.LoginUser +import com.miguelrr.capsshop.domain.model.ResponseSignUp +import com.miguelrr.capsshop.domain.model.User +import javax.inject.Inject + +class LogInUseCase @Inject constructor( + private val userRepository: UserRepository +) { + + suspend operator fun invoke(loginUser: LoginUser) : User{ + return userRepository.logIn(loginUser) + } } \ No newline at end of file diff --git a/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginFragment.kt b/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginFragment.kt index 8acb2ea..a2b7559 100644 --- a/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginFragment.kt +++ b/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginFragment.kt @@ -4,9 +4,14 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.lifecycle.ViewModelProvider +import androidx.navigation.fragment.findNavController +import com.miguelrr.capsshop.CapsShopApp.Companion.idUserSP import com.miguelrr.capsshop.databinding.FragmentLoginBinding +import com.miguelrr.capsshop.domain.model.LoginUser class LoginFragment : Fragment() { @@ -15,14 +20,14 @@ class LoginFragment : Fragment() { // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! + private val loginViewModel : LoginViewModel by viewModels() + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { - val loginViewModel = - ViewModelProvider(this).get(LoginViewModel::class.java) _binding = FragmentLoginBinding.inflate(inflater, container, false) val root: View = binding.root @@ -30,6 +35,36 @@ class LoginFragment : Fragment() { return root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + binding.btnLogin.setOnClickListener{ + if(!binding.tiLoginEmail.text.isNullOrEmpty() && !binding.tiLoginPassword.text.isNullOrEmpty()){ + val emailUsername = binding.tiLoginEmail.text.toString() + val password = binding.tiLoginPassword.text.toString() + val loginUser = LoginUser(emailUsername, password) + loginViewModel.logIn(loginUser) + } + } + + loginViewModel.user.observe(viewLifecycleOwner){ + if(it.idUser != -1){ + Toast.makeText(context, "Bienvenido ${it.username}", Toast.LENGTH_SHORT).show() + idUserSP.saveId(it.idUser) + }else{ + Toast.makeText(context, it.reason, Toast.LENGTH_SHORT).show() + } + } + + binding.tvLoginSignup.setOnClickListener{ + navigateSignUp() + } + } + + private fun navigateSignUp() { + findNavController().navigate(LoginFragmentDirections.actionNavigationLoginToNavigationSignup()) + } + override fun onDestroyView() { super.onDestroyView() _binding = null diff --git a/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginViewModel.kt b/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginViewModel.kt index 3704447..918a382 100644 --- a/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginViewModel.kt +++ b/app/src/main/java/com/miguelrr/capsshop/ui/login/LoginViewModel.kt @@ -3,7 +3,23 @@ package com.miguelrr.capsshop.ui.login import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.miguelrr.capsshop.domain.model.LoginUser +import com.miguelrr.capsshop.domain.model.User +import com.miguelrr.capsshop.domain.usecases.LogInUseCase +import kotlinx.coroutines.launch +import javax.inject.Inject -class LoginViewModel : ViewModel() { +class LoginViewModel @Inject constructor( + private val logInUseCase: LogInUseCase +): ViewModel() { + private val _user = MutableLiveData() + val user : LiveData get() = _user + + fun logIn(loginUser: LoginUser){ + viewModelScope.launch { + _user.postValue(logInUseCase(loginUser)) + } + } } \ No newline at end of file