Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 9f22eaf

Browse files
committed
implement messaging system to be displayed below the Tor information
1 parent 2fdf52e commit 9f22eaf

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/dashboard/DashboardFragment.kt

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ import android.widget.Button
1515
import android.widget.TextView
1616
import android.widget.Toast
1717
import androidx.core.app.NotificationCompat
18-
import androidx.lifecycle.LifecycleOwner
19-
import androidx.lifecycle.LiveData
20-
import androidx.lifecycle.MutableLiveData
21-
import androidx.lifecycle.Observer
18+
import androidx.core.content.ContextCompat
19+
import androidx.lifecycle.*
2220
import io.matthewnelson.encrypted_storage.Prefs
2321
import io.matthewnelson.sampleapp.App
2422
import io.matthewnelson.sampleapp.R
@@ -36,11 +34,20 @@ class DashboardFragment : Fragment() {
3634
companion object {
3735
private val _liveAppRestartButtonShow = MutableLiveData<Boolean>(false)
3836
private val liveAppRestartButtonShow: LiveData<Boolean> = _liveAppRestartButtonShow
39-
4037
fun librarySettingsWereChanged() {
4138
if (liveAppRestartButtonShow.value != true)
4239
_liveAppRestartButtonShow.value = true
4340
}
41+
42+
private const val EXCEPTION = "EXCEPTION: "
43+
private val _liveDashMessage = MutableLiveData<Pair<String, Long>?>(null)
44+
private val liveDashMessage: LiveData<Pair<String, Long>?> = _liveDashMessage
45+
fun showMessage(msg: String, showLength: Long, isException: Boolean = false) {
46+
_liveDashMessage.value = if (isException)
47+
Pair("$EXCEPTION\n$msg", showLength)
48+
else
49+
Pair(msg, showLength)
50+
}
4451
}
4552

4653
// Top row
@@ -53,6 +60,9 @@ class DashboardFragment : Fragment() {
5360
private lateinit var textViewSocksPort: TextView
5461
private lateinit var textViewHttpPort: TextView
5562

63+
// Messages
64+
private lateinit var textViewMessage: TextView
65+
5666
// Button
5767
private lateinit var buttonAppRestart: Button
5868

@@ -82,6 +92,7 @@ class DashboardFragment : Fragment() {
8292
textViewControlPort = view.findViewById(R.id.dash_text_view_port_control)
8393
textViewSocksPort = view.findViewById(R.id.dash_text_view_port_socks)
8494
textViewHttpPort = view.findViewById(R.id.dash_text_view_port_http)
95+
textViewMessage = view.findViewById(R.id.dash_text_view_message)
8596
buttonAppRestart = view.findViewById(R.id.dash_button_app_restart)
8697
}
8798

@@ -91,6 +102,19 @@ class DashboardFragment : Fragment() {
91102
buttonAppRestart.visibility = View.VISIBLE
92103
}
93104
})
105+
liveDashMessage.observe(owner, Observer {
106+
if (it != null) {
107+
textViewMessage.text = it.first
108+
textViewMessage.background = if (it.first.contains(EXCEPTION))
109+
ContextCompat.getDrawable(textViewMessage.context, R.drawable.rounded_rectangle_color_red)
110+
else
111+
ContextCompat.getDrawable(textViewMessage.context, R.drawable.rounded_rectangle_color_primary_light)
112+
textViewMessage.visibility = View.VISIBLE
113+
launchCleanUpMessageJob(it.second)
114+
} else {
115+
textViewMessage.visibility = View.GONE
116+
}
117+
})
94118
TorServiceController.appEventBroadcaster?.let {
95119
(it as MyEventBroadcaster).liveBandwidth.observe(owner, Observer { string ->
96120
if (string.isNullOrEmpty()) return@Observer
@@ -121,6 +145,17 @@ class DashboardFragment : Fragment() {
121145
}
122146
}
123147

148+
private var showMessageCleanUpJob: Job? = null
149+
150+
private fun launchCleanUpMessageJob(delayLength: Long) {
151+
showMessageCleanUpJob?.cancel()
152+
153+
showMessageCleanUpJob = lifecycleScope.launch {
154+
delay(delayLength)
155+
_liveDashMessage.value = null
156+
}
157+
}
158+
124159
private var killAppJob: Job? = null
125160

126161
private fun initButtons(context: Context, owner: LifecycleOwner) {

0 commit comments

Comments
 (0)