Skip to content

Commit

Permalink
Merge pull request #300 from LookUpGroup27/fix/location-bug-feed
Browse files Browse the repository at this point in the history
fix: location permission request in feed screen
  • Loading branch information
Ismaillat authored Dec 19, 2024
2 parents 1bdedab + 0854625 commit 43eed11
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import androidx.test.core.app.ApplicationProvider
Expand Down Expand Up @@ -158,13 +159,14 @@ class FeedScreenTest {
* feed. It handles the rendering of the FeedScreen with the specified posts and ensures a
* consistent setup across all tests.
*/
private fun setFeedScreenContent(initialNearbyPosts: List<Post>) {
private fun setFeedScreenContent(initialNearbyPosts: List<Post>, testNoLoca: Boolean = false) {
composeTestRule.setContent {
FeedScreen(
postsViewModel = postsViewModel,
navigationActions = navigationActions,
profileViewModel = profileViewModel,
initialNearbyPosts = initialNearbyPosts)
initialNearbyPosts = initialNearbyPosts,
testNoLoca = testNoLoca)
}
}

Expand Down Expand Up @@ -320,4 +322,14 @@ class FeedScreenTest {
// Assert: Placeholder image is displayed
composeTestRule.onNodeWithTag("no_images_placeholder").assertExists().assertIsDisplayed()
}

@Test
fun testEnableLocationButtonIsDisplayed() {
setFeedScreenContent(emptyList(), true)
composeTestRule
.onNodeWithTag("enable_location_button")
.assertExists() // Verify button is displayed
composeTestRule.onNodeWithText("Enable Location").assertExists() // Verify button text
composeTestRule.onNodeWithTag("enable_location_button").performClick()
}
}
77 changes: 51 additions & 26 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 @@ -5,6 +5,8 @@ import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
Expand Down Expand Up @@ -59,7 +61,8 @@ fun FeedScreen(
postsViewModel: PostsViewModel,
navigationActions: NavigationActions,
profileViewModel: ProfileViewModel,
initialNearbyPosts: List<Post>? = null
initialNearbyPosts: List<Post>? = null,
testNoLoca: Boolean = false
) {
// Fetch user profile
LaunchedEffect(Unit) {
Expand All @@ -79,40 +82,53 @@ fun FeedScreen(
// Location setup
val context = LocalContext.current
val locationProvider = LocationProviderSingleton.getInstance(context)
var locationPermissionGranted by remember { mutableStateOf(false) }
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)
}

// Posts-related state
val unfilteredPosts by
(initialNearbyPosts?.let { mutableStateOf(it) }
?: postsViewModel.nearbyPosts.collectAsState())
val nearbyPosts = unfilteredPosts.filter { it.userMail != userEmail }
val postRatings = remember { mutableStateMapOf<String, List<Boolean>>() }
// Permission request launcher
val permissionLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
locationPermissionGranted = isGranted
if (isGranted && !testNoLoca) {
locationProvider.requestLocationUpdates()
postsViewModel.fetchSortedPosts()
} else {
Toast.makeText(
context,
"Location permission is required. Please enable it in the app settings.",
Toast.LENGTH_LONG)
.show()
}
}

// Check for location permissions and fetch posts when granted.
LaunchedEffect(Unit) {
locationPermissionGranted =
ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED
Log.d("FeedScreen", "Location permission granted: $locationPermissionGranted")
// Trigger location updates when permission is granted
LaunchedEffect(locationPermissionGranted) {
if (locationPermissionGranted) {
locationProvider.requestLocationUpdates()

if (!locationPermissionGranted) {
Toast.makeText(
context, context.getString(R.string.location_permission_required), Toast.LENGTH_LONG)
.show()
} else {
while (locationProvider.currentLocation.value == null) {
delay(500)
delay(200) // Retry every 200ms
}
postsViewModel.fetchSortedPosts()
}
}

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

val postRatings = remember { mutableStateMapOf<String, List<Boolean>>() }

// Initialize post ratings based on the user profile.
LaunchedEffect(nearbyPosts, profile) {
nearbyPosts.forEach { post ->
Expand Down Expand Up @@ -175,12 +191,21 @@ fun FeedScreen(
modifier = Modifier.fillMaxSize().testTag("loading_indicator_test_tag"),
contentAlignment = Alignment.Center) {
when {
!locationPermissionGranted -> {
Text(
text = stringResource(R.string.location_permission_required),
style =
MaterialTheme.typography.bodyLarge.copy(
color = Color.White))
(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")
}
}
locationProvider.currentLocation.value == null -> {
CircularProgressIndicator(color = Color.White)
Expand Down

0 comments on commit 43eed11

Please sign in to comment.