Use stable list keys in Logcat and Tasker
This commit is contained in:
1 parent
3dc0173b9f
commit
258de20972
2 files changed
+17
-19
No files matched your search
@@ -14,7 +14,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
|
|||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.itemsIndexed
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.FloatingActionButton
|
import androidx.compose.material3.FloatingActionButton
|
||||||
@@ -213,7 +213,7 @@ fun LogcatScreen(
|
|||||||
.verticalScrollbar(listState),
|
.verticalScrollbar(listState),
|
||||||
contentPadding = NavigationBarsBottomPadding()
|
contentPadding = NavigationBarsBottomPadding()
|
||||||
) {
|
) {
|
||||||
itemsIndexed(items = logs, key = { index, _ -> index }) { _, log ->
|
items(items = logs, key = { it }) { log ->
|
||||||
LogcatItem(log = log, onLongClick = { Utils.setClipboard(context, log) })
|
LogcatItem(log = log, onLongClick = { Utils.setClipboard(context, log) })
|
||||||
ItemDivider()
|
ItemDivider()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,10 +39,14 @@ import com.v2ray.ang.ui.compose.SettingsSwitchItem
|
|||||||
import com.v2ray.ang.ui.compose.verticalScrollbar
|
import com.v2ray.ang.ui.compose.verticalScrollbar
|
||||||
import com.v2ray.ang.util.LogUtil
|
import com.v2ray.ang.util.LogUtil
|
||||||
|
|
||||||
|
data class TaskerItem(
|
||||||
|
val label: String,
|
||||||
|
val guid: String,
|
||||||
|
)
|
||||||
|
|
||||||
class TaskerActivity : BaseComponentActivity() {
|
class TaskerActivity : BaseComponentActivity() {
|
||||||
|
|
||||||
private var lstData: ArrayList<String> = ArrayList()
|
private val items = mutableListOf<TaskerItem>()
|
||||||
private var lstGuid: ArrayList<String> = ArrayList()
|
|
||||||
|
|
||||||
private val switchState = mutableStateOf(false)
|
private val switchState = mutableStateOf(false)
|
||||||
private val selectedPosition = mutableStateOf(-1)
|
private val selectedPosition = mutableStateOf(-1)
|
||||||
@@ -50,13 +54,11 @@ class TaskerActivity : BaseComponentActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
lstData.add("Default")
|
items.add(TaskerItem(label = "Default", guid = AppConfig.TASKER_DEFAULT_GUID))
|
||||||
lstGuid.add(AppConfig.TASKER_DEFAULT_GUID)
|
|
||||||
|
|
||||||
MmkvManager.decodeAllServerList().forEach { key ->
|
MmkvManager.decodeAllServerList().forEach { key ->
|
||||||
MmkvManager.decodeServerConfig(key)?.let { config ->
|
MmkvManager.decodeServerConfig(key)?.let { config ->
|
||||||
lstData.add(config.remarks)
|
items.add(TaskerItem(label = config.remarks, guid = key))
|
||||||
lstGuid.add(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +68,7 @@ class TaskerActivity : BaseComponentActivity() {
|
|||||||
@Composable
|
@Composable
|
||||||
override fun ScreenContent() {
|
override fun ScreenContent() {
|
||||||
TaskerScreen(
|
TaskerScreen(
|
||||||
items = lstData,
|
items = items,
|
||||||
switchState = switchState,
|
switchState = switchState,
|
||||||
selectedPosition = selectedPosition,
|
selectedPosition = selectedPosition,
|
||||||
onBackClick = { finish() },
|
onBackClick = { finish() },
|
||||||
@@ -84,10 +86,7 @@ class TaskerActivity : BaseComponentActivity() {
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
switchState.value = switch
|
switchState.value = switch
|
||||||
val pos = lstGuid.indexOf(guid.toString())
|
selectedPosition.value = items.indexOfFirst { it.guid == guid.toString() }
|
||||||
if (pos >= 0) {
|
|
||||||
selectedPosition.value = pos
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
LogUtil.e(AppConfig.TAG, "Failed to initialize Tasker settings", e)
|
LogUtil.e(AppConfig.TAG, "Failed to initialize Tasker settings", e)
|
||||||
@@ -102,13 +101,12 @@ class TaskerActivity : BaseComponentActivity() {
|
|||||||
|
|
||||||
val extraBundle = Bundle()
|
val extraBundle = Bundle()
|
||||||
extraBundle.putBoolean(AppConfig.TASKER_EXTRA_BUNDLE_SWITCH, switchState.value)
|
extraBundle.putBoolean(AppConfig.TASKER_EXTRA_BUNDLE_SWITCH, switchState.value)
|
||||||
extraBundle.putString(AppConfig.TASKER_EXTRA_BUNDLE_GUID, lstGuid[position])
|
extraBundle.putString(AppConfig.TASKER_EXTRA_BUNDLE_GUID, items[position].guid)
|
||||||
val intent = Intent()
|
val intent = Intent()
|
||||||
|
|
||||||
val remarks = lstData[position]
|
|
||||||
val blurb = getString(
|
val blurb = getString(
|
||||||
if (switchState.value) R.string.tasker_blurb_start else R.string.tasker_blurb_stop,
|
if (switchState.value) R.string.tasker_blurb_start else R.string.tasker_blurb_stop,
|
||||||
remarks
|
items[position].label
|
||||||
)
|
)
|
||||||
|
|
||||||
intent.putExtra(AppConfig.TASKER_EXTRA_BUNDLE, extraBundle)
|
intent.putExtra(AppConfig.TASKER_EXTRA_BUNDLE, extraBundle)
|
||||||
@@ -120,7 +118,7 @@ class TaskerActivity : BaseComponentActivity() {
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TaskerScreen(
|
fun TaskerScreen(
|
||||||
items: List<String>,
|
items: List<TaskerItem>,
|
||||||
switchState: MutableState<Boolean>,
|
switchState: MutableState<Boolean>,
|
||||||
selectedPosition: MutableState<Int>,
|
selectedPosition: MutableState<Int>,
|
||||||
onBackClick: () -> Unit,
|
onBackClick: () -> Unit,
|
||||||
@@ -158,7 +156,7 @@ fun TaskerScreen(
|
|||||||
.verticalScrollbar(listState),
|
.verticalScrollbar(listState),
|
||||||
contentPadding = NavigationBarsBottomPadding()
|
contentPadding = NavigationBarsBottomPadding()
|
||||||
) {
|
) {
|
||||||
itemsIndexed(items) { index, remarks ->
|
itemsIndexed(items, key = { _, item -> item.guid }) { index, item ->
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
@@ -171,7 +169,7 @@ fun TaskerScreen(
|
|||||||
onClick = { selectedPosition.value = index }
|
onClick = { selectedPosition.value = index }
|
||||||
)
|
)
|
||||||
Spacer(modifier = Modifier.width(8.dp))
|
Spacer(modifier = Modifier.width(8.dp))
|
||||||
Text(text = remarks, style = MaterialTheme.typography.bodyLarge)
|
Text(text = item.label, style = MaterialTheme.typography.bodyLarge)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user