Skip to content

Commit

Permalink
Refactor function to set Chip Color and text
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-BaptisteC committed Jan 14, 2024
1 parent 05ad873 commit 6ebacb6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toDrawable
Expand Down Expand Up @@ -169,14 +170,29 @@ class AppDetailFragment : Fragment(R.layout.fragment_app_detail) {
appReportTV.visibility = View.GONE
}
trackersChip.apply {
val trackerNum = app.exodusTrackers.size
text = if (app.exodusVersionCode == 0L) "?" else trackerNum.toString()
val trackerNum =
if (app.exodusVersionCode == 0L) "?" else app.exodusTrackers.size.toString()
text = trackerNum
setExodusColor(trackerNum)
setOnClickListener {
Toast.makeText(
context,
trackerNum + context.resources.getString(R.string.tracker_chip),
Toast.LENGTH_LONG,
).show()
}
}
permsChip.apply {
val permsNum = app.permissions.size
text = permsNum.toString()
val permsNum = app.permissions.size.toString()
text = permsNum
setExodusColor(permsNum)
setOnClickListener {
Toast.makeText(
context,
permsNum + context.resources.getString(R.string.permission_chip),
Toast.LENGTH_LONG,
).show()
}
}

sourceChip.text = app.source.name.lowercase().replaceFirstChar { it.uppercase() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class ADPermissionsFragment : Fragment(R.layout.fragment_a_d_permissions) {
permissionsStatusTV.text = getString(R.string.code_permission_found)
}
permissionsChip.apply {
val permsNum = app.permissions.size
text = permsNum.toString()
val permsNum = app.permissions.size.toString()
text = permsNum
setExodusColor(permsNum)
}
permissionsLearnGoogleTV.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class ADTrackersFragment : Fragment(R.layout.fragment_a_d_trackers) {
}
}
trackersChip.apply {
val trackerNum = app.exodusTrackers.size
text = if (app.exodusVersionCode == 0L) "?" else trackerNum.toString()
val trackerNum =
if (app.exodusVersionCode == 0L) "?" else app.exodusTrackers.size.toString()
text = trackerNum
setExodusColor(trackerNum)
}
trackersLearnTV.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.eu.exodus_privacy.exodusprivacy.fragments.apps.model
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.graphics.drawable.toDrawable
import androidx.navigation.findNavController
import androidx.recyclerview.widget.ListAdapter
Expand Down Expand Up @@ -64,14 +65,28 @@ class AppsRVAdapter(
}
}
trackersChip.apply {
val trackerNum = app.exodusTrackers.size
text = if (app.exodusVersionCode == 0L) "?" else trackerNum.toString()
val trackerNum =
if (app.exodusVersionCode == 0L) "?" else app.exodusTrackers.size.toString()
text = trackerNum
setExodusColor(trackerNum)
setOnClickListener{
Toast.makeText(
context, trackerNum + context.resources.getString(R.string.tracker_chip),
Toast.LENGTH_LONG,
).show()
}
}
permsChip.apply {
val permsNum = app.permissions.size
text = permsNum.toString()
val permsNum = app.permissions.size.toString()
text = permsNum
setExodusColor(permsNum)
setOnClickListener {
Toast.makeText(
context,
permsNum + context.resources.getString(R.string.permission_chip),
Toast.LENGTH_LONG,
).show()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,40 @@ import com.google.android.material.chip.Chip
import org.eu.exodus_privacy.exodusprivacy.R
import java.util.Locale

fun Chip.setExodusColor(size: Int) {
if (this.text != "?") {
fun Chip.setExodusColor(size: String) {
val textColorStateList: ColorStateList
val backgroundColorStateList: ColorStateList
if (size != "?") {
val colorRed = ContextCompat.getColor(context, R.color.colorRedLight)
val colorYellow = ContextCompat.getColor(context, R.color.colorYellow)
val colorGreen = ContextCompat.getColor(context, R.color.colorGreen)
val colorDark = ContextCompat.getColor(context, R.color.textColorDark)
val colorWhite = ContextCompat.getColor(context, R.color.textColorLikeWhite)

val textColorStateList = when (size) {
textColorStateList = when (size.toInt()) {
0 -> ColorStateList.valueOf(colorDark)
in 1..4 -> ColorStateList.valueOf(colorDark)
else -> ColorStateList.valueOf(colorWhite)
}

val backgroundColorStateList = when (size) {
backgroundColorStateList = when (size.toInt()) {
0 -> ColorStateList.valueOf(colorGreen)
in 1..4 -> ColorStateList.valueOf(colorYellow)
else -> ColorStateList.valueOf(colorRed)
}

this.chipIconTint = textColorStateList
this.setTextColor(textColorStateList)
this.chipBackgroundColor = backgroundColorStateList
} else {
val colorForeground =
textColorStateList =
ColorStateList.valueOf(
ContextCompat.getColor(
context,
com.google.android.material.R.color.m3_chip_text_color,
),
)
this.chipIconTint = colorForeground
this.setTextColor(colorForeground)
val colorBackground = ContextCompat.getColor(context, R.color.chipColor)
this.chipBackgroundColor = ColorStateList.valueOf(colorBackground)
backgroundColorStateList = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.chipColor))
}
this.chipIconTint = textColorStateList
this.setTextColor(textColorStateList)
this.chipBackgroundColor = backgroundColorStateList
}

fun getLanguage(): String {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<string name="nothing_here">Nothing to display currently. You can pull to refresh!</string>
<string name="back_button">Back</string>

<string name="permission_chip">&#160;permissions</string>
<string name="tracker_chip">&#160;trackers</string>

<!-- Menu -->
<string name="menu_action_search">Search</string>
<string name="menu_action_filter">Sort by</string>
Expand Down

0 comments on commit 6ebacb6

Please sign in to comment.