Added new backup dialogs

This commit is contained in:
merkost 2023-07-15 13:52:03 +10:00
parent 5120ba7863
commit 0e205e6cb3
6 changed files with 332 additions and 1 deletions

View File

@ -15,7 +15,7 @@ import kotlinx.android.synthetic.main.item_filter_contact_source.view.*
class FilterContactSourcesAdapter( class FilterContactSourcesAdapter(
val activity: SimpleActivity, val activity: SimpleActivity,
private val contactSources: List<ContactSource>, private val contactSources: List<ContactSource>,
private val displayContactSources: ArrayList<String> private val displayContactSources: List<String>
) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() { ) : RecyclerView.Adapter<FilterContactSourcesAdapter.ViewHolder>() {
private val selectedKeys = HashSet<Int>() private val selectedKeys = HashSet<Int>()

View File

@ -0,0 +1,18 @@
package com.simplemobiletools.contacts.pro.dialogs
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.contacts.pro.R
class DateTimePatternInfoDialog(activity: BaseSimpleActivity) {
init {
val view = activity.layoutInflater.inflate(R.layout.datetime_pattern_info_layout, null)
activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok) { _, _ -> { } }
.apply {
activity.setupDialogStuff(view, this)
}
}
}

View File

@ -0,0 +1,138 @@
package com.simplemobiletools.contacts.pro.dialogs
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.helpers.ContactsHelper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
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.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 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 selectedContactTypes = HashSet<ContactSource>()
private fun setContactTypes() {
ContactsHelper(activity).getContactSources { contactSources ->
val availableContactSources = contactSources.toSet()
if (config.autoBackupContactSources.isEmpty()) {
selectedContactTypes = contactSources.toHashSet()
} else {
selectedContactTypes = availableContactSources.filter { it.name in config.autoBackupContactSources }.toHashSet()
}
}
}
init {
setContactTypes()
view.apply {
backup_events_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 {
DateTimePatternInfoDialog(activity)
}
backup_events_filename_hint.setEndIconOnLongClickListener {
DateTimePatternInfoDialog(activity)
true
}
backup_events_folder.setOnClickListener {
selectBackupFolder()
}
manage_event_types_holder.setOnClickListener {
activity.runOnUiThread {
SelectContactTypesDialog(activity, selectedContactTypes.map { it.name }) {
selectedContactTypes = it
config.autoBackupContactSources = it.map { it.name }.toSet()
}
}
}
}
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
when {
filename.isEmpty() -> activity.toast(R.string.empty_name)
filename.isAValidFilename() -> {
val file = File(backupFolder, "$filename.ics")
if (file.exists() && !file.canWrite()) {
activity.toast(R.string.name_taken)
return@setOnClickListener
}
if (selectedContactTypes.isEmpty()) {
activity.toast(R.string.no_entries_for_exporting)
return@setOnClickListener
}
ensureBackgroundThread {
config.apply {
autoBackupFolder = backupFolder
autoBackupFilename = filename
if (autoBackupContactSources != selectedContactTypes) {
autoBackupContactSources = selectedContactTypes.map { it.type }.toSet()
}
}
activity.runOnUiThread {
onSuccess()
}
dialog.dismiss()
}
}
else -> activity.toast(R.string.invalid_name)
}
}
}
}
}
private fun selectBackupFolder() {
activity.hideKeyboard(view.backup_events_filename)
FilePickerDialog(activity, backupFolder, false, showFAB = true) { path ->
activity.handleSAFDialog(path) { grantedSAF ->
if (!grantedSAF) {
return@handleSAFDialog
}
activity.handleSAFDialogSdk30(path) { grantedSAF30 ->
if (!grantedSAF30) {
return@handleSAFDialogSdk30
}
backupFolder = path
view.backup_events_folder.setText(activity.humanizePath(path))
}
}
}
}
}

View File

@ -0,0 +1,82 @@
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()
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/date_time_pattern_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="@dimen/big_margin"
android:paddingTop="@dimen/big_margin"
android:text="@string/date_time_pattern_info"
android:textIsSelectable="true" />

View File

@ -0,0 +1,84 @@
<?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:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/backup_events_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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_margin"
android:layout_marginEnd="@dimen/activity_margin"
android:layout_marginBottom="@dimen/activity_margin"
android:hint="@string/folder">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/backup_events_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/backup_events_filename_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_margin"
android:layout_marginEnd="@dimen/activity_margin"
android:hint="@string/filename_without_json"
app:endIconDrawable="@drawable/ic_info_vector"
app:endIconMode="custom">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/backup_events_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/bigger_text_size" />
</com.simplemobiletools.commons.views.MyTextInputLayout>
<ImageView
android:id="@+id/select_event_types_divider"
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:layout_marginStart="@dimen/activity_margin"
android:background="@color/divider_grey"
android:importantForAccessibility="no" />
<RelativeLayout
android:id="@+id/manage_event_types_holder"
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>
</LinearLayout>
</ScrollView>