-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jhonn-y2j/new-reto
fix: reto indra
- Loading branch information
Showing
39 changed files
with
773 additions
and
267 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package com.project.retoandroid.app | ||
|
||
import com.project.retoandroid.data.repository.UserRepository | ||
import com.project.retoandroid.data.repository.LoginRepository | ||
import com.project.retoandroid.data.repository.MovieRepository | ||
|
||
object Injection { | ||
|
||
fun provideUserRepository(): UserRepository { | ||
return UserRepository.getInstance() | ||
fun provideMovieRepository(): MovieRepository { | ||
return MovieRepository.getInstance() | ||
} | ||
|
||
fun provideLoginRepository() = LoginRepository.getInstance() | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/project/retoandroid/app/login/LoginActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.project.retoandroid.app.login | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.project.retoandroid.R | ||
import com.project.retoandroid.app.movie.MainActivity | ||
import com.project.retoandroid.databinding.ActivityLoginBinding | ||
import com.project.retoandroid.utils.EventObserver | ||
import com.project.retoandroid.utils.toast | ||
|
||
class LoginActivity: AppCompatActivity() { | ||
|
||
private lateinit var binding: ActivityLoginBinding | ||
private lateinit var viewModel: LoginViewModel | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val factory = LoginViewModel.Factory(application) | ||
viewModel = ViewModelProvider(viewModelStore, factory).get(LoginViewModel::class.java) | ||
binding = DataBindingUtil.setContentView(this, R.layout.activity_login) | ||
|
||
binding.viewModel = viewModel | ||
binding.lifecycleOwner = this | ||
|
||
initViewModel() | ||
|
||
} | ||
|
||
private fun initViewModel() { | ||
|
||
viewModel.apply { | ||
|
||
errorMessage.observe(this@LoginActivity, EventObserver { | ||
|
||
toast(it) | ||
|
||
}) | ||
|
||
openMainActivity.observe(this@LoginActivity, EventObserver { | ||
|
||
MainActivity.startActivity(this@LoginActivity) | ||
this@LoginActivity.finish() | ||
|
||
}) | ||
|
||
} | ||
|
||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/project/retoandroid/app/login/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.project.retoandroid.app.login | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.* | ||
import com.project.retoandroid.app.Injection | ||
import com.project.retoandroid.data.raw.CredentialRaw | ||
import com.project.retoandroid.utils.Event | ||
import kotlinx.coroutines.launch | ||
|
||
class LoginViewModel( | ||
application: Application | ||
): AndroidViewModel(application) { | ||
|
||
val respository = Injection.provideLoginRepository() | ||
|
||
val input = MutableLiveData<String>() | ||
val pass = MutableLiveData<String>() | ||
val errorMessage = MutableLiveData<Event<String>>() | ||
val openMainActivity = MutableLiveData<Event<Any>>() | ||
|
||
fun login() { | ||
|
||
viewModelScope.launch { | ||
|
||
try { | ||
|
||
val email = input.value ?: "" | ||
val password = pass.value ?: "" | ||
|
||
if (email.isEmpty()) { | ||
errorMessage.value = Event("Ingrese su correo.") | ||
return@launch | ||
} | ||
|
||
if (password.isEmpty()) { | ||
errorMessage.value = Event("Ingrese su contraseña") | ||
return@launch | ||
} | ||
|
||
val credential = CredentialRaw(email, password) | ||
val data = respository.login(credential) | ||
|
||
if (data != null) { | ||
errorMessage.value = Event("Inicio sesión correctamente.") | ||
openMainActivity.value = Event(data) | ||
} else { | ||
errorMessage.value = Event("Correo o contraseña inválida.") | ||
} | ||
|
||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
class Factory( | ||
private val application: Application | ||
) : ViewModelProvider.NewInstanceFactory() { | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel?> create(modelClass: Class<T>): T = | ||
with(modelClass) { | ||
when { | ||
isAssignableFrom(LoginViewModel::class.java) -> | ||
LoginViewModel( | ||
application, | ||
) | ||
else -> throw IllegalArgumentException("Unknown ViewModel") | ||
} | ||
} as T | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/project/retoandroid/app/movie/MovieAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.project.retoandroid.app.movie | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.project.retoandroid.data.entity.Movie | ||
import com.project.retoandroid.databinding.RowMovieBinding | ||
|
||
class MovieAdapter( | ||
private val movieViewModel: MovieViewModel | ||
) : ListAdapter<Movie, MovieAdapter.ViewHolder>(Movie.DIFF_CALLBACK) { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
return ViewHolder.from(parent) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val movie = getItem(position) | ||
holder.bind(movieViewModel, movie) | ||
} | ||
|
||
class ViewHolder private constructor(val binding: RowMovieBinding) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
fun bind(movieViewModel: MovieViewModel, movie: Movie) { | ||
binding.movie = movie | ||
binding.viewModel = movieViewModel | ||
|
||
binding.executePendingBindings() | ||
} | ||
|
||
companion object { | ||
|
||
fun from(parent: ViewGroup): ViewHolder { | ||
val layoutInflater = LayoutInflater.from(parent.context) | ||
val binding = RowMovieBinding.inflate(layoutInflater, parent, false) | ||
return ViewHolder(binding) | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/project/retoandroid/app/movie/MovieBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.project.retoandroid.app.movie | ||
|
||
import android.widget.ImageView | ||
import androidx.databinding.BindingAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import coil.api.load | ||
import com.project.retoandroid.data.entity.Movie | ||
import com.project.retoandroid.data.service.ApiClient | ||
|
||
@BindingAdapter("users") | ||
fun bindUser(recyclerView: RecyclerView, movies: List<Movie>? = emptyList()) { | ||
movies ?: return | ||
(recyclerView.adapter as MovieAdapter).apply { | ||
submitList(movies) | ||
} | ||
} | ||
|
||
@BindingAdapter("image") | ||
fun bindImage(image: ImageView, value: String) { | ||
image.load("${ApiClient.urlImage}$value") | ||
} |
Oops, something went wrong.