creating the structure of contact exporting
This commit is contained in:
parent
04b345e0ac
commit
ca463d056b
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue