Skip to content

Commit

Permalink
Fix location permission
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Feb 5, 2024
1 parent c9cd5e1 commit 20be806
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ class HomeScreenViewModel @Inject constructor(
_state.value = _state.value.copy(currentTab = index)
}

fun shouldAskForBackgroundLocationPermission(ask: Boolean) {
_state.value = _state.value.copy(shouldAskForBackgroundLocationPermission = ask)
}

fun startTracking() {
shouldAskForBackgroundLocationPermission(false)
locationManager.startService()
}

Expand Down Expand Up @@ -151,7 +146,6 @@ class HomeScreenViewModel @Inject constructor(

data class HomeScreenState(
val currentTab: Int = 0,
val shouldAskForBackgroundLocationPermission: Boolean = false,
val spaces: List<SpaceInfo> = emptyList(),
val selectedSpaceId: String = "",
val selectedSpace: SpaceInfo? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.canopas.catchme.ui.flow.home.map

import android.Manifest
import androidx.annotation.DrawableRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.tween
Expand Down Expand Up @@ -54,11 +55,15 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.canopas.catchme.R
import com.canopas.catchme.data.utils.hasAllPermission
import com.canopas.catchme.data.utils.hasFineLocationPermission
import com.canopas.catchme.ui.flow.home.map.component.AddMemberBtn
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.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import com.google.accompanist.permissions.rememberPermissionState
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
Expand All @@ -73,14 +78,24 @@ import kotlinx.coroutines.launch
private const val DEFAULT_CAMERA_ZOOM = 15f
private const val DEFAULT_CAMERA_ZOOM_FOR_SELECTED_USER = 17f

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
@Composable
fun MapScreen() {
val viewModel = hiltViewModel<MapViewModel>()
val state by viewModel.state.collectAsState()
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp

val permissionState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
LaunchedEffect(permissionState) {
snapshotFlow { permissionState.status == PermissionStatus.Granted && state.currentCameraPosition == null }
.collect {
if (it) {
viewModel.startLocationTracking()
}
}
}

val bottomSheetState = rememberStandardBottomSheetState(
initialValue = SheetValue.Hidden,
skipHiddenState = false
Expand Down Expand Up @@ -183,7 +198,8 @@ fun MapScreenContent(modifier: Modifier) {
modifier = Modifier
.padding(horizontal = 16.dp)
.padding(bottom = 10.dp)
.fillMaxWidth().shadow(10.dp, shape = RoundedCornerShape(6.dp))
.fillMaxWidth()
.shadow(10.dp, shape = RoundedCornerShape(6.dp))
.background(AppTheme.colorScheme.surface, shape = RoundedCornerShape(6.dp))
.align(Alignment.CenterHorizontally),
contentPadding = PaddingValues(horizontal = 8.dp),
Expand Down Expand Up @@ -216,6 +232,22 @@ fun MapScreenContent(modifier: Modifier) {

@Composable
fun PermissionFooter(onClick: () -> Unit) {
val context = LocalContext.current
val hasLocationPermission = context.hasFineLocationPermission

val title = if (!hasLocationPermission) {
stringResource(id = R.string.home_permission_footer_title)
} else {
stringResource(id = R.string.home_permission_footer_missing_location_permission_title)
}

val subTitle =
if (!hasLocationPermission) {
stringResource(id = R.string.home_permission_footer_subtitle)
} else {
stringResource(id = R.string.home_permission_footer_missing_location_permission_subtitle)
}

Row(
modifier = Modifier
.height(72.dp)
Expand All @@ -229,15 +261,15 @@ fun PermissionFooter(onClick: () -> Unit) {
verticalArrangement = Arrangement.Center
) {
Text(
text = stringResource(id = R.string.home_permission_footer_missing_permission_title),
text = title,
style = AppTheme.appTypography.label1.copy(
color = AppTheme.colorScheme.textInversePrimary,
fontWeight = FontWeight.W600
)
)
Spacer(modifier = Modifier.height(2.dp))
Text(
text = stringResource(id = R.string.home_permission_footer_missing_permission_subtitle),
text = subTitle,
style = AppTheme.appTypography.body3.copy(color = AppTheme.colorScheme.textInversePrimary)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class MapViewModel @Inject constructor(
fun navigateToPermissionScreen() {
navigator.navigateTo(AppDestinations.enablePermissions.path)
}

fun startLocationTracking() {
viewModelScope.launch(appDispatcher.IO) {
val currentLocation = locationManager.getLastLocation()
_state.emit(_state.value.copy(currentCameraPosition = currentLocation))
locationManager.startService()
}
}
}

data class MapScreenState(
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
<string name="member_detail_empty_location_history">No Location History Found!</string>
<string name="home_permission_footer_title">Stay closed to your loved one</string>
<string name="home_permission_footer_subtitle">Allow CatchMe to access your location data.</string>
<string name="home_permission_footer_missing_permission_title">Location sharing is currently not possible</string>
<string name="home_permission_footer_missing_permission_subtitle">Some permissions are missing - grant access</string>
<string name="home_permission_footer_missing_location_permission_title">Location sharing is currently not possible</string>
<string name="home_permission_footer_missing_location_permission_subtitle">Some permissions are missing - grant access</string>

<string-array name="home_create_space_name_suggestion">
<item>Family</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,13 @@ class HomeScreenViewModelTest {
assert(viewModel.state.value.currentTab == 1)
}

@Test
fun `shouldAskForBackgroundLocationPermission should set shouldAskForBackgroundLocationPermission to true`() =
runTest {
setUp()
viewModel.shouldAskForBackgroundLocationPermission(true)
assert(viewModel.state.value.shouldAskForBackgroundLocationPermission)
}

@Test
fun `toggleSpaceSelection should toggle showSpaceSelectionPopup`() = runTest {
setUp()
viewModel.toggleSpaceSelection()
assert(viewModel.state.value.showSpaceSelectionPopup)
}

@Test
fun `startTracking should set shouldAskForBackgroundLocationPermission to false`() = runTest {
setUp()
viewModel.startTracking()
assert(!viewModel.state.value.shouldAskForBackgroundLocationPermission)
}

@Test
fun `startTracking should call locationManager startService`() = runTest {
setUp()
Expand Down

0 comments on commit 20be806

Please sign in to comment.