Skip to content

Instantly share code, notes, and snippets.

@ademirqueiroga
Created February 22, 2023 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ademirqueiroga/45373f4d7c1a91348ca24116adf345fa to your computer and use it in GitHub Desktop.
Save ademirqueiroga/45373f4d7c1a91348ca24116adf345fa to your computer and use it in GitHub Desktop.
Creating or updating programs
fun insertOrUpdateChannelPrograms(context: Context, channelId: Long) {
val existingPrograms = getChannelPrograms(channelId)
MovieList.list.forEach { movie ->
val existingProgram = existingPrograms.find {
it.internalProviderId == movie.id.toString()
}
val programBuilder = when (existingProgram) {
null -> PreviewProgram.Builder()
else -> PreviewProgram.Builder(existingProgram)
}
val program = programBuilder.setChannelId(channelId)
.setType(TvContractCompat.PreviewPrograms.TYPE_MOVIE)
.setTitle(title)
.setPreviewVideoUri(videoUrl?.toUri())
.setDurationMillis(Random.nextInt(0, 10) * 60 * 1000)
.setLastPlaybackPositionMillis(0)
.setGenre(genre)
.setDescription(description)
.setPosterArtUri(cardImageUrl?.toUri())
.setIntentUri("content://channelsample.com/movie/$id".toUri())
.setInternalProviderId(id.toString())
.build()
when (existingProgram) {
null -> context.contentResolver.insert(
/* url = */ TvContractCompat.PreviewPrograms.CONTENT_URI,
/* values = */ program.toContentValues()
)
else -> context.contentResolver.update(
/* uri = */ TvContractCompat.PreviewPrograms.CONTENT_URI,
/* values = */ program.toContentValues(),
/* where = */ null,
/* selectionArgs = */ null
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment