From 72e44d76b1623ad3c7f09cd077ba82807f906e23 Mon Sep 17 00:00:00 2001 From: FlyinPancake <36113055+FlyinPancake@users.noreply.github.com> Date: Thu, 2 Dec 2021 16:07:03 +0100 Subject: [PATCH] Yeet --- .idea/misc.xml | 2 +- .../flyinpancake/dndspells/ProfileActivity.kt | 11 ++++--- .../ProfileSpellRecyclerViewAdapter.kt | 25 +++++++++++---- .../dndspells/repository/SpellRepository.kt | 4 --- .../dndspells/viewmodel/SpellViewModel.kt | 3 -- .../res/layout/recyclerview_spell_row.xml | 32 +++++++++++-------- 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 5aa79b6..2034cfc 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 d188224..78d9b2e 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 faead98..24cfad1 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 a11bebc..3d0021e 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 4df2f4f..13182f3 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 46f6a62..94ee80a 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" /> -- GitLab