Skip to content
Draft
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 @@ -42,6 +42,7 @@ data class CatchUpItem(
val serviceId: String? = null,
val indexInResponse: Int? = null,
val contentType: ContentType? = null, // Null indicates unset, try to infer it
val imagePreviewUrl: String? = null,
) {

val clickUrl: String?
Expand Down Expand Up @@ -99,6 +100,7 @@ data class CatchUpItem(
serviceId = serviceId,
indexInResponse = index,
contentType = HTML,
imagePreviewUrl = "https://picsum.photos/seed/$index/300/300",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun CatchUpItem.toCatchUpDbItem(): CatchUpDbItem {
serviceId = serviceId,
indexInResponse = indexInResponse,
contentType = contentType?.name,
imagePreviewUrl = imagePreviewUrl,
imageUrl = imageInfo?.url,
imageDetailUrl = imageInfo?.detailUrl,
imageAnimatable = imageInfo?.animatable,
Expand Down Expand Up @@ -55,6 +56,7 @@ fun CatchUpDbItem.toCatchUpItem(): CatchUpItem {
serviceId = serviceId,
indexInResponse = indexInResponse,
contentType = contentType?.let { ContentType.valueOf(it) },
imagePreviewUrl = imagePreviewUrl,
imageInfo =
imageUrl?.let {
ImageInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ data class ServiceMeta(
val firstPageKey: Int?,
val pagesAreNumeric: Boolean = false,
val enabled: Boolean = true,
val supportsRichTextItems: Boolean = false,
) {
val enabledPreferenceKey = "service_config_${id}_enabled"
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS catchUpDbItem (
serviceId TEXT,
indexInResponse INTEGER AS Int,
contentType TEXT,
imagePreviewUrl TEXT,
-- Image info
imageUrl TEXT,
imageDetailUrl TEXT,
Expand Down Expand Up @@ -83,6 +84,7 @@ INSERT OR REPLACE INTO catchUpDbItem (
serviceId,
indexInResponse,
contentType,
imagePreviewUrl,
imageUrl,
imageDetailUrl,
imageAnimatable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class ProductHuntService(
indexInResponse = index + request.pageOffset,
serviceId = meta().id,
contentType = ContentType.HTML,
imagePreviewUrl = thumbnail?.url,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class RedditService(@InternalApi private val serviceMeta: ServiceMeta, private v
// If it's a selftext, mark it as HTML for summarizing.
contentType = if (link.isSelf) ContentType.HTML else null,
detailKey = link.id,
imagePreviewUrl = link.getPreviewUrl(),
)
}
DataResult(data, redditListingRedditResponse.data.after)
Expand Down Expand Up @@ -163,6 +164,7 @@ interface RedditMetaModule {
R.color.catchup_service_reddit_accent,
R.drawable.catchup_service_reddit_logo,
firstPageKey = null,
supportsRichTextItems = true,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package catchup.service.reddit.model
import androidx.annotation.Keep
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlin.math.abs
import kotlin.time.Instant

@Keep @JsonClass(generateAdapter = false) sealed interface RedditObject
Expand Down Expand Up @@ -83,7 +84,8 @@ data class RedditLink(
val selftext: String?,
@Json(name = "selftext_html") val selftextHtml: String?,
val stickied: Boolean,
val thumbnail: String,
val thumbnail: String?,
val preview: RedditPreview?,
val title: String,
val url: String,
val visited: Boolean,
Expand All @@ -102,7 +104,18 @@ data class RedditLink(
override val score: Int,
override val subreddit: String,
override val ups: Int,
) : RedditObject, RedditSubmission
) : RedditObject, RedditSubmission {
fun getPreviewUrl(preferredWidthPx: Int = Int.MAX_VALUE): String? {
if (preview == null) return null
if (!preview.enabled) return null
if (preview.images.isEmpty()) return null
preview.images.firstOrNull()?.resolutions?.let { resolutions ->
val bestFit = resolutions.minByOrNull { abs(it.width - preferredWidthPx) }
return bestFit?.url
}
return thumbnail?.takeUnless(String::isBlank)
}
}

@Keep
@JsonClass(generateAdapter = true)
Expand Down Expand Up @@ -140,3 +153,13 @@ data class RedditMore(
val depth: Int,
val children: List<String>,
) : RedditObject

@Keep
@JsonClass(generateAdapter = true)
data class RedditPreview(val images: List<RedditImage>, val enabled: Boolean) : RedditObject {
@JsonClass(generateAdapter = true)
data class RedditImage(val source: RedditImageSource, val resolutions: List<RedditImageSource>) {
@JsonClass(generateAdapter = true)
data class RedditImageSource(val url: String, val width: Int, val height: Int)
}
}
Loading