summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Shubhi <shubhisaxena@google.com> 2024-04-25 13:40:16 +0000
committer Shubhi <shubhisaxena@google.com> 2024-04-29 13:19:30 +0000
commit58f300960c110f7d608b3788cf461beae07ebf3d (patch)
tree5a8bee3ed261bc4857ba497ee98e95cd1f7cecd4
parent55d3b61f229c5755f1970fcbf068d9895cce57a9 (diff)
Implement DataServiceImpl#albumPagingSource in kotlin picker
Return the AlbumPagingSource that fetches albums from MediaProvider to inflate the Albums grid view. Bug: 329122491 Test: Manual (Screen recording - b/329122491#comment9) Change-Id: Id63cd2dc838fabc9d453b016f4d457c961877857
-rw-r--r--photopicker/src/com/android/photopicker/data/DataServiceImpl.kt61
1 files changed, 45 insertions, 16 deletions
diff --git a/photopicker/src/com/android/photopicker/data/DataServiceImpl.kt b/photopicker/src/com/android/photopicker/data/DataServiceImpl.kt
index 517af8f45..1f5d85e4e 100644
--- a/photopicker/src/com/android/photopicker/data/DataServiceImpl.kt
+++ b/photopicker/src/com/android/photopicker/data/DataServiceImpl.kt
@@ -27,6 +27,7 @@ import com.android.photopicker.data.model.Group.Album
import com.android.photopicker.data.model.Media
import com.android.photopicker.data.model.MediaPageKey
import com.android.photopicker.data.model.Provider
+import com.android.photopicker.data.paging.AlbumPagingSource
import com.android.photopicker.data.paging.MediaPagingSource
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
@@ -70,10 +71,14 @@ class DataServiceImpl(
private val _activeContentResolver = MutableStateFlow<ContentResolver>(
userStatus.value.activeContentResolver)
- private var mediaPagingSource: MediaPagingSource? = null
- // An internal lock to allow thread-safe updates to the media paging source
- private val mediaPagingSourceMutex = Mutex()
+ // Keep track of the photo grid media and album grid paging source so that we can invalidate
+ // them in case the underlying data changes.
+ private val mediaPagingSources: MutableList<MediaPagingSource> = mutableListOf()
+ private val albumPagingSources: MutableList<AlbumPagingSource> = mutableListOf()
+ // An internal lock to allow thread-safe updates to the [MediaPagingSource] and
+ // [AlbumPagingSource].
+ private val mediaPagingSourceMutex = Mutex()
/**
* Callback flow that listens to changes in the available providers and emits updated list of
@@ -125,7 +130,15 @@ class DataServiceImpl(
// Send refresh media request after the available providers are available.
refreshMedia(providers)
mediaPagingSourceMutex.withLock {
- mediaPagingSource?.invalidate()
+ mediaPagingSources.forEach {
+ mediaPagingSource -> mediaPagingSource.invalidate()
+ }
+ albumPagingSources.forEach {
+ albumPagingSource -> albumPagingSource.invalidate()
+ }
+
+ mediaPagingSources.clear()
+ albumPagingSources.clear()
}
}
}
@@ -193,8 +206,24 @@ class DataServiceImpl(
override fun albumContentPagingSource(albumId: String): PagingSource<MediaPageKey, Media> =
throw NotImplementedError("This method is not implemented yet.")
- override fun albumPagingSource(): PagingSource<MediaPageKey, Album> =
- throw NotImplementedError("This method is not implemented yet.")
+ override fun albumPagingSource(): PagingSource<MediaPageKey, Album> = runBlocking {
+ mediaPagingSourceMutex.withLock {
+ val availableProviders: List<Provider> = availableProviders.value
+ val contentResolver: ContentResolver = _activeContentResolver.value
+
+ Log.v(DataService.TAG, "Created an album paging source that queries " +
+ "$availableProviders")
+
+ val albumPagingSource = AlbumPagingSource(
+ contentResolver,
+ availableProviders,
+ mediaProviderClient
+ )
+
+ albumPagingSources.add(albumPagingSource)
+ albumPagingSource
+ }
+ }
override fun cloudMediaProviderDetails(
authority: String
@@ -206,16 +235,16 @@ class DataServiceImpl(
val availableProviders: List<Provider> = availableProviders.value
val contentResolver: ContentResolver = _activeContentResolver.value
- Log.v(DataService.TAG, "Created a paging source that queries $availableProviders")
+ Log.v(DataService.TAG, "Created a media paging source that queries $availableProviders")
- val immutableMediaPagingSource = MediaPagingSource(
- contentResolver,
- availableProviders,
- mediaProviderClient
+ val mediaPagingSource = MediaPagingSource(
+ contentResolver,
+ availableProviders,
+ mediaProviderClient
)
- mediaPagingSource = immutableMediaPagingSource
- immutableMediaPagingSource
+ mediaPagingSources.add(mediaPagingSource)
+ mediaPagingSource
}
}
@@ -229,12 +258,12 @@ class DataServiceImpl(
) {
if (availableProviders.isNotEmpty()) {
mediaProviderClient.refreshMedia(
- availableProviders,
- _activeContentResolver.value
+ availableProviders,
+ _activeContentResolver.value
)
} else {
Log.w(DataService.TAG,
- "Cannot refresh media when there are no providers available")
+ "Cannot refresh media when there are no providers available")
}
}