Added exporting function to ContactsHelper

This commit is contained in:
merkost 2023-07-15 13:48:45 +10:00
parent a66e0644b6
commit acee290ec5

View file

@ -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<String>, callback: (List<Contact>) -> Unit) {
getContacts(getAll = true) { receivedContacts ->
val contacts = receivedContacts.filter { it.source in selectedContactSources }
callback(contacts)
}
}
fun exportContacts(contacts: List<Contact>, outputStream: OutputStream): ExportResult {
return try {
val jsonString = Json.encodeToString(contacts)
outputStream.use {
it.write(jsonString.toByteArray())
}
ExportResult.EXPORT_OK
} catch (_: Error) {
ExportResult.EXPORT_FAIL
}
}
}