From acee290ec5e1df0e02d872a113a2e306e9f7416d Mon Sep 17 00:00:00 2001 From: merkost Date: Sat, 15 Jul 2023 13:48:45 +1000 Subject: [PATCH] Added exporting function to ContactsHelper --- .../commons/helpers/ContactsHelper.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt index b686d8c50..6ef07bc14 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/ContactsHelper.kt @@ -17,6 +17,9 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.models.PhoneNumber import com.simplemobiletools.commons.models.contacts.* import com.simplemobiletools.commons.overloads.times +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json +import java.io.OutputStream import java.util.Locale class ContactsHelper(val context: Context) { @@ -1555,4 +1558,23 @@ class ContactsHelper(val context: Context) { } } } + + fun getContactsToExport(selectedContactSources: Set, callback: (List) -> Unit) { + getContacts(getAll = true) { receivedContacts -> + val contacts = receivedContacts.filter { it.source in selectedContactSources } + callback(contacts) + } + } + + fun exportContacts(contacts: List, outputStream: OutputStream): ExportResult { + return try { + val jsonString = Json.encodeToString(contacts) + outputStream.use { + it.write(jsonString.toByteArray()) + } + ExportResult.EXPORT_OK + } catch (_: Error) { + ExportResult.EXPORT_FAIL + } + } }