Skip to content

Commit

Permalink
Creación de DogListActivity.kt, DogAdapter.kt y DogListViewModel.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
osuka11 committed Jul 6, 2022
1 parent 16b677d commit c29964a
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .idea/misc.xml

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

12 changes: 10 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}

android {
Expand All @@ -15,7 +16,9 @@ android {

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
dataBinding true
}
buildTypes {
release {
minifyEnabled false
Expand All @@ -33,10 +36,15 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0'
implementation 'androidx.activity:activity-ktx:1.5.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
android:theme="@style/Theme.DogApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
android:name=".doglist.DogListActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.MAIN" />

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

</activity>
<activity
android:name=".MainActivity"
android:exported="true">

</activity>
</application>

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/example/dogapp/Dog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.dogapp

data class Dog(val id: Long, val index:Int, val name: String, val type: String, val heigthFemale: Double,
val heighMale:Double, val imageUrl: String, val lifeExpectancy: String, val temperament: String,
val weightFemale: Double, val weighMale: Double)
6 changes: 5 additions & 1 deletion app/src/main/java/com/example/dogapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)



}
}

}
38 changes: 38 additions & 0 deletions app/src/main/java/com/example/dogapp/doglist/DogAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.dogapp.doglist

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.dogapp.Dog
import com.example.dogapp.databinding.ActivityDogListBinding
import com.example.dogapp.databinding.DogListItemBinding

class DogAdapter:ListAdapter<Dog, DogAdapter.DogViewHolder>(DiffCallback) {

companion object DiffCallback : DiffUtil.ItemCallback<Dog>() {
override fun areItemsTheSame(oldItem: Dog, newItem: Dog): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: Dog, newItem: Dog): Boolean {
return oldItem == newItem
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DogViewHolder {
val binding = DogListItemBinding.inflate(LayoutInflater.from(parent.context))
return DogViewHolder(binding)
}

override fun onBindViewHolder(holder: DogViewHolder, position: Int) {
val dog = getItem(position)
holder.bind(dog)
}
inner class DogViewHolder(private val binding: DogListItemBinding):RecyclerView.ViewHolder(binding.root){

fun bind(dog: Dog){
binding.dogName.text = dog.name
}
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/example/dogapp/doglist/DogListActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.dogapp.doglist

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.dogapp.Dog
import com.example.dogapp.databinding.ActivityDogListBinding

class DogListActivity : AppCompatActivity() {
private val dogListViewModel: DogListViewModel by viewModels() //Alternativa de declarar un ViewModel(mucho mejor), gracias a cierta dependencia que tenemos en el gradle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityDogListBinding.inflate(layoutInflater)
setContentView(binding.root)
val recycler = binding.dogRecycler
recycler.layoutManager = LinearLayoutManager(this)
val adapter = DogAdapter()
recycler.adapter = adapter
dogListViewModel.dogList.observe(this, Observer {
adapter.submitList(it)
})

}


}
29 changes: 29 additions & 0 deletions app/src/main/java/com/example/dogapp/doglist/DogListViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.dogapp.doglist

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.dogapp.Dog
import kotlinx.coroutines.launch

class DogListViewModel:ViewModel() {
/*
Declaraciones de LiveDatas
*/
private val _dogList = MutableLiveData<List<Dog>>()
val dogList:LiveData<List<Dog>>
get() = _dogList

private val repository = DogRepository() //Creacion de objeto repositorio

init {
downloadDogs()
}

private fun downloadDogs() {
viewModelScope.launch {
_dogList.value = repository.downloadDogs()
}
}
}
63 changes: 63 additions & 0 deletions app/src/main/java/com/example/dogapp/doglist/DogRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.example.dogapp.doglist

import com.example.dogapp.Dog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class DogRepository {

suspend fun downloadDogs():List<Dog>{
return withContext(Dispatchers.IO){
getFakeDogs()

}
}

private fun getFakeDogs(): MutableList<Dog> {
//Creacion de lista de perros
val dogList = mutableListOf<Dog>()
dogList.add(
Dog(
1, 1, "Chihuahua", "Toy", 5.4,
6.7, "", "12 - 15", "", 10.5,
12.3
)
)
dogList.add(
Dog(
2, 1, "Labrador", "Toy", 5.4,
6.7, "", "12 - 15", "", 10.5,
12.3
)
)
dogList.add(
Dog(
3, 1, "Retriever", "Toy", 5.4,
6.7, "", "12 - 15", "", 10.5,
12.3
)
)
dogList.add(
Dog(
4, 1, "San Bernardo", "Toy", 5.4,
6.7, "", "12 - 15", "", 10.5,
12.3
)
)
dogList.add(
Dog(
5, 1, "Husky", "Toy", 5.4,
6.7, "", "12 - 15", "", 10.5,
12.3
)
)
dogList.add(
Dog(
6, 1, "Xoloscuincle", "Toy", 5.4,
6.7, "", "12 - 15", "", 10.5,
12.3
)
)
return dogList
}
}
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_dog_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

</data>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dogRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".doglist.DogListActivity">

</androidx.recyclerview.widget.RecyclerView>
</layout>
35 changes: 21 additions & 14 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
xmlns:tools="http://schemas.android.com/tools">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<data>

</androidx.constraintlayout.widget.ConstraintLayout>
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/dog_list_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">


<TextView
android:id="@+id/dogName"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</TextView>


</layout>

0 comments on commit c29964a

Please sign in to comment.