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 19, 2024
1 parent 9334ccf commit c864058
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.eu.exodus_privacy.exodusprivacy.R
import org.eu.exodus_privacy.exodusprivacy.databinding.FragmentAppDetailBinding
import org.eu.exodus_privacy.exodusprivacy.fragments.appdetail.model.AppDetailVPAdapter
import org.eu.exodus_privacy.exodusprivacy.utils.openURL
import org.eu.exodus_privacy.exodusprivacy.utils.setExodusColor
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -164,16 +163,6 @@ class AppDetailFragment : Fragment(R.layout.fragment_app_detail) {
} else {
appReportTV.visibility = View.GONE
}
trackersChip.apply {
val trackerNum = app.exodusTrackers.size
text = if (app.exodusVersionCode == 0L) "?" else trackerNum.toString()
setExodusColor(trackerNum)
}
permsChip.apply {
val permsNum = app.permissions.size
text = permsNum.toString()
setExodusColor(permsNum)
}

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 @@ -64,13 +64,14 @@ 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)
}
permsChip.apply {
val permsNum = app.permissions.size
text = permsNum.toString()
val permsNum = app.permissions.size.toString()
text = permsNum
setExodusColor(permsNum)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,44 @@ import android.content.res.ColorStateList
import android.os.Build
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.text.isDigitsOnly
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.isDigitsOnly()) {
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.setTextColor(textColorStateList)
this.chipBackgroundColor = backgroundColorStateList
}

fun getLanguage(): String {
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/layout/fragment_a_d_permissions.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -20,15 +20,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
app:chipIcon="@drawable/ic_permission"
app:chipIconTint="@color/colorPrimary"
tools:chipBackgroundColor="@color/colorGreen"
tools:text="0" />

<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:layout_gravity="center"
android:maxLines="1"
android:text="@string/permissions"
android:textColor="?android:textColorPrimary"
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/res/layout/fragment_a_d_trackers.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
Expand All @@ -12,16 +13,17 @@
style="@style/Theme.Exodus.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipIcon="@drawable/ic_tracker"
app:layout_constraintBottom_toTopOf="@+id/trackersStatusTV"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
tools:text="0" />

<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:layout_marginStart="5dp"
android:maxLines="1"
android:text="@string/trackers"
android:textColor="?android:textColorPrimary"
Expand Down
87 changes: 29 additions & 58 deletions app/src/main/res/layout/fragment_app_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,71 +34,42 @@
tools:src="@drawable/ic_logo"/>

<com.google.android.material.textview.MaterialTextView
android:id="@+id/appNameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/appIconIV"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:ellipsize="end"
android:maxLines="2"
android:textAlignment="center"
android:textColor="?android:textColorPrimary"
android:textSize="22sp"
tools:text="@string/app_name" />

<com.google.android.material.chip.Chip
android:id="@+id/trackersChip"
style="@style/Theme.Exodus.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/permsChip"
app:layout_constraintTop_toBottomOf="@id/appNameTV"
app:layout_constraintHorizontal_chainStyle="packed"
android:layout_marginTop="20dp"
app:chipIcon="@drawable/ic_tracker"
tools:text="0" />

android:id="@+id/appNameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:ellipsize="end"
android:maxLines="2"
android:textAlignment="center"
android:textColor="?android:textColorPrimary"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/appIconIV"
tools:text="@string/app_name" />
<com.google.android.material.chip.Chip
android:id="@+id/permsChip"
style="@style/Theme.Exodus.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/trackersChip"
app:layout_constraintEnd_toStartOf="@+id/sourceChip"
app:layout_constraintTop_toBottomOf="@id/appNameTV"
android:layout_marginTop="20dp"
android:layout_marginStart="5dp"
android:textColor="@color/colorPrimary"
app:chipIcon="@drawable/ic_permission"
app:chipIconTint="@color/colorPrimary"
tools:chipBackgroundColor="@color/colorYellow"
tools:text="3" />

<com.google.android.material.chip.Chip
android:id="@+id/sourceChip"
style="@style/Theme.Exodus.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="20dp"
app:chipIcon="@drawable/ic_android"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/permsChip"
app:layout_constraintTop_toBottomOf="@id/appNameTV"
tools:text="Google" />

android:id="@+id/sourceChip"
style="@style/Theme.Exodus.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
app:chipIcon="@drawable/ic_android"
app:layout_constraintBottom_toTopOf="@+id/appIVTV"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appNameTV"
app:layout_constraintVertical_bias="0.5"
tools:text="Google" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/appIVTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/trackersChip"
app:layout_constraintTop_toBottomOf="@id/sourceChip"
android:layout_marginStart="30dp"
android:layout_marginTop="16dp"
android:ellipsize="end"
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/fragment_tracker_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@
android:id="@+id/appsListRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:layout_marginTop="5dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
36 changes: 27 additions & 9 deletions app/src/main/res/layout/recycler_view_app_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toEndOf="@id/appIconIV"
app:layout_constraintTop_toBottomOf="@id/appNameTV"
app:layout_constraintVertical_bias="1"
tools:text="Analyzed version is different that installed version" />
<com.google.android.material.chip.Chip
android:id="@+id/trackersChip"
Expand All @@ -60,26 +59,45 @@
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="4dp"
app:chipIcon="@drawable/ic_tracker"
android:layout_marginEnd="5dp"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toEndOf="@id/appIconIV"
app:layout_constraintTop_toBottomOf="@id/appVersionTV"
app:layout_constraintVertical_bias="1"
tools:text="0" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/trackersText"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:gravity="center_vertical"
android:maxLines="2"
android:text="@string/trackers"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/trackersChip"
app:layout_constraintTop_toBottomOf="@+id/appVersionTV" />
<com.google.android.material.chip.Chip
android:id="@+id/permsChip"
style="@style/Theme.Exodus.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="4dp"
android:layout_marginStart="5dp"
android:textColor="@color/textColorDark"
app:chipIcon="@drawable/ic_permission"
app:chipIconTint="@color/textColorDark"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toEndOf="@id/trackersChip"
app:layout_constraintTop_toBottomOf="@id/appVersionTV"
app:layout_constraintVertical_bias="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/trackersText"
app:layout_constraintTop_toBottomOf="@+id/appVersionTV"
app:layout_constraintVertical_bias="0.5"
tools:chipBackgroundColor="@color/colorYellow"
tools:text="3" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/permissionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="@string/permissions"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/permsChip"
app:layout_constraintTop_toBottomOf="@+id/appVersionTV"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 0 additions & 4 deletions app/src/main/res/layout/shimmer_layout_app_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:chipBackgroundColor="@color/colorGrey"
app:chipIcon="@drawable/ic_tracker"
app:chipIconTint="@color/colorGrey"
app:layout_constraintStart_toEndOf="@id/appIconIV"
app:layout_constraintTop_toBottomOf="@id/appVersionTV" />

Expand All @@ -69,8 +67,6 @@
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:chipBackgroundColor="@color/colorGrey"
app:chipIcon="@drawable/ic_permission"
app:chipIconTint="@color/colorGrey"
app:layout_constraintStart_toEndOf="@id/trackersChip"
app:layout_constraintTop_toBottomOf="@id/appVersionTV" />

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<item name="android:textColor">@color/chipIconTintColor</item>
<item name="chipBackgroundColor">@color/chipColor</item>
<item name="chipIconTint">@color/chipIconTintColor</item>
<item name="chipCornerRadius">50dp</item>
<item name="chipCornerRadius">20dp</item>
<item name="chipIconVisible">true</item>
<item name="android:checkable">false</item>
</style>
Expand Down

0 comments on commit c864058

Please sign in to comment.