allow reordering clips by Drag & Dropping

This commit is contained in:
tibbi 2022-01-28 22:26:40 +01:00
parent 109e6abb6d
commit 62b47c7c4f
6 changed files with 57 additions and 5 deletions

View File

@ -64,7 +64,7 @@ android {
}
dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:7ed441bded'
implementation 'com.github.SimpleMobileTools:Simple-Commons:18b6427fa9'
kapt 'androidx.room:room-compiler:2.3.0'
implementation 'androidx.room:room-runtime:2.3.0'

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.keyboard.adapters
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.ItemTouchHelper
@ -7,6 +8,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.interfaces.ItemMoveCallback
import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract
@ -27,6 +30,7 @@ class ClipsActivityAdapter(
private var touchHelper: ItemTouchHelper? = null
private var startReorderDragListener: StartReorderDragListener
private var wasClipMoved = false
init {
setupDragListener(true)
@ -66,7 +70,26 @@ class ClipsActivityAdapter(
override fun getItemKeyPosition(key: Int) = items.indexOfFirst { it.id?.toInt() == key }
override fun onActionModeDestroyed() {}
override fun onActionModeCreated() {
notifyDataSetChanged()
}
override fun onActionModeDestroyed() {
if (wasClipMoved) {
ensureBackgroundThread {
activity.clipsDB.deleteAll()
items.forEach { clip ->
clip.id = null
clip.id = activity.clipsDB.insertOrUpdate(clip)
}
}
activity.runOnUiThread {
notifyDataSetChanged()
}
}
wasClipMoved = false
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_clip_in_activity, parent)
@ -123,8 +146,16 @@ class ClipsActivityAdapter(
view.apply {
clip_value.text = clip.value
clip_value.setTextColor(textColor)
clip_drag_handle.applyColorFilter(textColor)
clip_drag_handle.beVisibleIf(selectedKeys.isNotEmpty())
clip_holder.isSelected = isSelected
clip_drag_handle.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
startReorderDragListener.requestDrag(holder)
}
false
}
}
}
@ -139,6 +170,7 @@ class ClipsActivityAdapter(
}
}
notifyItemMoved(fromPosition, toPosition)
wasClipMoved = true
}
override fun onRowSelected(myViewHolder: ViewHolder?) {}

View File

@ -16,4 +16,7 @@ interface ClipsDao {
@Query("DELETE FROM clips WHERE id = :id")
fun delete(id: Long)
@Query("DELETE FROM clips")
fun deleteAll()
}

View File

@ -12,7 +12,7 @@
android:layout_marginStart="@dimen/activity_margin"
android:layout_marginEnd="@dimen/activity_margin"
android:hint="@string/clip_text"
android:inputType="textCapSentences"
android:inputType="textCapSentences|textMultiLine"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" />

View File

@ -7,14 +7,30 @@
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/selector">
android:foreground="@drawable/selector"
android:paddingEnd="@dimen/normal_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/clip_value"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/clip_drag_handle"
android:ellipsize="end"
android:gravity="start"
android:maxLines="10"
android:padding="@dimen/activity_margin"
android:textSize="@dimen/normal_text_size"
tools:text="Hello, how are you?" />
<ImageView
android:id="@+id/clip_drag_handle"
android:layout_width="@dimen/clip_drag_icon_height"
android:layout_height="@dimen/clip_drag_icon_height"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:padding="@dimen/small_margin"
android:src="@drawable/ic_drag_handle_vector"
android:visibility="gone" />
</RelativeLayout>

View File

@ -4,6 +4,7 @@
<dimen name="top_small_number_margin_height">18dp</dimen>
<dimen name="toolbar_height">46dp</dimen>
<dimen name="toolbar_icon_height">32dp</dimen>
<dimen name="clip_drag_icon_height">40dp</dimen>
<dimen name="key_height">60dp</dimen>
<dimen name="vertical_correction">-10dp</dimen>