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

Commit 0b295ab

Browse files
authored
Merge pull request #67 from 05nelsonm/mn/feature/sample-app-dash-messages
Add to Sample App Dash Messaging
2 parents 51769d8 + 1e63af6 commit 0b295ab

File tree

19 files changed

+134
-38
lines changed

19 files changed

+134
-38
lines changed

sampleapp/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ dependencies {
108108

109109
implementation 'io.matthewnelson.encrypted-storage:encrypted-storage:2.0.0'
110110
implementation 'io.matthewnelson.topl-android:tor-binary:0.4.3.5a'
111-
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
112111

113112
testImplementation deps.junit
114113

sampleapp/src/main/java/io/matthewnelson/sampleapp/App.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ package io.matthewnelson.sampleapp
6868

6969
import android.app.Application
7070
import android.os.Process
71-
import android.widget.Toast
7271
import io.matthewnelson.encrypted_storage.Prefs
7372
import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
7473
import io.matthewnelson.sampleapp.topl_android.MyTorSettings
7574
import io.matthewnelson.sampleapp.ui.MainActivity
75+
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashMessage
76+
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashboardFragment
7677
import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
7778
import io.matthewnelson.topl_service.TorServiceController
7879
import io.matthewnelson.topl_service.notification.ServiceNotification
@@ -211,7 +212,13 @@ class App: Application() {
211212
)
212213
}
213214
} catch (e: Exception) {
214-
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
215+
e.message?.let {
216+
DashboardFragment.showMessage(
217+
DashMessage(
218+
"${DashMessage.EXCEPTION}$it", R.drawable.dash_message_color_red, 5_000
219+
)
220+
)
221+
}
215222
}
216223
}
217224
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.matthewnelson.sampleapp.ui.fragments.dashboard
2+
3+
import androidx.annotation.DrawableRes
4+
5+
class DashMessage(
6+
val message: String,
7+
@DrawableRes val background: Int,
8+
val showLength: Long
9+
) {
10+
companion object {
11+
const val EXCEPTION = "~~~ EXCEPTION ~~~\n"
12+
}
13+
}

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ 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
22-
import io.matthewnelson.encrypted_storage.Prefs
18+
import androidx.core.content.ContextCompat
19+
import androidx.lifecycle.*
2320
import io.matthewnelson.sampleapp.App
2421
import io.matthewnelson.sampleapp.R
2522
import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
2623
import io.matthewnelson.sampleapp.ui.MainActivity
2724
import io.matthewnelson.sampleapp.ui.fragments.home.HomeFragment
28-
import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
2925
import io.matthewnelson.topl_core_base.BaseConsts.TorState
3026
import io.matthewnelson.topl_service.TorServiceController
3127
import 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) {

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/home/HomeFragment.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@
6666
* */
6767
package io.matthewnelson.sampleapp.ui.fragments.home
6868

69-
import android.content.Context
7069
import android.os.Bundle
7170
import androidx.fragment.app.Fragment
7271
import android.view.LayoutInflater
7372
import android.view.View
7473
import android.view.ViewGroup
7574
import android.widget.Button
76-
import android.widget.Toast
7775
import io.matthewnelson.sampleapp.R
76+
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashMessage
77+
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashboardFragment
7878
import io.matthewnelson.topl_service.TorServiceController
7979
import io.matthewnelson.topl_service.prefs.TorServicePrefs
8080
import io.matthewnelson.topl_service.util.ServiceConsts
@@ -118,7 +118,7 @@ class HomeFragment : Fragment() {
118118
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
119119
super.onViewCreated(view, savedInstanceState)
120120
findViews(view)
121-
initButtons(view.context)
121+
initButtons()
122122
logMessageAdapter = LogMessageAdapter(viewLifecycleOwner, view)
123123
}
124124

@@ -139,7 +139,7 @@ class HomeFragment : Fragment() {
139139
buttonStop = view.findViewById(R.id.home_button_stop)
140140
}
141141

142-
private fun initButtons(context: Context) {
142+
private fun initButtons() {
143143
setButtonDebugText()
144144
buttonDebug.setOnClickListener {
145145
hasDebugLogs = !hasDebugLogs
@@ -154,7 +154,14 @@ class HomeFragment : Fragment() {
154154
try {
155155
TorServiceController.startTor()
156156
} catch (e: RuntimeException) {
157-
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
157+
DashboardFragment.showMessage(
158+
DashMessage(
159+
DashMessage.EXCEPTION +
160+
"TorServiceController.Builder.build() has not been called yet.",
161+
R.drawable.dash_message_color_red,
162+
3_000
163+
)
164+
)
158165
}
159166
}
160167
buttonStop.setOnClickListener {

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/settings/library/BackgroundManagerOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import io.matthewnelson.sampleapp.R
99

1010
class BackgroundManagerOptions(view: View, prefs: Prefs) {
1111

12-
private companion object {
12+
companion object {
1313
// Spinner BackgroundManager Policy
1414
const val RESPECT_RESOURCES = "Stop Service After"
1515
const val FOREGROUND = "Run In Foreground"

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/settings/library/ControllerOptions.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import kotlin.math.absoluteValue
1313

1414
class ControllerOptions(view: View, prefs: Prefs) {
1515

16-
private companion object {
17-
const val DISABLED = "Don't Stop Service"
18-
const val ENABLED = "Stop Service"
16+
companion object {
17+
const val DO_NOT_STOP_SERVICE = "Don't Stop Service"
18+
const val STOP_SERVICE = "Stop Service"
1919

2020
const val DEBUG = "Debug"
2121
const val RELEASE = "Release"
@@ -103,8 +103,8 @@ class ControllerOptions(view: View, prefs: Prefs) {
103103

104104
private fun initSpinnerControllerDisableStopOnTaskRemoved(context: Context) {
105105
val categoryDisable = arrayOf(
106-
DISABLED,
107-
ENABLED
106+
DO_NOT_STOP_SERVICE,
107+
STOP_SERVICE
108108
)
109109
adapterDisableStopOnTaskRemoved = ArrayAdapter(context, R.layout.spinner_list_item, categoryDisable)
110110
adapterDisableStopOnTaskRemoved.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
@@ -164,10 +164,10 @@ class ControllerOptions(view: View, prefs: Prefs) {
164164
when (parent.adapter) {
165165
adapterDisableStopOnTaskRemoved -> {
166166
disableStopServiceOnTaskRemoved = when (item.toString()) {
167-
DISABLED -> {
167+
DO_NOT_STOP_SERVICE -> {
168168
true
169169
}
170-
ENABLED -> {
170+
STOP_SERVICE -> {
171171
false
172172
}
173173
else -> {

sampleapp/src/main/java/io/matthewnelson/sampleapp/ui/fragments/settings/library/SettingsLibraryFragment.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ import android.view.LayoutInflater
7474
import android.view.View
7575
import android.view.ViewGroup
7676
import android.widget.Button
77-
import android.widget.Toast
7877
import io.matthewnelson.encrypted_storage.Prefs
7978
import io.matthewnelson.sampleapp.App
8079
import io.matthewnelson.sampleapp.R
80+
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashMessage
8181
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashboardFragment
8282
import io.matthewnelson.topl_service.lifecycle.BackgroundManager
83+
import io.matthewnelson.topl_service.util.ServiceConsts.BackgroundPolicy
8384

8485
class SettingsLibraryFragment : Fragment() {
8586

@@ -150,7 +151,19 @@ class SettingsLibraryFragment : Fragment() {
150151
controllerOptions.buildConfigDebug
151152
)
152153
} catch (e: Exception) {
153-
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
154+
e.message?.let {
155+
val msg = if (it.contains(BackgroundPolicy.RUN_IN_FOREGROUND))
156+
"${DashMessage.EXCEPTION}Selected Controller option\n" +
157+
">>> ${ControllerOptions.DO_NOT_STOP_SERVICE} <<<\n" +
158+
"requires BackgroundManager Policy of\n" +
159+
">>> ${BackgroundManagerOptions.FOREGROUND} <<< and\n" +
160+
">>> ${BackgroundManagerOptions.KILL_APP} <<<"
161+
else
162+
it
163+
DashboardFragment.showMessage(
164+
DashMessage(msg, R.drawable.dash_message_color_red, 8_000L)
165+
)
166+
}
154167
return
155168
}
156169

@@ -160,7 +173,9 @@ class SettingsLibraryFragment : Fragment() {
160173

161174
if (bmChanges || nChanges || cChanges) {
162175
DashboardFragment.librarySettingsWereChanged()
163-
Toast.makeText(context, "Settings Saved", Toast.LENGTH_SHORT).show()
176+
DashboardFragment.showMessage(
177+
DashMessage("Settings Saved", R.drawable.dash_message_color_green, 3_000L)
178+
)
164179
}
165180
}
166181
}

sampleapp/src/main/res/drawable/rounded_rectangle_color_grey.xml renamed to sampleapp/src/main/res/drawable/button_color_grey.xml

File renamed without changes.

sampleapp/src/main/res/drawable/rounded_rectangle_color_secondary.xml renamed to sampleapp/src/main/res/drawable/button_color_secondary.xml

File renamed without changes.

0 commit comments

Comments
 (0)