[tbb-commits] [Git][tpo/applications/firefox-android][firefox-android-115.2.1-13.5-1] fixup! Add Tor integration and UI

Dan Ballard (@dan) git at gitlab.torproject.org
Fri Jun 14 00:07:34 UTC 2024



Dan Ballard pushed to branch firefox-android-115.2.1-13.5-1 at The Tor Project / Applications / firefox-android


Commits:
6727ad88 by clairehurst at 2024-06-14T00:07:06+00:00
fixup! Add Tor integration and UI

- - - - -


2 changed files:

- fenix/app/src/main/java/org/mozilla/fenix/tor/TorLogsComposeFragment.kt
- fenix/app/src/main/java/org/mozilla/fenix/tor/TorLogsViewModel.kt


Changes:

=====================================
fenix/app/src/main/java/org/mozilla/fenix/tor/TorLogsComposeFragment.kt
=====================================
@@ -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)
                 }
             }


=====================================
fenix/app/src/main/java/org/mozilla/fenix/tor/TorLogsViewModel.kt
=====================================
@@ -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



View it on GitLab: https://gitlab.torproject.org/tpo/applications/firefox-android/-/commit/6727ad887b52a1c1ad8b07342f81cfa3d0c30787

-- 
View it on GitLab: https://gitlab.torproject.org/tpo/applications/firefox-android/-/commit/6727ad887b52a1c1ad8b07342f81cfa3d0c30787
You're receiving this email because of your account on gitlab.torproject.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.torproject.org/pipermail/tbb-commits/attachments/20240614/cb7fa952/attachment-0001.htm>


More information about the tbb-commits mailing list