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

Commit 713e4f7

Browse files
committed
modify showMessage method to accept a DashMessage object instead of individual arguments
1 parent 278237b commit 713e4f7

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ 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
7676
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashboardFragment
7777
import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
7878
import io.matthewnelson.topl_service.TorServiceController
@@ -213,7 +213,11 @@ class App: Application() {
213213
}
214214
} catch (e: Exception) {
215215
e.message?.let {
216-
DashboardFragment.showMessage(it, 5_000, true)
216+
DashboardFragment.showMessage(
217+
DashMessage(
218+
"${DashMessage.EXCEPTION}$it", R.drawable.rounded_rectangle_color_red, 5_000
219+
)
220+
)
217221
}
218222
}
219223
}
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: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ import android.widget.Toast
1717
import androidx.core.app.NotificationCompat
1818
import androidx.core.content.ContextCompat
1919
import androidx.lifecycle.*
20-
import io.matthewnelson.encrypted_storage.Prefs
2120
import io.matthewnelson.sampleapp.App
2221
import io.matthewnelson.sampleapp.R
2322
import io.matthewnelson.sampleapp.topl_android.MyEventBroadcaster
2423
import io.matthewnelson.sampleapp.ui.MainActivity
2524
import io.matthewnelson.sampleapp.ui.fragments.home.HomeFragment
26-
import io.matthewnelson.sampleapp.ui.fragments.settings.library.LibraryPrefs
2725
import io.matthewnelson.topl_core_base.BaseConsts.TorState
2826
import io.matthewnelson.topl_service.TorServiceController
2927
import kotlinx.coroutines.*
@@ -39,14 +37,10 @@ class DashboardFragment : Fragment() {
3937
_liveAppRestartButtonShow.value = true
4038
}
4139

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)
40+
private val _liveDashMessage = MutableLiveData<DashMessage?>(null)
41+
private val liveDashMessage: LiveData<DashMessage?> = _liveDashMessage
42+
fun showMessage(dashboardMessage: DashMessage) {
43+
_liveDashMessage.value = dashboardMessage
5044
}
5145
}
5246

@@ -104,13 +98,10 @@ class DashboardFragment : Fragment() {
10498
})
10599
liveDashMessage.observe(owner, Observer {
106100
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)
101+
textViewMessage.text = it.message
102+
textViewMessage.background = ContextCompat.getDrawable(textViewMessage.context, it.background)
112103
textViewMessage.visibility = View.VISIBLE
113-
launchCleanUpMessageJob(it.second)
104+
launchCleanUpMessageJob(it.showLength)
114105
} else {
115106
textViewMessage.visibility = View.GONE
116107
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import android.view.View
7373
import android.view.ViewGroup
7474
import android.widget.Button
7575
import io.matthewnelson.sampleapp.R
76+
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashMessage
7677
import io.matthewnelson.sampleapp.ui.fragments.dashboard.DashboardFragment
7778
import io.matthewnelson.topl_service.TorServiceController
7879
import io.matthewnelson.topl_service.prefs.TorServicePrefs
@@ -154,7 +155,13 @@ class HomeFragment : Fragment() {
154155
TorServiceController.startTor()
155156
} catch (e: RuntimeException) {
156157
DashboardFragment.showMessage(
157-
"TorServiceController.Builder.build() has not been called yet.", 3_000, true)
158+
DashMessage(
159+
DashMessage.EXCEPTION +
160+
"TorServiceController.Builder.build() has not been called yet.",
161+
R.drawable.rounded_rectangle_color_red,
162+
3_000
163+
)
164+
)
158165
}
159166
}
160167
buttonStop.setOnClickListener {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ 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
8383
import io.matthewnelson.topl_service.util.ServiceConsts.BackgroundPolicy
@@ -153,14 +153,16 @@ class SettingsLibraryFragment : Fragment() {
153153
} catch (e: Exception) {
154154
e.message?.let {
155155
val msg = if (it.contains(BackgroundPolicy.RUN_IN_FOREGROUND))
156-
"Selected Controller option\n" +
156+
"${DashMessage.EXCEPTION}Selected Controller option\n" +
157157
">>> ${ControllerOptions.DO_NOT_STOP_SERVICE} <<<\n" +
158158
"requires BackgroundManager Policy of\n" +
159159
">>> ${BackgroundManagerOptions.FOREGROUND} <<< and\n" +
160160
">>> ${BackgroundManagerOptions.KILL_APP} <<<"
161161
else
162162
it
163-
DashboardFragment.showMessage(msg, 8_000L, true)
163+
DashboardFragment.showMessage(
164+
DashMessage(msg, R.drawable.rounded_rectangle_color_red, 8_000L)
165+
)
164166
}
165167
return
166168
}
@@ -171,7 +173,9 @@ class SettingsLibraryFragment : Fragment() {
171173

172174
if (bmChanges || nChanges || cChanges) {
173175
DashboardFragment.librarySettingsWereChanged()
174-
Toast.makeText(context, "Settings Saved", Toast.LENGTH_SHORT).show()
176+
DashboardFragment.showMessage(
177+
DashMessage("Settings Saved", R.drawable.rounded_rectangle_color_green, 3_000L)
178+
)
175179
}
176180
}
177181
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="rectangle">
4+
<solid android:color="@color/green" />
5+
<corners android:radius="8dp" />
6+
</shape>

0 commit comments

Comments
 (0)