Show background and icon when swiping to archive

This commit is contained in:
Moez Bhatti
2018-03-29 16:40:58 -04:00
parent 76e1aef66b
commit 58ea5d0ecf
6 changed files with 63 additions and 31 deletions
@@ -18,20 +18,67 @@
*/ */
package feature.conversations 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.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper 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.PublishSubject
import io.reactivex.subjects.Subject import io.reactivex.subjects.Subject
import javax.inject.Inject import javax.inject.Inject
class ConversationItemTouchCallback @Inject constructor() : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
class ConversationItemTouchCallback @Inject constructor(private val context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
val swipes: Subject<Long> = PublishSubject.create() 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 { override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
return false 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) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
swipes.onNext(viewHolder.itemId) swipes.onNext(viewHolder.itemId)
} }
@@ -133,14 +133,17 @@ class MainActivity : QkThemedActivity<MainViewModel>(), MainView {
// Set the theme color tint to the progressbar and FAB // Set the theme color tint to the progressbar and FAB
colors.theme colors.theme
.doOnNext { color -> syncingProgress.indeterminateTintList = ColorStateList.valueOf(color) } .doOnNext { color -> syncingProgress.indeterminateTintList = ColorStateList.valueOf(color) }
.doOnNext { color -> itemTouchCallback.color = color }
.doOnNext { color -> compose.setBackgroundTint(color) } .doOnNext { color -> compose.setBackgroundTint(color) }
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe() .subscribe()
// Set the FAB compose icon color // Set the FAB compose icon color
colors.textPrimaryOnTheme colors.textPrimaryOnTheme
.doOnNext { color -> compose.setTint(color) }
.doOnNext { color -> itemTouchCallback.iconColor = color}
.autoDisposable(scope()) .autoDisposable(scope())
.subscribe { color -> compose.setTint(color) } .subscribe()
// Set the hamburger icon color // Set the hamburger icon color
colors.textSecondary colors.textSecondary
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20.54,5.23l-1.39,-1.68C18.88,3.21 18.47,3 18,3H6c-0.47,0 -0.88,0.21 -1.16,0.55L3.46,5.23C3.17,5.57 3,6.02 3,6.5V19c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V6.5c0,-0.48 -0.17,-0.93 -0.46,-1.27zM12,17.5L6.5,12H10v-2h4v2h3.5L12,17.5zM5.12,5l0.81,-1h12l0.94,1H5.12z"/>
</vector>
@@ -1,27 +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/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M10,4H4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2V8c0,-1.1 -0.9,-2 -2,-2h-8l-2,-2z"/>
</vector>
@@ -78,7 +78,7 @@
android:id="@+id/archive" android:id="@+id/archive"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:icon="@drawable/ic_folder_black_24dp" app:icon="@drawable/ic_archive_black_24dp"
app:title="@string/info_archive" /> app:title="@string/info_archive" />
<common.widget.PreferenceView <common.widget.PreferenceView
@@ -46,7 +46,7 @@
<ImageView <ImageView
android:id="@+id/archivedIcon" android:id="@+id/archivedIcon"
style="@style/DrawerIcon" style="@style/DrawerIcon"
android:src="@drawable/ic_folder_black_24dp" /> android:src="@drawable/ic_archive_black_24dp" />
<common.widget.QkTextView <common.widget.QkTextView
style="@style/DrawerText" style="@style/DrawerText"