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

View file

@ -312,7 +312,7 @@
<string name="go_back_30">-30</string> <string name="go_back_30">-30</string>
<string name="go_forward_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_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_time_left" formatted="true">%dm\nremaining</string>
<string name="resume_remaining" formatted="true">%s\nremaining</string> <string name="resume_remaining" formatted="true">%s\nremaining</string>
<string name="status_ongoing">Ongoing</string> <string name="status_ongoing">Ongoing</string>