|
8 | 8 |
|
9 | 9 | package com.example.executorchllamademo |
10 | 10 |
|
11 | | -import android.app.AlertDialog |
12 | 11 | import android.os.Build |
13 | 12 | 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 |
17 | 23 | 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 |
20 | 27 |
|
21 | | -class LogsActivity : AppCompatActivity() { |
| 28 | +class LogsActivity : ComponentActivity() { |
22 | 29 |
|
23 | | - private lateinit var logsAdapter: LogsAdapter |
| 30 | + private var appearanceMode by mutableStateOf(AppearanceMode.SYSTEM) |
24 | 31 |
|
25 | 32 | override fun onCreate(savedInstanceState: Bundle?) { |
26 | 33 | super.onCreate(savedInstanceState) |
27 | | - setContentView(R.layout.activity_logs) |
28 | 34 |
|
29 | 35 | if (Build.VERSION.SDK_INT >= 21) { |
30 | 36 | window.statusBarColor = ContextCompat.getColor(this, R.color.status_bar) |
31 | 37 | window.navigationBarColor = ContextCompat.getColor(this, R.color.nav_bar) |
32 | 38 | } |
33 | 39 |
|
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() |
50 | 41 |
|
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 | + } |
54 | 48 |
|
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 | + } |
58 | 55 | } |
59 | 56 |
|
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 | + } |
75 | 67 | } |
76 | 68 | } |
77 | 69 |
|
78 | | - override fun onDestroy() { |
79 | | - super.onDestroy() |
80 | | - ETLogging.getInstance().saveLogs() |
| 70 | + override fun onResume() { |
| 71 | + super.onResume() |
| 72 | + loadAppearanceMode() |
81 | 73 | } |
82 | 74 | } |
0 commit comments