Skip to content
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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ internal class HttpTransactionDatabaseRepository(private val database: ChuckerDa
transactionDao.deleteAll()
}

override suspend fun deleteSelectedTransactions(selectedTransactions: List<Long>) {
transactionDao.deleteSelected(selectedTransactions)
}

override suspend fun insertTransaction(transaction: HttpTransaction) {
val id = transactionDao.insert(transaction)
transaction.id = id ?: 0
Expand All @@ -57,4 +61,8 @@ internal class HttpTransactionDatabaseRepository(private val database: ChuckerDa
val timestamp = minTimestamp ?: 0L
return transactionDao.getTransactionsInTimeRange(timestamp)
}

override suspend fun getSelectedTransactions(selectedTransactions: List<Long>): List<HttpTransaction> {
return transactionDao.getSelectedTransactions(selectedTransactions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple
* with [HttpTransaction] and [HttpTransactionTuple]. Please use [HttpTransactionDatabaseRepository] that
* uses Room and SqLite to run those operations.
*/
@Suppress("TooManyFunctions")
internal interface HttpTransactionRepository {
suspend fun insertTransaction(transaction: HttpTransaction)

Expand All @@ -18,6 +19,8 @@ internal interface HttpTransactionRepository {

suspend fun deleteAllTransactions()

suspend fun deleteSelectedTransactions(selectedTransactions: List<Long>)

fun getSortedTransactionTuples(): LiveData<List<HttpTransactionTuple>>

fun getFilteredTransactionTuples(
Expand All @@ -30,4 +33,6 @@ internal interface HttpTransactionRepository {
suspend fun getAllTransactions(): List<HttpTransaction>

fun getTransactionsInTimeRange(minTimestamp: Long?): List<HttpTransaction>

suspend fun getSelectedTransactions(selectedTransactions: List<Long>): List<HttpTransaction>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.room.Update
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple

@Suppress("TooManyFunctions")
@Dao
internal interface HttpTransactionDao {
@Query(
Expand Down Expand Up @@ -39,6 +40,9 @@ internal interface HttpTransactionDao {
@Query("DELETE FROM transactions")
suspend fun deleteAll(): Int

@Query("DELETE FROM transactions WHERE id IN (:selectedTransactions)")
suspend fun deleteSelected(selectedTransactions: List<Long>)

@Query("SELECT * FROM transactions WHERE id = :id")
fun getById(id: Long): LiveData<HttpTransaction?>

Expand All @@ -48,6 +52,9 @@ internal interface HttpTransactionDao {
@Query("SELECT * FROM transactions")
suspend fun getAll(): List<HttpTransaction>

@Query("SELECT * FROM transactions WHERE id IN (:selectedTransactions)")
suspend fun getSelectedTransactions(selectedTransactions: List<Long>): List<HttpTransaction>

@Query("SELECT * FROM transactions WHERE requestDate >= :timestamp")
fun getTransactionsInTimeRange(timestamp: Long): List<HttpTransaction>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal class MainActivity :

private lateinit var mainBinding: ChuckerActivityMainBinding
private lateinit var transactionsAdapter: TransactionAdapter
private var isMultipleSelected = false

private val applicationName: CharSequence
get() = applicationInfo.loadLabel(packageManager)
Expand All @@ -67,9 +68,19 @@ internal class MainActivity :

mainBinding = ChuckerActivityMainBinding.inflate(layoutInflater)
transactionsAdapter =
TransactionAdapter(this) { transactionId ->
TransactionActivity.start(this, transactionId)
}
TransactionAdapter(
context = this,
longPress = { transactionId ->
viewModel.selectItem(transactionId)
},
onTransactionClick = { transactionId ->
if (isMultipleSelected) {
viewModel.selectItem(transactionId)
} else {
TransactionActivity.start(this, transactionId)
}
},
)

with(mainBinding) {
setContentView(root)
Expand Down Expand Up @@ -99,6 +110,9 @@ internal class MainActivity :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
handleNotificationsPermission()
}
viewModel.isItemSelected.observe(this) {
isMultipleSelected = it
}
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
Expand Down Expand Up @@ -150,14 +164,21 @@ internal class MainActivity :
getClearDialogData(),
onPositiveClick = {
viewModel.clearTransactions()
resetSelection()
},
onNegativeClick = null,
)
true
}
R.id.share_text -> {
showDialog(
getExportDialogData(R.string.chucker_export_text_http_confirmation),
getExportDialogData(
if (viewModel.isItemSelected.value == true) {
R.string.chucker_export_text_selected_http_confirmation
} else {
R.string.chucker_export_text_http_confirmation
}
),
onPositiveClick = {
exportTransactions(EXPORT_TXT_FILE_NAME) { transactions ->
TransactionListDetailsSharable(transactions, encodeUrls = false)
Expand All @@ -169,7 +190,13 @@ internal class MainActivity :
}
R.id.share_har -> {
showDialog(
getExportDialogData(R.string.chucker_export_har_http_confirmation),
getExportDialogData(
if (viewModel.isItemSelected.value == true) {
R.string.chucker_export_har_selected_http_confirmation
} else {
R.string.chucker_export_har_http_confirmation
}
),
onPositiveClick = {
exportTransactions(EXPORT_HAR_FILE_NAME) { transactions ->
TransactionDetailsHarSharable(
Expand Down Expand Up @@ -198,6 +225,10 @@ internal class MainActivity :
return true
}

private fun resetSelection() {
transactionsAdapter.clearSelections()
}

private fun exportTransactions(
fileName: String,
block: suspend (List<HttpTransaction>) -> Sharable,
Expand All @@ -224,18 +255,25 @@ internal class MainActivity :
if (shareIntent != null) {
startActivity(shareIntent)
} else {
showToast(applicationContext.getString(R.string.chucker_export_no_file))
showToast(
applicationContext.getString(
R.string.chucker_export_no_file
)
)
}
}
}

private fun getClearDialogData(): DialogData =
DialogData(
title = getString(R.string.chucker_clear),
message = getString(R.string.chucker_clear_http_confirmation),
positiveButtonText = getString(R.string.chucker_clear),
negativeButtonText = getString(R.string.chucker_cancel),
)
private fun getClearDialogData(): DialogData = DialogData(
title = getString(R.string.chucker_clear),
message = getString(
if (viewModel.isItemSelected.value == true)
R.string.chucker_clear_selected_http_confirmation
else
R.string.chucker_clear_http_confirmation),
positiveButtonText = getString(R.string.chucker_clear),
negativeButtonText = getString(R.string.chucker_cancel)
)

private fun getExportDialogData(
@StringRes dialogMessage: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ->
Expand All @@ -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!!)
} 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!!)
} else {
RepositoryProvider.transaction().deleteAllTransactions()
}
}
NotificationHelper.clearBuffer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,9 +23,13 @@ import javax.net.ssl.HttpsURLConnection
internal class TransactionAdapter internal constructor(
context: Context,
private val onTransactionClick: (Long) -> Unit,
) : ListAdapter<HttpTransactionTuple, TransactionAdapter.TransactionViewHolder>(
TransactionDiffCallback,
private val longPress: (Long) -> Unit,
) : ListAdapter<
HttpTransactionTuple,
TransactionAdapter.TransactionViewHolder>(
TransactionDiffCallback
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatting here is odd and is causing ktlint to fail

) {
private val selectedPos = mutableListOf<Int>()
private val colorDefault: Int = ContextCompat.getColor(context, R.color.chucker_status_default)
private val colorRequested: Int =
ContextCompat.getColor(
Expand All @@ -35,6 +40,22 @@ 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)
val outValue = TypedValue()
@Suppress("UnusedPrivateProperty")
private val defaultColor =
context.theme.resolveAttribute(
android.R.attr.selectableItemBackground,
outValue,
true,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting is odd also here


fun clearSelections() {
val pos = selectedPos
selectedPos.clear()
pos.forEach {
notifyItemRemoved(it)
}
}

override fun onCreateViewHolder(
parent: ViewGroup,
Expand Down Expand Up @@ -63,14 +84,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
} ?: kotlin.run { false }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} ?: kotlin.run { false }
} ?: false

}
}

@SuppressLint("SetTextI18n")
fun bind(transaction: HttpTransactionTuple) {
transactionId = transaction.id

if (selectedPos.find { it == adapterPosition } != null) {
itemView.setBackgroundColor(color400)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not used this.
Let's create a new color resource as:

<color name="chucker_status_highlighted">#FF9800</color>

} else {
itemView.setBackgroundResource(outValue.resourceId)
}
itemBinding.apply {
displayGraphQlFields(transaction.graphQlOperationName, transaction.graphQlDetected)
path.text = "${transaction.method} ${transaction.getFormattedPath(encode = false)}"
Expand Down Expand Up @@ -134,6 +180,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)
}
}
5 changes: 4 additions & 1 deletion library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
<string name="chucker_share_all_transactions_title">Share transactions</string>
<string name="chucker_share_transaction_subject">Transaction details</string>
<string name="chucker_share_all_transactions_subject">All transactions</string>
<string name="chucker_clear_http_confirmation">Do you want to clear complete network calls history?</string>
<string name="chucker_clear_http_confirmation">Do you want to clear the complete network calls history?</string>
<string name="chucker_clear_selected_http_confirmation">Do you want to clear the selected network transactions?</string>
<string name="chucker_export_text_http_confirmation">Do you want to export all network transactions as a text file?</string>
<string name="chucker_export_text_selected_http_confirmation">Do you want to export selected network transactions as a text file?</string>
<string name="chucker_export_har_selected_http_confirmation">Do you want to export selected network transactions as a .har file?</string>
<string name="chucker_export_har_http_confirmation">Do you want to export all network transactions as a .har file?</string>
<string name="chucker_export_separator">==================</string>
<string name="chucker_export_prefix">/* Export Start */</string>
Expand Down
Loading