Skip to content

Commit

Permalink
[FEAT] Enable swipe to delete on arcs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkrauz committed Oct 23, 2020
1 parent 8c2f091 commit 769cf9f
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 8 deletions.
3 changes: 3 additions & 0 deletions data/src/main/java/com/peterkrauz/grimoire/data/dao/ArcDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ interface ArcDao {

@Query("SELECT * FROM arc WHERE id = :arcId")
suspend fun findById(arcId: Long): ArcEntity

@Query("DELETE FROM arc WHERE id = :arcId")
suspend fun deleteById(arcId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ class ArcRepositoryImpl @Inject constructor(private val dao: ArcDao) : ArcReposi
val entities = dao.findAll()
return entities.map(ArcMapper::fromEntity)
}

override suspend fun findById(arcId: Long): Arc {
val entity = dao.findById(arcId)
return ArcMapper.fromEntity(entity)
}

override suspend fun deleteById(arcId: Long) {
dao.deleteById(arcId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.peterkrauz.grimoire.domain.entity

interface ArcRepository {

suspend fun findAll(): List<Arc>
suspend fun createArc(form: CreateArcForm): Arc
suspend fun findAll(): List<Arc>
suspend fun findById(arcId: Long): Arc
suspend fun deleteById(arcId: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.peterkrauz.grimoire.common.extension.safeNavigate
import com.peterkrauz.grimoire.common.extension.snackBar
import com.peterkrauz.grimoire.common.snackbar.SnackBarType
import com.peterkrauz.grimoire.domain.entity.Arc
import com.peterkrauz.grimoire.presentation.home.arclist.ArcSwipeCallback
import com.peterkrauz.grimoire.presentation.home.arclist.ArcsAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.fragment_arcs.*

Expand All @@ -30,7 +33,7 @@ class ArcsFragment : Fragment(R.layout.fragment_arcs) {

override fun onResume() {
super.onResume()
viewModel.fetchArcs()
viewModel.loadArcs()
}

private fun setupButtons() {
Expand All @@ -44,13 +47,18 @@ class ArcsFragment : Fragment(R.layout.fragment_arcs) {
setHasFixedSize(true)
adapter = arcsAdapter
layoutManager = LinearLayoutManager(requireContext())
}.also { recyclerView ->
ItemTouchHelper(
ArcSwipeCallback(::onSwiped)
).attachToRecyclerView(recyclerView)
}
}

private fun observeViewModel() {
viewModel.arcsLiveData.observe(viewLifecycleOwner, ::setArcs)
viewModel.stateLiveData.observe(viewLifecycleOwner, ::setState)
viewModel.errorLiveEvent.observe(viewLifecycleOwner) { setError() }
viewModel.arcDeletedLiveEvent.observe(viewLifecycleOwner) { onArcDeleted() }
}

private fun setArcs(arcs: List<Arc>) {
Expand All @@ -64,4 +72,11 @@ class ArcsFragment : Fragment(R.layout.fragment_arcs) {
}

private fun setError() = snackBar(R.string.generic_error_msg, SnackBarType.ERROR)

private fun onArcDeleted() = snackBar(R.string.arc_delete_success)

private fun onSwiped(position: Int) {
val arc = arcsAdapter.getItemAt(position)
viewModel.onSwiped(arc)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,27 @@ class ArcsViewModel @ViewModelInject constructor(
}

val errorLiveEvent = SingleLiveEvent<Unit>()
val arcDeletedLiveEvent = SingleLiveEvent<Unit>()

private val _arcsLiveData = MutableLiveData<List<Arc>>()
val arcsLiveData: LiveData<List<Arc>>
get() = _arcsLiveData

fun fetchArcs() {
fun loadArcs() {
perform(ArcsState.LOADING, ArcsState.IDLE) {
_arcsLiveData.postValue(arcRepository.findAll())
}
}

fun onSwiped(arc: Arc) {
perform(ArcsState.LOADING, ArcsState.IDLE) {
arcRepository.deleteById(arc.id)
arcDeletedLiveEvent.call()
}.also {
loadArcs()
}
}

override fun handleCoroutineError(ctx: CoroutineContext, error: Throwable) {
errorLiveEvent.call()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.peterkrauz.grimoire.presentation.home.arclist

import androidx.recyclerview.widget.ItemTouchHelper.LEFT
import androidx.recyclerview.widget.ItemTouchHelper.RIGHT
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.RecyclerView

class ArcSwipeCallback(
private val onItemSwiped: ((Int) -> Unit)
) : SimpleCallback(0, LEFT or RIGHT) {

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
onItemSwiped(viewHolder.adapterPosition)
}

override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
) = false
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.peterkrauz.grimoire.presentation.home
package com.peterkrauz.grimoire.presentation.home.arclist

import android.view.View
import androidx.recyclerview.widget.RecyclerView.ViewHolder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.peterkrauz.grimoire.presentation.home
package com.peterkrauz.grimoire.presentation.home.arclist

import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.peterkrauz.grimoire.common.extension.inflate
import com.peterkrauz.grimoire.domain.entity.Arc
import com.peterkrauz.grimoire.presentation.home.R

class ArcsAdapter : ListAdapter<Arc, ArcViewHolder>(ArcDiffCallback) {

Expand All @@ -16,6 +17,8 @@ class ArcsAdapter : ListAdapter<Arc, ArcViewHolder>(ArcDiffCallback) {
holder.bind(getItem(position))
}

fun getItemAt(position: Int): Arc = getItem(position)

private object ArcDiffCallback : DiffUtil.ItemCallback<Arc>() {
override fun areItemsTheSame(old: Arc, new: Arc): Boolean {
return old.id == new.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
android:id="@+id/editTextDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLines="3"
android:imeOptions="actionNext"
android:nextFocusDown="@id/buttonCreate">

Expand Down
6 changes: 4 additions & 2 deletions presentation/home/src/main/res/layout/item_arc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginHorizontal="8dp"
app:cardUseCompatPadding="true">

<androidx.constraintlayout.widget.ConstraintLayout
Expand All @@ -22,7 +21,7 @@
android:fontFamily="@font/kumbh_sans_bold"
android:textSize="21sp"
android:maxLines="1"
android:layout_marginTop="4dp"
android:layout_marginTop="8dp"
tools:text="There and back again"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -36,6 +35,7 @@
android:textSize="16sp"
android:maxLines="3"
android:ellipsize="middle"
android:layout_marginTop="4dp"
tools:text="Once upon a time..."
app:layout_constraintTop_toBottomOf="@id/textViewArcName"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -47,6 +47,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read_more"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
app:layout_constraintTop_toBottomOf="@id/textViewArcDescription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
Expand Down
1 change: 1 addition & 0 deletions presentation/home/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="error_invalid_title">Arc needs a title</string>
<string name="error_invalid_description">Arc needs a description</string>
<string name="arc_created_success">Arc created successfully!</string>
<string name="arc_delete_success">Arc deleted with success!</string>
</resources>

0 comments on commit 769cf9f

Please sign in to comment.