creating the structure of contact exporting

This commit is contained in:
tibbi 2018-01-15 16:18:19 +01:00
parent 04b345e0ac
commit ca463d056b
2 changed files with 57 additions and 1 deletions

View File

@ -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<Contact>) {
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()
}
}
}

View File

@ -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<Contact>, 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
})
}
}