@@ -35,56 +35,64 @@ class AndroidDefaultContactRepository(
3535 private val ioDispatcher : CoroutineDispatcher = Dispatchers .IO ,
3636) : ContactRepository {
3737
38- override val allContacts: Flow <List <Contact >> = permissionFlow
39- .getPermissionState(Manifest .permission.READ_CONTACTS )
40- .transform { state ->
38+ override val allContacts: Flow <List <Contact >> =
39+ permissionFlow.getPermissionState(Manifest .permission.READ_CONTACTS ).transform { state ->
4140 if (state.isGranted) {
4241 emit(getContacts())
4342 } else {
4443 emit(emptyList())
4544 }
4645 }
4746
48- private suspend fun getContacts (): List <Contact > = withContext(ioDispatcher) {
49- buildList {
50- var cursor: Cursor ? = null
51- try {
52- val projection = arrayOf(
53- ContactsContract .CommonDataKinds .Phone .CONTACT_ID ,
54- ContactsContract .CommonDataKinds .Phone .NUMBER ,
55- ContactsContract .CommonDataKinds .Phone .DISPLAY_NAME ,
56- ContactsContract .CommonDataKinds .Phone .PHOTO_URI ,
57- )
47+ private suspend fun getContacts (): List <Contact > =
48+ withContext(ioDispatcher) {
49+ buildList {
50+ var cursor: Cursor ? = null
51+ try {
52+ val projection =
53+ arrayOf(
54+ ContactsContract .CommonDataKinds .Phone .CONTACT_ID ,
55+ ContactsContract .CommonDataKinds .Phone .NUMBER ,
56+ ContactsContract .CommonDataKinds .Phone .DISPLAY_NAME ,
57+ ContactsContract .CommonDataKinds .Phone .PHOTO_URI ,
58+ )
5859
59- val order = " ${ContactsContract .CommonDataKinds .Phone .CONTACT_ID } ASC"
60+ val order = " ${ContactsContract .CommonDataKinds .Phone .CONTACT_ID } ASC"
6061
61- cursor = contentResolver.query(
62- ContactsContract .CommonDataKinds .Phone .CONTENT_URI ,
63- projection,
64- null ,
65- null ,
66- order,
67- )
68- if (cursor != null ) {
69- while (cursor.moveToNext()) {
70- val contactId = cursor.getStringOrNull(
71- cursor.getColumnIndex(ContactsContract .CommonDataKinds .Phone .CONTACT_ID ),
72- )
73- val name = cursor.getStringOrNull(
74- cursor.getColumnIndex(ContactsContract .CommonDataKinds .Phone .DISPLAY_NAME ),
75- )
76- val number = cursor.getStringOrNull(
77- cursor.getColumnIndex(ContactsContract .CommonDataKinds .Phone .NUMBER ),
62+ cursor =
63+ contentResolver.query(
64+ ContactsContract .CommonDataKinds .Phone .CONTENT_URI ,
65+ projection,
66+ null ,
67+ null ,
68+ order,
7869 )
70+ if (cursor != null ) {
71+ while (cursor.moveToNext()) {
72+ val contactId =
73+ cursor.getStringOrNull(
74+ cursor.getColumnIndex(
75+ ContactsContract .CommonDataKinds .Phone .CONTACT_ID ),
76+ )
77+ val name =
78+ cursor.getStringOrNull(
79+ cursor.getColumnIndex(
80+ ContactsContract .CommonDataKinds .Phone .DISPLAY_NAME ),
81+ )
82+ val number =
83+ cursor.getStringOrNull(
84+ cursor.getColumnIndex(
85+ ContactsContract .CommonDataKinds .Phone .NUMBER ),
86+ )
7987
80- if (contactId != null && name != null && number != null ) {
81- add(Contact (contactId, name, number))
88+ if (contactId != null && name != null && number != null ) {
89+ add(Contact (contactId, name, number))
90+ }
8291 }
8392 }
93+ } finally {
94+ cursor?.close()
8495 }
85- } finally {
86- cursor?.close()
8796 }
8897 }
89- }
9098}
0 commit comments