Skip to content

Commit

Permalink
configured login
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelRom3ro committed Apr 6, 2024
1 parent a70dcc7 commit c8d339f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
}
39 changes: 37 additions & 2 deletions app/src/main/java/com/miguelrr/capsshop/ui/login/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -15,21 +20,51 @@ 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

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>()
val user : LiveData<User> get() = _user

fun logIn(loginUser: LoginUser){
viewModelScope.launch {
_user.postValue(logInUseCase(loginUser))
}
}
}

0 comments on commit c8d339f

Please sign in to comment.