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

Add support for rich item previews #1631

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
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 @@ -119,6 +119,7 @@ constructor(
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 @@ -89,6 +89,7 @@ constructor(@InternalApi private val serviceMeta: ServiceMeta, private val api:
// 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 @@ -166,6 +167,7 @@ abstract class 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 kotlinx.datetime.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)
}
}