-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert sample app activity to Compose
- Loading branch information
1 parent
3290398
commit 2ba00bd
Showing
5 changed files
with
153 additions
and
64 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
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
97 changes: 97 additions & 0 deletions
97
sample/src/main/java/com/google/accompanist/sample/MainScreen.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,97 @@ | ||
/* | ||
* Copyright 2024 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.accompanist.sample | ||
|
||
import android.content.Intent | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.systemBars | ||
import androidx.compose.foundation.layout.windowInsetsBottomHeight | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.ListItem | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.material3.TopAppBarDefaults | ||
import androidx.compose.material3.rememberTopAppBarState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.input.nestedscroll.nestedScroll | ||
import androidx.compose.ui.res.stringResource | ||
|
||
data class AccompanistSample( | ||
val title: String, | ||
val intent: Intent | ||
) | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun MainScreen( | ||
listData: List<AccompanistSample>, | ||
onItemClick: (Intent) -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
Surface(modifier) { | ||
Column { | ||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState()) | ||
TopAppBar( | ||
title = { Text(stringResource(R.string.app_name)) }, | ||
scrollBehavior = scrollBehavior | ||
) | ||
|
||
ContentList( | ||
listData, | ||
onItemClick, | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.nestedScroll(scrollBehavior.nestedScrollConnection) | ||
) | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
private fun ContentList( | ||
listData: List<AccompanistSample>, | ||
onItemClick: (Intent) -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
LazyColumn( | ||
modifier = modifier | ||
) { | ||
items(listData) { | ||
ListItem( | ||
headlineText = { Text(it.title) }, | ||
modifier = Modifier.clickable { onItemClick(it.intent) } | ||
) | ||
} | ||
|
||
item { | ||
Spacer( | ||
Modifier.windowInsetsBottomHeight( | ||
WindowInsets.systemBars | ||
) | ||
) | ||
} | ||
} | ||
} |