<repo_name>cavigna/CheckApartment_Kotlin<file_sep>/app/src/main/java/com/example/checkapartmentkt/model/Departamento.kt
package com.example.checkapartmentkt.model

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey

@Entity(tableName = "departamentos")
data class Departamento(
    @PrimaryKey(autoGenerate = true)
  
    var id: Int = 1,
    val nombre : String,
    val unidad : String,
    val direccion : String,
    val urlFoto : String,
    val luces :Int,
    val bath : Int,
    val cocina: Int,
    val dormitorio : Int,
    val terminaciones : Int,
    var puntaje : Int ,
){


    //constructor():this(id=0)
    fun calcularPuntaje(){
        this.puntaje = (this.luces + this.bath + this.cocina + this.dormitorio) * this.terminaciones
    }

    //(luces + bath + cocina + dormitorio) * terminaciones
}


<file_sep>/app/src/main/java/com/example/checkapartmentkt/fragments/ui/ListFragment.kt
package com.example.checkapartmentkt.fragments.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.checkapartmentkt.R
import com.example.checkapartmentkt.adapter.DeptoListAdapter
import com.example.checkapartmentkt.databinding.FragmentListBinding
import com.example.checkapartmentkt.viewmodel.DeptoViewModel

class ListFragment : Fragment() {
    private lateinit var binding: FragmentListBinding
    //private val viewModel:DeptoViewModel = ViewModelProvider(requireActivity()).get(DeptoViewModel::class.java)
    private val viewModel: DeptoViewModel by activityViewModels()
    val adapter = DeptoListAdapter()


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentListBinding.inflate(inflater, container, false)

        val recyclerView = binding.recyclerView
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(this.requireContext())


        viewModel.allDepartamento.observe(viewLifecycleOwner){ departamentos ->
            departamentos.let { adapter.submitList(departamentos) }

        }

        return binding.root


    }


//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//         //viewModel = ViewModelProvider(requireActivity()).get(DeptoViewModel::class.java)
//
//    }
}


<file_sep>/app/src/main/java/com/example/checkapartmentkt/dao/DepartamentoDao.kt
package com.example.checkapartmentkt.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.checkapartmentkt.model.Departamento
import kotlinx.coroutines.flow.Flow

@Dao
interface DepartamentoDao {
    @Insert
    suspend fun agregarDeaprtamento(dpto :Departamento)

    @Update
    suspend fun actualizarDepartamento(depto: Departamento)

    @Query("SELECT * FROM departamentos")
     fun allDeptos() : Flow<List<Departamento>>


}<file_sep>/app/src/main/java/com/example/checkapartmentkt/db/BaseDeDatos.kt
package com.example.checkapartmentkt.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.checkapartmentkt.dao.DepartamentoDao
import com.example.checkapartmentkt.model.Departamento
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Database(entities = [Departamento::class], version = 1, exportSchema = false)
abstract class BaseDeDatos : RoomDatabase() {
    abstract fun dao(): DepartamentoDao

    companion object {
        @Volatile
        private var INSTANCE: BaseDeDatos? = null

        fun getBaseDeDatos(context: Context, scope: CoroutineScope): BaseDeDatos {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    BaseDeDatos::class.java,
                    "db_deparamentods"
                )
                    .addCallback(DeptoCallback(scope))
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }


    private class DeptoCallback(private val scope: CoroutineScope) : RoomDatabase.Callback() {
        override fun onCreate(db: SupportSQLiteDatabase) {
            super.onCreate(db)
            INSTANCE?.let { baseDeDatos ->
                scope.launch {
                    popularBaseDatos(baseDeDatos.dao())
                }
            }
        }

        suspend fun popularBaseDatos(dao: DepartamentoDao) {

            val listado = listOf(
                Departamento(
                    nombre = "<NAME>",
                    unidad = "505",
                    direccion = "San Martín 970",
                    urlFoto = "https://http2.mlstatic.com/D_NQ_NP_927221-MLC45497689153_042021-O.webp",
                    bath = 0, luces = 0, cocina = 0, dormitorio = 0, terminaciones = 0, puntaje = 0
                ),

                Departamento(
                    nombre = "<NAME>",
                    unidad = "709",
                    direccion = "Zeneto 1252",
                    urlFoto = "https://http2.mlstatic.com/D_NQ_NP_852648-MLC41532980127_042020-O.webp",
                    bath = 0, luces = 0, cocina = 0, dormitorio = 0, terminaciones = 0, puntaje = 0
                ),
                Departamento(
                    nombre = "<NAME>",
                    unidad = "312",
                    direccion = "Santa Isabel 330",
                    urlFoto = "https://http2.mlstatic.com/D_NQ_NP_787028-MLC41467659060_042020-O.webp",
                    bath = 0, luces = 0, cocina = 0, dormitorio = 0, terminaciones = 0, puntaje = 0
                ),
                Departamento(
                    nombre = "<NAME>",
                    unidad = "404",
                    direccion ="Eyzaguirre 771",
                    urlFoto = "https://http2.mlstatic.com/D_NQ_NP_940668-MLC45093828481_032021-O.webp",
                    bath = 0, luces = 0, cocina = 0, dormitorio = 0, terminaciones = 0, puntaje = 0
                ),


            )

            for(depto in listado){
                dao.agregarDeaprtamento(depto)
            }





        }
    }
}

