3
3
Fork 1
mirror of https://github.com/recloudstream/cloudstream.git synced 2024-08-15 01:53:11 +00:00

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
app/src/main
java/com/lagradost/cloudstream3/ui/download
res/values

View file

@ -106,52 +106,65 @@ class DownloadViewModel : ViewModel() {
// Only update list if different from the previous one to prevent duplicate initialization
if (visual != previousVisual) {
previousVisual = 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)
}
updateStorageStats(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 {
context.let { ctx ->
val ids: List<Int> = event.items.mapNotNull { it?.id }
.takeIf { it.isNotEmpty() } ?: return@let
val builder: AlertDialog.Builder = AlertDialog.Builder(ctx)
val dialogClickListener =
DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
deleteFilesAndUpdateSettings(ctx, ids, this@launchSafe)
}
DialogInterface.BUTTON_NEGATIVE -> {
val ids: List<Int> = event.items.mapNotNull { it?.id }
.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 -> {
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_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>