-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
TestContactStore.kt
346 lines (325 loc) · 16.3 KB
/
TestContactStore.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.alexstyl.contactstore.test
import android.provider.ContactsContract
import com.alexstyl.contactstore.Contact
import com.alexstyl.contactstore.ContactColumn
import com.alexstyl.contactstore.ContactOperation
import com.alexstyl.contactstore.ContactPredicate
import com.alexstyl.contactstore.ContactStore
import com.alexstyl.contactstore.DisplayNameStyle
import com.alexstyl.contactstore.ExperimentalContactStoreApi
import com.alexstyl.contactstore.MutableContact
import com.alexstyl.contactstore.PartialContact
import com.alexstyl.contactstore.SaveRequest
import com.alexstyl.contactstore.containsColumn
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
/**
* An implementation of [ContactStore] usable for testing. The store holds a snapshot of contacts
* that will be used for any operations on the store.
*
* You can initialize the store by providing a list of [StoredContact]s.
* This can be used for emulating scenarios where a pre-populated store is needed.
*
* The implementation of this class tries to match the behavior of AOSP as much as possible.
* Different OEMs might have altered the behavior of their ContactProvider and as a result different results might be returned.
*
* Do not use this class as a source of truth of how a real device will behave.
*/
@ExperimentalContactStoreApi
public class TestContactStore(
contactsSnapshot: List<StoredContact> = emptyList()
) : ContactStore {
private val snapshot: MutableStateFlow<List<StoredContact>> = MutableStateFlow(contactsSnapshot)
override suspend fun execute(request: SaveRequest) {
request.requests.forEach { operation ->
when (operation) {
is ContactOperation.Delete -> deleteContact(withId = operation.contactId)
is ContactOperation.Insert -> insertContact(operation.contact)
is ContactOperation.Update -> updateContact(operation.contact)
}
}
}
private suspend fun deleteContact(withId: Long) {
snapshot.emit(
snapshot.value.dropWhile { it.contactId == withId }
)
}
private suspend fun insertContact(contact: MutableContact) {
val current = snapshot.value
snapshot.emit(
current.toMutableList()
.apply {
add(
StoredContact(
contactId = current.size.toLong(),
isStarred = contact.isStarred,
prefix = contact.takeIfContains(ContactColumn.Names) { contact.prefix },
firstName = contact.takeIfContains(ContactColumn.Names) { contact.firstName },
middleName = contact.takeIfContains(ContactColumn.Names) { contact.middleName },
lastName = contact.takeIfContains(ContactColumn.Names) { contact.lastName },
suffix = contact.takeIfContains(ContactColumn.Names) { contact.suffix },
phoneticMiddleName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticMiddleName },
phoneticFirstName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticFirstName },
phoneticLastName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticLastName },
phoneticNameStyle = contact.takeIfContains(ContactColumn.Names) { contact.phoneticNameStyle }
?: ContactsContract.PhoneticNameStyle.UNDEFINED,
imageData = contact.takeIfContains(ContactColumn.Image) { contact.imageData },
organization = contact.takeIfContains(ContactColumn.Organization) { contact.organization },
jobTitle = contact.takeIfContains(ContactColumn.Organization) { contact.jobTitle },
webAddresses = contact.takeIfContains(ContactColumn.WebAddresses) { contact.webAddresses }
.orEmpty(),
phones = contact.takeIfContains(ContactColumn.Phones) { contact.phones }
.orEmpty(),
mails = contact.takeIfContains(ContactColumn.Mails) { contact.mails }
.orEmpty(),
events = contact.takeIfContains(ContactColumn.Events) { contact.events }
.orEmpty(),
postalAddresses = contact.takeIfContains(ContactColumn.PostalAddresses) { contact.postalAddresses }
.orEmpty(),
note = contact.takeIfContains(ContactColumn.Note) { contact.note },
nickname = contact.takeIfContains(ContactColumn.Nickname) { contact.nickname },
groups = contact
.takeIfContains(ContactColumn.GroupMemberships) { contact.groups }
.orEmpty(),
fullNameStyle = contact.takeIfContains(ContactColumn.Names) { contact.fullNameStyle }
?: ContactsContract.FullNameStyle.UNDEFINED
)
)
}
.toList()
)
}
private fun <T> Contact.takeIfContains(required: ContactColumn, function: () -> T?): T? {
return if (containsColumn(required)) {
function.invoke()
} else {
null
}
}
private suspend fun updateContact(contact: MutableContact) {
val currentContact = snapshot.value
.find { it.contactId == contact.contactId } ?: return
val updatedContact = currentContact.copy(
firstName = contact.takeIfContains(ContactColumn.Names) { contact.firstName }
?: currentContact.firstName,
isStarred = contact.isStarred,
prefix = contact.takeIfContains(ContactColumn.Names) { contact.prefix }
?: currentContact.prefix,
middleName = contact.takeIfContains(ContactColumn.Names) { contact.middleName }
?: currentContact.middleName,
lastName = contact.takeIfContains(ContactColumn.Names) { contact.lastName }
?: currentContact.lastName,
suffix = contact.takeIfContains(ContactColumn.Names) { contact.suffix }
?: currentContact.suffix,
phoneticMiddleName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticMiddleName }
?: currentContact.phoneticMiddleName,
phoneticFirstName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticFirstName }
?: currentContact.phoneticFirstName,
phoneticLastName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticLastName }
?: currentContact.phoneticLastName,
phoneticNameStyle = contact.takeIfContains(ContactColumn.Names) { contact.phoneticNameStyle }
?: currentContact.phoneticNameStyle,
imageData = contact.takeIfContains(ContactColumn.Image) { contact.imageData }
?: currentContact.imageData,
organization = contact.takeIfContains(ContactColumn.Organization) { contact.organization }
?: currentContact.organization,
jobTitle = contact.takeIfContains(ContactColumn.Organization) { contact.jobTitle }
?: currentContact.jobTitle,
webAddresses = contact.takeIfContains(ContactColumn.WebAddresses) { contact.webAddresses }
?: currentContact.webAddresses,
phones = contact.takeIfContains(ContactColumn.Phones) { contact.phones }
?: currentContact.phones,
mails = contact.takeIfContains(ContactColumn.Mails) { contact.mails }
?: currentContact.mails,
events = contact.takeIfContains(ContactColumn.Events) { contact.events }
?: currentContact.events,
postalAddresses = contact.takeIfContains(ContactColumn.PostalAddresses) { contact.postalAddresses }
?: currentContact.postalAddresses,
note = contact.takeIfContains(ContactColumn.Note) { contact.note } ?: currentContact.note,
nickname = contact.takeIfContains(ContactColumn.Nickname) { contact.nickname }
?: currentContact.nickname,
groups = contact
.takeIfContains(ContactColumn.GroupMemberships) { contact.groups }
?: currentContact.groups,
fullNameStyle = contact.takeIfContains(ContactColumn.Names) { contact.fullNameStyle }
?: currentContact.fullNameStyle
)
val newList = snapshot.value.toMutableList()
.replace(updatedContact) {
it.contactId == contact.contactId
}
.toList()
snapshot.emit(newList)
}
private fun <T> List<T>.replace(newValue: T, block: (T) -> Boolean): List<T> {
return map {
if (block(it)) newValue else it
}
}
override fun fetchContacts(
predicate: ContactPredicate?,
columnsToFetch: List<ContactColumn>,
displayNameStyle: DisplayNameStyle
): Flow<List<Contact>> {
return snapshot
.map { contacts ->
contacts.filter { current ->
matchesPredicate(contact = current, predicate)
}.keepColumns(columnsToFetch)
}
}
private fun matchesPredicate(
contact: StoredContact,
predicate: ContactPredicate?
): Boolean {
if (predicate == null) return true
return when (predicate) {
is ContactPredicate.ContactLookup -> matchesContact(predicate, contact)
is ContactPredicate.MailLookup -> {
val query = predicate.mailAddress.raw
return contact.mails.any { it.value.raw.startsWith(query, ignoreCase = true) }
}
is ContactPredicate.NameLookup -> matchesName(predicate, contact)
is ContactPredicate.PhoneLookup -> matchesPhone(predicate, contact)
}
}
private fun matchesContact(
predicate: ContactPredicate.ContactLookup,
contact: StoredContact
): Boolean {
val isInIds = predicate.inContactIds.orEmpty().contains(contact.contactId)
val isFavorite = predicate.isFavorite?.let { contact.isStarred == it } ?: true
return isInIds && isFavorite
}
private fun matchesPhone(
predicate: ContactPredicate.PhoneLookup,
contact: StoredContact
): Boolean {
val query = predicate.phoneNumber.raw
return contact.phones.any { it.value.raw.startsWith(query, ignoreCase = true) }
}
private fun matchesName(
predicate: ContactPredicate.NameLookup,
contact: StoredContact
): Boolean {
val query = predicate.partOfName
val matchesName = contact.prefix.orEmpty().startsWith(query, ignoreCase = true)
|| contact.firstName.orEmpty().startsWith(query, ignoreCase = true)
|| contact.middleName.orEmpty().startsWith(query, ignoreCase = true)
|| contact.lastName.orEmpty().startsWith(query, ignoreCase = true)
|| contact.suffix.orEmpty().startsWith(query, ignoreCase = true)
val matchesNick = contact.nickname.orEmpty().startsWith(query, ignoreCase = true)
return matchesName || matchesNick
}
private fun List<StoredContact>.keepColumns(columnsToFetch: List<ContactColumn>): List<Contact> {
return map {
PartialContact(
displayName = displayName(it),
contactId = it.contactId,
lookupKey = null,
columns = columnsToFetch,
isStarred = it.isStarred,
firstName = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.firstName },
lastName = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.lastName },
prefix = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.prefix },
middleName = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.middleName },
suffix = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.suffix },
phoneticFirstName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.phoneticFirstName },
phoneticMiddleName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.phoneticMiddleName },
phoneticLastName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.phoneticLastName },
fullNameStyle = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.fullNameStyle }
?: ContactsContract.FullNameStyle.UNDEFINED,
phoneticNameStyle = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.phoneticNameStyle }
?: ContactsContract.FullNameStyle.UNDEFINED,
imageData = valueIfPresent(columnsToFetch, ContactColumn.Image) { it.imageData },
organization = valueIfPresent(
columnsToFetch,
ContactColumn.Organization
) { it.organization },
jobTitle = valueIfPresent(
columnsToFetch,
ContactColumn.Organization
) { it.jobTitle },
webAddresses = valueIfPresent(
columnsToFetch,
ContactColumn.WebAddresses
) { it.webAddresses }.orEmpty(),
phones = valueIfPresent(
columnsToFetch,
ContactColumn.Phones
) { it.phones }.orEmpty(),
mails = valueIfPresent(columnsToFetch, ContactColumn.Mails) { it.mails }.orEmpty(),
events = valueIfPresent(
columnsToFetch,
ContactColumn.Events
) { it.events }.orEmpty(),
postalAddresses = valueIfPresent(
columnsToFetch,
ContactColumn.PostalAddresses
) { it.postalAddresses }.orEmpty(),
note = valueIfPresent(columnsToFetch, ContactColumn.Note) { it.note },
nickname = valueIfPresent(columnsToFetch, ContactColumn.Nickname) { it.nickname },
groups = valueIfPresent(
columnsToFetch,
ContactColumn.GroupMemberships
) { it.groups }.orEmpty()
)
}
}
private fun displayName(it: StoredContact): String {
return with(it) {
buildString {
prefix?.let { append(it) }
if (fullNameStyle == ContactsContract.FullNameStyle.UNDEFINED || fullNameStyle == ContactsContract.FullNameStyle.WESTERN) {
firstName?.let { appendWord(it) }
middleName?.let { appendWord(it) }
lastName?.let { appendWord(it) }
} else if (fullNameStyle == ContactsContract.FullNameStyle.CHINESE) {
lastName?.let { appendWord(it) }
middleName?.let { appendWord(it, separator = "") }
firstName?.let { appendWord(it, separator = "") }
} else {
lastName?.let { appendWord(it) }
middleName?.let { appendWord(it) }
firstName?.let { appendWord(it) }
}
suffix?.let {
appendWord(it, separator = ", ")
}
}
}
}
private fun StringBuilder.appendWord(word: String, separator: String = " ") {
if (word.isNotBlank()) {
if (isNotEmpty()) append(separator)
append(word)
}
}
private fun <T> valueIfPresent(
columns: List<ContactColumn>,
requiredColumn: ContactColumn,
value: () -> T?
): T? {
return if (columns.contains(requiredColumn)) {
value()
} else {
null
}
}
}