@@ -15,17 +15,13 @@ import android.widget.Button
1515import android.widget.TextView
1616import android.widget.Toast
1717import androidx.core.app.NotificationCompat
18- import androidx.lifecycle.LifecycleOwner
19- import androidx.lifecycle.LiveData
20- import androidx.lifecycle.MutableLiveData
21- import androidx.lifecycle.Observer
22- import io.matthewnelson.encrypted_storage.Prefs
18+ import androidx.core.content.ContextCompat
19+ import androidx.lifecycle.*
2320import io.matthewnelson.sampleapp.App
2421import io.matthewnelson.sampleapp.R
2522import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
2623import io.matthewnelson.sampleapp.ui.MainActivity
2724import io.matthewnelson.sampleapp.ui.fragments.home.HomeFragment
28- import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
2925import io.matthewnelson.topl_core_base.BaseConsts.TorState
3026import io.matthewnelson.topl_service.TorServiceController
3127import kotlinx.coroutines.*
@@ -36,11 +32,16 @@ class DashboardFragment : Fragment() {
3632 companion object {
3733 private val _liveAppRestartButtonShow = MutableLiveData <Boolean >(false )
3834 private val liveAppRestartButtonShow: LiveData <Boolean > = _liveAppRestartButtonShow
39-
4035 fun librarySettingsWereChanged () {
4136 if (liveAppRestartButtonShow.value != true )
4237 _liveAppRestartButtonShow .value = true
4338 }
39+
40+ private val _liveDashMessage = MutableLiveData <DashMessage ?>(null )
41+ private val liveDashMessage: LiveData <DashMessage ?> = _liveDashMessage
42+ fun showMessage (dashboardMessage : DashMessage ) {
43+ _liveDashMessage .value = dashboardMessage
44+ }
4445 }
4546
4647 // Top row
@@ -53,6 +54,9 @@ class DashboardFragment : Fragment() {
5354 private lateinit var textViewSocksPort: TextView
5455 private lateinit var textViewHttpPort: TextView
5556
57+ // Messages
58+ private lateinit var textViewMessage: TextView
59+
5660 // Button
5761 private lateinit var buttonAppRestart: Button
5862
@@ -82,6 +86,7 @@ class DashboardFragment : Fragment() {
8286 textViewControlPort = view.findViewById(R .id.dash_text_view_port_control)
8387 textViewSocksPort = view.findViewById(R .id.dash_text_view_port_socks)
8488 textViewHttpPort = view.findViewById(R .id.dash_text_view_port_http)
89+ textViewMessage = view.findViewById(R .id.dash_text_view_message)
8590 buttonAppRestart = view.findViewById(R .id.dash_button_app_restart)
8691 }
8792
@@ -91,6 +96,16 @@ class DashboardFragment : Fragment() {
9196 buttonAppRestart.visibility = View .VISIBLE
9297 }
9398 })
99+ liveDashMessage.observe(owner, Observer {
100+ if (it != null ) {
101+ textViewMessage.text = it.message
102+ textViewMessage.background = ContextCompat .getDrawable(textViewMessage.context, it.background)
103+ textViewMessage.visibility = View .VISIBLE
104+ launchCleanUpMessageJob(it.showLength)
105+ } else {
106+ textViewMessage.visibility = View .GONE
107+ }
108+ })
94109 TorServiceController .appEventBroadcaster?.let {
95110 (it as MyEventBroadcaster ).liveBandwidth.observe(owner, Observer { string ->
96111 if (string.isNullOrEmpty()) return @Observer
@@ -121,6 +136,17 @@ class DashboardFragment : Fragment() {
121136 }
122137 }
123138
139+ private var showMessageCleanUpJob: Job ? = null
140+
141+ private fun launchCleanUpMessageJob (delayLength : Long ) {
142+ showMessageCleanUpJob?.cancel()
143+
144+ showMessageCleanUpJob = lifecycleScope.launch {
145+ delay(delayLength)
146+ _liveDashMessage .value = null
147+ }
148+ }
149+
124150 private var killAppJob: Job ? = null
125151
126152 private fun initButtons (context : Context , owner : LifecycleOwner ) {
0 commit comments