-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ContactStore.kt
68 lines (63 loc) · 2.75 KB
/
ContactStore.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.alexstyl.contactstore
import android.accounts.AccountManager
import android.content.Context
import com.alexstyl.contactstore.ContactStore.Companion.newInstance
import com.alexstyl.contactstore.utils.DateTimeFormatParser
import kotlinx.coroutines.flow.Flow
/**
* A store that can be used to retrieve information about the contacts of the device (via [fetchContacts]) or edit them (via [execute]).
*
* @see [newInstance]
*
*/
public interface ContactStore {
@Deprecated(
"Prefer the version of this function that receives a lambda",
ReplaceWith("execute {}")
)
public suspend fun execute(request: SaveRequest)
/**
* Returns a [Flow] that emits the contacts of the device matching the given [predicate].
*
* The Flow will continue emitting once a change is detected (i.e. an other app adds a new contact or a Content Provider syncs a new account) and never completes.
* Changes caused by other apps might take some seconds to register, as the underlying implementation uses Android's ContentObserver.
*
* @param predicate The conditions that a contact need to meet in order to be fetched
* @param columnsToFetch The columns of the contact you need to be fetched
* @param displayNameStyle The preferred style for the [Contact.displayName] to be returned. The fetched contacts' sorting order will match this option.
*/
public fun fetchContacts(
predicate: ContactPredicate? = null,
columnsToFetch: List<ContactColumn> = emptyList(),
displayNameStyle: DisplayNameStyle = DisplayNameStyle.Primary
): Flow<List<Contact>>
public companion object {
/**
* The entry point to ContactStore
*/
public fun newInstance(context: Context): ContactStore {
val contentResolver = context.contentResolver
val resources = context.resources
val contactsQueries = ContactQueries(
contentResolver = contentResolver,
dateParser = DateTimeFormatParser(),
resources = context.resources,
accountInfoResolver = AccountInfoResolver(
context,
context.getSystemService(Context.ACCOUNT_SERVICE) as AccountManager,
context.packageManager
)
)
return AndroidContactStore(
contentResolver = contentResolver,
newContactOperationsFactory = NewContactOperationsFactory(),
existingContactOperationsFactory = ExistingContactOperationsFactory(
contentResolver,
resources,
contactsQueries
),
contactQueries = contactsQueries,
)
}
}
}