Skip to content

Commit 15a2677

Browse files
committed
add version status badge
1 parent b498e22 commit 15a2677

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

app/src/main/java/ru/yourok/torrserve/ui/activities/main/MainActivity.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import androidx.drawerlayout.widget.DrawerLayout
2222
import androidx.lifecycle.ViewModelProvider
2323
import androidx.lifecycle.lifecycleScope
2424
import com.google.android.material.floatingactionbutton.FloatingActionButton
25+
import com.google.android.material.shape.CornerFamily
26+
import com.google.android.material.shape.MaterialShapeDrawable
27+
import com.google.android.material.shape.ShapeAppearanceModel
2528
import com.google.firebase.analytics.FirebaseAnalytics
2629
import kotlinx.coroutines.Dispatchers
2730
import kotlinx.coroutines.launch
@@ -163,7 +166,17 @@ class MainActivity : AppCompatActivity() {
163166
private fun updateStatus() {
164167
val host = viewModel.getHost()
165168
val hostView = findViewById<TextView>(R.id.tvCurrentHost)
169+
val statusView = findViewById<TextView>(R.id.tvStatus)
166170
val hostColor = ThemeUtil.getColorFromAttr(this, R.attr.colorHost)
171+
val inactiveColor = ThemeUtil.getColorFromAttr(this, R.attr.colorOnSurface)
172+
val labelTextColor = ThemeUtil.getColorFromAttr(this@MainActivity, R.attr.colorSurface)
173+
val versionColorList = ColorStateList.valueOf(ThemeUtil.getColorFromAttr(this@MainActivity, R.attr.colorPrimary))
174+
val radius = dp2px(2.0f).toFloat()
175+
val shapeAppearanceModel = ShapeAppearanceModel()
176+
.toBuilder()
177+
.setAllCorners(CornerFamily.ROUNDED, radius)
178+
.build()
179+
167180
host.observe(this) {
168181
hostView?.text = if (it.startsWith("https", true)) {
169182
val sIcon = SpannableString(" ")
@@ -176,17 +189,29 @@ class MainActivity : AppCompatActivity() {
176189
}
177190
val data = viewModel.get()
178191
data.observe(this) {
179-
findViewById<TextView>(R.id.tvStatus)?.text = it
180-
if (it.equals(getString(R.string.server_not_responding)))
181-
hostView.apply {
182-
setTextColor(ThemeUtil.getColorFromAttr(this@MainActivity, R.attr.colorOnSurface))
192+
statusView?.text = it
193+
if (it.equals(getString(R.string.server_not_responding))) {
194+
statusView?.apply {
195+
background = null
196+
setTextColor(inactiveColor)
197+
}
198+
hostView?.apply {
199+
setTextColor(inactiveColor)
183200
alpha = 0.75f
184201
}
185-
else
186-
hostView.apply {
202+
} else {
203+
statusView?.apply {
204+
val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
205+
shapeDrawable.fillColor = versionColorList.withAlpha(160) // 160
206+
shapeDrawable.setStroke(2.0f, versionColorList.withAlpha(100)) // 100
207+
background = shapeDrawable
208+
setTextColor(labelTextColor)
209+
}
210+
hostView?.apply {
187211
setTextColor(hostColor)
188212
alpha = 1.0f
189213
}
214+
}
190215
}
191216
}
192217

app/src/main/res/layout/navigation_menu.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@
5555

5656
<TextView
5757
android:id="@+id/tvStatus"
58-
android:layout_width="match_parent"
58+
android:layout_width="wrap_content"
5959
android:layout_height="wrap_content"
60+
android:layout_gravity="center_horizontal"
61+
android:includeFontPadding="true"
62+
android:paddingStart="4dp"
63+
android:paddingLeft="4dp"
64+
android:paddingEnd="4dp"
65+
android:paddingRight="4dp"
6066
android:gravity="center"
6167
tools:text="Status"
6268
android:textSize="12sp" />

0 commit comments

Comments
 (0)