mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
86 lines
3.3 KiB
Kotlin
86 lines
3.3 KiB
Kotlin
/*
|
|
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
|
*
|
|
* This file is part of QKSMS.
|
|
*
|
|
* QKSMS is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* QKSMS is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package feature.conversations
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.graphics.Canvas
|
|
import android.graphics.Paint
|
|
import android.support.v7.widget.RecyclerView
|
|
import android.support.v7.widget.helper.ItemTouchHelper
|
|
import androidx.graphics.drawable.toBitmap
|
|
import com.moez.QKSMS.R
|
|
import common.util.extensions.dpToPx
|
|
import io.reactivex.subjects.PublishSubject
|
|
import io.reactivex.subjects.Subject
|
|
import javax.inject.Inject
|
|
|
|
|
|
class ConversationItemTouchCallback @Inject constructor(private val context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
|
|
|
|
val swipes: Subject<Long> = PublishSubject.create()
|
|
|
|
private var paint = Paint()
|
|
private lateinit var icon: Bitmap
|
|
|
|
private var iconLength = 24.dpToPx(context)
|
|
|
|
var color: Int = 0
|
|
set(value) {
|
|
field = value
|
|
paint.color = value
|
|
}
|
|
|
|
var iconColor: Int = 0
|
|
set(value) {
|
|
field = value
|
|
icon = context.resources.getDrawable(R.drawable.ic_archive_black_24dp)
|
|
.apply { setTint(value) }
|
|
.toBitmap(iconLength, iconLength)
|
|
}
|
|
|
|
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
|
|
return false
|
|
}
|
|
|
|
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
|
|
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
|
|
val itemView = viewHolder.itemView
|
|
|
|
if (dX > 0) {
|
|
c.drawRect(itemView.left.toFloat(), itemView.top.toFloat(), dX, itemView.bottom.toFloat(), paint)
|
|
c.drawBitmap(icon, itemView.left.toFloat() + iconLength,
|
|
itemView.top.toFloat() + (itemView.bottom.toFloat() - itemView.top.toFloat() - icon.height) / 2,
|
|
paint)
|
|
} else if (dX < 0) {
|
|
c.drawRect(itemView.right.toFloat() + dX, itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat(), paint)
|
|
c.drawBitmap(icon, itemView.right.toFloat() - iconLength - icon.width,
|
|
itemView.top.toFloat() + (itemView.bottom.toFloat() - itemView.top.toFloat() - icon.height) / 2,
|
|
paint)
|
|
}
|
|
|
|
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
|
|
}
|
|
}
|
|
|
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
|
swipes.onNext(viewHolder.itemId)
|
|
}
|
|
|
|
} |