Skip to content

Commit b963851

Browse files
feat: added ability to copy logs in android (#255)
1 parent 8b022c2 commit b963851

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

‎android/app/src/main/java/com/therealaleph/mhrv/ui/HomeScreen.kt‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.therealaleph.mhrv.ui
22

3+
import android.widget.Toast
34
import androidx.compose.animation.AnimatedVisibility
45
import androidx.compose.foundation.background
56
import androidx.compose.foundation.layout.*
@@ -9,6 +10,7 @@ import androidx.compose.foundation.lazy.rememberLazyListState
910
import androidx.compose.foundation.rememberScrollState
1011
import androidx.compose.foundation.shape.RoundedCornerShape
1112
import androidx.compose.foundation.text.KeyboardOptions
13+
import androidx.compose.foundation.text.selection.SelectionContainer
1214
import androidx.compose.foundation.verticalScroll
1315
import androidx.compose.material.icons.Icons
1416
import androidx.compose.material.icons.filled.CheckCircle
@@ -23,7 +25,9 @@ import androidx.compose.runtime.saveable.rememberSaveable
2325
import androidx.compose.ui.Alignment
2426
import androidx.compose.ui.Modifier
2527
import androidx.compose.ui.draw.clip
28+
import androidx.compose.ui.platform.LocalClipboardManager
2629
import androidx.compose.ui.platform.LocalContext
30+
import androidx.compose.ui.text.AnnotatedString
2731
import androidx.compose.ui.text.font.FontFamily
2832
import androidx.compose.ui.text.input.ImeAction
2933
import androidx.compose.ui.text.input.KeyboardType
@@ -1231,6 +1235,8 @@ private fun LiveLogPane() {
12311235
val lines = remember { mutableStateListOf<String>() }
12321236
val listState = rememberLazyListState()
12331237
val scope = rememberCoroutineScope()
1238+
val clipboard = LocalClipboardManager.current
1239+
val ctx = LocalContext.current
12341240

12351241
// Pull from the ring buffer periodically. We pull even while the
12361242
// section is collapsed (cheap), so re-expanding shows fresh tail.
@@ -1260,24 +1266,41 @@ private fun LiveLogPane() {
12601266
color = MaterialTheme.colorScheme.onSurfaceVariant,
12611267
modifier = Modifier.weight(1f),
12621268
)
1263-
TextButton(onClick = { lines.clear() }) { Text("Clear") }
1269+
TextButton(
1270+
enabled = lines.isNotEmpty(),
1271+
onClick = {
1272+
clipboard.setText(AnnotatedString(lines.joinToString("\n")))
1273+
Toast.makeText(
1274+
ctx,
1275+
ctx.getString(R.string.snack_logs_copied),
1276+
Toast.LENGTH_SHORT,
1277+
).show()
1278+
},
1279+
) { Text(stringResource(R.string.btn_copy)) }
1280+
TextButton(onClick = { lines.clear() }) { Text(stringResource(R.string.btn_clear)) }
12641281
}
12651282
Surface(
12661283
color = MaterialTheme.colorScheme.surfaceVariant,
12671284
shape = RoundedCornerShape(8.dp),
12681285
modifier = Modifier.fillMaxWidth().heightIn(min = 160.dp, max = 320.dp),
12691286
) {
1270-
LazyColumn(
1271-
state = listState,
1272-
modifier = Modifier.padding(8.dp),
1273-
) {
1274-
items(lines) { line ->
1275-
Text(
1276-
line,
1277-
style = MaterialTheme.typography.bodySmall,
1278-
fontFamily = FontFamily.Monospace,
1279-
fontSize = 11.sp,
1280-
)
1287+
// SelectionContainer makes log lines selectable for manual
1288+
// copy of partial ranges. Cross-line selection works within the
1289+
// currently rendered window; for "copy everything" the Copy
1290+
// button above is the reliable path.
1291+
SelectionContainer {
1292+
LazyColumn(
1293+
state = listState,
1294+
modifier = Modifier.padding(8.dp),
1295+
) {
1296+
items(lines) { line ->
1297+
Text(
1298+
line,
1299+
style = MaterialTheme.typography.bodySmall,
1300+
fontFamily = FontFamily.Monospace,
1301+
fontSize = 11.sp,
1302+
)
1303+
}
12811304
}
12821305
}
12831306
}

‎android/app/src/main/res/values-fa/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<string name="btn_test">تست</string>
2525
<string name="btn_add">افزودن</string>
2626
<string name="btn_clear">پاک</string>
27+
<string name="btn_copy">کپی</string>
2728
<string name="btn_install">نصب</string>
2829
<string name="btn_cancel">انصراف</string>
2930

@@ -78,6 +79,7 @@
7879
<string name="snack_google_ip_updated">google_ip به %1$s به‌روزرسانی شد</string>
7980
<string name="snack_google_ip_current">google_ip قبلاً به‌روز است (%1$s)</string>
8081
<string name="snack_dns_lookup_failed">خطای DNS — اتصال شبکه را بررسی کنید</string>
82+
<string name="snack_logs_copied">لاگ‌ها در کلیپ‌بورد کپی شدند</string>
8183

8284
<!-- Usage today card -->
8385
<string name="sec_usage_today">مصرف امروز (تخمینی)</string>

‎android/app/src/main/res/values/strings.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<string name="btn_test">Test</string>
2525
<string name="btn_add">Add</string>
2626
<string name="btn_clear">Clear</string>
27+
<string name="btn_copy">Copy</string>
2728
<string name="btn_install">Install</string>
2829
<string name="btn_cancel">Cancel</string>
2930

@@ -78,6 +79,7 @@
7879
<string name="snack_google_ip_updated">google_ip updated to %1$s</string>
7980
<string name="snack_google_ip_current">google_ip already current (%1$s)</string>
8081
<string name="snack_dns_lookup_failed">DNS lookup failed — check network</string>
82+
<string name="snack_logs_copied">Logs copied to clipboard</string>
8183

8284
<!-- Usage today card -->
8385
<string name="sec_usage_today">Usage today (estimated)</string>

0 commit comments

Comments
 (0)