refactor(reactions): shrink reaction SMS payload and dedupe swipe code

Prepares the send-reactions feature for public release by cutting the
per-reaction SMS size and removing duplicated swipe-action code.

Payload (EmojiReactionRepositoryImpl):
- Stop re-embedding the replied-to text in the hidden payload; it already
  shows in the visible fallback line. The reaction target is resolved by
  sender address + timestamp, and the text-fallback matcher now recovers
  the quote from the visible line.
- Encode the payload at 3 bits per invisible char (8-symbol alphabet)
  instead of 1, and word-cap the visible quote. A reaction's invisible run
  drops from ~1,200 chars (~10-20 SMS segments) to ~83 (1-2 segments).

Deduplication:
- Extract SwipeItemTouchCallback base; ConversationItemTouchCallback and
  MessageItemTouchCallback become thin subclasses (removes dead fields).
- Extract BaseSwipeActionsPresenter and share SwipeActionsView/State;
  delete the cloned MessageSwipeActions View/State. Centralize the message
  swipe-action icon map.
- Merge the two near-identical reaction-send streams in ComposeViewModel.

Minor: enumerate the full emoji set off the main thread in EmojiPicker;
reuse a single Handler for the reaction pulse in MessagesAdapter.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
octoshrimpy
2026-06-14 20:26:05 -04:00
parent 2ca015a0ae
commit 1c08c0dc20
13 changed files with 429 additions and 461 deletions
@@ -29,6 +29,9 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.common.util.extensions.dpToPx
import dev.octoshrimpy.quik.common.util.extensions.resolveThemeColor
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
/**
* A lightweight, dependency-free emoji picker. Renders a themed, scrollable grid of emoji in a
@@ -80,6 +83,36 @@ object EmojiPicker {
val sheet = BottomSheetDialog(context)
// Show the common emoji immediately; the full set probes thousands of code points for
// glyphs, so it's enumerated off the main thread and appended when ready — opening the
// sheet never blocks the UI thread.
val items = ArrayList(commonEmojis)
val gridAdapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
object : RecyclerView.ViewHolder(TextView(context).apply {
textSize = 22f
gravity = Gravity.CENTER
includeFontPadding = false
setTextColor(textColor)
setBackgroundResource(R.drawable.ripple)
layoutParams = RecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, cell
)
}) {}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val emoji = items[position]
(holder.itemView as TextView).text = emoji
holder.itemView.setOnClickListener {
onPicked(emoji)
sheet.dismiss()
}
}
override fun getItemCount() = items.size
}
val recycler = RecyclerView(context).apply {
setBackgroundColor(backgroundColor)
layoutManager = GridLayoutManager(context, span)
@@ -87,32 +120,23 @@ object EmojiPicker {
ViewGroup.LayoutParams.MATCH_PARENT,
(context.resources.displayMetrics.heightPixels * 0.5f).toInt()
)
adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
object : RecyclerView.ViewHolder(TextView(context).apply {
textSize = 22f
gravity = Gravity.CENTER
includeFontPadding = false
setTextColor(textColor)
setBackgroundResource(R.drawable.ripple)
layoutParams = RecyclerView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, cell
)
}) {}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val emoji = emojis[position]
(holder.itemView as TextView).text = emoji
holder.itemView.setOnClickListener {
onPicked(emoji)
sheet.dismiss()
}
}
override fun getItemCount() = emojis.size
}
adapter = gridAdapter
}
// emojis starts with commonEmojis (distinct), so only the generated tail is appended
val load = Single.fromCallable { emojis }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { full ->
val tail = full.drop(items.size)
if (tail.isNotEmpty()) {
val start = items.size
items.addAll(tail)
gridAdapter.notifyItemRangeInserted(start, tail.size)
}
}
sheet.setOnDismissListener { load.dispose() }
sheet.setContentView(recycler)
sheet.show()
}
@@ -0,0 +1,160 @@
/*
* 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 dev.octoshrimpy.quik.common.util
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import androidx.core.graphics.drawable.toBitmap
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import dev.octoshrimpy.quik.common.util.extensions.dpToPx
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlin.math.max
import kotlin.math.min
/**
* Shared swipe-to-action behaviour for the conversations list and the messages list: draws the
* themed background and action icon while a row is swiped, and emits `(itemId, direction)` on
* release. Subclasses supply which prefs drive the left/right actions, how an action maps to an
* icon, and (optionally) what to do after a swipe completes.
*/
abstract class SwipeItemTouchCallback(
colors: Colors,
disposables: CompositeDisposable,
private val context: Context
) : ItemTouchHelper.SimpleCallback(0, 0) {
val swipes: Subject<Pair<Long, Int>> = PublishSubject.create()
/**
* Setting the adapter allows us to animate back to the original position
*/
var adapter: RecyclerView.Adapter<*>? = null
private val backgroundPaint = Paint()
protected var rightAction = 0
protected var leftAction = 0
private var swipeRightIcon: Bitmap? = null
private var swipeLeftIcon: Bitmap? = null
private val iconLength = 24.dpToPx(context)
/** Action value that means "no swipe action" (0 for both lists). */
protected abstract val noneAction: Int
protected abstract fun rightActionObservable(): Observable<Int>
protected abstract fun leftActionObservable(): Observable<Int>
protected abstract fun iconForAction(action: Int, tint: Int): Bitmap?
init {
disposables += colors.themeObservable()
.doOnNext { theme -> backgroundPaint.color = theme.theme }
.subscribeOn(Schedulers.io())
.subscribe()
disposables += Observables
.combineLatest(rightActionObservable(), leftActionObservable(), colors.themeObservable()
) { right, left, theme ->
rightAction = right
swipeRightIcon = iconForAction(right, theme.textPrimary)
leftAction = left
swipeLeftIcon = iconForAction(left, theme.textPrimary)
setDefaultSwipeDirs((if (right == noneAction) 0 else ItemTouchHelper.RIGHT)
or (if (left == noneAction) 0 else ItemTouchHelper.LEFT))
}
.subscribeOn(Schedulers.io())
.subscribe()
}
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(), backgroundPaint)
swipeRightIcon?.let { icon ->
val availablePx = dX.toInt() - iconLength
if (availablePx > 0) {
val src = Rect(0, 0, min(availablePx, icon.width), icon.height)
val dstTop = itemView.top + (itemView.bottom - itemView.top - icon.height) / 2
val dst = Rect(iconLength, dstTop, iconLength + src.width(), dstTop + src.height())
c.drawBitmap(icon, src, dst, null)
}
}
} else if (dX < 0) {
c.drawRect(itemView.right.toFloat() + dX, itemView.top.toFloat(),
itemView.right.toFloat(), itemView.bottom.toFloat(), backgroundPaint)
swipeLeftIcon?.let { icon ->
val availablePx = -dX.toInt() - iconLength
if (availablePx > 0) {
val src = Rect(max(0, icon.width - availablePx), 0, icon.width, icon.height)
val dstTop = itemView.top + (itemView.bottom - itemView.top - icon.height) / 2
val dst = Rect(itemView.right - iconLength - src.width(), dstTop,
itemView.right - iconLength, dstTop + src.height())
c.drawBitmap(icon, src, dst, null)
}
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
swipes.onNext(Pair(viewHolder.itemId, direction))
// Default: nothing is removed by a swipe, so always animate back to neutral
adapter?.notifyItemChanged(viewHolder.adapterPosition)
}
/** Convert a drawable resource into a tinted, icon-sized bitmap. */
protected fun actionBitmap(@androidx.annotation.DrawableRes res: Int?, tint: Int): Bitmap? =
res?.let(context.resources::getDrawable)
?.apply { setTint(tint) }
?.toBitmap(iconLength, iconLength)
}
@@ -789,10 +789,13 @@ class ComposeViewModel @Inject constructor(
.autoDisposable(view.scope())
.subscribe { messageId -> view.showReactionPicker(messageId) }
// Send a reaction: build a quik-encoded body and send it as a normal message. The sent
// message comes back to this device and is parsed as a reaction (isEmojiReaction = true),
// so it is hidden from the conversation automatically.
view.sendReactionIntent
// Send a reaction — from the picker (sendReactionIntent) or the configured default reaction
// on double-tap. Build a quik-encoded body and send it as a normal message; the sent
// message comes back to this device, is parsed as a reaction (isEmojiReaction = true), and
// is hidden from the conversation automatically.
Observable.merge(
view.sendReactionIntent,
view.doubleTapMessageIntent.map { messageId -> Pair(messageId, prefs.defaultReactionEmoji.get()) })
// permission checks touch the UI, so keep them on the main thread
.observeOn(AndroidSchedulers.mainThread())
.filter {
@@ -822,34 +825,6 @@ class ComposeViewModel @Inject constructor(
messageRepo.sendNewMessages(subId, addresses, body, emptyList(), sendAsGroup)
}
// Double-tap a message sends the configured default reaction
view.doubleTapMessageIntent
.observeOn(AndroidSchedulers.mainThread())
.filter {
when {
!permissionManager.isDefaultSms() -> { view.requestDefaultSms(); false }
!permissionManager.hasSendSms() -> { view.requestSmsPermission(); false }
else -> true
}
}
.observeOn(Schedulers.io())
.withLatestFrom(state, conversation) { messageId, state, conversation ->
Triple(messageId, state, conversation)
}
.autoDisposable(view.scope())
.subscribe { (messageId, state, conversation) ->
val targetMessage = messageRepo.getMessage(messageId) ?: return@subscribe
val addresses = conversation.recipients.map { it.address }
if (addresses.isEmpty()) return@subscribe
val subId = state.subscription?.subscriptionId ?: -1
val sendAsGroup = addresses.size > 1
val emoji = prefs.defaultReactionEmoji.get()
val body = reactions.buildReactionBody(emoji, targetMessage)
messageRepo.sendNewMessages(subId, addresses, body, emptyList(), sendAsGroup)
}
// Configurable message swipe actions
view.swipeMessageIntent
.observeOn(AndroidSchedulers.mainThread())
@@ -20,141 +20,29 @@ package dev.octoshrimpy.quik.feature.compose
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import androidx.core.graphics.drawable.toBitmap
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.common.util.Colors
import dev.octoshrimpy.quik.common.util.extensions.dpToPx
import dev.octoshrimpy.quik.common.util.SwipeItemTouchCallback
import dev.octoshrimpy.quik.feature.settings.swipe.messageSwipeActionIcon
import dev.octoshrimpy.quik.util.Preferences
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import javax.inject.Inject
import kotlin.math.max
import kotlin.math.min
class MessageItemTouchCallback @Inject constructor(
colors: Colors,
disposables: CompositeDisposable,
prefs: Preferences,
private val context: Context
) : ItemTouchHelper.SimpleCallback(0, 0) {
private val prefs: Preferences,
context: Context
) : SwipeItemTouchCallback(colors, disposables, context) {
val swipes: Subject<Pair<Long, Int>> = PublishSubject.create()
override val noneAction = Preferences.MESSAGE_SWIPE_ACTION_NONE
/**
* Setting the adapter allows us to animate back to the original position
*/
var adapter: RecyclerView.Adapter<*>? = null
override fun rightActionObservable(): Observable<Int> = prefs.messageSwipeRight.asObservable()
override fun leftActionObservable(): Observable<Int> = prefs.messageSwipeLeft.asObservable()
private val backgroundPaint = Paint()
private var rightAction = 0
private var swipeRightIcon: Bitmap? = null
private var leftAction = 0
private var swipeLeftIcon: Bitmap? = null
private val iconLength = 24.dpToPx(context)
init {
disposables += colors.themeObservable()
.doOnNext { theme -> backgroundPaint.color = theme.theme }
.subscribeOn(Schedulers.io())
.subscribe()
disposables += Observables
.combineLatest(prefs.messageSwipeRight.asObservable(), prefs.messageSwipeLeft.asObservable(), colors.themeObservable()
) { right, left, theme ->
rightAction = right
swipeRightIcon = iconForAction(right, theme.textPrimary)
leftAction = left
swipeLeftIcon = iconForAction(left, theme.textPrimary)
setDefaultSwipeDirs((if (right == Preferences.MESSAGE_SWIPE_ACTION_NONE) 0 else ItemTouchHelper.RIGHT)
or (if (left == Preferences.MESSAGE_SWIPE_ACTION_NONE) 0 else ItemTouchHelper.LEFT))
}
.subscribeOn(Schedulers.io())
.subscribe()
}
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(), backgroundPaint)
swipeRightIcon?.let { icon ->
val availablePx = dX.toInt() - iconLength
if (availablePx > 0) {
val src = Rect(0, 0, min(availablePx, icon.width), icon.height)
val dstTop = itemView.top + (itemView.bottom - itemView.top - icon.height) / 2
val dst = Rect(iconLength, dstTop, iconLength + src.width(), dstTop + src.height())
c.drawBitmap(icon, src, dst, null)
}
}
} else if (dX < 0) {
c.drawRect(itemView.right.toFloat() + dX, itemView.top.toFloat(),
itemView.right.toFloat(), itemView.bottom.toFloat(), backgroundPaint)
swipeLeftIcon?.let { icon ->
val availablePx = -dX.toInt() - iconLength
if (availablePx > 0) {
val src = Rect(max(0, icon.width - availablePx), 0, icon.width, icon.height)
val dstTop = itemView.top + (itemView.bottom - itemView.top - icon.height) / 2
val dst = Rect(itemView.right - iconLength - src.width(), dstTop,
itemView.right - iconLength, dstTop + src.height())
c.drawBitmap(icon, src, dst, null)
}
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
swipes.onNext(Pair(viewHolder.itemId, direction))
// Messages are never archived/removed by a swipe, so always snap back to neutral
adapter?.notifyItemChanged(viewHolder.adapterPosition)
}
private fun iconForAction(action: Int, tint: Int): Bitmap? {
val res = when (action) {
Preferences.MESSAGE_SWIPE_ACTION_REACT -> R.drawable.ic_favorite_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_REPLY -> R.drawable.ic_reply_white_24dp
Preferences.MESSAGE_SWIPE_ACTION_COPY -> R.drawable.ic_content_copy_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_FORWARD -> R.drawable.ic_forward_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_DELETE -> R.drawable.ic_delete_white_24dp
else -> null
}
return res?.let(context.resources::getDrawable)
?.apply { setTint(tint) }
?.toBitmap(iconLength, iconLength)
}
override fun iconForAction(action: Int, tint: Int): Bitmap? =
actionBitmap(messageSwipeActionIcon(action).takeIf { it != 0 }, tint)
// Messages are never archived/removed by a swipe, so the base onSwiped (always snap back to
// neutral) is exactly what we want.
}
@@ -123,6 +123,8 @@ class MessagesAdapter @Inject constructor(
private var pendingReactionPulse = false
private var reactionPulseOrder = 0
private val pulsedReactionIds = mutableSetOf<Long>()
private val mainHandler = Handler(Looper.getMainLooper())
private val clearReactionPulse = Runnable { pendingReactionPulse = false }
// click events passed back to compose view model
val partClicks: Subject<Long> = PublishSubject.create()
@@ -148,7 +150,8 @@ class MessagesAdapter @Inject constructor(
if (value?.first?.lastMessage?.isEmojiReaction == true && !pendingReactionPulse) {
pendingReactionPulse = true
reactionPulseOrder = 0
Handler(Looper.getMainLooper()).postDelayed({ pendingReactionPulse = false }, 1500)
mainHandler.removeCallbacks(clearReactionPulse)
mainHandler.postDelayed(clearReactionPulse, 1500)
}
updateData(value?.second)
@@ -20,146 +20,47 @@ package dev.octoshrimpy.quik.feature.conversations
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import androidx.core.graphics.drawable.toBitmap
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.common.util.Colors
import dev.octoshrimpy.quik.common.util.extensions.dpToPx
import dev.octoshrimpy.quik.common.util.SwipeItemTouchCallback
import dev.octoshrimpy.quik.util.Preferences
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.Observables
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import javax.inject.Inject
import kotlin.math.max
import kotlin.math.min
class ConversationItemTouchCallback @Inject constructor(
colors: Colors,
disposables: CompositeDisposable,
prefs: Preferences,
private val context: Context
) : ItemTouchHelper.SimpleCallback(0, 0) {
private val prefs: Preferences,
context: Context
) : SwipeItemTouchCallback(colors, disposables, context) {
val swipes: Subject<Pair<Long, Int>> = PublishSubject.create()
override val noneAction = Preferences.SWIPE_ACTION_NONE
/**
* Setting the adapter allows us to animate back to the original position
*/
var adapter: RecyclerView.Adapter<*>? = null
override fun rightActionObservable(): Observable<Int> = prefs.swipeRight.asObservable()
override fun leftActionObservable(): Observable<Int> = prefs.swipeLeft.asObservable()
private val backgroundPaint = Paint()
private var rightAction = 0
private var swipeRightIcon: Bitmap? = null
private var leftAction = 0
private var swipeLeftIcon: Bitmap? = null
private val iconLength = 24.dpToPx(context)
init {
disposables += colors.themeObservable()
.doOnNext { theme -> backgroundPaint.color = theme.theme }
.subscribeOn(Schedulers.io())
.subscribe()
disposables += Observables
.combineLatest(prefs.swipeRight.asObservable(), prefs.swipeLeft.asObservable(), colors.themeObservable()
) { right, left, theme ->
rightAction = right
swipeRightIcon = iconForAction(right, theme.textPrimary)
leftAction = left
swipeLeftIcon = iconForAction(left, theme.textPrimary)
setDefaultSwipeDirs((if (right == Preferences.SWIPE_ACTION_NONE) 0 else ItemTouchHelper.RIGHT)
or (if (left == Preferences.SWIPE_ACTION_NONE) 0 else ItemTouchHelper.LEFT))
}
.subscribeOn(Schedulers.io())
.subscribe()
}
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(), backgroundPaint)
swipeRightIcon?.let { icon ->
val availablePx = dX.toInt() - iconLength
if (availablePx > 0) {
val src = Rect(0, 0, min(availablePx, icon.width), icon.height)
val dstTop = itemView.top + (itemView.bottom - itemView.top - icon.height) / 2
val dst = Rect(iconLength, dstTop, iconLength + src.width(), dstTop + src.height())
c.drawBitmap(icon, src, dst, null)
}
}
} else if (dX < 0) {
c.drawRect(itemView.right.toFloat() + dX, itemView.top.toFloat(),
itemView.right.toFloat(), itemView.bottom.toFloat(), backgroundPaint)
swipeLeftIcon?.let { icon ->
val availablePx = -dX.toInt() - iconLength
if (availablePx > 0) {
val src = Rect(max(0, icon.width - availablePx), 0, icon.width, icon.height)
val dstTop = itemView.top + (itemView.bottom - itemView.top - icon.height) / 2
val dst = Rect(itemView.right - iconLength - src.width(), dstTop,
itemView.right - iconLength, dstTop + src.height())
c.drawBitmap(icon, src, dst, null)
}
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
}
override fun iconForAction(action: Int, tint: Int): Bitmap? = actionBitmap(when (action) {
Preferences.SWIPE_ACTION_ARCHIVE -> R.drawable.ic_archive_white_24dp
Preferences.SWIPE_ACTION_DELETE -> R.drawable.ic_delete_white_24dp
Preferences.SWIPE_ACTION_BLOCK -> R.drawable.ic_block_white_24dp
Preferences.SWIPE_ACTION_CALL -> R.drawable.ic_call_white_24dp
Preferences.SWIPE_ACTION_READ -> R.drawable.ic_check_white_24dp
Preferences.SWIPE_ACTION_UNREAD -> R.drawable.ic_markunread_black_24dp
Preferences.SWIPE_ACTION_SPEAK -> R.drawable.ic_speaker_black_24dp
else -> null
}, tint)
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
swipes.onNext(Pair(viewHolder.itemId, direction))
// This will trigger the animation back to neutral state
// Archive removes the row, so only animate back to neutral for the other actions
val action = if (direction == ItemTouchHelper.RIGHT) rightAction else leftAction
if (action != Preferences.SWIPE_ACTION_ARCHIVE) {
adapter?.notifyItemChanged(viewHolder.adapterPosition)
}
}
private fun iconForAction(action: Int, tint: Int): Bitmap? {
val res = when (action) {
Preferences.SWIPE_ACTION_ARCHIVE -> R.drawable.ic_archive_white_24dp
Preferences.SWIPE_ACTION_DELETE -> R.drawable.ic_delete_white_24dp
Preferences.SWIPE_ACTION_BLOCK -> R.drawable.ic_block_white_24dp
Preferences.SWIPE_ACTION_CALL -> R.drawable.ic_call_white_24dp
Preferences.SWIPE_ACTION_READ -> R.drawable.ic_check_white_24dp
Preferences.SWIPE_ACTION_UNREAD -> R.drawable.ic_markunread_black_24dp
Preferences.SWIPE_ACTION_SPEAK -> R.drawable.ic_speaker_black_24dp
else -> null
}
return res?.let(context.resources::getDrawable)
?.apply { setTint(tint) }
?.toBitmap(iconLength, iconLength)
}
}
}
@@ -0,0 +1,79 @@
/*
* 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 dev.octoshrimpy.quik.feature.settings.swipe
import android.content.Context
import androidx.annotation.ArrayRes
import androidx.annotation.DrawableRes
import com.f2prateek.rx.preferences2.Preference
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import dev.octoshrimpy.quik.common.base.QkPresenter
import io.reactivex.rxkotlin.plusAssign
/**
* Shared presenter for the two "swipe actions" settings screens (conversations and messages). The
* screens are identical apart from which prefs back the left/right action, the labels array, and
* how an action maps to an icon, so subclasses supply only those.
*/
abstract class BaseSwipeActionsPresenter(
context: Context,
@ArrayRes labelsArrayRes: Int,
private val rightPref: Preference<Int>,
private val leftPref: Preference<Int>
) : QkPresenter<SwipeActionsView, SwipeActionsState>(SwipeActionsState()) {
private val actionLabels = context.resources.getStringArray(labelsArrayRes)
init {
disposables += rightPref.asObservable()
.subscribe { action -> newState { copy(rightLabel = actionLabels[action], rightIcon = iconForAction(action)) } }
disposables += leftPref.asObservable()
.subscribe { action -> newState { copy(leftLabel = actionLabels[action], leftIcon = iconForAction(action)) } }
}
override fun bindIntents(view: SwipeActionsView) {
super.bindIntents(view)
view.actionClicks()
.map { action ->
when (action) {
SwipeActionsView.Action.RIGHT -> rightPref.get()
SwipeActionsView.Action.LEFT -> leftPref.get()
}
}
.autoDisposable(view.scope())
.subscribe(view::showSwipeActions)
view.actionSelected()
.withLatestFrom(view.actionClicks()) { actionId, action ->
when (action) {
SwipeActionsView.Action.RIGHT -> rightPref.set(actionId)
SwipeActionsView.Action.LEFT -> leftPref.set(actionId)
}
}
.autoDisposable(view.scope())
.subscribe()
}
@DrawableRes
protected abstract fun iconForAction(action: Int): Int
}
@@ -20,11 +20,15 @@ package dev.octoshrimpy.quik.feature.settings.swipe
import androidx.annotation.DrawableRes
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.util.Preferences
data class MessageSwipeActionsState(
@DrawableRes val rightIcon: Int = R.drawable.ic_favorite_black_24dp,
val rightLabel: String = "",
@DrawableRes val leftIcon: Int = R.drawable.ic_reply_white_24dp,
val leftLabel: String = ""
)
/** Drawable shown for a message swipe [action], or 0 when there is no icon. */
@DrawableRes
fun messageSwipeActionIcon(action: Int): Int = when (action) {
Preferences.MESSAGE_SWIPE_ACTION_REACT -> R.drawable.ic_favorite_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_REPLY -> R.drawable.ic_reply_white_24dp
Preferences.MESSAGE_SWIPE_ACTION_COPY -> R.drawable.ic_content_copy_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_FORWARD -> R.drawable.ic_forward_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_DELETE -> R.drawable.ic_delete_white_24dp
else -> 0
}
@@ -39,7 +39,7 @@ import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import javax.inject.Inject
class MessageSwipeActionsController : QkController<SwipeActionsControllerBinding, MessageSwipeActionsView, MessageSwipeActionsState, MessageSwipeActionsPresenter>(), MessageSwipeActionsView {
class MessageSwipeActionsController : QkController<SwipeActionsControllerBinding, SwipeActionsView, SwipeActionsState, MessageSwipeActionsPresenter>(), SwipeActionsView {
override fun inflateBinding(inflater: LayoutInflater, container: ViewGroup): SwipeActionsControllerBinding =
SwipeActionsControllerBinding.inflate(inflater, container, false)
@@ -51,7 +51,7 @@ class MessageSwipeActionsController : QkController<SwipeActionsControllerBinding
/**
* Allows us to subscribe to [actionClicks] more than once
*/
private val actionClicks: Subject<MessageSwipeActionsView.Action> = PublishSubject.create()
private val actionClicks: Subject<SwipeActionsView.Action> = PublishSubject.create()
init {
appComponent.inject(this)
@@ -71,8 +71,8 @@ class MessageSwipeActionsController : QkController<SwipeActionsControllerBinding
binding.left.postDelayed({ binding.left.animateLayoutChanges = true }, 100)
Observable.merge(
binding.right.clicks().map { MessageSwipeActionsView.Action.RIGHT },
binding.left.clicks().map { MessageSwipeActionsView.Action.LEFT })
binding.right.clicks().map { SwipeActionsView.Action.RIGHT },
binding.left.clicks().map { SwipeActionsView.Action.LEFT })
.autoDisposable(scope())
.subscribe(actionClicks)
}
@@ -84,7 +84,7 @@ class MessageSwipeActionsController : QkController<SwipeActionsControllerBinding
showBackButton(true)
}
override fun actionClicks(): Observable<MessageSwipeActionsView.Action> = actionClicks
override fun actionClicks(): Observable<SwipeActionsView.Action> = actionClicks
override fun actionSelected(): Observable<Int> = actionsDialog.adapter.menuItemClicks
@@ -93,7 +93,7 @@ class MessageSwipeActionsController : QkController<SwipeActionsControllerBinding
activity?.let(actionsDialog::show)
}
override fun render(state: MessageSwipeActionsState) {
override fun render(state: SwipeActionsState) {
binding.rightIcon.isVisible = state.rightIcon != 0
binding.rightIcon.setImageResource(state.rightIcon)
binding.rightLabel.text = state.rightLabel
@@ -20,61 +20,18 @@ package dev.octoshrimpy.quik.feature.settings.swipe
import android.content.Context
import androidx.annotation.DrawableRes
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.common.base.QkPresenter
import dev.octoshrimpy.quik.util.Preferences
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject
class MessageSwipeActionsPresenter @Inject constructor(
context: Context,
private val prefs: Preferences
) : QkPresenter<MessageSwipeActionsView, MessageSwipeActionsState>(MessageSwipeActionsState()) {
init {
val actionLabels = context.resources.getStringArray(R.array.settings_message_swipe_actions)
disposables += prefs.messageSwipeRight.asObservable()
.subscribe { action -> newState { copy(rightLabel = actionLabels[action], rightIcon = iconForAction(action)) } }
disposables += prefs.messageSwipeLeft.asObservable()
.subscribe { action -> newState { copy(leftLabel = actionLabels[action], leftIcon = iconForAction(action)) } }
}
override fun bindIntents(view: MessageSwipeActionsView) {
super.bindIntents(view)
view.actionClicks()
.map { action ->
when (action) {
MessageSwipeActionsView.Action.RIGHT -> prefs.messageSwipeRight.get()
MessageSwipeActionsView.Action.LEFT -> prefs.messageSwipeLeft.get()
}
}
.autoDisposable(view.scope())
.subscribe(view::showSwipeActions)
view.actionSelected()
.withLatestFrom(view.actionClicks()) { actionId, action ->
when (action) {
MessageSwipeActionsView.Action.RIGHT -> prefs.messageSwipeRight.set(actionId)
MessageSwipeActionsView.Action.LEFT -> prefs.messageSwipeLeft.set(actionId)
}
}
.autoDisposable(view.scope())
.subscribe()
}
prefs: Preferences
) : BaseSwipeActionsPresenter(
context, R.array.settings_message_swipe_actions, prefs.messageSwipeRight, prefs.messageSwipeLeft
) {
@DrawableRes
private fun iconForAction(action: Int) = when (action) {
Preferences.MESSAGE_SWIPE_ACTION_REACT -> R.drawable.ic_favorite_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_REPLY -> R.drawable.ic_reply_white_24dp
Preferences.MESSAGE_SWIPE_ACTION_COPY -> R.drawable.ic_content_copy_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_FORWARD -> R.drawable.ic_forward_black_24dp
Preferences.MESSAGE_SWIPE_ACTION_DELETE -> R.drawable.ic_delete_white_24dp
else -> 0
}
override fun iconForAction(action: Int) = messageSwipeActionIcon(action)
}
@@ -1,33 +0,0 @@
/*
* 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 dev.octoshrimpy.quik.feature.settings.swipe
import dev.octoshrimpy.quik.common.base.QkViewContract
import io.reactivex.Observable
interface MessageSwipeActionsView : QkViewContract<MessageSwipeActionsState> {
enum class Action { LEFT, RIGHT }
fun actionClicks(): Observable<Action>
fun actionSelected(): Observable<Int>
fun showSwipeActions(selected: Int)
}
@@ -20,55 +20,17 @@ package dev.octoshrimpy.quik.feature.settings.swipe
import android.content.Context
import androidx.annotation.DrawableRes
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import dev.octoshrimpy.quik.R
import dev.octoshrimpy.quik.common.base.QkPresenter
import dev.octoshrimpy.quik.util.Preferences
import io.reactivex.rxkotlin.plusAssign
import javax.inject.Inject
class SwipeActionsPresenter @Inject constructor(
context: Context,
private val prefs: Preferences
) : QkPresenter<SwipeActionsView, SwipeActionsState>(SwipeActionsState()) {
init {
val actionLabels = context.resources.getStringArray(R.array.settings_swipe_actions)
disposables += prefs.swipeRight.asObservable()
.subscribe { action -> newState { copy(rightLabel = actionLabels[action], rightIcon = iconForAction(action)) } }
disposables += prefs.swipeLeft.asObservable()
.subscribe { action -> newState { copy(leftLabel = actionLabels[action], leftIcon = iconForAction(action)) } }
}
override fun bindIntents(view: SwipeActionsView) {
super.bindIntents(view)
view.actionClicks()
.map { action ->
when (action) {
SwipeActionsView.Action.RIGHT -> prefs.swipeRight.get()
SwipeActionsView.Action.LEFT -> prefs.swipeLeft.get()
}
}
.autoDisposable(view.scope())
.subscribe(view::showSwipeActions)
view.actionSelected()
.withLatestFrom(view.actionClicks()) { actionId, action ->
when (action) {
SwipeActionsView.Action.RIGHT -> prefs.swipeRight.set(actionId)
SwipeActionsView.Action.LEFT -> prefs.swipeLeft.set(actionId)
}
}
.autoDisposable(view.scope())
.subscribe()
}
prefs: Preferences
) : BaseSwipeActionsPresenter(context, R.array.settings_swipe_actions, prefs.swipeRight, prefs.swipeLeft) {
@DrawableRes
private fun iconForAction(action: Int) = when (action) {
override fun iconForAction(action: Int) = when (action) {
Preferences.SWIPE_ACTION_ARCHIVE -> R.drawable.ic_archive_white_24dp
Preferences.SWIPE_ACTION_DELETE -> R.drawable.ic_delete_white_24dp
Preferences.SWIPE_ACTION_BLOCK -> R.drawable.ic_block_white_24dp
@@ -79,4 +41,4 @@ class SwipeActionsPresenter @Inject constructor(
else -> 0
}
}
}