diff --git a/.idea/misc.xml b/.idea/misc.xml index 5aa79b62b958a17339e043920ab48f35299f96a7..2034cfc8acfbf36b82eefa3af2e84377e20a4a9d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,7 +6,7 @@ <entry key="app/src/main/res/layout/activity_main.xml" value="0.16510416666666666" /> <entry key="app/src/main/res/layout/activity_profile.xml" value="0.165" /> <entry key="app/src/main/res/layout/recyclerview_main_menu_row.xml" value="0.1" /> - <entry key="app/src/main/res/layout/recyclerview_spell_row.xml" value="0.16510416666666666" /> + <entry key="app/src/main/res/layout/recyclerview_spell_row.xml" value="0.1" /> <entry key="app/src/main/res/menu/menu_main.xml" value="0.25" /> </map> </option> diff --git a/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt b/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt index d1882243c1d5830d5adfdf606691aedbf2683cd2..78d9b2e1fa2485743d72ede06e49cb62c819a49d 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/ProfileActivity.kt @@ -2,7 +2,7 @@ package com.flyinpancake.dndspells import androidx.appcompat.app.AppCompatActivity import android.os.Bundle -import androidx.lifecycle.map +import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import com.flyinpancake.dndspells.adapter.ProfileSpellRecyclerViewAdapter import com.flyinpancake.dndspells.model.Spell @@ -15,7 +15,7 @@ class ProfileActivity : AppCompatActivity(), ProfileSpellRecyclerViewAdapter.Spe const val KEY_NAME = "KEY_NAME" } private lateinit var binding : ActivityProfileBinding - private val spellListViewModel = SpellViewModel() + private lateinit var spellListViewModel : SpellViewModel private val recyclerViewAdapter = ProfileSpellRecyclerViewAdapter() override fun onCreate(savedInstanceState: Bundle?) { binding = ActivityProfileBinding.inflate(this.layoutInflater) @@ -24,6 +24,11 @@ class ProfileActivity : AppCompatActivity(), ProfileSpellRecyclerViewAdapter.Spe binding.tvCharacterName.text = intent.getStringExtra(KEY_NAME)!! + spellListViewModel = ViewModelProvider(this).get(SpellViewModel::class.java) + spellListViewModel.allSpells.observe(this,{ spells -> + recyclerViewAdapter.submitList(spells) + }) + setupSpellList() } @@ -31,8 +36,6 @@ class ProfileActivity : AppCompatActivity(), ProfileSpellRecyclerViewAdapter.Spe private fun setupSpellList() { - spellListViewModel.allSpells.map { spells -> recyclerViewAdapter.addAll(spells) } - binding.rvSpellList.adapter = recyclerViewAdapter binding.rvSpellList.layoutManager = LinearLayoutManager(this) } diff --git a/app/src/main/java/com/flyinpancake/dndspells/adapter/ProfileSpellRecyclerViewAdapter.kt b/app/src/main/java/com/flyinpancake/dndspells/adapter/ProfileSpellRecyclerViewAdapter.kt index faead98c3c18a1c546c4f91f893142d28c649c9e..24cfad181dc4fa79fd7580dd5a485cb892b14831 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/adapter/ProfileSpellRecyclerViewAdapter.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/adapter/ProfileSpellRecyclerViewAdapter.kt @@ -1,12 +1,29 @@ package com.flyinpancake.dndspells.adapter +import android.os.Parcel +import android.os.Parcelable 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.flyinpancake.dndspells.model.Spell import com.flyinpancake.dndspells.databinding.RecyclerviewSpellRowBinding -class ProfileSpellRecyclerViewAdapter: RecyclerView.Adapter<ProfileSpellRecyclerViewAdapter.ViewHolder>() { +class ProfileSpellRecyclerViewAdapter : ListAdapter<Spell, ProfileSpellRecyclerViewAdapter.ViewHolder>(itemCallback) { + + companion object { + object itemCallback: DiffUtil.ItemCallback<Spell>() { + override fun areItemsTheSame(oldItem: Spell, newItem: Spell): Boolean { + return oldItem == newItem + } + + override fun areContentsTheSame(oldItem: Spell, newItem: Spell): Boolean { + return newItem.name == oldItem.name + } + + } + } val spells = mutableListOf<Spell>() @@ -25,7 +42,7 @@ class ProfileSpellRecyclerViewAdapter: RecyclerView.Adapter<ProfileSpellRecycler ) override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val spell = spells[position] + val spell = this.getItem(position) holder.binding.SpellComponents.text = spell.components holder.binding.SpellLevel.text = "${spell.level}" @@ -33,13 +50,9 @@ class ProfileSpellRecyclerViewAdapter: RecyclerView.Adapter<ProfileSpellRecycler holder.binding.tvSpellName.text = spell.name } - override fun getItemCount() = spells.size - var itemClickListener: SpellListItemClickListener? = null interface SpellListItemClickListener { fun onItemClick(spell: Spell, binding: RecyclerviewSpellRowBinding) } - - fun addAll(spellList: List<Spell>) = spells.addAll(spellList) } \ No newline at end of file diff --git a/app/src/main/java/com/flyinpancake/dndspells/repository/SpellRepository.kt b/app/src/main/java/com/flyinpancake/dndspells/repository/SpellRepository.kt index a11bebcba600bb26bdd3ecac15c5af8b217c2173..3d0021eeacbaea658ccb108af3d1a789cb8f1832 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/repository/SpellRepository.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/repository/SpellRepository.kt @@ -29,10 +29,6 @@ class SpellRepository(private val spellDao: SpellDao) { suspend fun nuke() = withContext(Dispatchers.IO) { spellDao.nukeSpells() } - - suspend fun getAllSpellst() = withContext(Dispatchers.IO) { - - } } private fun Spell.toRoomModel(): RoomSpell { diff --git a/app/src/main/java/com/flyinpancake/dndspells/viewmodel/SpellViewModel.kt b/app/src/main/java/com/flyinpancake/dndspells/viewmodel/SpellViewModel.kt index 4df2f4f94fd226ea157426814a4983dea485189e..13182f3d289ebf7c5b4fb8ce03e0a067958703a9 100644 --- a/app/src/main/java/com/flyinpancake/dndspells/viewmodel/SpellViewModel.kt +++ b/app/src/main/java/com/flyinpancake/dndspells/viewmodel/SpellViewModel.kt @@ -30,7 +30,4 @@ class SpellViewModel: ViewModel() { fun nuke() = viewModelScope.launch { repo.nuke() } - - - } \ No newline at end of file diff --git a/app/src/main/res/layout/recyclerview_spell_row.xml b/app/src/main/res/layout/recyclerview_spell_row.xml index 46f6a627fcfb133ba142a9a886420cfc1c7e27e0..94ee80a7a6791c1ffc9e482654852dddc1913357 100644 --- a/app/src/main/res/layout/recyclerview_spell_row.xml +++ b/app/src/main/res/layout/recyclerview_spell_row.xml @@ -3,7 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content"> <TextView android:id="@+id/tvSpellName" @@ -11,7 +11,7 @@ android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="8dp" - android:text="Power word kill" + tools:text="Power word kill" android:textSize="28sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -22,35 +22,41 @@ android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" - android:text="Level 9" + tools:text="Level 9" android:textSize="28sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> - <TextView - android:layout_width="wrap_content" - android:layout_height="wrap_content"/> <TextView android:id="@+id/SpellSchool" android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_height="27dp" android:layout_marginStart="8dp" + android:layout_marginTop="8dp" android:layout_marginBottom="8dp" - android:text="school" + tools:text="school" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintStart_toStartOf="parent" /> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/tvSpellName" + app:layout_constraintVertical_bias="1.0" /> <TextView android:id="@+id/SpellComponents" - android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_width="0dp" + android:layout_height="19dp" + android:layout_marginStart="16dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" - android:text="Components" + android:ellipsize="end" + android:maxLines="1" + tools:text="ComponentsComponentsComponentsComponentsComponents" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toEndOf="parent" /> + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toEndOf="@id/SpellSchool" + app:layout_constraintTop_toBottomOf="@+id/SpellLevel" + app:layout_constraintVertical_bias="1.0" />