Skip to content

Commit

Permalink
Convert sample app activity to Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
bentrengrove committed Dec 10, 2024
1 parent 3290398 commit 2ba00bd
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 64 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
3 changes: 2 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
<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
)
}
97 changes: 97 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,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
)
)
}
}
}

0 comments on commit 2ba00bd

Please sign in to comment.