Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Add drag-and-drop sorting to grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 26, 2024
1 parent ca16c22 commit 78c5f76
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 233 deletions.
33 changes: 0 additions & 33 deletions app/src/main/java/com/neko/v2ray/helper/OnStartDragListener.java

This file was deleted.

4 changes: 1 addition & 3 deletions app/src/main/kotlin/com/neko/v2ray/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,9 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = adapter

val callback = SimpleItemTouchHelperCallback(adapter)
mItemTouchHelper = ItemTouchHelper(callback)
mItemTouchHelper = ItemTouchHelper(SimpleItemTouchHelperCallback(adapter))
mItemTouchHelper?.attachToRecyclerView(binding.recyclerView)


val toggle = ActionBarDrawerToggle(
this, binding.drawerLayout, binding.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
)
Expand Down
21 changes: 3 additions & 18 deletions app/src/main/kotlin/com/neko/v2ray/ui/MainRecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,31 +230,16 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
class FooterViewHolder(val itemFooterBinding: ItemRecyclerFooterBinding) :
BaseViewHolder(itemFooterBinding.root)

override fun onItemDismiss(position: Int) {
val guid = mActivity.mainViewModel.serversCache.getOrNull(position)?.guid ?: return
if (guid != MmkvManager.getSelectServer()) {
// mActivity.alert(R.string.del_config_comfirm) {
// positiveButton(android.R.string.ok) {
mActivity.mainViewModel.removeServer(guid)
notifyItemRemoved(position)
// }
// show()
// }
}
}

override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
mActivity.mainViewModel.swapServer(fromPosition, toPosition)
notifyItemMoved(fromPosition, toPosition)
// position is changed, since position is used by click callbacks, need to update range
if (toPosition > fromPosition)
notifyItemRangeChanged(fromPosition, toPosition - fromPosition + 1)
else
notifyItemRangeChanged(toPosition, fromPosition - toPosition + 1)
return true
}

override fun onItemMoveCompleted() {
// do nothing
}

override fun onItemDismiss(position: Int) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class RoutingEditActivity : BaseActivity() {
return false
}

Log.d("====", Gson().toJson(rulesetItem))
SettingsManager.saveRoutingRuleset(position, rulesetItem)
toast(R.string.toast_success)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.appbar.CollapsingToolbarLayout
Expand All @@ -16,6 +17,7 @@ import com.neko.v2ray.R
import com.neko.v2ray.databinding.ActivityRoutingSettingBinding
import com.neko.v2ray.dto.RulesetItem
import com.neko.v2ray.extension.toast
import com.neko.v2ray.helper.SimpleItemTouchHelperCallback
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.SettingsManager
Expand All @@ -28,6 +30,7 @@ class RoutingSettingActivity : BaseActivity() {

var rulesets: MutableList<RulesetItem> = mutableListOf()
private val adapter by lazy { RoutingSettingRecyclerAdapter(this) }
private var mItemTouchHelper: ItemTouchHelper? = null
private val routing_domain_strategy: Array<out String> by lazy {
resources.getStringArray(R.array.routing_domain_strategy)
}
Expand All @@ -47,6 +50,9 @@ class RoutingSettingActivity : BaseActivity() {
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = adapter

mItemTouchHelper = ItemTouchHelper(SimpleItemTouchHelperCallback(adapter))
mItemTouchHelper?.attachToRecyclerView(binding.recyclerView)

val found = Utils.arrayFind(routing_domain_strategy, settingsStorage?.decodeString(AppConfig.PREF_ROUTING_DOMAIN_STRATEGY) ?: "")
found.let { binding.spDomainStrategy.setSelection(if (it >= 0) it else 0) }
binding.spDomainStrategy.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
Expand Down Expand Up @@ -109,7 +115,7 @@ class RoutingSettingActivity : BaseActivity() {
else -> super.onOptionsItemSelected(item)
}

private fun refreshData() {
fun refreshData() {
rulesets = MmkvManager.decodeRoutingRulesets() ?: mutableListOf()
adapter.notifyDataSetChanged()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package com.neko.v2ray.ui
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.neko.v2ray.databinding.ItemRecyclerRoutingSettingBinding
import com.neko.v2ray.helper.ItemTouchHelperAdapter
import com.neko.v2ray.helper.ItemTouchHelperViewHolder
import com.neko.v2ray.ui.MainRecyclerAdapter.BaseViewHolder
import com.neko.v2ray.util.SettingsManager

class RoutingSettingRecyclerAdapter(val activity: RoutingSettingActivity) :
RecyclerView.Adapter<RoutingSettingRecyclerAdapter.MainViewHolder>() {
class RoutingSettingRecyclerAdapter(val activity: RoutingSettingActivity) : RecyclerView.Adapter<RoutingSettingRecyclerAdapter.MainViewHolder>(),
ItemTouchHelperAdapter {

private var mActivity: RoutingSettingActivity = activity
override fun getItemCount() = mActivity.rulesets.size
Expand Down Expand Up @@ -47,5 +51,28 @@ class RoutingSettingRecyclerAdapter(val activity: RoutingSettingActivity) :
}

class MainViewHolder(val itemRoutingSettingBinding: ItemRecyclerRoutingSettingBinding) :
RecyclerView.ViewHolder(itemRoutingSettingBinding.root)
BaseViewHolder(itemRoutingSettingBinding.root), ItemTouchHelperViewHolder

open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY)
}

fun onItemClear() {
itemView.setBackgroundColor(0)
}
}

override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
SettingsManager.swapRoutingRuleset(fromPosition, toPosition)
notifyItemMoved(fromPosition, toPosition)
return true
}

override fun onItemMoveCompleted() {
mActivity.refreshData()
}

override fun onItemDismiss(position: Int) {
}
}
4 changes: 1 addition & 3 deletions app/src/main/kotlin/com/neko/v2ray/ui/SubEditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ class SubEditActivity : BaseActivity() {
private fun saveServer(): Boolean {
val subItem: SubscriptionItem
val json = subStorage?.decodeString(editSubId)
var subId = editSubId
if (!json.isNullOrBlank()) {
subItem = Gson().fromJson(json, SubscriptionItem::class.java)
} else {
subId = Utils.getUuid()
subItem = SubscriptionItem()
}

Expand All @@ -147,7 +145,7 @@ class SubEditActivity : BaseActivity() {
// return false
// }

subStorage?.encode(subId, Gson().toJson(subItem))
MmkvManager.encodeSubscription(editSubId, subItem)
toast(R.string.toast_success)
finish()
return true
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/kotlin/com/neko/v2ray/ui/SubSettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package com.neko.v2ray.ui

import android.content.Intent
import android.os.Bundle
import com.google.android.material.appbar.MaterialToolbar
import android.view.Menu
import android.view.MenuItem
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.neko.v2ray.R
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.neko.v2ray.databinding.ActivitySubSettingBinding
import com.neko.v2ray.databinding.LayoutProgressBinding
import com.neko.v2ray.dto.SubscriptionItem
import com.neko.v2ray.extension.toast
import com.neko.v2ray.helper.SimpleItemTouchHelperCallback
import com.neko.v2ray.util.AngConfigManager
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.SoftInputAssist
Expand All @@ -27,6 +29,7 @@ class SubSettingActivity : BaseActivity() {

var subscriptions: List<Pair<String, SubscriptionItem>> = listOf()
private val adapter by lazy { SubSettingRecyclerAdapter(this) }
private var mItemTouchHelper: ItemTouchHelper? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -36,19 +39,20 @@ class SubSettingActivity : BaseActivity() {
val toolbarLayout = findViewById<CollapsingToolbarLayout>(R.id.collapsing_toolbar)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
softInputAssist = SoftInputAssist(this)

binding.recyclerView.setHasFixedSize(true)
binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = adapter

softInputAssist = SoftInputAssist(this)
mItemTouchHelper = ItemTouchHelper(SimpleItemTouchHelperCallback(adapter))
mItemTouchHelper?.attachToRecyclerView(binding.recyclerView)
}

override fun onResume() {
softInputAssist.onResume()
super.onResume()
subscriptions = MmkvManager.decodeSubscriptions()
adapter.notifyDataSetChanged()
refreshData()
}

override fun onPause() {
Expand Down Expand Up @@ -97,4 +101,9 @@ class SubSettingActivity : BaseActivity() {
else -> super.onOptionsItemSelected(item)

}

fun refreshData() {
subscriptions = MmkvManager.decodeSubscriptions()
adapter.notifyDataSetChanged()
}
}
37 changes: 31 additions & 6 deletions app/src/main/kotlin/com/neko/v2ray/ui/SubSettingRecyclerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import android.view.View
import android.view.ViewGroup
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ItemQrcodeBinding
import com.neko.v2ray.databinding.ItemRecyclerSubSettingBinding
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager.subStorage
import com.neko.v2ray.helper.ItemTouchHelperAdapter
import com.neko.v2ray.helper.ItemTouchHelperViewHolder
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.QRCodeDecoder
import com.neko.v2ray.util.SettingsManager
import com.neko.v2ray.util.Utils

class SubSettingRecyclerAdapter(val activity: SubSettingActivity) :
RecyclerView.Adapter<SubSettingRecyclerAdapter.MainViewHolder>() {
class SubSettingRecyclerAdapter(val activity: SubSettingActivity) : RecyclerView.Adapter<SubSettingRecyclerAdapter.MainViewHolder>(), ItemTouchHelperAdapter {

private var mActivity: SubSettingActivity = activity

Expand All @@ -44,7 +45,8 @@ class SubSettingRecyclerAdapter(val activity: SubSettingActivity) :
}
holder.itemSubSettingBinding.chkEnable.setOnCheckedChangeListener { _, isChecked ->
subItem.enabled = isChecked
subStorage?.encode(subId, Gson().toJson(subItem))
MmkvManager.encodeSubscription(subId, subItem)

}

if (TextUtils.isEmpty(subItem.url)) {
Expand Down Expand Up @@ -92,5 +94,28 @@ class SubSettingRecyclerAdapter(val activity: SubSettingActivity) :
}

class MainViewHolder(val itemSubSettingBinding: ItemRecyclerSubSettingBinding) :
RecyclerView.ViewHolder(itemSubSettingBinding.root)
BaseViewHolder(itemSubSettingBinding.root), ItemTouchHelperViewHolder

open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY)
}

fun onItemClear() {
itemView.setBackgroundColor(0)
}
}

override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
SettingsManager.swapSubscriptions(fromPosition, toPosition)
notifyItemMoved(fromPosition, toPosition)
return true
}

override fun onItemMoveCompleted() {
mActivity.refreshData()
}

override fun onItemDismiss(position: Int) {
}
}
Loading

0 comments on commit 78c5f76

Please sign in to comment.