-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show progress indicator when importing/exporting messages
One small step towards FossifyOrg/General-Discussion#120
- Loading branch information
1 parent
5ed8c0a
commit f66aa77
Showing
5 changed files
with
157 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 94 additions & 15 deletions
109
app/src/main/kotlin/org/fossify/messages/dialogs/ExportMessagesDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,126 @@ | ||
package org.fossify.messages.dialogs | ||
|
||
import android.annotation.SuppressLint | ||
import android.net.Uri | ||
import android.provider.DocumentsContract | ||
import androidx.appcompat.app.AlertDialog | ||
import org.fossify.commons.extensions.* | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.encodeToStream | ||
import org.fossify.commons.extensions.getAlertDialogBuilder | ||
import org.fossify.commons.extensions.getCurrentFormattedDateTime | ||
import org.fossify.commons.extensions.getProperPrimaryColor | ||
import org.fossify.commons.extensions.isAValidFilename | ||
import org.fossify.commons.extensions.setupDialogStuff | ||
import org.fossify.commons.extensions.showErrorToast | ||
import org.fossify.commons.extensions.toast | ||
import org.fossify.commons.extensions.value | ||
import org.fossify.commons.helpers.ensureBackgroundThread | ||
import org.fossify.messages.R | ||
import org.fossify.messages.activities.SimpleActivity | ||
import org.fossify.messages.databinding.DialogExportMessagesBinding | ||
import org.fossify.messages.extensions.config | ||
import org.fossify.messages.helpers.MessagesReader | ||
|
||
class ExportMessagesDialog( | ||
private val activity: SimpleActivity, | ||
private val callback: (fileName: String) -> Unit, | ||
) { | ||
private val config = activity.config | ||
private var dialog: AlertDialog? = null | ||
|
||
init { | ||
val binding = DialogExportMessagesBinding.inflate(activity.layoutInflater).apply { | ||
exportSmsCheckbox.isChecked = config.exportSms | ||
exportMmsCheckbox.isChecked = config.exportMms | ||
exportMessagesFilename.setText( | ||
activity.getString(R.string.messages) + "_" + activity.getCurrentFormattedDateTime() | ||
) | ||
} | ||
@SuppressLint("SetTextI18n") | ||
private val binding = DialogExportMessagesBinding.inflate(activity.layoutInflater).apply { | ||
exportSmsCheckbox.isChecked = config.exportSms | ||
exportMmsCheckbox.isChecked = config.exportMms | ||
exportMessagesFilename.setText( | ||
"${activity.getString(R.string.messages)}_${activity.getCurrentFormattedDateTime()}" | ||
) | ||
} | ||
|
||
init { | ||
activity.getAlertDialogBuilder() | ||
.setPositiveButton(org.fossify.commons.R.string.ok, null) | ||
.setNegativeButton(org.fossify.commons.R.string.cancel, null) | ||
.apply { | ||
activity.setupDialogStuff(binding.root, this, R.string.export_messages) { alertDialog -> | ||
activity.setupDialogStuff( | ||
view = binding.root, | ||
dialog = this, | ||
titleId = R.string.export_messages | ||
) { alertDialog -> | ||
dialog = alertDialog | ||
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { | ||
config.exportSms = binding.exportSmsCheckbox.isChecked | ||
config.exportMms = binding.exportMmsCheckbox.isChecked | ||
val filename = binding.exportMessagesFilename.value | ||
when { | ||
filename.isEmpty() -> activity.toast(org.fossify.commons.R.string.empty_name) | ||
filename.isAValidFilename() -> { | ||
callback(filename) | ||
alertDialog.dismiss() | ||
} | ||
|
||
filename.isAValidFilename() -> callback(filename) | ||
else -> activity.toast(org.fossify.commons.R.string.invalid_name) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun exportMessages(uri: Uri) { | ||
dialog!!.apply { | ||
setCanceledOnTouchOutside(false) | ||
arrayOf( | ||
binding.exportMmsCheckbox, | ||
binding.exportSmsCheckbox, | ||
getButton(AlertDialog.BUTTON_POSITIVE), | ||
getButton(AlertDialog.BUTTON_NEGATIVE) | ||
).forEach { | ||
it.isEnabled = false | ||
it.alpha = 0.6f | ||
} | ||
|
||
binding.exportProgress.setIndicatorColor(activity.getProperPrimaryColor()) | ||
binding.exportProgress.post { | ||
binding.exportProgress.show() | ||
} | ||
export(uri) | ||
} | ||
} | ||
|
||
@OptIn(kotlinx.serialization.ExperimentalSerializationApi::class) | ||
private fun export(uri: Uri) { | ||
ensureBackgroundThread { | ||
var success = false | ||
try { | ||
MessagesReader(activity).getMessagesToExport( | ||
getSms = config.exportSms, | ||
getMms = config.exportMms | ||
) { messagesToExport -> | ||
if (messagesToExport.isEmpty()) { | ||
activity.toast(org.fossify.commons.R.string.no_entries_for_exporting) | ||
dismiss() | ||
return@getMessagesToExport | ||
} | ||
val json = Json { encodeDefaults = true } | ||
activity.contentResolver.openOutputStream(uri)!!.buffered() | ||
.use { outputStream -> | ||
json.encodeToStream(messagesToExport, outputStream) | ||
} | ||
success = true | ||
activity.toast(org.fossify.commons.R.string.exporting_successful) | ||
} | ||
} catch (e: Throwable) { | ||
activity.showErrorToast(e.toString()) | ||
} finally { | ||
if (!success) { | ||
// delete the file to avoid leaving behind an empty/corrupt file | ||
try { | ||
DocumentsContract.deleteDocument(activity.contentResolver, uri) | ||
} catch (ignored: Exception) { | ||
// ignored because we don't want to show two error messages | ||
} | ||
} | ||
|
||
dismiss() | ||
} | ||
} | ||
} | ||
|
||
private fun dismiss() = dialog?.dismiss() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters