Refactor RoutingSetting adapter to use listener interface
This commit is contained in:
1 parent
de1c8bcfcc
commit
bb1f677d45
5 files changed
+30
-14
No files matched your search
@@ -8,8 +8,9 @@ interface BaseAdapterListener {
|
|||||||
/**
|
/**
|
||||||
* Request the host to edit the specified item.
|
* Request the host to edit the specified item.
|
||||||
* @param guid Unique identifier (GUID) of the item
|
* @param guid Unique identifier (GUID) of the item
|
||||||
|
* @param position Current position in the adapter (optional; host should validate it)
|
||||||
*/
|
*/
|
||||||
fun onEdit(guid: String)
|
fun onEdit(guid: String, position: Int)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request the host to remove the specified item. Position is provided for optional animation or validation.
|
* Request the host to remove the specified item. Position is provided for optional animation or validation.
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ import kotlinx.coroutines.withContext
|
|||||||
|
|
||||||
class RoutingSettingActivity : BaseActivity() {
|
class RoutingSettingActivity : BaseActivity() {
|
||||||
private val binding by lazy { ActivityRoutingSettingBinding.inflate(layoutInflater) }
|
private val binding by lazy { ActivityRoutingSettingBinding.inflate(layoutInflater) }
|
||||||
|
private val ownerActivity: RoutingSettingActivity
|
||||||
|
get() = this
|
||||||
private val viewModel: RoutingSettingsViewModel by viewModels()
|
private val viewModel: RoutingSettingsViewModel by viewModels()
|
||||||
private lateinit var adapter: RoutingSettingRecyclerAdapter
|
private lateinit var adapter: RoutingSettingRecyclerAdapter
|
||||||
private var mItemTouchHelper: ItemTouchHelper? = null
|
private var mItemTouchHelper: ItemTouchHelper? = null
|
||||||
@@ -57,7 +58,7 @@ class RoutingSettingActivity : BaseActivity() {
|
|||||||
//setContentView(binding.root)
|
//setContentView(binding.root)
|
||||||
setContentViewWithToolbar(binding.root, showHomeAsUp = true, title = getString(R.string.routing_settings_title))
|
setContentViewWithToolbar(binding.root, showHomeAsUp = true, title = getString(R.string.routing_settings_title))
|
||||||
|
|
||||||
adapter = RoutingSettingRecyclerAdapter(this, viewModel)
|
adapter = RoutingSettingRecyclerAdapter(viewModel, ActivityAdapterListener())
|
||||||
|
|
||||||
binding.recyclerView.setHasFixedSize(true)
|
binding.recyclerView.setHasFixedSize(true)
|
||||||
binding.recyclerView.layoutManager = LinearLayoutManager(this)
|
binding.recyclerView.layoutManager = LinearLayoutManager(this)
|
||||||
@@ -202,4 +203,23 @@ class RoutingSettingActivity : BaseActivity() {
|
|||||||
viewModel.reload()
|
viewModel.reload()
|
||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private inner class ActivityAdapterListener : BaseAdapterListener {
|
||||||
|
override fun onEdit(guid: String, position: Int) {
|
||||||
|
startActivity(
|
||||||
|
Intent(ownerActivity, RoutingEditActivity::class.java)
|
||||||
|
.putExtra("position", position)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onRemove(guid: String, position: Int) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onShare(url: String) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onRefreshData() {
|
||||||
|
refreshData()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.v2ray.ang.ui
|
package com.v2ray.ang.ui
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
@@ -13,12 +12,11 @@ import com.v2ray.ang.helper.ItemTouchHelperViewHolder
|
|||||||
import com.v2ray.ang.viewmodel.RoutingSettingsViewModel
|
import com.v2ray.ang.viewmodel.RoutingSettingsViewModel
|
||||||
|
|
||||||
class RoutingSettingRecyclerAdapter(
|
class RoutingSettingRecyclerAdapter(
|
||||||
val activity: RoutingSettingActivity,
|
private val viewModel: RoutingSettingsViewModel,
|
||||||
private val viewModel: RoutingSettingsViewModel
|
private val adapterListener: BaseAdapterListener?
|
||||||
) : RecyclerView.Adapter<RoutingSettingRecyclerAdapter.MainViewHolder>(),
|
) : RecyclerView.Adapter<RoutingSettingRecyclerAdapter.MainViewHolder>(),
|
||||||
ItemTouchHelperAdapter {
|
ItemTouchHelperAdapter {
|
||||||
|
|
||||||
private var mActivity: RoutingSettingActivity = activity
|
|
||||||
override fun getItemCount() = viewModel.getAll().size
|
override fun getItemCount() = viewModel.getAll().size
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
|
||||||
@@ -33,10 +31,7 @@ class RoutingSettingRecyclerAdapter(
|
|||||||
holder.itemView.setBackgroundColor(Color.TRANSPARENT)
|
holder.itemView.setBackgroundColor(Color.TRANSPARENT)
|
||||||
|
|
||||||
holder.itemRoutingSettingBinding.layoutEdit.setOnClickListener {
|
holder.itemRoutingSettingBinding.layoutEdit.setOnClickListener {
|
||||||
mActivity.startActivity(
|
adapterListener?.onEdit("", position)
|
||||||
Intent(mActivity, RoutingEditActivity::class.java)
|
|
||||||
.putExtra("position", position)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
holder.itemRoutingSettingBinding.chkEnable.setOnCheckedChangeListener { it, isChecked ->
|
holder.itemRoutingSettingBinding.chkEnable.setOnCheckedChangeListener { it, isChecked ->
|
||||||
@@ -76,7 +71,7 @@ class RoutingSettingRecyclerAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onItemMoveCompleted() {
|
override fun onItemMoveCompleted() {
|
||||||
mActivity.refreshData()
|
adapterListener?.onRefreshData()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onItemDismiss(position: Int) {
|
override fun onItemDismiss(position: Int) {
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class SubSettingActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private inner class ActivityAdapterListener : BaseAdapterListener {
|
private inner class ActivityAdapterListener : BaseAdapterListener {
|
||||||
override fun onEdit(guid: String) {
|
override fun onEdit(guid: String, position: Int) {
|
||||||
startActivity(
|
startActivity(
|
||||||
Intent(ownerActivity, SubEditActivity::class.java)
|
Intent(ownerActivity, SubEditActivity::class.java)
|
||||||
.putExtra("subId", guid)
|
.putExtra("subId", guid)
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class SubSettingRecyclerAdapter(
|
|||||||
holder.itemView.setBackgroundColor(Color.TRANSPARENT)
|
holder.itemView.setBackgroundColor(Color.TRANSPARENT)
|
||||||
|
|
||||||
holder.itemSubSettingBinding.layoutEdit.setOnClickListener {
|
holder.itemSubSettingBinding.layoutEdit.setOnClickListener {
|
||||||
adapterListener?.onEdit(subId)
|
adapterListener?.onEdit(subId, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
holder.itemSubSettingBinding.layoutRemove.setOnClickListener {
|
holder.itemSubSettingBinding.layoutRemove.setOnClickListener {
|
||||||
|
|||||||
Reference in new issue
Block a user