Skip to content
Snippets Groups Projects
Verified Commit 9767d2ff authored by clairehurst's avatar clairehurst Committed by Pier Angelo Vendrame
Browse files

fixup! Add Tor integration and UI

parent d9fe32b1
No related branches found
Tags debian-version-0.1.1.16-rc-1
No related merge requests found
......@@ -23,13 +23,19 @@ import androidx.compose.material.Icon
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import mozilla.components.ui.colors.PhotonColors
import org.mozilla.fenix.R
......@@ -53,17 +59,37 @@ class TorLogsComposeFragment : Fragment() {
@Composable
private fun TorLogs(paddingValues: PaddingValues) {
val torLogsState = remember { mutableStateOf<List<TorLog>>(emptyList()) }
val lifecycleOwner = LocalLifecycleOwner.current
val scrollState = rememberScrollState()
DisposableEffect(viewModel.torLogs(), lifecycleOwner) {
val observer = Observer<List<TorLog>> { logs ->
torLogsState.value = logs
}
viewModel.torLogs().observe(lifecycleOwner, observer)
onDispose {
viewModel.torLogs().removeObserver(observer)
}
}
val torLogs = torLogsState.value
LaunchedEffect(torLogs) {
scrollState.animateScrollTo(scrollState.maxValue)
}
SelectionContainer {
Column(
// Column instead of LazyColumn so that you can select all the logs, and not just one "screen" at a time
// The logs won't be too big so loading them all instead of just whats visible shouldn't be a big deal
modifier = Modifier
.fillMaxSize()
.verticalScroll(state = rememberScrollState(), reverseScrolling = true)
.verticalScroll(scrollState)
.padding(paddingValues)
.background(PhotonColors.Ink50), // Standard background color
) {
for (log in viewModel.torLogs) {
for (log in torLogs) {
LogRow(log = log)
}
}
......
......@@ -12,6 +12,8 @@ import android.os.Build
import android.widget.Toast
import androidx.compose.runtime.Stable
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import java.sql.Timestamp
......@@ -21,12 +23,18 @@ class TorLogsViewModel(application: Application) : AndroidViewModel(application)
private val clipboardManager =
application.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val torLogs: MutableList<TorLog> = mutableListOf(
TorLog(
"---------------" + application.getString(R.string.tor_initializing_log) + "---------------",
),
private val _torLogs: MutableLiveData<List<TorLog>> = MutableLiveData(
mutableListOf(TorLog("---------------" + application.getString(R.string.tor_initializing_log) + "---------------")),
)
fun torLogs(): LiveData<List<TorLog>> {
return _torLogs
}
private fun addLog(log: TorLog) {
_torLogs.value = _torLogs.value?.plus(log) ?: return
}
init {
setupClipboardListener()
torController.registerTorLogListener(this)
......@@ -34,14 +42,16 @@ class TorLogsViewModel(application: Application) : AndroidViewModel(application)
.filter { !(it.second!!.startsWith("Circuit") && it.first == "ON") }
// Keep synchronized with format in onTorStatusUpdate
.flatMap { listOf(TorLog("[${it.first}] ${it.second}")) }
torLogs.addAll(currentEntries)
for (log in currentEntries) {
addLog(log)
}
}
override fun onLog(type: String?, message: String?) {
if (message == null || type == null) return
if (type == "ON" && type.startsWith("Circuit")) return
torLogs.add(TorLog("[$type] $message"))
addLog(TorLog("[$type] $message"))
}
override fun onCleared() {
......@@ -74,7 +84,8 @@ class TorLogsViewModel(application: Application) : AndroidViewModel(application)
private fun getAllTorLogs(): String {
var ret = ""
for (log in torLogs) {
for (log in torLogs().value
?: return getApplication<Application>().getString(R.string.default_error_msg)) {
ret += log.text + '\n'
}
return ret
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment