Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color downloaded torrents differently #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package org.equeim.tremotesf.ui.torrentslistfragment

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.os.Build
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.Menu
Expand Down Expand Up @@ -125,7 +127,16 @@ class TorrentsAdapter(private val fragment: TorrentsListFragment) :
}
etaTextView.text = FormatUtils.formatDuration(context, torrent.eta)

progressBar.progress = (torrent.percentDone * 100).toInt()
val progress = (torrent.percentDone * 100).toInt()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Torrent.isFinished is better suited for this check.

progressBar.progress = progress
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
progressBar.progressTintList = if (progress == 100) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK minimum API here is L, not M.
For pre-Lollipop versions we could probably use the same solution as in ProgressBar.fixPreLollipopColor() extension function in ui/utils/Extensions.kt (and make it more generic function).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, I think I'm ready to drop Android 4.x support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK minimum API here is L, not M.

This is what Android Studio told me... 🤷‍♂️ 😅 Happy to change it if you'd like.

On the other hand, I think I'm ready to drop Android 4.x support.

So should I leave it as is?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what Android Studio told me... man_shrugging sweat_smile Happy to change it if you'd like.

I think it was for getColor() call, which indeed requires API 23. Luckily, there is ContextCompat.getColor() which handles this for us.

So should I leave it as is?

I already raised minSdkVersion to 21, so with ContextCompat.getColor() we can just drop version check.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we need to call setProgressTintList() only when isFinished changes, since AFAIK it can perform some expensive operations even when passed ColorStateList is the same as before. It is especially important since update() is called from onBindViewHolder() which will be called frequently when scrolling (we can just cache isFinished value inside TorrentsViewHolder).

ColorStateList.valueOf(context.getColor(R.color.color_downloaded_torrents))
} else {
ColorStateList.valueOf(context.getColor(R.color.color_primary))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use null here, to reset progressTintList.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe not....

Copy link
Owner

@equeim equeim Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that Android sets default tint with primary color so we indeed need to restore it.
Anyway, we need to get the color from R.attr.colorPrimary theme attribute instead of raw color resource since there is two themes with different primary colors. We can do it either by using MaterialColors helper or directly from theme.

}
}

downloadSpeedTextView.text = context.getString(
R.string.download_speed_string,
FormatUtils.formatByteSpeed(
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<color name="color_primary">#ef5350</color>
<color name="color_on_primary">#ffffff</color>
<color name="color_background">@color/color_background_dark</color>
<color name="color_downloaded_torrents">#50cf50</color>

<color name="color_primary_old">#80cbc4</color>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<color name="color_on_primary">#ffffff</color>
<color name="color_background">#ffffff</color>
<color name="color_background_dark">#ff202020</color>
<color name="color_downloaded_torrents">#50af50</color>

<color name="color_primary_old">#008577</color>

Expand Down