Skip to content

Commit

Permalink
Merge pull request #1807 from google/ben/samplecompose
Browse files Browse the repository at this point in the history
Convert sample app activity to Compose
  • Loading branch information
bentrengrove authored Dec 11, 2024
2 parents 7927785 + edede14 commit a03d4df
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = 34
compileSdk = 35

defaultConfig {
minSdk = 21
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ plugins {
}

android {
compileSdk = 34
compileSdk = 35

defaultConfig {
applicationId = "com.google.accompanist.sample"
Expand Down
5 changes: 3 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material.Light.NoActionBar">
android:theme="@android:style/Theme.Material.NoActionBar">

<profileable android:shell="true"
tools:targetApi="q" />

<activity
android:name="com.google.accompanist.sample.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Material.Light"
android:theme="@android:style/Theme.Material.Light.NoActionBar"
android:windowSoftInputMode="adjustResize"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
113 changes: 52 additions & 61 deletions sample/src/main/java/com/google/accompanist/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,43 @@
* limitations under the License.
*/

@file:Suppress("DEPRECATION") // ListActivity

package com.google.accompanist.sample

import android.annotation.SuppressLint
import android.app.ListActivity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ListView
import android.widget.SimpleAdapter
import java.text.Collator
import java.util.ArrayList
import java.util.Collections
import java.util.Comparator
import java.util.HashMap
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable

/**
* A [ListActivity] which automatically populates the list of sample activities in this app
* A list which automatically populates the list of sample activities in this app
* with the category `com.google.accompanist.sample.SAMPLE_CODE`.
*/
class MainActivity : ListActivity() {
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

listAdapter = SimpleAdapter(
this,
getData(intent.getStringExtra(EXTRA_PATH)),
android.R.layout.simple_list_item_1,
arrayOf("title"),
intArrayOf(android.R.id.text1)
)
enableEdgeToEdge()
val data = getData(intent.getStringExtra(EXTRA_PATH))

listView.isTextFilterEnabled = true
setContent {
AccompanistM3SampleTheme {
MainScreen(
listData = data,
onItemClick = { startActivity(it) }
)
}
}
}

private fun getData(prefix: String?): List<Map<String, Any>> {
val myData = ArrayList<Map<String, Any>>()
private fun getData(prefix: String?): List<AccompanistSample> {
val myData = mutableListOf<AccompanistSample>()

val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory("com.google.accompanist.sample.SAMPLE_CODE")
Expand All @@ -69,7 +68,7 @@ class MainActivity : ListActivity() {
prefixWithSlash = "$prefix/"
}

val entries = HashMap<String, Boolean>()
val entries = mutableMapOf<String, Boolean>()

list.forEach { info ->
val labelSeq = info.loadLabel(packageManager)
Expand All @@ -78,22 +77,24 @@ class MainActivity : ListActivity() {
if (prefixWithSlash.isNullOrEmpty() || label.startsWith(prefixWithSlash)) {
val labelPath = label.split("/".toRegex()).toTypedArray()
val nextLabel = if (prefixPath == null) labelPath[0] else labelPath[prefixPath.size]
if (prefixPath?.size ?: 0 == labelPath.size - 1) {
addItem(
data = myData,
name = nextLabel,
intent = activityIntent(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name
if ((prefixPath?.size ?: 0) == labelPath.size - 1) {
myData.add(
AccompanistSample(
title = nextLabel,
intent = activityIntent(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name
)
)
)
} else {
if (entries[nextLabel] == null) {
addItem(
data = myData,
name = nextLabel,
intent = browseIntent(
if (prefix == "") nextLabel else "$prefix/$nextLabel"
myData.add(
AccompanistSample(
title = nextLabel,
intent = browseIntent(
if (prefix == "") nextLabel else "$prefix/$nextLabel"
)
)
)
entries[nextLabel] = true
Expand All @@ -102,7 +103,7 @@ class MainActivity : ListActivity() {
}
}

Collections.sort(myData, sDisplayNameComparator)
myData.sortBy { it.title }

return myData
}
Expand All @@ -119,30 +120,20 @@ class MainActivity : ListActivity() {
result.putExtra(EXTRA_PATH, path)
return result
}
}

private fun addItem(data: MutableList<Map<String, Any>>, name: String, intent: Intent) {
val temp = mutableMapOf<String, Any>()
temp["title"] = name
temp["intent"] = intent
data += temp
}

@Deprecated("Deprecated in Java")
override fun onListItemClick(l: ListView, v: View, position: Int, id: Long) {
val map = l.getItemAtPosition(position) as Map<*, *>
val intent = map["intent"] as Intent?
startActivity(intent)
}

companion object {
private const val EXTRA_PATH = "com.example.android.apis.Path"

private val sDisplayNameComparator = object : Comparator<Map<String, Any>> {
private val collator = Collator.getInstance()
private const val EXTRA_PATH = "com.example.android.apis.Path"

override fun compare(map1: Map<String, Any>, map2: Map<String, Any>): Int {
return collator.compare(map1["title"], map2["title"])
}
}
}
/**
* TODO Migrate whole sample app to m3 and move to Theme.kt
*/
@Composable
fun AccompanistM3SampleTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
MaterialTheme(
colorScheme = if (darkTheme) darkColorScheme() else lightColorScheme(),
content = content
)
}
104 changes: 104 additions & 0 deletions sample/src/main/java/com/google/accompanist/sample/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.layout.windowInsetsPadding
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()
.windowInsetsPadding(
WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal)
)
.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
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
Expand All @@ -33,6 +32,7 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -56,7 +56,7 @@ class DraggableFoldAwareColumnSample : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
AccompanistSampleTheme {
Box(modifier = Modifier.fillMaxSize()) {
Surface(modifier = Modifier.fillMaxSize()) {
DraggableExample(this@DraggableFoldAwareColumnSample)
}
}
Expand Down

0 comments on commit a03d4df

Please sign in to comment.