Skip to content

Commit a408e8b

Browse files
authored
LlamaDemo Use jetpack compose (#169)
1 parent 52e99de commit a408e8b

56 files changed

Lines changed: 3505 additions & 3241 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

llm/android/LlamaDemo/app/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,22 @@ android {
244244
}
245245
kotlinOptions { jvmTarget = "1.8" }
246246
buildFeatures { compose = true }
247-
composeOptions { kotlinCompilerExtensionVersion = "1.4.3" }
247+
composeOptions { kotlinCompilerExtensionVersion = "1.5.14" }
248248
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
249249
}
250250

251251
dependencies {
252252
implementation("androidx.core:core-ktx:1.9.0")
253253
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
254+
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
254255
implementation("androidx.activity:activity-compose:1.7.0")
255256
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
256257
implementation("androidx.compose.ui:ui")
257258
implementation("androidx.compose.ui:ui-graphics")
258259
implementation("androidx.compose.ui:ui-tooling-preview")
259260
implementation("androidx.compose.material3:material3")
261+
implementation("androidx.compose.material:material-icons-extended")
262+
implementation("io.coil-kt:coil-compose:2.4.0")
260263
implementation("androidx.appcompat:appcompat:1.6.1")
261264
implementation("androidx.camera:camera-core:1.3.0-rc02")
262265
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")

llm/android/LlamaDemo/app/src/androidTest/java/com/example/executorchllamademo/UIWorkflowTest.kt

Lines changed: 469 additions & 753 deletions
Large diffs are not rendered by default.

llm/android/LlamaDemo/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
android:name=".MainActivity"
6565
android:exported="true"
6666
android:label="@string/app_name"
67+
android:windowSoftInputMode="adjustResize"
6768
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
6869
<intent-filter>
6970
<action android:name="android.intent.action.MAIN" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
package com.example.executorchllamademo
10+
11+
enum class AppearanceMode(val displayName: String) {
12+
SYSTEM("System"),
13+
LIGHT("Light"),
14+
DARK("Dark");
15+
16+
companion object {
17+
fun fromDisplayName(name: String): AppearanceMode {
18+
return values().find { it.displayName == name } ?: SYSTEM
19+
}
20+
}
21+
}

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/DemoSharedPreferences.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ class DemoSharedPreferences(private val context: Context) {
2727
) ?: ""
2828
}
2929

30-
fun addMessages(messageAdapter: MessageAdapter) {
30+
fun addMessages(messages: List<Message>) {
3131
val editor = sharedPreferences.edit()
3232
val gson = Gson()
33-
val msgJSON = gson.toJson(messageAdapter.savedMessages)
33+
val msgJSON = gson.toJson(messages)
3434
editor.putString(context.getString(R.string.saved_messages_json_key), msgJSON)
3535
editor.apply()
3636
}

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/LogsActivity.kt

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,67 @@
88

99
package com.example.executorchllamademo
1010

11-
import android.app.AlertDialog
1211
import android.os.Build
1312
import android.os.Bundle
14-
import android.widget.ImageButton
15-
import android.widget.ListView
16-
import androidx.appcompat.app.AppCompatActivity
13+
import android.util.Log
14+
import androidx.activity.ComponentActivity
15+
import androidx.activity.compose.setContent
16+
import androidx.compose.foundation.isSystemInDarkTheme
17+
import androidx.compose.foundation.layout.fillMaxSize
18+
import androidx.compose.material3.Surface
19+
import androidx.compose.runtime.getValue
20+
import androidx.compose.runtime.mutableStateOf
21+
import androidx.compose.runtime.setValue
22+
import androidx.compose.ui.Modifier
1723
import androidx.core.content.ContextCompat
18-
import androidx.core.view.ViewCompat
19-
import androidx.core.view.WindowInsetsCompat
24+
import com.example.executorchllamademo.ui.screens.LogsScreen
25+
import com.example.executorchllamademo.ui.theme.LlamaDemoTheme
26+
import com.google.gson.Gson
2027

21-
class LogsActivity : AppCompatActivity() {
28+
class LogsActivity : ComponentActivity() {
2229

23-
private lateinit var logsAdapter: LogsAdapter
30+
private var appearanceMode by mutableStateOf(AppearanceMode.SYSTEM)
2431

2532
override fun onCreate(savedInstanceState: Bundle?) {
2633
super.onCreate(savedInstanceState)
27-
setContentView(R.layout.activity_logs)
2834

2935
if (Build.VERSION.SDK_INT >= 21) {
3036
window.statusBarColor = ContextCompat.getColor(this, R.color.status_bar)
3137
window.navigationBarColor = ContextCompat.getColor(this, R.color.nav_bar)
3238
}
3339

34-
ViewCompat.setOnApplyWindowInsetsListener(requireViewById(R.id.main)) { v, insets ->
35-
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
36-
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
37-
insets
38-
}
39-
40-
setupLogs()
41-
setupClearLogsButton()
42-
}
43-
44-
override fun onResume() {
45-
super.onResume()
46-
logsAdapter.clear()
47-
logsAdapter.addAll(ETLogging.getInstance().getLogs())
48-
logsAdapter.notifyDataSetChanged()
49-
}
40+
loadAppearanceMode()
5041

51-
private fun setupLogs() {
52-
val logsListView = requireViewById<ListView>(R.id.logsListView)
53-
logsAdapter = LogsAdapter(this, R.layout.logs_message)
42+
setContent {
43+
val isDarkTheme = when (appearanceMode) {
44+
AppearanceMode.LIGHT -> false
45+
AppearanceMode.DARK -> true
46+
AppearanceMode.SYSTEM -> isSystemInDarkTheme()
47+
}
5448

55-
logsListView.adapter = logsAdapter
56-
logsAdapter.addAll(ETLogging.getInstance().getLogs())
57-
logsAdapter.notifyDataSetChanged()
49+
LlamaDemoTheme(darkTheme = isDarkTheme) {
50+
Surface(modifier = Modifier.fillMaxSize()) {
51+
LogsScreen()
52+
}
53+
}
54+
}
5855
}
5956

60-
private fun setupClearLogsButton() {
61-
val clearLogsButton = requireViewById<ImageButton>(R.id.clearLogsButton)
62-
clearLogsButton.setOnClickListener {
63-
AlertDialog.Builder(this)
64-
.setTitle("Delete Logs History")
65-
.setMessage("Do you really want to delete logs history?")
66-
.setIcon(android.R.drawable.ic_dialog_alert)
67-
.setPositiveButton(android.R.string.yes) { _, _ ->
68-
// Clear the messageAdapter and sharedPreference
69-
ETLogging.getInstance().clearLogs()
70-
logsAdapter.clear()
71-
logsAdapter.notifyDataSetChanged()
72-
}
73-
.setNegativeButton(android.R.string.no, null)
74-
.show()
57+
private fun loadAppearanceMode() {
58+
val prefs = DemoSharedPreferences(this)
59+
val settingsJson = prefs.getSettings()
60+
if (settingsJson.isNotEmpty()) {
61+
try {
62+
val settings = Gson().fromJson(settingsJson, SettingsFields::class.java)
63+
appearanceMode = settings.appearanceMode
64+
} catch (e: Exception) {
65+
Log.e("LogsActivity", "Error loading appearance mode", e)
66+
}
7567
}
7668
}
7769

78-
override fun onDestroy() {
79-
super.onDestroy()
80-
ETLogging.getInstance().saveLogs()
70+
override fun onResume() {
71+
super.onResume()
72+
loadAppearanceMode()
8173
}
8274
}

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/LogsAdapter.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)