<file_sep>/app/src/main/java/com/example/checkapartmentkt/adapter/DeptoListAdapter.kt
package com.example.checkapartmentkt.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.checkapartmentkt.R
import com.example.checkapartmentkt.adapter.DeptoListAdapter.DeptoViewHolder
import com.example.checkapartmentkt.databinding.ItemRowBinding
import com.example.checkapartmentkt.model.Departamento

class DeptoListAdapter : ListAdapter<Departamento, DeptoViewHolder>(DeptoComparador()) {


    class DeptoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val binding = ItemRowBinding.bind(itemView)
        val tvNombre = binding.tvNombreRow;
        val tvUnidad = binding.tvUnidadRow;
        val tvDireccion = binding.tvDireccionRow;
        val tvPuntaje = binding.tvPuntajeRow;
        val imagenDpto = binding.imgViewRow;


        companion object {
            fun create(parent: ViewGroup): DeptoViewHolder {
                val view: View = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_row, parent, false)
                return DeptoViewHolder(view)
            }
        }

        fun unirDatos(depto: Departamento) {
            tvNombre.text = depto.nombre
            tvDireccion.text =  depto.direccion
            tvPuntaje.text = depto.puntaje.toString()
            tvUnidad.text = depto.unidad
            Glide.with(itemView).load(depto.urlFoto).fitCenter().into(imagenDpto)
        }


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeptoViewHolder {
        return DeptoViewHolder.create(parent)
    }

    override fun onBindViewHolder(holder: DeptoViewHolder, position: Int) {
        val depto = getItem(position)
        holder.unirDatos(depto)
    }


}


class DeptoComparador : DiffUtil.ItemCallback<Departamento>() {
    override fun areItemsTheSame(oldItem: Departamento, newItem: Departamento): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: Departamento, newItem: Departamento): Boolean {
        return oldItem.id == newItem.id
    }
}



<file_sep>/app/src/main/java/com/example/checkapartmentkt/viewmodel/DeptoViewModel.kt
package com.example.checkapartmentkt.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.asLiveData
import com.example.checkapartmentkt.model.Departamento
import com.example.checkapartmentkt.repository.Repository

class DeptoViewModel(private val repository: Repository) : ViewModel() {

    val allDepartamento : LiveData<List<Departamento>> = repository.allDeptos.asLiveData()

    suspend fun acutalizarDepartamento(depto: Departamento){
        repository.actualizarDepartamento(depto)
    }
}


class DeptoViewModelFactory(private val repository: Repository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(DeptoViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return DeptoViewModel(repository) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}<file_sep>/app/src/main/java/com/example/checkapartmentkt/application/DepartamentoApplication.kt
package com.example.checkapartmentkt.application

import android.app.Application
import com.example.checkapartmentkt.db.BaseDeDatos
import com.example.checkapartmentkt.repository.Repository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob

class DepartamentoApplication : Application() {

    val applicationScope = CoroutineScope(SupervisorJob())

    val baseDeDatos by lazy { BaseDeDatos.getBaseDeDatos(this, applicationScope) }
    val repository by lazy { Repository(baseDeDatos.dao()) }
}<file_sep>/app/src/main/java/com/example/checkapartmentkt/repository/Repository.kt
package com.example.checkapartmentkt.repository

import androidx.annotation.WorkerThread
import com.example.checkapartmentkt.dao.DepartamentoDao
import com.example.checkapartmentkt.model.Departamento
import kotlinx.coroutines.flow.Flow

class Repository (private val dao: DepartamentoDao){
    val allDeptos : Flow<List<Departamento>> = dao.allDeptos()

    @Suppress("RedundantSuspendModifier")
    @WorkerThread
    suspend fun agregarDepartamento(depto: Departamento){
        dao.agregarDeaprtamento(depto)
    }

    @Suppress("RedundantSuspendModifier")
    @WorkerThread
    suspend fun actualizarDepartamento(depto: Departamento){
        dao.actualizarDepartamento(depto)
    }

}<file_sep>/app/src/main/java/com/example/checkapartmentkt/MainActivity.kt
package com.example.checkapartmentkt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.navigation.fragment.NavHostFragment
import com.example.checkapartmentkt.application.DepartamentoApplication
import com.example.checkapartmentkt.databinding.ActivityMainBinding
import com.example.checkapartmentkt.viewmodel.DeptoViewModel
import com.example.checkapartmentkt.viewmodel.DeptoViewModelFactory

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
     val viewModel: DeptoViewModel by viewModels{
        DeptoViewModelFactory((application as DepartamentoApplication).repository)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =  ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)



        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
        val navController = navHostFragment.navController


    }
}