adding a helper exportContactsTo function

This commit is contained in:
tibbi
2020-03-16 23:16:06 +01:00
parent eb4be51f92
commit 1ee2e591d5

View File

@ -44,6 +44,7 @@ import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.fragment_favorites.* import kotlinx.android.synthetic.main.fragment_favorites.*
import kotlinx.android.synthetic.main.fragment_groups.* import kotlinx.android.synthetic.main.fragment_groups.*
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStream
import java.util.* import java.util.*
class MainActivity : SimpleActivity(), RefreshContactsListener { class MainActivity : SimpleActivity(), RefreshContactsListener {
@ -514,27 +515,27 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
private fun tryExportContacts() { private fun tryExportContacts() {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) { if (it) {
exportContacts() ExportContactsDialog(this, config.lastExportPath, false) { file, ignoredContactSources ->
config.lastExportPath = file.absolutePath.getParentPath()
getFileOutputStream(file.toFileDirItem(this), true) {
exportContactsTo(ignoredContactSources, it)
}
}
} }
} }
} }
private fun exportContacts() { private fun exportContactsTo(ignoredContactSources: HashSet<String>, outputStream: OutputStream?) {
ExportContactsDialog(this, config.lastExportPath, false) { file, ignoredContactSources -> ContactsHelper(this).getContacts(true, ignoredContactSources) { contacts ->
config.lastExportPath = file.absolutePath.getParentPath() if (contacts.isEmpty()) {
ContactsHelper(this).getContacts(true, ignoredContactSources) { contacts -> toast(R.string.no_entries_for_exporting)
if (contacts.isEmpty()) { } else {
toast(R.string.no_entries_for_exporting) VcfExporter().exportContacts(this, outputStream, contacts, true) { result ->
} else { toast(when (result) {
getFileOutputStream(file.toFileDirItem(this), true) { VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
VcfExporter().exportContacts(this, it, contacts, true) { result -> VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
toast(when (result) { else -> R.string.exporting_failed
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful })
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
else -> R.string.exporting_failed
})
}
}
} }
} }
} }