Skip to content

Commit

Permalink
Add limit to fetching recent items from firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulyaara committed Oct 5, 2024
1 parent c33938b commit 77ed340
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.kafka.data.feature

import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QueryDocumentSnapshot
import com.google.firebase.firestore.ktx.snapshots
import com.kafka.base.ApplicationScope
import com.kafka.data.entities.RecentItem
import com.kafka.data.feature.firestore.FirestoreGraph
import kotlinx.coroutines.flow.map
import com.kafka.base.ApplicationScope
import javax.inject.Inject

@ApplicationScope
class RecentItemRepository @Inject constructor(private val firestoreGraph: FirestoreGraph) {
fun observeRecentItems(uid: String) = firestoreGraph.getRecentItemsCollection(uid)
.snapshots()
.map { snapshot ->
snapshot.map { mapRecentItem(it) }
.sortedByDescending { it.createdAt }
.distinctBy { it.itemId }
}
fun observeRecentItems(uid: String, limit: Int) =
firestoreGraph.getRecentItemsCollection(uid)
.orderBy("createdAt", Query.Direction.DESCENDING)
.limit(limit.toLong())
.snapshots()
.map { snapshot ->
snapshot.map { mapRecentItem(it) }
.distinctBy { it.itemId }
}

private fun mapRecentItem(queryDocumentSnapshot: QueryDocumentSnapshot): RecentItem {
return queryDocumentSnapshot.toObject(RecentItem::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ObserveHomepage @Inject constructor(

override fun createObservable(params: Unit): Flow<Homepage> {
return combine(
observeRecentItems.execute(Unit),
observeRecentItems.execute(ObserveRecentItems.Params(RECENT_ITEMS_LIMIT)),
homepageRepository.observeHomepageCollection(),
) { recentItems, collection ->
val collectionWithRecentItems = collection.map {
Expand All @@ -36,3 +36,5 @@ class ObserveHomepage @Inject constructor(
}.flowOn(coroutineDispatchers.io)
}
}

private const val RECENT_ITEMS_LIMIT = 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ class ObserveRecentItems @Inject constructor(
private val dispatchers: CoroutineDispatchers,
private val accountRepository: AccountRepository,
private val recentItemRepository: RecentItemRepository,
) : SubjectInteractor<Unit, ImmutableList<RecentItemWithProgress>>() {
) : SubjectInteractor<ObserveRecentItems.Params, ImmutableList<RecentItemWithProgress>>() {

override fun createObservable(params: Unit): Flow<ImmutableList<RecentItemWithProgress>> {
override fun createObservable(params: Params): Flow<ImmutableList<RecentItemWithProgress>> {
return accountRepository.observeCurrentFirebaseUser()
.filterNotNull()
.flatMapLatest { user -> recentItemRepository.observeRecentItems(user.uid) }
.flatMapLatest { user ->
recentItemRepository.observeRecentItems(user.uid, params.limit)
}
.map { it.map { RecentItemWithProgress(it, 0) } } // todo: add actual progress
.onStart { emit(emptyList()) }
.map { it.toPersistentList() }
.flowOn(dispatchers.io)
}

data class Params(val limit: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package com.kafka.homepage
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kafka.data.model.SearchFilter
import com.kafka.remote.config.RemoteConfig
import com.kafka.remote.config.showFeaturedItemLabels
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import com.kafka.analytics.logger.Analytics
import com.kafka.base.extensions.stateInDefault
import com.kafka.common.ObservableLoadingCounter
import com.kafka.common.UiMessageManager
import com.kafka.common.collectStatus
import com.kafka.common.shareText
import com.kafka.data.model.SearchFilter
import com.kafka.domain.interactors.UpdateHomepage
import com.kafka.domain.interactors.UpdateRecommendations
import com.kafka.domain.interactors.recent.RemoveRecentItem
Expand All @@ -26,7 +20,12 @@ import com.kafka.navigation.Navigator
import com.kafka.navigation.deeplink.Config
import com.kafka.navigation.graph.RootScreen
import com.kafka.navigation.graph.Screen
import com.kafka.homepage.R
import com.kafka.remote.config.RemoteConfig
import com.kafka.remote.config.showFeaturedItemLabels
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

class HomepageViewModel @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package com.kafka.homepage.recent

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kafka.data.entities.RecentItem
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import com.kafka.analytics.logger.Analytics
import com.kafka.base.extensions.stateInDefault
import com.kafka.data.entities.RecentItem
import com.kafka.domain.interactors.recent.RemoveAllRecentItems
import com.kafka.domain.interactors.recent.RemoveRecentItem
import com.kafka.domain.observers.ObserveRecentItems
import com.kafka.navigation.Navigator
import com.kafka.navigation.graph.Screen
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

class RecentViewModel @Inject constructor(
Expand All @@ -32,7 +32,7 @@ class RecentViewModel @Inject constructor(
)

init {
observeRecentItems(Unit)
observeRecentItems(ObserveRecentItems.Params(RECENT_ITEMS_LIMIT))
}

fun openItemDetail(itemId: String) {
Expand All @@ -55,6 +55,8 @@ class RecentViewModel @Inject constructor(
}
}

private const val RECENT_ITEMS_LIMIT = 50

data class RecentViewState(
val recentItems: List<RecentItem> = persistentListOf(),
)

0 comments on commit 77ed340

Please sign in to comment.