Dialogs refactoring
This commit is contained in:
parent
5a8cc0f14d
commit
30b100b62f
|
@ -2,76 +2,49 @@ package com.simplemobiletools.smsmessenger.dialogs
|
||||||
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
|
||||||
import com.simplemobiletools.commons.extensions.*
|
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.R
|
||||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||||
import com.simplemobiletools.smsmessenger.extensions.config
|
import com.simplemobiletools.smsmessenger.extensions.config
|
||||||
import com.simplemobiletools.smsmessenger.helpers.EXPORT_FILE_EXT
|
import kotlinx.android.synthetic.main.dialog_export_messages.view.export_messages_filename
|
||||||
import kotlinx.android.synthetic.main.dialog_export_messages.view.*
|
import kotlinx.android.synthetic.main.dialog_export_messages.view.export_mms_checkbox
|
||||||
import java.io.File
|
import kotlinx.android.synthetic.main.dialog_export_messages.view.export_sms_checkbox
|
||||||
|
|
||||||
class ExportMessagesDialog(
|
class ExportMessagesDialog(
|
||||||
private val activity: SimpleActivity,
|
private val activity: SimpleActivity,
|
||||||
private val path: String,
|
private val callback: (fileName: String) -> Unit,
|
||||||
private val hidePath: Boolean,
|
|
||||||
private val callback: (file: File) -> Unit,
|
|
||||||
) {
|
) {
|
||||||
private var realPath = if (path.isEmpty()) activity.internalStoragePath else path
|
|
||||||
private val config = activity.config
|
private val config = activity.config
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_export_messages, null) as ViewGroup).apply {
|
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_sms_checkbox.isChecked = config.exportSms
|
||||||
export_mms_checkbox.isChecked = config.exportMms
|
export_mms_checkbox.isChecked = config.exportMms
|
||||||
|
export_messages_filename.setText("${activity.getString(R.string.messages)}_${activity.getCurrentFormattedDateTime()}")
|
||||||
|
}
|
||||||
|
|
||||||
if (hidePath) {
|
activity.getAlertDialogBuilder().setPositiveButton(R.string.ok, null).setNegativeButton(R.string.cancel, null).apply {
|
||||||
export_messages_folder_hint.beGone()
|
activity.setupDialogStuff(view, this, R.string.export_messages) { alertDialog ->
|
||||||
} else {
|
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||||
export_messages_folder.setOnClickListener {
|
config.exportSms = view.export_sms_checkbox.isChecked
|
||||||
activity.hideKeyboard(export_messages_filename)
|
config.exportMms = view.export_mms_checkbox.isChecked
|
||||||
FilePickerDialog(activity, realPath, false, showFAB = true) {
|
val filename = view.export_messages_filename.value
|
||||||
export_messages_folder.setText(activity.humanizePath(it))
|
when {
|
||||||
realPath = it
|
filename.isEmpty() -> activity.toast(R.string.empty_name)
|
||||||
|
filename.isAValidFilename() -> {
|
||||||
|
callback(filename)
|
||||||
|
alertDialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> activity.toast(R.string.invalid_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
|
||||||
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)
|
|
||||||
alertDialog.dismiss()
|
|
||||||
}
|
|
||||||
else -> activity.toast(R.string.invalid_name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,13 @@ import com.simplemobiletools.smsmessenger.R
|
||||||
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||||
import com.simplemobiletools.smsmessenger.extensions.config
|
import com.simplemobiletools.smsmessenger.extensions.config
|
||||||
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter
|
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter
|
||||||
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_OK
|
import com.simplemobiletools.smsmessenger.models.MessagesBackup
|
||||||
import com.simplemobiletools.smsmessenger.helpers.MessagesImporter.ImportResult.IMPORT_PARTIAL
|
import com.simplemobiletools.smsmessenger.models.ImportResult
|
||||||
import kotlinx.android.synthetic.main.dialog_import_messages.view.*
|
import kotlinx.android.synthetic.main.dialog_import_messages.view.*
|
||||||
|
|
||||||
class ImportMessagesDialog(
|
class ImportMessagesDialog(
|
||||||
private val activity: SimpleActivity,
|
private val activity: SimpleActivity,
|
||||||
private val path: String,
|
private val messages: List<MessagesBackup>,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val config = activity.config
|
private val config = activity.config
|
||||||
|
@ -48,7 +48,7 @@ class ImportMessagesDialog(
|
||||||
config.importSms = view.import_sms_checkbox.isChecked
|
config.importSms = view.import_sms_checkbox.isChecked
|
||||||
config.importMms = view.import_mms_checkbox.isChecked
|
config.importMms = view.import_mms_checkbox.isChecked
|
||||||
ensureBackgroundThread {
|
ensureBackgroundThread {
|
||||||
MessagesImporter(activity).importMessages(path) {
|
MessagesImporter(activity).importMessages(messages) {
|
||||||
handleParseResult(it)
|
handleParseResult(it)
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
}
|
}
|
||||||
|
@ -58,11 +58,12 @@ class ImportMessagesDialog(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleParseResult(result: MessagesImporter.ImportResult) {
|
private fun handleParseResult(result: ImportResult) {
|
||||||
activity.toast(
|
activity.toast(
|
||||||
when (result) {
|
when (result) {
|
||||||
IMPORT_OK -> R.string.importing_successful
|
ImportResult.IMPORT_OK -> R.string.importing_successful
|
||||||
IMPORT_PARTIAL -> R.string.importing_some_entries_failed
|
ImportResult.IMPORT_PARTIAL -> R.string.importing_some_entries_failed
|
||||||
|
ImportResult.IMPORT_FAIL -> R.string.importing_failed
|
||||||
else -> R.string.no_items_found
|
else -> R.string.no_items_found
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,21 +14,6 @@
|
||||||
android:paddingTop="@dimen/activity_margin"
|
android:paddingTop="@dimen/activity_margin"
|
||||||
android:paddingEnd="@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
|
<com.simplemobiletools.commons.views.MyTextInputLayout
|
||||||
android:id="@+id/export_messages_filename_hint"
|
android:id="@+id/export_messages_filename_hint"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
Loading…
Reference in New Issue