|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2023 Sebastiano Barezzi |
| 2 | + * SPDX-FileCopyrightText: 2023-2025 Sebastiano Barezzi |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | package dev.sebaubuntu.athena |
7 | 7 |
|
8 | | -import android.content.Intent |
9 | | -import android.net.Uri |
10 | | -import android.os.Build |
11 | 8 | import android.os.Bundle |
12 | | -import android.view.Menu |
13 | | -import android.view.MenuItem |
14 | | -import android.view.View |
| 9 | +import androidx.activity.ComponentActivity |
| 10 | +import androidx.activity.compose.setContent |
15 | 11 | import androidx.activity.enableEdgeToEdge |
16 | | -import androidx.activity.result.contract.ActivityResultContracts |
17 | | -import androidx.appcompat.app.AppCompatActivity |
18 | | -import androidx.core.view.isVisible |
19 | | -import androidx.lifecycle.lifecycleScope |
20 | | -import androidx.navigation.fragment.NavHostFragment |
21 | | -import androidx.navigation.ui.AppBarConfiguration |
22 | | -import androidx.navigation.ui.setupWithNavController |
23 | | -import com.google.android.material.appbar.MaterialToolbar |
24 | | -import com.google.android.material.dialog.MaterialAlertDialogBuilder |
25 | | -import com.google.android.material.progressindicator.LinearProgressIndicator |
26 | | -import com.google.android.material.snackbar.Snackbar |
27 | | -import dev.sebaubuntu.athena.models.data.Section |
28 | | -import dev.sebaubuntu.athena.models.data.Section.Companion.listSerializer |
29 | | -import dev.sebaubuntu.athena.models.data.Section.Companion.toSerializable |
30 | | -import dev.sebaubuntu.athena.models.data.Subsection |
31 | | -import dev.sebaubuntu.athena.sections.SectionEnum |
32 | | -import kotlinx.coroutines.Dispatchers |
33 | | -import kotlinx.coroutines.async |
34 | | -import kotlinx.coroutines.awaitAll |
35 | | -import kotlinx.coroutines.flow.single |
36 | | -import kotlinx.coroutines.flow.take |
37 | | -import kotlinx.coroutines.launch |
38 | | -import kotlinx.coroutines.withContext |
39 | | -import kotlinx.serialization.json.Json |
40 | | -import java.io.FileWriter |
| 12 | +import androidx.compose.runtime.CompositionLocalProvider |
| 13 | +import dev.sebaubuntu.athena.ui.AthenaApp |
| 14 | +import dev.sebaubuntu.athena.ui.LocalPermissionsManager |
| 15 | +import dev.sebaubuntu.athena.utils.PermissionsManager |
41 | 16 |
|
42 | | -class MainActivity : AppCompatActivity() { |
43 | | - // Views |
44 | | - private val contentView by lazy { findViewById<View>(android.R.id.content) } |
45 | | - private val linearProgressIndicator by lazy { findViewById<LinearProgressIndicator>(R.id.linearProgressIndicator) } |
46 | | - private val toolbar by lazy { findViewById<MaterialToolbar>(R.id.toolbar) } |
47 | | - |
48 | | - // Fragments |
49 | | - private val navHostFragment by lazy { supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment } |
50 | | - |
51 | | - private val navController by lazy { navHostFragment.navController } |
52 | | - |
53 | | - // JSON export |
54 | | - private val requestPermissionsContract = registerForActivityResult( |
55 | | - ActivityResultContracts.RequestMultiplePermissions() |
56 | | - ) { |
57 | | - if (it.isNotEmpty()) { |
58 | | - if (it.all { permission -> permission.value }) { |
59 | | - createDocumentContract.launch("data.json") |
60 | | - } else { |
61 | | - Snackbar.make( |
62 | | - contentView, R.string.export_data_missing_permissions, Snackbar.LENGTH_LONG |
63 | | - ).show() |
64 | | - } |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - private val createDocumentContract = registerForActivityResult( |
69 | | - ActivityResultContracts.CreateDocument(JSON_MIME_TYPE) |
70 | | - ) { |
71 | | - it?.also { uri -> |
72 | | - exportData(uri) |
73 | | - } |
74 | | - } |
| 17 | +class MainActivity : ComponentActivity() { |
| 18 | + private val permissionsManager = PermissionsManager(this) |
75 | 19 |
|
76 | 20 | override fun onCreate(savedInstanceState: Bundle?) { |
77 | 21 | super.onCreate(savedInstanceState) |
78 | 22 |
|
79 | | - setContentView(R.layout.activity_main) |
80 | | - |
81 | 23 | // Enable edge-to-edge |
82 | 24 | enableEdgeToEdge() |
83 | 25 |
|
84 | | - setSupportActionBar(toolbar) |
85 | | - |
86 | | - val appBarConfiguration = AppBarConfiguration(navController.graph) |
87 | | - toolbar.setupWithNavController(navController, appBarConfiguration) |
88 | | - } |
89 | | - |
90 | | - override fun onCreateOptionsMenu(menu: Menu?): Boolean { |
91 | | - menuInflater.inflate(R.menu.activity_main, menu) |
92 | | - return true |
93 | | - } |
94 | | - |
95 | | - override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { |
96 | | - R.id.exportData -> { |
97 | | - MaterialAlertDialogBuilder(this) |
98 | | - .setTitle(R.string.export_data) |
99 | | - .setMessage(R.string.export_data_description) |
100 | | - .setPositiveButton(R.string.yes) { _, _ -> |
101 | | - requestPermissionsContract.launch( |
102 | | - SectionEnum.entries.map { |
103 | | - it.clazz.requiredPermissions.toList() |
104 | | - }.flatten().toTypedArray() |
105 | | - ) |
106 | | - } |
107 | | - .setNegativeButton(R.string.no) { _, _ -> |
108 | | - // Do nothing |
109 | | - } |
110 | | - .show() |
111 | | - |
112 | | - true |
113 | | - } |
114 | | - |
115 | | - else -> super.onOptionsItemSelected(item) |
116 | | - } |
117 | | - |
118 | | - private fun exportData(uri: Uri) { |
119 | | - lifecycleScope.launch { |
120 | | - linearProgressIndicator.progress = 0 |
121 | | - linearProgressIndicator.isVisible = true |
122 | | - |
123 | | - withContext(Dispatchers.IO) { |
124 | | - contentResolver.openFileDescriptor(uri, "wt")?.use { parcelFileDescriptor -> |
125 | | - FileWriter(parcelFileDescriptor.fileDescriptor).use { fileWriter -> |
126 | | - val sections = SectionEnum.entries.map { it.clazz } |
127 | | - |
128 | | - withContext(Dispatchers.Main) { |
129 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
130 | | - linearProgressIndicator.min = 0 |
131 | | - } |
132 | | - linearProgressIndicator.max = sections.size |
133 | | - } |
134 | | - |
135 | | - val sectionToData = mutableMapOf<Section, List<Subsection>>() |
136 | | - |
137 | | - val updateData: suspend ( |
138 | | - Section, List<Subsection>? |
139 | | - ) -> Unit = { section, data -> |
140 | | - data?.let { |
141 | | - sectionToData[section] = it |
142 | | - } |
143 | | - |
144 | | - withContext(Dispatchers.Main) { |
145 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
146 | | - linearProgressIndicator.setProgress( |
147 | | - sectionToData.size, true |
148 | | - ) |
149 | | - } else { |
150 | | - linearProgressIndicator.progress = sectionToData.size |
151 | | - } |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - sections.map { |
156 | | - async { |
157 | | - updateData( |
158 | | - it, |
159 | | - runCatching { |
160 | | - it.dataFlow(this@MainActivity) |
161 | | - }.getOrNull()?.take(1)?.single() |
162 | | - ) |
163 | | - } |
164 | | - }.awaitAll() |
165 | | - |
166 | | - val jsonData = Json.encodeToString( |
167 | | - listSerializer<Map<Section, List<Subsection>>>(), |
168 | | - sectionToData.toSerializable() |
169 | | - ) |
170 | | - |
171 | | - fileWriter.write(jsonData) |
172 | | - |
173 | | - withContext(Dispatchers.Main) { |
174 | | - linearProgressIndicator.progress = linearProgressIndicator.max |
175 | | - linearProgressIndicator.isVisible = false |
176 | | - |
177 | | - Snackbar |
178 | | - .make(contentView, R.string.export_data_done, Snackbar.LENGTH_SHORT) |
179 | | - .setAction(R.string.export_data_open_file) { |
180 | | - startActivity( |
181 | | - Intent.createChooser( |
182 | | - Intent(Intent.ACTION_VIEW, uri).apply { |
183 | | - type = JSON_MIME_TYPE |
184 | | - flags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
185 | | - }, |
186 | | - null, |
187 | | - ) |
188 | | - ) |
189 | | - } |
190 | | - .show() |
191 | | - } |
192 | | - } |
193 | | - } |
| 26 | + setContent { |
| 27 | + CompositionLocalProvider(LocalPermissionsManager provides permissionsManager) { |
| 28 | + AthenaApp() |
194 | 29 | } |
195 | 30 | } |
196 | 31 | } |
197 | 32 |
|
198 | 33 | companion object { |
199 | | - private const val JSON_MIME_TYPE = "application/json" |
200 | | - |
201 | 34 | init { |
202 | 35 | System.loadLibrary("athena") |
203 | 36 | } |
|
0 commit comments