1
1
package com.sanmer.mrepo.ui.screens.settings
2
2
3
+ import androidx.activity.compose.rememberLauncherForActivityResult
4
+ import androidx.activity.result.contract.ActivityResultContracts
3
5
import androidx.compose.foundation.layout.Column
4
6
import androidx.compose.foundation.layout.WindowInsets
5
7
import androidx.compose.foundation.layout.fillMaxWidth
@@ -9,22 +11,28 @@ import androidx.compose.foundation.verticalScroll
9
11
import androidx.compose.material3.Icon
10
12
import androidx.compose.material3.IconButton
11
13
import androidx.compose.material3.Scaffold
14
+ import androidx.compose.material3.SnackbarDuration
15
+ import androidx.compose.material3.SnackbarHost
16
+ import androidx.compose.material3.SnackbarHostState
12
17
import androidx.compose.material3.TopAppBar
13
18
import androidx.compose.material3.TopAppBarDefaults
14
19
import androidx.compose.material3.TopAppBarScrollBehavior
15
20
import androidx.compose.runtime.Composable
16
21
import androidx.compose.runtime.getValue
17
22
import androidx.compose.runtime.mutableStateOf
18
23
import androidx.compose.runtime.remember
24
+ import androidx.compose.runtime.rememberCoroutineScope
19
25
import androidx.compose.runtime.setValue
20
26
import androidx.compose.ui.Modifier
21
27
import androidx.compose.ui.input.nestedscroll.nestedScroll
28
+ import androidx.compose.ui.platform.LocalContext
22
29
import androidx.compose.ui.res.painterResource
23
30
import androidx.compose.ui.res.stringResource
24
31
import androidx.hilt.navigation.compose.hiltViewModel
25
32
import androidx.navigation.NavController
26
33
import com.sanmer.mrepo.BuildConfig
27
34
import com.sanmer.mrepo.R
35
+ import com.sanmer.mrepo.app.Logcat
28
36
import com.sanmer.mrepo.datastore.WorkingMode
29
37
import com.sanmer.mrepo.ui.component.SettingNormalItem
30
38
import com.sanmer.mrepo.ui.component.TopAppBarTitle
@@ -35,24 +43,55 @@ import com.sanmer.mrepo.ui.screens.settings.items.RootItem
35
43
import com.sanmer.mrepo.ui.utils.navigateSingleTopTo
36
44
import com.sanmer.mrepo.ui.utils.none
37
45
import com.sanmer.mrepo.viewmodel.SettingsViewModel
46
+ import kotlinx.coroutines.launch
38
47
39
48
@Composable
40
49
fun SettingsScreen (
41
50
navController : NavController ,
42
51
viewModel : SettingsViewModel = hiltViewModel()
43
52
) {
44
53
val userPreferences = LocalUserPreferences .current
45
-
46
54
val scrollBehavior = TopAppBarDefaults .pinnedScrollBehavior()
55
+ val scope = rememberCoroutineScope()
56
+ val snackbarHostState = remember { SnackbarHostState () }
57
+
58
+ val context = LocalContext .current
59
+ val launcher = rememberLauncherForActivityResult(
60
+ ActivityResultContracts .CreateDocument (" */*" )
61
+ ) { uri ->
62
+ if (uri == null ) return @rememberLauncherForActivityResult
63
+
64
+ scope.launch {
65
+ Logcat .writeTo(context, uri)
66
+ .onSuccess {
67
+ val message = context.getString(R .string.install_logs_saved)
68
+ snackbarHostState.showSnackbar(
69
+ message = message,
70
+ duration = SnackbarDuration .Short
71
+ )
72
+ }.onFailure {
73
+ val message = context.getString(
74
+ R .string.install_logs_save_failed,
75
+ it.message ? : context.getString(R .string.unknown_error)
76
+ )
77
+ snackbarHostState.showSnackbar(
78
+ message = message,
79
+ duration = SnackbarDuration .Short
80
+ )
81
+ }
82
+ }
83
+ }
47
84
48
85
Scaffold (
49
86
modifier = Modifier .nestedScroll(scrollBehavior.nestedScrollConnection),
50
87
topBar = {
51
88
TopBar (
89
+ exportLog = { launcher.launch(Logcat .logfile) },
52
90
isRoot = userPreferences.isRoot,
53
91
scrollBehavior = scrollBehavior
54
92
)
55
93
},
94
+ snackbarHost = { SnackbarHost (snackbarHostState) },
56
95
contentWindowInsets = WindowInsets .none
57
96
) { innerPadding ->
58
97
Column (
@@ -115,15 +154,24 @@ fun SettingsScreen(
115
154
116
155
@Composable
117
156
private fun TopBar (
157
+ exportLog : () -> Unit ,
118
158
isRoot : Boolean ,
119
159
scrollBehavior : TopAppBarScrollBehavior
120
160
) = TopAppBar (
121
161
title = {
122
162
TopAppBarTitle (text = stringResource(id = R .string.page_settings))
123
163
},
124
164
actions = {
125
- var expanded by remember { mutableStateOf(false ) }
165
+ IconButton (
166
+ onClick = exportLog
167
+ ) {
168
+ Icon (
169
+ painter = painterResource(id = R .drawable.bug),
170
+ contentDescription = null
171
+ )
172
+ }
126
173
174
+ var expanded by remember { mutableStateOf(false ) }
127
175
IconButton (
128
176
onClick = { expanded = true },
129
177
enabled = isRoot
0 commit comments