I am trying to read a file line by line and each line is a new item in a LazyList. I have a flow that I would subscribe to, to listen for each new line
class LogcatService(private val coroutineScope: CoroutineScope) {
private var _mutableLogcatListener: MutableSharedFlow<String> = MutableSharedFlow(0, 1, BufferOverflow.DROP_OLDEST)
val logcatListener: SharedFlow<String> = _mutableLogcatListener
private var _logcatJob: Job? = null
fun startLogcat(){
_logcatJob = coroutineScope.launch {
Runtime.getRuntime().exec("logcat -c")
Runtime.getRuntime().exec("logcat")
.inputStream
.bufferedReader()
.useLines { lines ->
lines.forEach { newLine ->
_mutableLogcatListener.tryEmit(newLine)
}
}
}
}
fun stopLogcat(){
_logcatJob?.cancel()
}
}
Then in the activity I would listen to the flow to append each line that was read
@Composable
fun LogcatList(logcatService: LogcatService){
val value = logcatService.logcatListener.collectAsState(initial = "")
val content by remember { mutableStateOf(mutableListOf<String>())}
content.add(value.value)
LazyColumn {
items(content) { message ->
Text(text = message,
)
}
}
}
It however I dont seem to get any flow updates in the composable.
I am pretty new to jetpack compose so I dont know what I am doing wrong. Using regular xml android I would just append the item to recyclerview and that would be that but I am trying to learn compose.