-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add InfoScreen to datalayer wear sample (#1895)
- Loading branch information
Showing
7 changed files
with
195 additions
and
4 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
87 changes: 87 additions & 0 deletions
87
...r/src/main/java/com/google/android/horologist/datalayer/sample/screens/info/InfoScreen.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,87 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.datalayer.sample.screens.info | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Done | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.wear.compose.material.Text | ||
import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices | ||
import com.google.android.horologist.compose.layout.ScalingLazyColumn | ||
import com.google.android.horologist.compose.layout.ScalingLazyColumnState | ||
import com.google.android.horologist.compose.layout.belowTimeTextPreview | ||
import com.google.android.horologist.compose.material.Button | ||
import com.google.android.horologist.datalayer.sample.R | ||
|
||
@Composable | ||
fun InfoScreen( | ||
onDismissClick: () -> Unit, | ||
columnState: ScalingLazyColumnState, | ||
modifier: Modifier = Modifier, | ||
infoScreenViewModel: InfoScreenViewModel = hiltViewModel(), | ||
) { | ||
InfoScreen( | ||
message = infoScreenViewModel.message, | ||
onDismissClick = onDismissClick, | ||
columnState = columnState, | ||
modifier = modifier, | ||
) | ||
} | ||
|
||
@Composable | ||
fun InfoScreen( | ||
message: String, | ||
onDismissClick: () -> Unit, | ||
columnState: ScalingLazyColumnState, | ||
modifier: Modifier = Modifier, | ||
) { | ||
ScalingLazyColumn( | ||
columnState = columnState, | ||
modifier = modifier | ||
.fillMaxSize(), | ||
) { | ||
item { | ||
Text(text = message, modifier = Modifier.padding(top = 20.dp)) | ||
} | ||
item { | ||
Button( | ||
imageVector = Icons.Default.Done, | ||
contentDescription = stringResource(id = R.string.info_done_button_content_description), | ||
onClick = onDismissClick, | ||
modifier = Modifier.padding(top = 10.dp), | ||
) | ||
} | ||
} | ||
} | ||
|
||
@WearPreviewDevices | ||
@Composable | ||
fun InfoScreenPreview() { | ||
InfoScreen( | ||
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " + | ||
"incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud " + | ||
"exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", | ||
onDismissClick = { }, | ||
columnState = belowTimeTextPreview(), | ||
) | ||
} |
60 changes: 60 additions & 0 deletions
60
.../java/com/google/android/horologist/datalayer/sample/screens/info/InfoScreenNavigation.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,60 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.datalayer.sample.screens.info | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.navigation.NavController | ||
import androidx.navigation.NavGraphBuilder | ||
import androidx.navigation.NavType | ||
import androidx.navigation.navArgument | ||
import com.google.android.horologist.compose.navscaffold.scrollable | ||
import com.google.android.horologist.datalayer.sample.Screen | ||
import java.net.URLDecoder | ||
import java.net.URLEncoder | ||
|
||
private const val messageArg = "message" | ||
private const val routePrefix = "infoScreen" | ||
|
||
private val URL_CHARACTER_ENCODING = Charsets.UTF_8.name() | ||
|
||
const val infoScreenRoute = "$routePrefix/{$messageArg}" | ||
|
||
internal class InfoScreenArgs(val message: String) { | ||
constructor(savedStateHandle: SavedStateHandle) : | ||
this(URLDecoder.decode(checkNotNull(savedStateHandle[messageArg]), URL_CHARACTER_ENCODING)) | ||
} | ||
|
||
fun NavController.navigateToInfoScreen(message: String) { | ||
val encodedMessage = URLEncoder.encode(message, URL_CHARACTER_ENCODING) | ||
this.navigate("$routePrefix/$encodedMessage") | ||
} | ||
|
||
fun NavGraphBuilder.infoScreen( | ||
onDismissClick: () -> Unit, | ||
) { | ||
scrollable( | ||
route = Screen.InfoScreen.route, | ||
arguments = listOf( | ||
navArgument(messageArg) { type = NavType.StringType }, | ||
), | ||
) { | ||
InfoScreen( | ||
onDismissClick = onDismissClick, | ||
columnState = it.columnState, | ||
) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...n/java/com/google/android/horologist/datalayer/sample/screens/info/InfoScreenViewModel.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,30 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.datalayer.sample.screens.info | ||
|
||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
|
||
class InfoScreenViewModel( | ||
savedStateHandle: SavedStateHandle, | ||
) : ViewModel() { | ||
|
||
private val infoScreenArgs: InfoScreenArgs = InfoScreenArgs(savedStateHandle) | ||
|
||
val message: String | ||
get() = infoScreenArgs.message | ||
} |
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