Skip to content

Commit

Permalink
Merge pull request #1 from jhonn-y2j/new-reto
Browse files Browse the repository at this point in the history
fix: reto indra
  • Loading branch information
jhonn-y2j authored Apr 30, 2021
2 parents 8a25688 + d33397e commit cd43c70
Show file tree
Hide file tree
Showing 39 changed files with 773 additions and 267 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.retoandroid">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RetoAndroid">
<activity android:name=".app.user.MainActivity">
android:supportsRtl="true">
<activity
android:name=".app.movieDetail.MovieDetailsActivity"
android:theme="@style/Theme.RetoAndroid.noAction.NoActionBar"/>
<activity
android:name=".app.login.LoginActivity"
android:theme="@style/Theme.RetoAndroid.noAction.NoActionBar"
android:screenOrientation="sensorPortrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".app.userDetail.UserDetailActivity" />

<activity android:name=".app.movie.MainActivity"
android:theme="@style/Theme.RetoAndroid"
android:screenOrientation="sensorPortrait" />
</application>

</manifest>
9 changes: 6 additions & 3 deletions app/src/main/java/com/project/retoandroid/app/Injection.kt
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()

}
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()

})

}

}

}
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
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
package com.project.retoandroid.app.user
package com.project.retoandroid.app.movie

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.project.retoandroid.R
import com.project.retoandroid.app.userDetail.UserDetailActivity
import com.project.retoandroid.app.movieDetail.MovieDetailsActivity
import com.project.retoandroid.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: UserViewModel
private lateinit var viewModel: MovieViewModel

companion object {

fun startActivity(activity: Activity) {
activity.startActivity(Intent(activity.applicationContext, MainActivity::class.java))
}

}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val factory = UserViewModel.Factory(application)
viewModel = ViewModelProvider(viewModelStore, factory).get(UserViewModel::class.java)
val factory = MovieViewModel.Factory(application)
viewModel = ViewModelProvider(viewModelStore, factory).get(MovieViewModel::class.java)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

binding.viewModel = viewModel
binding.lifecycleOwner = this

binding.rviUsers.adapter = UserAdapter(viewModel)
binding.rviMovies.adapter = MovieAdapter(viewModel)

supportActionBar?.title = "Peliculas"

initViewModel()

Expand All @@ -34,11 +45,11 @@ class MainActivity : AppCompatActivity() {

viewModel.apply {

userSelected.observe(this@MainActivity, Observer {
movieSelected.observe(this@MainActivity, {

it?.let {

UserDetailActivity.startActivity(it, this@MainActivity)
MovieDetailsActivity.startActivity(it, this@MainActivity)

}

Expand Down
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)
}

}

}
}
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")
}
Loading

0 comments on commit cd43c70

Please sign in to comment.