Skip to content

Commit

Permalink
list drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
SaeedDev94 committed Feb 9, 2024
1 parent ceb92cd commit 6d0b890
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/io/github/saeeddev94/xray/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
Expand Down Expand Up @@ -269,7 +270,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
val ref = db.profileDao().find(profile.id)
val id = ref.id
db.profileDao().delete(ref)
db.profileDao().fixIndex(index)
db.profileDao().fixDeleteIndex(index)
runOnUiThread {
if (selectedProfile == id) {
Settings.selectedProfile = 0L
Expand Down Expand Up @@ -316,6 +317,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
})
profilesList.adapter = profileAdapter
profilesList.layoutManager = LinearLayoutManager(applicationContext)
ItemTouchHelper(ProfileTouchHelper(profileAdapter)).also { it.attachToRecyclerView(profilesList) }
}
}.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ProfileActivity : AppCompatActivity() {
val db = XrayDatabase.ref(applicationContext)
if (profile.id == 0L) {
profile.id = db.profileDao().insert(profile)
db.profileDao().shiftIndex()
db.profileDao().fixInsertIndex()
} else {
db.profileDao().update(profile)
}
Expand Down
29 changes: 27 additions & 2 deletions app/src/main/java/io/github/saeeddev94/xray/ProfileAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import io.github.saeeddev94.xray.database.ProfileList
import io.github.saeeddev94.xray.database.XrayDatabase

class ProfileAdapter(
private var context: Context,
private var profiles: List<ProfileList>,
private var profiles: ArrayList<ProfileList>,
private var callback: ProfileClickListener,
) : RecyclerView.Adapter<ProfileAdapter.ViewHolder>() {
) : RecyclerView.Adapter<ProfileAdapter.ViewHolder>(), ProfileTouchCallback {

override fun onCreateViewHolder(container: ViewGroup, type: Int): ViewHolder {
val linearLayout = LinearLayout(context)
Expand Down Expand Up @@ -45,6 +46,30 @@ class ProfileAdapter(
}
}

override fun onItemMoved(fromPosition: Int, toPosition: Int): Boolean {
profiles.add(toPosition, profiles.removeAt(fromPosition))
notifyItemMoved(fromPosition, toPosition)
if (toPosition > fromPosition) {
notifyItemRangeChanged(fromPosition, toPosition - fromPosition + 1)
} else {
notifyItemRangeChanged(toPosition, fromPosition - toPosition + 1)
}
return true
}

override fun onItemMoveCompleted(startPosition: Int, endPosition: Int) {
val id = profiles[endPosition].id
Thread {
val db = XrayDatabase.ref(context)
db.profileDao().updateIndex(endPosition, id)
if (startPosition > endPosition) {
db.profileDao().fixMoveUpIndex(endPosition, startPosition, id)
} else {
db.profileDao().fixMoveDownIndex(startPosition, endPosition, id)
}
}.start()
}

class ViewHolder(item: View) : RecyclerView.ViewHolder(item) {
var activeIndicator: LinearLayout = item.findViewById(R.id.activeIndicator)
var profileCard: CardView = item.findViewById(R.id.profileCard)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github.saeeddev94.xray

interface ProfileTouchCallback {
fun onItemMoved(fromPosition: Int, toPosition: Int): Boolean
fun onItemMoveCompleted(startPosition: Int, endPosition: Int)
}
42 changes: 42 additions & 0 deletions app/src/main/java/io/github/saeeddev94/xray/ProfileTouchHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.saeeddev94.xray

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class ProfileTouchHelper(private var adapter: ProfileTouchCallback) : ItemTouchHelper.Callback() {

private var startPosition: Int = -1

override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
): Int = makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0)

override fun onMove(
recyclerView: RecyclerView,
source: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean = adapter.onItemMoved(source.adapterPosition, target.adapterPosition)

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

override fun onSelectedChanged(
viewHolder: RecyclerView.ViewHolder?,
actionState: Int
) {
if (actionState != ItemTouchHelper.ACTION_STATE_DRAG) return
startPosition = viewHolder!!.adapterPosition
}

override fun clearView(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder
) {
val endPosition = viewHolder.adapterPosition
adapter.onItemMoveCompleted(startPosition, endPosition)
}
}
13 changes: 11 additions & 2 deletions app/src/main/java/io/github/saeeddev94/xray/database/ProfileDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ interface ProfileDao {
fun all(): List<ProfileList>

@Query("UPDATE profiles SET `index` = `index` + 1")
fun shiftIndex()
fun fixInsertIndex()

@Query("UPDATE profiles SET `index` = `index` - 1 WHERE `index` > :index")
fun fixIndex(index: Int)
fun fixDeleteIndex(index: Int)

@Query("UPDATE profiles SET `index` = :index WHERE `id` = :id")
fun updateIndex(index: Int, id: Long)

@Query("UPDATE profiles SET `index` = `index` + 1 WHERE `index` >= :start AND `index` < :end AND `id` NOT IN (:exclude)")
fun fixMoveUpIndex(start: Int, end: Int, exclude: Long)

@Query("UPDATE profiles SET `index` = `index` - 1 WHERE `index` > :start AND `index` <= :end AND `id` NOT IN (:exclude)")
fun fixMoveDownIndex(start: Int, end: Int, exclude: Long)

@Query("SELECT * FROM profiles WHERE `id` = :id")
fun find(id: Long): Profile
Expand Down

0 comments on commit 6d0b890

Please sign in to comment.