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,52 +106,65 @@ 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)
try {
val stat = StatFs(Environment.getExternalStorageDirectory().path)
val localBytesAvailable = stat.availableBytes
val localTotalBytes = stat.blockSizeLong * stat.blockCountLong
val localDownloadedBytes = visual.sumOf { it.totalBytes }
_usedBytes.postValue(localTotalBytes - localBytesAvailable - localDownloadedBytes)
_availableBytes.postValue(localBytesAvailable)
_downloadBytes.postValue(localDownloadedBytes)
} catch (t: Throwable) {
_downloadBytes.postValue(0)
logError(t)
}
_headerCards.postValue(visual) _headerCards.postValue(visual)
} }
} }
private fun updateStorageStats(visual: List<VisualDownloadHeaderCached>) {
try {
val stat = StatFs(Environment.getExternalStorageDirectory().path)
val localBytesAvailable = stat.availableBytes
val localTotalBytes = stat.blockSizeLong * stat.blockCountLong
val localDownloadedBytes = visual.sumOf { it.totalBytes }
_usedBytes.postValue(localTotalBytes - localBytesAvailable - localDownloadedBytes)
_availableBytes.postValue(localBytesAvailable)
_downloadBytes.postValue(localDownloadedBytes)
} catch (t: Throwable) {
_downloadBytes.postValue(0)
logError(t)
}
}
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@launchSafe
.takeIf { it.isNotEmpty() } ?: return@let
val builder: AlertDialog.Builder = AlertDialog.Builder(ctx) val names: List<String> = event.items.mapNotNull { it?.name }
val dialogClickListener = .takeIf { it.isNotEmpty() } ?: return@launchSafe
DialogInterface.OnClickListener { _, which ->
when (which) { showDeleteConfirmationDialog(context, ids, names)
DialogInterface.BUTTON_POSITIVE -> { }
deleteFilesAndUpdateSettings(ctx, ids, this@launchSafe)
} private fun showDeleteConfirmationDialog(context: Context, ids: List<Int>, names: List<String>) {
DialogInterface.BUTTON_NEGATIVE -> { 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 -> {
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)
)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
} catch (e: Exception) {
logError(e)
} }
try {
builder.setTitle(R.string.delete_files)
.setMessage(message)
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)
.show().setDefaultFocus()
} catch (e: Exception) {
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>