Skip to content

Commit

Permalink
Preview render fix for HomeScreen and SenorsScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
umer0586 committed Oct 20, 2024
1 parent 65c9a68 commit a4a1a21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ import com.github.umer0586.sensagram.viewmodel.HomeScreenEvent
import com.github.umer0586.sensagram.viewmodel.HomeScreenUiState
import com.github.umer0586.sensagram.viewmodel.HomeScreenViewModel
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionState
import com.google.accompanist.permissions.rememberPermissionState


@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun HomeScreen(
viewModel: HomeScreenViewModel = viewModel(),
Expand All @@ -80,22 +82,26 @@ fun HomeScreen(
uiState = uiState,
landscapeMode = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE,
onUiEvent = viewModel::onUiEvent,
postNotificationPermissionState = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) rememberPermissionState(
android.Manifest.permission.POST_NOTIFICATIONS
) else null
)


}


@SuppressLint("InlinedApi")
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun HomeScreenContent(
uiState: HomeScreenUiState,
landscapeMode: Boolean = false,
onUiEvent: (HomeScreenEvent) -> Unit
onUiEvent: (HomeScreenEvent) -> Unit,
// PermissionState cannot be used within a Composable function that is being rendered in @Preview mode.
postNotificationPermissionState: PermissionState? = null
) {

val postNotificationPermissionState = rememberPermissionState(android.Manifest.permission.POST_NOTIFICATIONS)


if (!landscapeMode) {
Box(
Expand Down Expand Up @@ -138,7 +144,7 @@ fun HomeScreenContent(
// Whether user grant this permission or not we will start service anyway
// If permission is not granted foreground notification will not be shown
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
postNotificationPermissionState.launchPermissionRequest()
postNotificationPermissionState?.launchPermissionRequest()
}

onUiEvent(HomeScreenEvent.OnStartSubmit)
Expand Down Expand Up @@ -188,7 +194,7 @@ fun HomeScreenContent(
// Whether user grant this permission or not we will start service anyway
// If permission is not granted foreground notification will not be shown
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
postNotificationPermissionState.launchPermissionRequest()
postNotificationPermissionState?.launchPermissionRequest()
}
onUiEvent(HomeScreenEvent.OnStartSubmit)
},
Expand Down Expand Up @@ -263,6 +269,7 @@ private fun InfoCard(
}
}

@OptIn(ExperimentalPermissionsApi::class)
@Preview(showBackground = true, showSystemUi = true, name = "Home screen streaming")
@Composable
fun HomeScreenContentPreview2(
Expand All @@ -286,6 +293,7 @@ fun HomeScreenContentPreview2(

}

@OptIn(ExperimentalPermissionsApi::class)
@Preview(
showBackground = true,
showSystemUi = true,
Expand Down Expand Up @@ -315,6 +323,7 @@ fun HomeScreenContentLandScapePreview(

}

@OptIn(ExperimentalPermissionsApi::class)
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun HomeScreenContentPreview(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ fun SensorsScreen(viewModel: SensorsScreenViewModel = viewModel()) {
SensorScreenContent(
sensors = viewModel.availableSensors,
uiState = uiState,
onUiEvent = viewModel::onUiEvent
onUiEvent = viewModel::onUiEvent,
locationPermissionState = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
)

}
Expand All @@ -83,7 +84,7 @@ private fun SensorScreenContent(
uiState: SensorScreenUiState,
onUiEvent: (SensorScreenEvent) -> Unit,
// Don't declare PermissionState as local variable as we can't use it in preview
locationPermissionState: PermissionState? = rememberPermissionState(Manifest.permission.ACCESS_FINE_LOCATION)
locationPermissionState: PermissionState? = null
){

val sheetState = rememberModalBottomSheetState()
Expand Down

0 comments on commit a4a1a21

Please sign in to comment.