Skip to content

Instantly share code, notes, and snippets.

View stefanmedack's full-sized avatar

Stefan Medack stefanmedack

View GitHub Profile
@stefanmedack
stefanmedack / expand_github_pr.js
Created January 22, 2019 11:11
Expand 'Load diff' for Github PRs
Array.from(document.querySelectorAll('div'))
.filter(el => el.textContent === 'Load diff')
.forEach(it => it.click())
val moshi = Moshi.Builder()
.add(DefaultOnDataMismatchAdapter.newFactory(VideoType::class.java, VideoType.unknown)) /* 1 */
.add(DefaultOnDataMismatchAdapter.newFactory(Video::class.java, null)) /* 2 */
.add(DefaultOnDataMismatchAdapter.newFactory(Content::class.java, null)) /* 3 */
.add(FilterNullValuesFromListAdapter.newFactory(Content::class.java)) /* 4 */
.add(DefaultOnDataMismatchAdapter.newFactory(Article::class.java, null)) /* 5 */
.add(KotlinJsonAdapterFactory())
.build()
data class Image(
val url: String,
val source: String? = null
)
data class Video(
val id: String,
@Json(name = "video_url") val videoUrl: String,
@Json(name = "preview_image") val previewImage: Image,
@Json(name = "video_title") val videoTitle: String = EMPTY_STRING,
val duration: Int = 0,
@Json(name = "view_count") val viewCount: Int = 0,
val type: VideoType = VideoType.unknown
)
package com.ajnsnewmedia.kitchenstories.moshi.adapter
import com.squareup.moshi.*
import java.io.IOException
import java.lang.reflect.Type
class FilterNullValuesFromListAdapter<T : Any> private constructor(private val delegate: JsonAdapter<List<T?>>) : JsonAdapter<List<T>>() {
@Throws(IOException::class)
@stefanmedack
stefanmedack / DefaultOnDataMismatchAdapter.kt
Last active June 7, 2022 14:17
Adapter for Moshi taking in a class definition and a default return value in case of a parsing error. Kotlin version of same-name-class from Moshi examples: https://github.com/square/moshi/blob/master/examples/src/main/java/com/squareup/moshi/recipes/DefaultOnDataMismatchAdapter.java
package com.ajnsnewmedia.kitchenstories.moshi.adapter
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import timber.log.Timber
import java.io.IOException
import java.lang.reflect.Type
public class Article {
public String id;
public String title;
public Image cell_image;
public Video video;
public User author;
public String subtitle;
public List<Content> content;
public int like_count;
public List<Comment> comments;
data class Article(
val id: String,
val title: String,
@Json(name = "cell_image") val image: Image,
val author: User,
val video: Video? = null,
val subtitle: String = EMPTY_STRING,
val content: List<Content> = listOf(),
@Json(name = "like_count") val likeCount: Int = 0,
val comments: List<Comment> = listOf(),