Skip to content

Commit

Permalink
Remove dependency injection from BaseRowItem
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed May 22, 2024
1 parent 38e826d commit 8d04ff3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.text.format.DateFormat
import androidx.core.content.ContextCompat
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.constant.ImageType
import org.jellyfin.androidtv.util.ImageHelper
import org.jellyfin.androidtv.util.TimeUtils
import org.jellyfin.androidtv.util.sdk.getFullName
import org.jellyfin.androidtv.util.sdk.getSubName
Expand Down Expand Up @@ -63,15 +64,15 @@ open class BaseItemDtoBaseRowItem @JvmOverloads constructor(
override fun getChildCountStr(): String? {
// Playlist
if (baseItem?.type == BaseItemKind.PLAYLIST) {
val childCount = baseItem?.cumulativeRunTimeTicks?.ticks?.let { duration ->
val childCount = baseItem.cumulativeRunTimeTicks?.ticks?.let { duration ->
TimeUtils.formatMillis(duration.inWholeMilliseconds)
}
if (childCount != null) return childCount
}

// Folder
if (baseItem?.isFolder == true && baseItem?.type != BaseItemKind.MUSIC_ARTIST) {
val childCount = baseItem?.childCount
if (baseItem?.isFolder == true && baseItem.type != BaseItemKind.MUSIC_ARTIST) {
val childCount = baseItem.childCount
if (childCount != null && childCount > 0) return childCount.toString()
}

Expand All @@ -80,39 +81,40 @@ open class BaseItemDtoBaseRowItem @JvmOverloads constructor(
}

override fun getCardName(context: Context) = when {
baseItem?.type == BaseItemKind.AUDIO && baseItem?.albumArtist != null -> baseItem?.albumArtist
baseItem?.type == BaseItemKind.AUDIO && baseItem?.album != null -> baseItem?.album
baseItem?.type == BaseItemKind.AUDIO && baseItem.albumArtist != null -> baseItem.albumArtist
baseItem?.type == BaseItemKind.AUDIO && baseItem.album != null -> baseItem.album
else -> baseItem?.getFullName(context)
}

override fun getFullName(context: Context) = baseItem?.getFullName(context)
override fun getName(context: Context) = when (baseItem?.type) {
BaseItemKind.AUDIO -> baseItem?.getFullName(context)
BaseItemKind.AUDIO -> baseItem.getFullName(context)
else -> baseItem?.name
}

override fun getSummary(context: Context) = baseItem?.overview

override fun getSubText(context: Context) = when (baseItem?.type) {
BaseItemKind.TV_CHANNEL -> baseItem?.number
BaseItemKind.TV_CHANNEL -> baseItem.number
BaseItemKind.TV_PROGRAM,
BaseItemKind.PROGRAM -> baseItem?.episodeTitle ?: baseItem?.channelName
BaseItemKind.PROGRAM -> baseItem.episodeTitle ?: baseItem.channelName

BaseItemKind.RECORDING -> {
val title = listOfNotNull(
baseItem?.channelName,
baseItem?.episodeTitle
baseItem.channelName,
baseItem.episodeTitle
).joinToString(" - ")

val timestamp = buildString {
append(SimpleDateFormat("d MMM").format(TimeUtils.getDate(baseItem!!.startDate)))
append(SimpleDateFormat("d MMM").format(TimeUtils.getDate(baseItem.startDate)))

Check warning

Code scanning / Android Lint

Implied locale in date format Warning

To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
append(" ")
append(
(DateFormat.getTimeFormat(context)
.format(TimeUtils.getDate(baseItem!!.startDate)))
.format(TimeUtils.getDate(baseItem.startDate)))
)
append(" - ")
append(
DateFormat.getTimeFormat(context).format(TimeUtils.getDate(baseItem!!.endDate))
DateFormat.getTimeFormat(context).format(TimeUtils.getDate(baseItem.endDate))
)
}

Expand All @@ -124,6 +126,7 @@ open class BaseItemDtoBaseRowItem @JvmOverloads constructor(

override fun getImageUrl(
context: Context,
imageHelper: ImageHelper,
imageType: ImageType,
fillWidth: Int,
fillHeight: Int
Expand Down Expand Up @@ -152,32 +155,30 @@ open class BaseItemDtoBaseRowItem @JvmOverloads constructor(
), fillWidth, fillHeight
)

else -> getPrimaryImageUrl(context, fillHeight)
else -> imageHelper.getPrimaryImageUrl(
baseItem!!,
preferParentThumb,
null,
fillHeight
)
}
}

override fun getPrimaryImageUrl(
override fun getBadgeImage(
context: Context,
fillHeight: Int,
) = imageHelper.getPrimaryImageUrl(
baseItem!!,
preferParentThumb,
null,
fillHeight
)

override fun getBadgeImage(context: Context) = when (baseItem?.type) {
imageHelper: ImageHelper,
) = when (baseItem?.type) {
BaseItemKind.LIVE_TV_PROGRAM,
BaseItemKind.PROGRAM -> when {
baseItem?.seriesTimerId != null -> R.drawable.ic_record_series_red
baseItem?.timerId != null -> R.drawable.ic_record_red
baseItem.seriesTimerId != null -> R.drawable.ic_record_series_red
baseItem.timerId != null -> R.drawable.ic_record_red

else -> null
}

else -> when {
baseItem?.criticRating != null -> when {
baseItem!!.criticRating!! > 59f -> R.drawable.ic_rt_fresh
baseItem.criticRating!! > 59f -> R.drawable.ic_rt_fresh

Check warning

Code scanning / detekt

Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers. Warning

This expression contains a magic number. Consider defining it to a well named constant.
else -> R.drawable.ic_rt_rotten
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jellyfin.androidtv.ui.itemhandling

import android.content.Context
import org.jellyfin.androidtv.constant.ImageType
import org.jellyfin.androidtv.util.ImageHelper
import org.jellyfin.sdk.model.api.BaseItemPerson

class BaseItemPersonBaseRowItem(
Expand All @@ -9,9 +11,12 @@ class BaseItemPersonBaseRowItem(
baseRowType = BaseRowType.Person,
staticHeight = true,
) {
override fun getPrimaryImageUrl(
override fun getImageUrl(
context: Context,
fillHeight: Int,
imageHelper: ImageHelper,
imageType: ImageType,
fillWidth: Int,
fillHeight: Int
) = imageHelper.getPrimaryImageUrl(person, fillHeight)

override fun getItemId() = person.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import org.jellyfin.androidtv.util.ImageHelper
import org.jellyfin.sdk.model.api.BaseItemDto
import org.jellyfin.sdk.model.api.BaseItemKind
import org.jellyfin.sdk.model.api.SeriesTimerInfoDto
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.UUID

// TODO: Move properties to relevant classes only (e.g. baseItem should be in
// BaseItemDtoBaseRowItem)
abstract class BaseRowItem protected constructor(

Check warning

Code scanning / detekt

Always override hashCode when you override equals. All hash-based collections depend on objects meeting the equals-contract. Two equal objects must produce the same hashcode. When inheriting equals or hashcode, override the inherited and call the super method for clarification. Warning

A class should always override hashCode when overriding equals and the other way around.
val baseRowType: BaseRowType,
val index: Int = 0,
Expand All @@ -25,9 +21,7 @@ abstract class BaseRowItem protected constructor(
val chapterInfo: ChapterItemInfo? = null,
val seriesTimerInfo: SeriesTimerInfoDto? = null,
val gridButton: GridButton? = null,
) : KoinComponent {
val imageHelper by inject<ImageHelper>()

) {

Check warning

Code scanning / detekt

The more parameters a function has the more complex it is. Long parameter lists are often used to control complex algorithms and violate the Single Responsibility Principle. Prefer functions with short parameter lists. Warning

The constructor(baseRowType: BaseRowType, index: Int, staticHeight: Boolean, preferParentThumb: Boolean, selectAction: BaseRowItemSelectAction, baseItem: BaseItemDto?, chapterInfo: ChapterItemInfo?, seriesTimerInfo: SeriesTimerInfoDto?, gridButton: GridButton?) has too many parameters. The current threshold is set to 7.
open val showCardInfoOverlay: Boolean = false
open fun getBaseItemType(): BaseItemKind? = null
open fun isFavorite(): Boolean = false
Expand All @@ -39,18 +33,18 @@ abstract class BaseRowItem protected constructor(

open fun getImageUrl(
context: Context,
imageHelper: ImageHelper,
imageType: ImageType,
fillWidth: Int,
fillHeight: Int,
) = getPrimaryImageUrl(context, fillHeight)
): String? = null

open fun getPrimaryImageUrl(context: Context, fillHeight: Int): String? = null
open fun getFullName(context: Context): String? = null
open fun getName(context: Context): String? = null
open fun getItemId(): UUID? = null
open fun getSubText(context: Context): String? = null
open fun getSummary(context: Context): String? = null
open fun getBadgeImage(context: Context): Drawable? = null
open fun getBadgeImage(context: Context, imageHelper: ImageHelper): Drawable? = null

override fun equals(other: Any?): Boolean {
if (other is BaseRowItem) return other.getItemId() == getItemId()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jellyfin.androidtv.ui.itemhandling

import android.content.Context
import org.jellyfin.androidtv.constant.ImageType
import org.jellyfin.androidtv.data.model.ChapterItemInfo
import org.jellyfin.androidtv.util.ImageHelper
import org.jellyfin.androidtv.util.TimeUtils
import org.jellyfin.sdk.model.extensions.ticks

Expand All @@ -12,9 +14,12 @@ class ChapterItemInfoBaseRowItem(
staticHeight = true,
chapterInfo = item,
) {
override fun getPrimaryImageUrl(
override fun getImageUrl(
context: Context,
fillHeight: Int,
imageHelper: ImageHelper,
imageType: ImageType,
fillWidth: Int,
fillHeight: Int
) = chapterInfo?.imagePath

override fun getItemId() = chapterInfo?.itemId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jellyfin.androidtv.ui.itemhandling

import android.content.Context
import org.jellyfin.androidtv.constant.ImageType
import org.jellyfin.androidtv.ui.GridButton
import org.jellyfin.androidtv.util.ImageHelper

class GridButtonBaseRowItem(
item: GridButton,
Expand All @@ -10,9 +12,12 @@ class GridButtonBaseRowItem(
staticHeight = true,
gridButton = item,
) {
override fun getPrimaryImageUrl(
override fun getImageUrl(
context: Context,
fillHeight: Int,
imageHelper: ImageHelper,
imageType: ImageType,
fillWidth: Int,
fillHeight: Int
) = gridButton?.imageRes?.let {
imageHelper.getResourceUrl(context, it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.jellyfin.androidtv.ui.itemhandling

import android.content.Context
import org.jellyfin.androidtv.R
import org.jellyfin.androidtv.constant.ImageType
import org.jellyfin.androidtv.util.ImageHelper
import org.jellyfin.androidtv.util.apiclient.getSeriesOverview
import org.jellyfin.sdk.model.api.SeriesTimerInfoDto
import org.jellyfin.sdk.model.serializer.toUUIDOrNull
Expand All @@ -12,9 +14,12 @@ class SeriesTimerInfoDtoBaseRowItem(
baseRowType = BaseRowType.SeriesTimer,
seriesTimerInfo = item,
) {
override fun getPrimaryImageUrl(
override fun getImageUrl(
context: Context,
fillHeight: Int,
imageHelper: ImageHelper,
imageType: ImageType,
fillWidth: Int,
fillHeight: Int
) = imageHelper.getResourceUrl(
context,
R.drawable.tile_land_series_timer
Expand All @@ -28,5 +33,6 @@ class SeriesTimerInfoDtoBaseRowItem(
else seriesTimerInfo?.channelName,
seriesTimerInfo?.dayPattern
).joinToString(" ")

override fun getSummary(context: Context) = seriesTimerInfo?.getSeriesOverview(context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
import java.util.Locale;
import java.util.Map;

import kotlin.Lazy;

public class CardPresenter extends Presenter {
private int mStaticHeight = 150;
private ImageType mImageType = ImageType.POSTER;
private double aspect;

private boolean mShowInfo = true;

private boolean isUserView = false;

private boolean isUniformAspect = false;
private final Lazy<ImageHelper> imageHelper = KoinJavaComponent.<ImageHelper>inject(ImageHelper.class);

public CardPresenter() {
super();
Expand Down Expand Up @@ -104,8 +104,7 @@ public void setItem(BaseRowItem m, ImageType imageType, int lHeight, int pHeight
} else if (imageType.equals(ImageType.THUMB)) {
aspect = ImageHelper.ASPECT_RATIO_16_9;
} else {
ImageHelper imageHelper = KoinJavaComponent.<ImageHelper>get(ImageHelper.class);
aspect = Utils.getSafeValue(imageHelper.getImageAspectRatio(itemDto, m.getPreferParentThumb()), ImageHelper.ASPECT_RATIO_7_9);
aspect = Utils.getSafeValue(imageHelper.getValue().getImageAspectRatio(itemDto, m.getPreferParentThumb()), ImageHelper.ASPECT_RATIO_7_9);
}
switch (itemDto.getType()) {
case AUDIO:
Expand Down Expand Up @@ -370,7 +369,7 @@ public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
if (rowItem.getBaseItem() != null && rowItem.getBaseItemType() != BaseItemKind.USER_VIEW) {
RatingType ratingType = KoinJavaComponent.<UserPreferences>get(UserPreferences.class).get(UserPreferences.Companion.getDefaultRatingType());
if (ratingType == RatingType.RATING_TOMATOES) {
Drawable badge = rowItem.getBadgeImage(holder.view.getContext());
Drawable badge = rowItem.getBadgeImage(holder.view.getContext(), imageHelper.getValue());
holder.mCardView.setRating(null);
if (badge != null) {
holder.mCardView.setBadgeImage(badge);
Expand Down Expand Up @@ -410,7 +409,7 @@ public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
int fillHeight = Math.round(holder.getCardHeight() * holder.mCardView.getResources().getDisplayMetrics().density);

holder.updateCardViewImage(
rowItem.getImageUrl(holder.mCardView.getContext(), mImageType, fillWidth, fillHeight),
rowItem.getImageUrl(holder.mCardView.getContext(), imageHelper.getValue(), mImageType, fillWidth, fillHeight),
blurHash
);
}
Expand Down

0 comments on commit 8d04ff3

Please sign in to comment.