Dialog refactoring
This commit is contained in:
parent
a518ba8a4e
commit
57c07e33fc
|
@ -1,69 +1,78 @@
|
|||
package com.simplemobiletools.contacts.pro.dialogs
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
||||
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
|
||||
import com.simplemobiletools.commons.extensions.hideKeyboard
|
||||
import com.simplemobiletools.commons.extensions.humanizePath
|
||||
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.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.ContactsHelper
|
||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||
import com.simplemobiletools.commons.models.contacts.Contact
|
||||
import com.simplemobiletools.commons.models.contacts.ContactSource
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
|
||||
import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter
|
||||
import com.simplemobiletools.contacts.pro.extensions.config
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_events_filename
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_events_filename_hint
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_events_folder
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.manage_event_types_holder
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_contact_sources_list
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_contacts_filename
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_contacts_filename_hint
|
||||
import kotlinx.android.synthetic.main.dialog_manage_automatic_backups.view.backup_contacts_folder
|
||||
import java.io.File
|
||||
|
||||
class ManageAutoBackupsDialog(private val activity: SimpleActivity, onSuccess: () -> Unit) {
|
||||
private val view = (activity.layoutInflater.inflate(R.layout.dialog_manage_automatic_backups, null) as ViewGroup)
|
||||
private val config = activity.config
|
||||
private var backupFolder = config.autoBackupFolder
|
||||
private var contactSources = mutableListOf<ContactSource>()
|
||||
private var selectedContactSources = config.autoBackupContactSources
|
||||
private var contacts = ArrayList<Contact>()
|
||||
private var isContactSourcesReady = false
|
||||
private var isContactsReady = false
|
||||
|
||||
init {
|
||||
view.apply {
|
||||
backup_events_folder.setText(activity.humanizePath(backupFolder))
|
||||
backup_contacts_folder.setText(activity.humanizePath(backupFolder))
|
||||
val filename = config.autoBackupFilename.ifEmpty {
|
||||
"${activity.getString(R.string.contacts)}_%Y%M%D_%h%m%s"
|
||||
}
|
||||
|
||||
backup_events_filename.setText(filename)
|
||||
backup_events_filename_hint.setEndIconOnClickListener {
|
||||
backup_contacts_filename.setText(filename)
|
||||
backup_contacts_filename_hint.setEndIconOnClickListener {
|
||||
DateTimePatternInfoDialog(activity)
|
||||
}
|
||||
|
||||
backup_events_filename_hint.setEndIconOnLongClickListener {
|
||||
backup_contacts_filename_hint.setEndIconOnLongClickListener {
|
||||
DateTimePatternInfoDialog(activity)
|
||||
true
|
||||
}
|
||||
|
||||
backup_events_folder.setOnClickListener {
|
||||
backup_contacts_folder.setOnClickListener {
|
||||
selectBackupFolder()
|
||||
}
|
||||
|
||||
manage_event_types_holder.setOnClickListener {
|
||||
activity.runOnUiThread {
|
||||
SelectContactTypesDialog(activity, selectedContactSources.toList()) {
|
||||
selectedContactSources = it.map { it.name }.toSet()
|
||||
config.autoBackupContactSources = it.map { it.name }.toSet()
|
||||
}
|
||||
}
|
||||
ContactsHelper(activity).getContactSources { sources ->
|
||||
contactSources = sources
|
||||
isContactSourcesReady = true
|
||||
processDataIfReady(this)
|
||||
}
|
||||
|
||||
ContactsHelper(activity).getContacts(getAll = true) { receivedContacts ->
|
||||
contacts = receivedContacts
|
||||
isContactsReady = true
|
||||
processDataIfReady(this)
|
||||
}
|
||||
}
|
||||
|
||||
activity.getAlertDialogBuilder()
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.apply {
|
||||
activity.setupDialogStuff(view, this, R.string.manage_automatic_backups) { dialog ->
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||
val filename = view.backup_events_filename.value
|
||||
if (view.backup_contact_sources_list.adapter == null) {
|
||||
return@setOnClickListener
|
||||
}
|
||||
val filename = view.backup_contacts_filename.value
|
||||
when {
|
||||
filename.isEmpty() -> activity.toast(R.string.empty_name)
|
||||
filename.isAValidFilename() -> {
|
||||
|
@ -73,11 +82,14 @@ class ManageAutoBackupsDialog(private val activity: SimpleActivity, onSuccess: (
|
|||
return@setOnClickListener
|
||||
}
|
||||
|
||||
if (selectedContactSources.isEmpty()) {
|
||||
val selectedSources = (view.backup_contact_sources_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
|
||||
if (selectedSources.isEmpty()) {
|
||||
activity.toast(R.string.no_entries_for_exporting)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
config.autoBackupContactSources = selectedSources.map { it.name }.toSet()
|
||||
|
||||
ensureBackgroundThread {
|
||||
config.apply {
|
||||
autoBackupFolder = backupFolder
|
||||
|
@ -99,8 +111,27 @@ class ManageAutoBackupsDialog(private val activity: SimpleActivity, onSuccess: (
|
|||
}
|
||||
}
|
||||
|
||||
private fun processDataIfReady(view: View) {
|
||||
if (!isContactSourcesReady || !isContactsReady) {
|
||||
return
|
||||
}
|
||||
|
||||
val contactSourcesWithCount = mutableListOf<ContactSource>()
|
||||
for (source in contactSources) {
|
||||
val count = contacts.filter { it.source == source.name }.count()
|
||||
contactSourcesWithCount.add(source.copy(count = count))
|
||||
}
|
||||
|
||||
contactSources.clear()
|
||||
contactSources.addAll(contactSourcesWithCount)
|
||||
|
||||
activity.runOnUiThread {
|
||||
view.backup_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, selectedContactSources.toList())
|
||||
}
|
||||
}
|
||||
|
||||
private fun selectBackupFolder() {
|
||||
activity.hideKeyboard(view.backup_events_filename)
|
||||
activity.hideKeyboard(view.backup_contacts_filename)
|
||||
FilePickerDialog(activity, backupFolder, false, showFAB = true) { path ->
|
||||
activity.handleSAFDialog(path) { grantedSAF ->
|
||||
if (!grantedSAF) {
|
||||
|
@ -113,7 +144,7 @@ class ManageAutoBackupsDialog(private val activity: SimpleActivity, onSuccess: (
|
|||
}
|
||||
|
||||
backupFolder = path
|
||||
view.backup_events_folder.setText(activity.humanizePath(path))
|
||||
view.backup_contacts_folder.setText(activity.humanizePath(path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
package com.simplemobiletools.contacts.pro.dialogs
|
||||
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
|
||||
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||
import com.simplemobiletools.commons.helpers.ContactsHelper
|
||||
import com.simplemobiletools.commons.models.contacts.Contact
|
||||
import com.simplemobiletools.commons.models.contacts.ContactSource
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
|
||||
import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter
|
||||
import kotlinx.android.synthetic.main.dialog_filter_contact_sources.view.filter_contact_sources_list
|
||||
|
||||
class SelectContactTypesDialog(
|
||||
val activity: SimpleActivity,
|
||||
private val selectedContactTypes: List<String>,
|
||||
val callback: (HashSet<ContactSource>) -> Unit
|
||||
) {
|
||||
private var dialog: AlertDialog? = null
|
||||
private val view = activity.layoutInflater.inflate(R.layout.dialog_filter_contact_sources, null)
|
||||
|
||||
private var contactSources = mutableListOf<ContactSource>()
|
||||
private var contacts = listOf<Contact>()
|
||||
private var isContactSourcesReady = false
|
||||
private var isContactsReady = false
|
||||
|
||||
init {
|
||||
ContactsHelper(activity).getContactSources { sources ->
|
||||
contactSources = sources
|
||||
isContactSourcesReady = true
|
||||
processDataIfReady()
|
||||
}
|
||||
|
||||
ContactsHelper(activity).getContacts(getAll = true) { receivedContacts ->
|
||||
contacts = receivedContacts
|
||||
isContactsReady = true
|
||||
processDataIfReady()
|
||||
}
|
||||
}
|
||||
|
||||
private fun processDataIfReady() {
|
||||
if (!isContactSourcesReady) {
|
||||
return
|
||||
}
|
||||
|
||||
val contactSourcesWithCount = mutableListOf<ContactSource>()
|
||||
for (contactSource in contactSources) {
|
||||
val count = if (isContactsReady) {
|
||||
contacts.count { it.source == contactSource.name }
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
contactSourcesWithCount.add(contactSource.copy(count = count))
|
||||
}
|
||||
|
||||
contactSources.clear()
|
||||
contactSources.addAll(contactSourcesWithCount)
|
||||
|
||||
activity.runOnUiThread {
|
||||
view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, contactSourcesWithCount, selectedContactTypes.toList())
|
||||
if (dialog == null) {
|
||||
activity.runOnUiThread {
|
||||
activity.getAlertDialogBuilder()
|
||||
.setPositiveButton(R.string.ok) { _, _ -> confirmContactTypes() }
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.apply {
|
||||
activity.setupDialogStuff(view, this) { alertDialog ->
|
||||
dialog = alertDialog
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun confirmContactTypes() {
|
||||
val adapter = view.filter_contact_sources_list.adapter as FilterContactSourcesAdapter
|
||||
val selectedItems = adapter.getSelectedContactSources()
|
||||
callback(selectedItems.toHashSet())
|
||||
dialog?.dismiss()
|
||||
}
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/export_events_scrollview"
|
||||
android:id="@+id/backup_contacts_scrollview"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/backup_events_holder"
|
||||
android:id="@+id/backup_contacts_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:paddingTop="@dimen/activity_margin">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextInputLayout
|
||||
android:id="@+id/backup_events_folder_hint"
|
||||
android:id="@+id/backup_contacts_folder_hint"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/activity_margin"
|
||||
|
@ -22,7 +22,7 @@
|
|||
android:hint="@string/folder">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/backup_events_folder"
|
||||
android:id="@+id/backup_contacts_folder"
|
||||
style="@style/UnclickableEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
@ -30,7 +30,7 @@
|
|||
</com.simplemobiletools.commons.views.MyTextInputLayout>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextInputLayout
|
||||
android:id="@+id/backup_events_filename_hint"
|
||||
android:id="@+id/backup_contacts_filename_hint"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="@dimen/activity_margin"
|
||||
|
@ -40,7 +40,7 @@
|
|||
app:endIconMode="custom">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/backup_events_filename"
|
||||
android:id="@+id/backup_contacts_filename"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textCapWords"
|
||||
|
@ -51,34 +51,33 @@
|
|||
</com.simplemobiletools.commons.views.MyTextInputLayout>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/select_event_types_divider"
|
||||
android:id="@+id/backup_contacts_divider"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="@dimen/divider_height"
|
||||
android:layout_height="1px"
|
||||
android:layout_marginStart="@dimen/activity_margin"
|
||||
android:layout_marginTop="@dimen/medium_margin"
|
||||
android:layout_marginEnd="@dimen/activity_margin"
|
||||
android:layout_marginBottom="@dimen/medium_margin"
|
||||
android:background="@color/divider_grey"
|
||||
android:importantForAccessibility="no" />
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/manage_event_types_holder"
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/backup_contacts_pick_sources_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="@dimen/activity_margin"
|
||||
android:paddingEnd="@dimen/activity_margin"
|
||||
android:text="@string/include_contact_sources"
|
||||
android:textSize="@dimen/smaller_text_size" />
|
||||
|
||||
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||
android:id="@+id/backup_contact_sources_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:paddingVertical="@dimen/medium_margin"
|
||||
android:paddingStart="@dimen/normal_margin"
|
||||
android:paddingEnd="@dimen/activity_margin">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/manage_event_types"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@null"
|
||||
android:clickable="false"
|
||||
android:paddingHorizontal="@dimen/medium_margin"
|
||||
android:paddingVertical="@dimen/normal_margin"
|
||||
android:text="@string/include_contact_sources"
|
||||
android:textSize="@dimen/normal_text_size" />
|
||||
|
||||
</RelativeLayout>
|
||||
android:clipToPadding="false"
|
||||
android:overScrollMode="never"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
|
||||
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
|
Loading…
Reference in New Issue