Improve handling and message

This commit is contained in:
Luna712 2024-07-05 15:53:10 -06:00 committed by GitHub
parent 4179292f10
commit 3016a5176f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 38 deletions

View file

@ -106,7 +106,12 @@ class DownloadViewModel : ViewModel() {
// Only update list if different from the previous one to prevent duplicate initialization
if (visual != previousVisual) {
previousVisual = visual
updateStorageStats(visual)
_headerCards.postValue(visual)
}
}
private fun updateStorageStats(visual: List<VisualDownloadHeaderCached>) {
try {
val stat = StatFs(Environment.getExternalStorageDirectory().path)
val localBytesAvailable = stat.availableBytes
@ -120,32 +125,41 @@ class DownloadViewModel : ViewModel() {
_downloadBytes.postValue(0)
logError(t)
}
_headerCards.postValue(visual)
}
}
fun handleMultiDelete(context: Context, event: DownloadDeleteEvent) = viewModelScope.launchSafe {
context.let { ctx ->
val ids: List<Int> = event.items.mapNotNull { it?.id }
.takeIf { it.isNotEmpty() } ?: return@let
val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
.takeIf { it.isNotEmpty() } ?: return@launchSafe
val names: List<String> = event.items.mapNotNull { it?.name }
.takeIf { it.isNotEmpty() } ?: return@launchSafe
showDeleteConfirmationDialog(context, ids, names)
}
private fun showDeleteConfirmationDialog(context: Context, ids: List<Int>, names: List<String>) {
val formattedNames = names.joinToString(separator = "\n") { "- $it" }
val message = context.getString(R.string.delete_message_multiple).format(formattedNames)
val builder: AlertDialog.Builder = AlertDialog.Builder(context)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
deleteFilesAndUpdateSettings(ctx, ids, this@launchSafe)
viewModelScope.launchSafe {
deleteFilesAndUpdateSettings(context, ids, this)
updateList(context)
}
}
DialogInterface.BUTTON_NEGATIVE -> {
// Do nothing on cancel
}
}
}
try {
builder.setTitle(R.string.delete_files)
.setMessage(
ctx.getString(R.string.delete_multiple_message)
)
.setMessage(message)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
@ -154,4 +168,3 @@ class DownloadViewModel : ViewModel() {
}
}
}
}

View file

@ -312,7 +312,7 @@
<string name="go_back_30">-30</string>
<string name="go_forward_30">+30</string>
<string name="delete_message" formatted="true">This will permanently delete %s\nAre you sure?</string>
<string name="delete_multiple_message" formatted="true">This will permanently delete all of: %s\nAre you sure?</string>
<string name="delete_message_multiple" formatted="true">Are you sure you want to delete the following items?\n\n%s</string>
<string name="resume_time_left" formatted="true">%dm\nremaining</string>
<string name="resume_remaining" formatted="true">%s\nremaining</string>
<string name="status_ongoing">Ongoing</string>