diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt index f37a138a..2afb7fae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -22,7 +22,10 @@ import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog import com.simplemobiletools.contacts.dialogs.ImportContactsDialog import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.onTabSelectionChanged +import com.simplemobiletools.contacts.helpers.ContactsHelper +import com.simplemobiletools.contacts.helpers.VcfExporter import com.simplemobiletools.contacts.interfaces.RefreshContactsListener +import com.simplemobiletools.contacts.models.Contact import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_contacts.* import kotlinx.android.synthetic.main.fragment_favorites.* @@ -281,7 +284,23 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { private fun exportContacts() { FilePickerDialog(this, pickFile = false, showFAB = true) { ExportContactsDialog(this, it) { file, contactSources -> - + Thread { + ContactsHelper(this).getContacts { + val contacts = it.filter { contactSources.contains(it.source) } + if (contacts.isEmpty()) { + toast(R.string.no_entries_for_exporting) + } else { + toast(R.string.exporting) + VcfExporter().exportContacts(this, file, contacts as ArrayList) { + toast(when (it) { + VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful + VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed + else -> R.string.exporting_failed + }) + } + } + } + }.start() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt new file mode 100644 index 00000000..f014f987 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -0,0 +1,37 @@ +package com.simplemobiletools.contacts.helpers + +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.getFileOutputStream +import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.* +import com.simplemobiletools.contacts.models.Contact +import java.io.File + +class VcfExporter() { + enum class ExportResult { + EXPORT_FAIL, EXPORT_OK, EXPORT_PARTIAL + } + + private var contactsExported = 0 + private var contactsFailed = 0 + + fun exportContacts(activity: BaseSimpleActivity, file: File, contacts: ArrayList, callback: (result: ExportResult) -> Unit) { + activity.getFileOutputStream(file) { + if (it == null) { + callback(EXPORT_FAIL) + return@getFileOutputStream + } + + it.bufferedWriter().use { out -> + for (contact in contacts) { + + } + } + } + + callback(when { + contactsExported == 0 -> EXPORT_FAIL + contactsFailed > 0 -> EXPORT_PARTIAL + else -> EXPORT_OK + }) + } +}