-
-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for select multiple request. #1130
base: main
Are you sure you want to change the base?
Changes from 13 commits
73437f8
6a9eb8d
abdb4c5
0850127
d80d386
d453d6d
7dad7ff
882d623
9e41073
5b58ed1
4a8ecdd
5ce5445
05427fe
d5461e4
c107409
137d8c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import android.text.TextUtils | |
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.distinctUntilChanged | ||
import androidx.lifecycle.switchMap | ||
import androidx.lifecycle.viewModelScope | ||
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction | ||
|
@@ -14,6 +15,11 @@ import kotlinx.coroutines.launch | |
|
||
internal class MainViewModel : ViewModel() { | ||
private val currentFilter = MutableLiveData("") | ||
private val selectedItemId: MutableLiveData<MutableList<Long>> = | ||
MutableLiveData<MutableList<Long>>(mutableListOf()) | ||
|
||
private var _isItemSelected = MutableLiveData<Boolean>(false) | ||
val isItemSelected = _isItemSelected.distinctUntilChanged() | ||
|
||
val transactions: LiveData<List<HttpTransactionTuple>> = | ||
currentFilter.switchMap { searchQuery -> | ||
|
@@ -32,15 +38,38 @@ internal class MainViewModel : ViewModel() { | |
} | ||
} | ||
|
||
suspend fun getAllTransactions(): List<HttpTransaction> = RepositoryProvider.transaction().getAllTransactions() | ||
fun selectItem(itemId: Long) { | ||
viewModelScope.launch { | ||
if (selectedItemId.value?.contains(itemId) == true) { | ||
selectedItemId.value?.remove(itemId) | ||
_isItemSelected.value = selectedItemId.value.isNullOrEmpty().not() == true | ||
} else { | ||
selectedItemId.value?.add(itemId) | ||
_isItemSelected.value = true | ||
} | ||
} | ||
} | ||
|
||
suspend fun getAllTransactions(): List<HttpTransaction> { | ||
return if (isItemSelected.value == true) { | ||
RepositoryProvider.transaction().getSelectedTransactions(selectedItemId.value!!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a safe check |
||
} else { | ||
RepositoryProvider.transaction().getAllTransactions() | ||
} | ||
} | ||
|
||
fun updateItemsFilter(searchQuery: String) { | ||
currentFilter.value = searchQuery | ||
} | ||
|
||
fun clearTransactions() { | ||
viewModelScope.launch { | ||
RepositoryProvider.transaction().deleteAllTransactions() | ||
if (isItemSelected.value == true) { | ||
_isItemSelected.value = false | ||
RepositoryProvider.transaction().deleteSelectedTransactions(selectedItemId.value!!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, can we use safe check ? instead of !! ? |
||
} else { | ||
RepositoryProvider.transaction().deleteAllTransactions() | ||
} | ||
} | ||
NotificationHelper.clearBuffer() | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ package com.chuckerteam.chucker.internal.ui.transaction | |||||||||
import android.annotation.SuppressLint | ||||||||||
import android.content.Context | ||||||||||
import android.content.res.ColorStateList | ||||||||||
import android.util.TypedValue | ||||||||||
import android.view.LayoutInflater | ||||||||||
import android.view.ViewGroup | ||||||||||
import androidx.appcompat.content.res.AppCompatResources | ||||||||||
|
@@ -22,9 +23,14 @@ import javax.net.ssl.HttpsURLConnection | |||||||||
internal class TransactionAdapter internal constructor( | ||||||||||
context: Context, | ||||||||||
private val onTransactionClick: (Long) -> Unit, | ||||||||||
) : ListAdapter<HttpTransactionTuple, TransactionAdapter.TransactionViewHolder>( | ||||||||||
private val longPress: (Long) -> Unit, | ||||||||||
) : ListAdapter< | ||||||||||
HttpTransactionTuple, | ||||||||||
TransactionAdapter.TransactionViewHolder, | ||||||||||
>( | ||||||||||
TransactionDiffCallback, | ||||||||||
) { | ||||||||||
private val selectedPos = mutableListOf<Int>() | ||||||||||
private val colorDefault: Int = ContextCompat.getColor(context, R.color.chucker_status_default) | ||||||||||
private val colorRequested: Int = | ||||||||||
ContextCompat.getColor( | ||||||||||
|
@@ -35,6 +41,32 @@ internal class TransactionAdapter internal constructor( | |||||||||
private val color500: Int = ContextCompat.getColor(context, R.color.chucker_status_500) | ||||||||||
private val color400: Int = ContextCompat.getColor(context, R.color.chucker_status_400) | ||||||||||
private val color300: Int = ContextCompat.getColor(context, R.color.chucker_status_300) | ||||||||||
private val chuckerStatusHighlighted: Int = ContextCompat.getColor(context, R.color.chucker_status_highlighted) | ||||||||||
val outValue = TypedValue() | ||||||||||
|
||||||||||
@Suppress("UnusedPrivateProperty") | ||||||||||
private val defaultColor = | ||||||||||
context.theme.resolveAttribute( | ||||||||||
android.R.attr.selectableItemBackground, | ||||||||||
outValue, | ||||||||||
true, | ||||||||||
) | ||||||||||
|
||||||||||
fun getSelectedItem(): List<Int> { | ||||||||||
return selectedPos | ||||||||||
} | ||||||||||
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
fun setSelectedItem(selectedItem: List<Int>) { | ||||||||||
selectedPos.addAll(selectedItem) | ||||||||||
} | ||||||||||
|
||||||||||
fun clearSelections() { | ||||||||||
val pos = selectedPos | ||||||||||
selectedPos.clear() | ||||||||||
pos.forEach { | ||||||||||
notifyItemRemoved(it) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
override fun onCreateViewHolder( | ||||||||||
parent: ViewGroup, | ||||||||||
|
@@ -63,14 +95,39 @@ internal class TransactionAdapter internal constructor( | |||||||||
itemView.setOnClickListener { | ||||||||||
transactionId?.let { | ||||||||||
onTransactionClick.invoke(it) | ||||||||||
if (selectedPos.isNotEmpty()) { | ||||||||||
if (selectedPos.contains(adapterPosition)) { | ||||||||||
selectedPos.remove(adapterPosition) | ||||||||||
} else { | ||||||||||
selectedPos.add(adapterPosition) | ||||||||||
} | ||||||||||
notifyItemChanged(adapterPosition) | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
itemView.setOnLongClickListener { | ||||||||||
transactionId?.let { | ||||||||||
longPress.invoke(it) | ||||||||||
if (selectedPos.contains(adapterPosition)) { | ||||||||||
selectedPos.remove(adapterPosition) | ||||||||||
} else { | ||||||||||
selectedPos.add(adapterPosition) | ||||||||||
} | ||||||||||
notifyItemChanged(adapterPosition) | ||||||||||
true | ||||||||||
} ?: false | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
@SuppressLint("SetTextI18n") | ||||||||||
fun bind(transaction: HttpTransactionTuple) { | ||||||||||
transactionId = transaction.id | ||||||||||
|
||||||||||
if (selectedPos.find { it == adapterPosition } != null) { | ||||||||||
itemView.setBackgroundColor(chuckerStatusHighlighted) | ||||||||||
} else { | ||||||||||
itemView.setBackgroundResource(outValue.resourceId) | ||||||||||
} | ||||||||||
itemBinding.apply { | ||||||||||
displayGraphQlFields(transaction.graphQlOperationName, transaction.graphQlDetected) | ||||||||||
path.text = "${transaction.method} ${transaction.getFormattedPath(encode = false)}" | ||||||||||
|
@@ -134,6 +191,7 @@ private fun ChuckerListItemTransactionBinding.displayGraphQlFields( | |||||||||
graphqlPath.isVisible = graphQLDetected | ||||||||||
|
||||||||||
if (graphQLDetected) { | ||||||||||
graphqlPath.text = graphQlOperationName ?: root.resources.getString(R.string.chucker_graphql_operation_is_empty) | ||||||||||
graphqlPath.text = graphQlOperationName | ||||||||||
?: root.resources.getString(R.string.chucker_graphql_operation_is_empty) | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of this, the function is now getting too long and letting detekt fail:
You can see it in the CI signal.
Can you export this + lines 193/199 to separate functions