Skip to content

Commit

Permalink
refactor: merge initial implementation to the new feed merged into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismaillat committed Dec 19, 2024
1 parent 1cff7db commit 0854625
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions app/src/main/java/com/github/lookupgroup27/lookup/ui/feed/Feed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,25 @@ fun FeedScreen(
// Location setup
val context = LocalContext.current
val locationProvider = LocationProviderSingleton.getInstance(context)
val proximityAndTimePostFetcher by remember {
mutableStateOf(ProximityAndTimePostFetcher(postsViewModel, context))
}

var locationPermissionGranted by remember {
mutableStateOf(
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
}

// Initialize PostsViewModel with context
LaunchedEffect(Unit) {
Log.d("FeedScreen", "Setting context in PostsViewModel")
postsViewModel.setContext(context)
}

// Permission request launcher
val permissionLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
locationPermissionGranted = isGranted
if (isGranted && !testNoLoca) {
locationProvider.requestLocationUpdates()
proximityAndTimePostFetcher.fetchSortedPosts()
postsViewModel.fetchSortedPosts()
} else {
Toast.makeText(
context,
Expand All @@ -114,15 +116,15 @@ fun FeedScreen(
locationProvider.requestLocationUpdates()

while (locationProvider.currentLocation.value == null) {
delay(500) // Retry every 500ms
delay(200) // Retry every 200ms
}
postsViewModel.fetchSortedPosts()
}
}

val unfilteredPosts by
(initialNearbyPosts?.let { mutableStateOf(it) }
?: proximityAndTimePostFetcher.nearbyPosts.collectAsState())
?: postsViewModel.nearbyPosts.collectAsState())
val nearbyPosts = unfilteredPosts.filter { it.userMail != userEmail }

val postRatings = remember { mutableStateMapOf<String, List<Boolean>>() }
Expand Down Expand Up @@ -192,18 +194,18 @@ fun FeedScreen(
(testNoLoca || !locationPermissionGranted) -> {
Log.d("FeedScreen", "Location permission not granted")

// Show permission request button
Button(
onClick = {
permissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION)
},
modifier = Modifier.testTag("enable_location_button"),
colors =
ButtonDefaults.buttonColors(
MaterialTheme.colorScheme.primary)) {
Text("Enable Location")
}
// Show permission request button
Button(
onClick = {
permissionLauncher.launch(
Manifest.permission.ACCESS_FINE_LOCATION)
},
modifier = Modifier.testTag("enable_location_button"),
colors =
ButtonDefaults.buttonColors(
MaterialTheme.colorScheme.primary)) {
Text("Enable Location")
}
}
locationProvider.currentLocation.value == null -> {
CircularProgressIndicator(color = Color.White)
Expand Down

0 comments on commit 0854625

Please sign in to comment.