-
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.
Creación de DogListActivity.kt, DogAdapter.kt y DogListViewModel.kt
- Loading branch information
Showing
12 changed files
with
247 additions
and
20 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
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,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) |
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
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/dogapp/doglist/DogAdapter.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,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
28
app/src/main/java/com/example/dogapp/doglist/DogListActivity.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,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
29
app/src/main/java/com/example/dogapp/doglist/DogListViewModel.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,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
63
app/src/main/java/com/example/dogapp/doglist/DogRepository.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,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 | ||
} | ||
} |
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,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> |
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,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> |
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,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> |