Skip to content

Commit

Permalink
Add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Feb 2, 2024
1 parent 3cd70bf commit 12081cc
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 123 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ dependencies {
implementation("androidx.compose.material3:material3")
implementation("androidx.navigation:navigation-compose:2.7.6")
implementation("androidx.hilt:hilt-navigation-compose:1.1.0")
implementation("com.google.firebase:firebase-firestore-ktx:24.10.1")

// Test
testImplementation("junit:junit:4.13.2")
Expand Down Expand Up @@ -137,5 +138,8 @@ dependencies {

implementation("com.google.maps.android:maps-compose:4.3.0")

// Paging
implementation("androidx.paging:paging-compose:3.2.1")

implementation(project(":data"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.canopas.catchme.ui.component
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -61,4 +60,4 @@ fun UserProfile(
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import androidx.annotation.DrawableRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand All @@ -37,7 +32,6 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.canopas.catchme.R
import com.canopas.catchme.ui.flow.home.map.component.MapMarker
import com.canopas.catchme.ui.flow.home.map.component.MapUserItem
import com.canopas.catchme.ui.flow.home.map.member.MemberDetailBottomSheetContent
import com.canopas.catchme.ui.theme.AppTheme
import com.google.android.gms.maps.CameraUpdateFactory
Expand All @@ -49,7 +43,6 @@ import com.google.maps.android.compose.MapProperties
import com.google.maps.android.compose.MapUiSettings
import com.google.maps.android.compose.rememberCameraPositionState
import kotlinx.coroutines.launch
import timber.log.Timber

private const val DEFAULT_CAMERA_ZOOM = 15f
private const val DEFAULT_CAMERA_ZOOM_FOR_SELECTED_USER = 17f
Expand All @@ -69,7 +62,7 @@ fun MapScreen() {
val scaffoldState = rememberBottomSheetScaffoldState(
bottomSheetState = bottomSheetState
)

LaunchedEffect(bottomSheetState) {
snapshotFlow { bottomSheetState.currentValue }
.collect {
Expand All @@ -82,7 +75,7 @@ fun MapScreen() {
BottomSheetScaffold(
scaffoldState = scaffoldState,
sheetContainerColor = AppTheme.colorScheme.surface,
sheetPeekHeight = if(state.showUserDetails) (screenHeight / 3).dp else 0.dp,
sheetPeekHeight = if (state.showUserDetails) (screenHeight / 3).dp else 0.dp,
sheetContent = {
state.selectedUser?.let { MemberDetailBottomSheetContent(state.selectedUser!!) }
}
Expand Down Expand Up @@ -174,7 +167,7 @@ fun MapScreenContent(modifier: Modifier) {

@Composable
private fun MapView(
cameraPositionState: CameraPositionState,
cameraPositionState: CameraPositionState
) {
val viewModel = hiltViewModel<MapViewModel>()
val state by viewModel.state.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MapViewModel @Inject constructor(
}
}

fun dismissMemberDetail() {
fun dismissMemberDetail() {
_state.value = _state.value.copy(showUserDetails = false, selectedUser = null)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
package com.canopas.catchme.ui.flow.home.map.component

import android.util.Log
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.canopas.catchme.data.models.location.ApiLocation
import com.canopas.catchme.data.models.user.ApiUser
import com.canopas.catchme.ui.component.UserProfile
Expand Down Expand Up @@ -86,4 +71,4 @@ fun MarkerContent(user: ApiUser, isSelected: Boolean) {
user = user
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.canopas.catchme.ui.flow.home.map.component

import android.content.Context
import android.location.Geocoder
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
Expand All @@ -26,32 +21,24 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.canopas.catchme.R
import com.canopas.catchme.data.models.user.ApiUser
import com.canopas.catchme.data.models.user.UserInfo
import com.canopas.catchme.ui.component.UserProfile
import com.canopas.catchme.ui.theme.AppTheme
import com.canopas.catchme.ui.theme.InterFontFamily
import com.canopas.catchme.utils.getAddress
import com.google.android.gms.maps.model.LatLng
import timber.log.Timber
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit
import kotlin.math.abs

Expand Down Expand Up @@ -155,7 +142,7 @@ fun MapUserItem(
}
}

private fun getTimeAgoString(context: Context, timestamp: Long): String {
private fun getTimeAgoString(context: Context, timestamp: Long): String {
val now = System.currentTimeMillis()
val duration = abs(timestamp - now)
val days = TimeUnit.MILLISECONDS.toDays(duration)
Expand All @@ -181,4 +168,3 @@ private fun getTimeAgoString(context: Context, timestamp: Long): String {
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.canopas.catchme.ui.flow.home.map.member

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.canopas.catchme.data.models.location.ApiLocation
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QuerySnapshot
import kotlinx.coroutines.tasks.await

class LocationHistoryPagingSource(private var query: Query) :
PagingSource<QuerySnapshot, ApiLocation>() {

override fun getRefreshKey(state: PagingState<QuerySnapshot, ApiLocation>): QuerySnapshot? =
null

override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot, ApiLocation> =
try {
val currentPage = params.key ?: query.get().await()
if (currentPage.isEmpty) {
val lists = currentPage.toObjects(ApiLocation::class.java)
LoadResult.Page(data = lists, prevKey = null, nextKey = null)
} else {
val lastVisibleProduct = currentPage.documents[currentPage.size() - 1]
val nextPage = query.startAfter(lastVisibleProduct).get().await()
val lists = currentPage.toObjects(ApiLocation::class.java)

LoadResult.Page(
data = lists,
prevKey = null,
nextKey = nextPage
)
}
} catch (e: Exception) {
LoadResult.Error(e)
}
}
Loading

0 comments on commit 12081cc

Please sign in to comment.