Skip to content

Commit f22c632

Browse files
committed
Use jetpack compose
1 parent 52e99de commit f22c632

18 files changed

Lines changed: 2586 additions & 1255 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,14 @@ android {
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("io.coil-kt:coil-compose:2.4.0")
260262
implementation("androidx.appcompat:appcompat:1.6.1")
261263
implementation("androidx.camera:camera-core:1.3.0-rc02")
262264
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ class DemoSharedPreferences(private val context: Context) {
2828
}
2929

3030
fun addMessages(messageAdapter: MessageAdapter) {
31+
addMessages(messageAdapter.savedMessages)
32+
}
33+
34+
fun addMessages(messages: List<Message>) {
3135
val editor = sharedPreferences.edit()
3236
val gson = Gson()
33-
val msgJSON = gson.toJson(messageAdapter.savedMessages)
37+
val msgJSON = gson.toJson(messages)
3438
editor.putString(context.getString(R.string.saved_messages_json_key), msgJSON)
3539
editor.apply()
3640
}

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

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,35 @@
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 androidx.activity.ComponentActivity
14+
import androidx.activity.compose.setContent
15+
import androidx.compose.foundation.layout.fillMaxSize
16+
import androidx.compose.material3.Surface
17+
import androidx.compose.ui.Modifier
1718
import androidx.core.content.ContextCompat
18-
import androidx.core.view.ViewCompat
19-
import androidx.core.view.WindowInsetsCompat
19+
import com.example.executorchllamademo.ui.screens.LogsScreen
20+
import com.example.executorchllamademo.ui.theme.LlamaDemoTheme
2021

21-
class LogsActivity : AppCompatActivity() {
22-
23-
private lateinit var logsAdapter: LogsAdapter
22+
class LogsActivity : ComponentActivity() {
2423

2524
override fun onCreate(savedInstanceState: Bundle?) {
2625
super.onCreate(savedInstanceState)
27-
setContentView(R.layout.activity_logs)
2826

2927
if (Build.VERSION.SDK_INT >= 21) {
3028
window.statusBarColor = ContextCompat.getColor(this, R.color.status_bar)
3129
window.navigationBarColor = ContextCompat.getColor(this, R.color.nav_bar)
3230
}
3331

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-
}
50-
51-
private fun setupLogs() {
52-
val logsListView = requireViewById<ListView>(R.id.logsListView)
53-
logsAdapter = LogsAdapter(this, R.layout.logs_message)
54-
55-
logsListView.adapter = logsAdapter
56-
logsAdapter.addAll(ETLogging.getInstance().getLogs())
57-
logsAdapter.notifyDataSetChanged()
58-
}
59-
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()
32+
setContent {
33+
LlamaDemoTheme(darkTheme = false) {
34+
Surface(modifier = Modifier.fillMaxSize()) {
35+
LogsScreen(
36+
onBackPressed = { finish() }
37+
)
7238
}
73-
.setNegativeButton(android.R.string.no, null)
74-
.show()
39+
}
7540
}
7641
}
77-
78-
override fun onDestroy() {
79-
super.onDestroy()
80-
ETLogging.getInstance().saveLogs()
81-
}
8242
}

0 commit comments

Comments
 (0)