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 3 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 @@ -36,6 +36,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 @@ -55,4 +59,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 @@ -19,6 +19,8 @@ internal interface HttpTransactionRepository {

suspend fun deleteAllTransactions()

suspend fun deleteSelectedTransactions(selectedTransactions: List<Long>)

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

fun getFilteredTransactionTuples(code: String, path: String): LiveData<List<HttpTransactionTuple>>
Expand All @@ -28,4 +30,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 @@ -40,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 @@ -49,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 @@ -46,6 +46,7 @@ internal class MainActivity :

private lateinit var mainBinding: ChuckerActivityMainBinding
private lateinit var transactionsAdapter: TransactionAdapter
private val selectedTransactions = mutableListOf<Long>()

private val applicationName: CharSequence
get() = applicationInfo.loadLabel(packageManager)
Expand All @@ -66,9 +67,26 @@ internal class MainActivity :
super.onCreate(savedInstanceState)

mainBinding = ChuckerActivityMainBinding.inflate(layoutInflater)
transactionsAdapter = TransactionAdapter(this) { transactionId ->
TransactionActivity.start(this, transactionId)
}
transactionsAdapter =
TransactionAdapter(
context = this,
longPress = { transactionId ->
selectedTransactions.add(transactionId)
},
onTransactionClick = { transactionId ->
if (selectedTransactions.isNotEmpty()) {
if (selectedTransactions.contains(transactionId)) {
rohitjakhar marked this conversation as resolved.
Show resolved Hide resolved
selectedTransactions.remove(transactionId)
} else {
selectedTransactions.add(
transactionId
)
}
} else {
TransactionActivity.start(this, transactionId)
}
}
)

with(mainBinding) {
setContentView(root)
Expand Down Expand Up @@ -109,6 +127,7 @@ internal class MainActivity :
) == PackageManager.PERMISSION_GRANTED -> {
/* We have permission, all good */
}

shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
Snackbar.make(
mainBinding.root,
Expand All @@ -123,6 +142,7 @@ internal class MainActivity :
}
}.show()
}

else -> {
permissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
}
Expand All @@ -148,15 +168,16 @@ internal class MainActivity :
showDialog(
getClearDialogData(),
onPositiveClick = {
viewModel.clearTransactions()
if (selectedTransactions.isNotEmpty()) viewModel.clearSelectedTransactions(selectedTransactions) else viewModel.clearTransactions()
Copy link
Member

Choose a reason for hiding this comment

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

Can we move this logic inside hte ViewModel? Let's just have a viewModel.clearTransactions(selectedTransactions) that behaves differently wether the selectedTransactions is empty or not

},
onNegativeClick = null
)
true
}

R.id.share_text -> {
showDialog(
getExportDialogData(R.string.chucker_export_text_http_confirmation),
getExportDialogData(if (selectedTransactions.isNotEmpty()) 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 @@ -166,9 +187,10 @@ internal class MainActivity :
)
true
}

R.id.share_har -> {
showDialog(
getExportDialogData(R.string.chucker_export_har_http_confirmation),
getExportDialogData(if (selectedTransactions.isNotEmpty()) 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 All @@ -184,6 +206,7 @@ internal class MainActivity :
)
true
}

else -> {
super.onOptionsItemSelected(item)
}
Expand All @@ -203,7 +226,8 @@ internal class MainActivity :
) {
val applicationContext = this.applicationContext
lifecycleScope.launch {
val transactions = viewModel.getAllTransactions()
val transactions =
if (selectedTransactions.isNotEmpty()) viewModel.getSelectedTransactions(selectedTransactions) else viewModel.getAllTransactions()
if (transactions.isEmpty()) {
showToast(applicationContext.getString(R.string.chucker_export_empty_text))
return@launch
Expand All @@ -229,7 +253,7 @@ internal class MainActivity :

private fun getClearDialogData(): DialogData = DialogData(
title = getString(R.string.chucker_clear),
message = getString(R.string.chucker_clear_http_confirmation),
message = getString(if (selectedTransactions.isNotEmpty()) 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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal class MainViewModel : ViewModel() {

suspend fun getAllTransactions(): List<HttpTransaction> = RepositoryProvider.transaction().getAllTransactions()

suspend fun getSelectedTransactions(selectedTransaction: List<Long>): List<HttpTransaction> = RepositoryProvider.transaction().getSelectedTransactions(selectedTransaction)

fun updateItemsFilter(searchQuery: String) {
currentFilter.value = searchQuery
}
Expand All @@ -44,4 +46,10 @@ internal class MainViewModel : ViewModel() {
}
NotificationHelper.clearBuffer()
}

fun clearSelectedTransactions(selectedTransaction: List<Long>) {
viewModelScope.launch {
RepositoryProvider.transaction().deleteSelectedTransactions(selectedTransaction)
}
}
}
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 @@ -21,11 +22,13 @@ import javax.net.ssl.HttpsURLConnection

internal class TransactionAdapter internal constructor(
context: Context,
private val onTransactionClick: (Long) -> Unit
private val onTransactionClick: (Long) -> Unit,
private val longPress: (Long) -> Unit
) : ListAdapter<HttpTransactionTuple, TransactionAdapter.TransactionViewHolder>(
TransactionDiffCallback
) {

private val selectedPos = mutableListOf<Number>()
private val colorDefault: Int = ContextCompat.getColor(context, R.color.chucker_status_default)
private val colorRequested: Int = ContextCompat.getColor(
context,
Expand All @@ -35,6 +38,8 @@ 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()
private val defaultColor = context.theme.resolveAttribute(android.R.attr.selectableItemBackground, outValue, true)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder {
val viewBinding = ChuckerListItemTransactionBinding.inflate(
Expand All @@ -58,14 +63,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 All @@ -87,7 +117,6 @@ internal class TransactionAdapter internal constructor(
code.text = "!!!"
}
}

setStatusColor(transaction)
}

Expand Down Expand Up @@ -128,6 +157,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)
}
}
3 changes: 3 additions & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
<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>
rohitjakhar marked this conversation as resolved.
Show resolved Hide resolved
<string name="chucker_clear_selected_http_confirmation">Do you want to clear selected network calls history?</string>
rohitjakhar marked this conversation as resolved.
Show resolved Hide resolved
<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