Dialogs refactoring

This commit is contained in:
merkost 2023-07-18 16:22:14 +10:00
parent 5a8cc0f14d
commit 30b100b62f
3 changed files with 34 additions and 75 deletions

View File

@ -2,72 +2,45 @@ package com.simplemobiletools.smsmessenger.dialogs
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
import com.simplemobiletools.commons.extensions.getCurrentFormattedDateTime
import com.simplemobiletools.commons.extensions.isAValidFilename
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.helpers.EXPORT_FILE_EXT
import kotlinx.android.synthetic.main.dialog_export_messages.view.*
import java.io.File
import kotlinx.android.synthetic.main.dialog_export_messages.view.export_messages_filename
import kotlinx.android.synthetic.main.dialog_export_messages.view.export_mms_checkbox
import kotlinx.android.synthetic.main.dialog_export_messages.view.export_sms_checkbox
class ExportMessagesDialog(
private val activity: SimpleActivity,
private val path: String,
private val hidePath: Boolean,
private val callback: (file: File) -> Unit,
private val callback: (fileName: String) -> Unit,
) {
private var realPath = if (path.isEmpty()) activity.internalStoragePath else path
private val config = activity.config
init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_export_messages, null) as ViewGroup).apply {
export_messages_folder.setText(activity.humanizePath(realPath))
export_messages_filename.setText("${activity.getString(R.string.messages)}_${activity.getCurrentFormattedDateTime()}")
export_sms_checkbox.isChecked = config.exportSms
export_mms_checkbox.isChecked = config.exportMms
if (hidePath) {
export_messages_folder_hint.beGone()
} else {
export_messages_folder.setOnClickListener {
activity.hideKeyboard(export_messages_filename)
FilePickerDialog(activity, realPath, false, showFAB = true) {
export_messages_folder.setText(activity.humanizePath(it))
realPath = it
}
}
}
export_messages_filename.setText("${activity.getString(R.string.messages)}_${activity.getCurrentFormattedDateTime()}")
}
activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.apply {
activity.getAlertDialogBuilder().setPositiveButton(R.string.ok, null).setNegativeButton(R.string.cancel, null).apply {
activity.setupDialogStuff(view, this, R.string.export_messages) { alertDialog ->
alertDialog.showKeyboard(view.export_messages_filename)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
config.exportSms = view.export_sms_checkbox.isChecked
config.exportMms = view.export_mms_checkbox.isChecked
val filename = view.export_messages_filename.value
when {
filename.isEmpty() -> activity.toast(R.string.empty_name)
filename.isAValidFilename() -> {
val file = File(realPath, "$filename$EXPORT_FILE_EXT")
if (!hidePath && file.exists()) {
activity.toast(R.string.name_taken)
return@setOnClickListener
}
if (!view.export_sms_checkbox.isChecked && !view.export_mms_checkbox.isChecked) {
activity.toast(R.string.no_option_selected)
return@setOnClickListener
}
config.exportSms = view.export_sms_checkbox.isChecked
config.exportMms = view.export_mms_checkbox.isChecked
config.lastExportPath = file.absolutePath.getParentPath()
callback(file)
callback(filename)
alertDialog.dismiss()
}
else -> activity.toast(R.string.invalid_name)
}
}

View File

@ -10,13 +10,13 @@ import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_OK
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_PARTIAL
import com.simplemobiletools.smsmessenger.models.MessagesBackup
import com.simplemobiletools.smsmessenger.models.ImportResult
import kotlinx.android.synthetic.main.dialog_import_messages.view.*
class ImportMessagesDialog(
private val activity: SimpleActivity,
private val path: String,
private val messages: List<MessagesBackup>,
) {
private val config = activity.config
@ -48,7 +48,7 @@ class ImportMessagesDialog(
config.importSms = view.import_sms_checkbox.isChecked
config.importMms = view.import_mms_checkbox.isChecked
ensureBackgroundThread {
MessagesImporter(activity).importMessages(path) {
MessagesImporter(activity).importMessages(messages) {
handleParseResult(it)
alertDialog.dismiss()
}
@ -58,11 +58,12 @@ class ImportMessagesDialog(
}
}
private fun handleParseResult(result: MessagesImporter.ImportResult) {
private fun handleParseResult(result: ImportResult) {
activity.toast(
when (result) {
IMPORT_OK -> R.string.importing_successful
IMPORT_PARTIAL -> R.string.importing_some_entries_failed
ImportResult.IMPORT_OK -> R.string.importing_successful
ImportResult.IMPORT_PARTIAL -> R.string.importing_some_entries_failed
ImportResult.IMPORT_FAIL -> R.string.importing_failed
else -> R.string.no_items_found
}
)

View File

@ -14,21 +14,6 @@
android:paddingTop="@dimen/activity_margin"
android:paddingEnd="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextInputLayout
android:id="@+id/export_messages_folder_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:hint="@string/folder">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/export_messages_folder"
style="@style/UnclickableEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.simplemobiletools.commons.views.MyTextInputLayout>
<com.simplemobiletools.commons.views.MyTextInputLayout
android:id="@+id/export_messages_filename_hint"
android:layout_width="match_parent"