-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create specific viewmodel for 'nearby' screen and improve p…
…ermission handling
- Loading branch information
Showing
10 changed files
with
147 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 50 additions & 6 deletions
56
app/src/main/java/io/github/balexei/vitrasaparada/ui/nearby/NearbyRoute.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,58 @@ | ||
package io.github.balexei.vitrasaparada.ui.nearby | ||
|
||
import android.Manifest | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import io.github.balexei.vitrasaparada.ui.MainViewModel | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.res.stringResource | ||
import com.google.accompanist.permissions.ExperimentalPermissionsApi | ||
import com.google.accompanist.permissions.isGranted | ||
import com.google.accompanist.permissions.rememberPermissionState | ||
import com.google.accompanist.permissions.shouldShowRationale | ||
import io.github.balexei.vitrasaparada.R | ||
|
||
|
||
@OptIn(ExperimentalPermissionsApi::class) | ||
@Composable | ||
fun NearbyRoute(viewModel: MainViewModel) { | ||
val currentLocation by viewModel.currentLocation.collectAsState() | ||
val nearbyStops by viewModel.nearbyStops.collectAsState() | ||
NearbyScreen(currentLocation = currentLocation, stops = nearbyStops, setFavourite = viewModel::setFavourite,) | ||
fun NearbyRoute(viewModel: NearbyViewModel) { | ||
val permissionState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION) | ||
LaunchedEffect(Unit) { | ||
if (!permissionState.status.isGranted) { | ||
permissionState.launchPermissionRequest() | ||
} | ||
} | ||
DisposableEffect(permissionState.status.isGranted) { | ||
if (permissionState.status.isGranted) { | ||
viewModel.startLocationUpdates() | ||
} | ||
onDispose { | ||
viewModel.stopLocationUpdates() | ||
} | ||
} | ||
|
||
when { | ||
permissionState.status.isGranted -> { | ||
val currentLocation by viewModel.currentLocation.collectAsState() | ||
val nearbyStops by viewModel.nearbyStops.collectAsState() | ||
NearbyScreen( | ||
currentLocation = currentLocation, | ||
stops = nearbyStops, | ||
setFavourite = viewModel::setFavourite, | ||
) | ||
} | ||
|
||
permissionState.status.shouldShowRationale -> { | ||
PermissionRequiredScreen(message = stringResource(id = R.string.location_rationale_message), | ||
onRequestPermission = { permissionState.launchPermissionRequest() }) | ||
} | ||
|
||
else -> { | ||
PermissionRequiredScreen( | ||
message = stringResource(id = R.string.location_required_message), | ||
onRequestPermission = { permissionState.launchPermissionRequest() }, | ||
showSettingsButton = true | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
app/src/main/java/io/github/balexei/vitrasaparada/ui/nearby/PermissionRequiredScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.balexei.vitrasaparada.ui.nearby | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.provider.Settings | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import io.github.balexei.vitrasaparada.R | ||
|
||
@Composable | ||
fun PermissionRequiredScreen( | ||
message: String, | ||
onRequestPermission: () -> Unit, | ||
showSettingsButton: Boolean = false | ||
) { | ||
val context = LocalContext.current | ||
|
||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text(message) | ||
Spacer(modifier = Modifier.height(16.dp)) | ||
Button(onClick = onRequestPermission) { | ||
Text(stringResource(id = R.string.grant_permission)) | ||
} | ||
if (showSettingsButton) { | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
Button(onClick = { | ||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { | ||
data = Uri.fromParts("package", context.packageName, null) | ||
} | ||
context.startActivity(intent) | ||
}) { | ||
Text(stringResource(id = R.string.open_settings)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters