Created
August 9, 2019 09:01
-
-
Save seanghay/366001a7d4dd48fd1c48de53c65e37d3 to your computer and use it in GitHub Desktop.
Lorem Picsum Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Size | |
internal fun picsum(width: Int, height: Int): Picsum = | |
Picsum(Size(width, height)) | |
class Picsum constructor( | |
private var size: Size, | |
private var id: Int? = null, | |
private var blur: Int = 0, | |
private var grayscale: Boolean = false, | |
private var random: Boolean = false | |
) { | |
fun id(id: Int): Picsum { | |
this.id = id | |
return this | |
} | |
fun blur(blur: Int): Picsum { | |
this.blur = blur | |
return this | |
} | |
fun grayscale(): Picsum { | |
grayscale = true | |
return this | |
} | |
fun random(): Picsum { | |
random = true | |
return this | |
} | |
fun get(): String { | |
var url = BASE_URL | |
if (id != null) url += "/id/$id" | |
url += "/${size.width}/${size.height}" | |
var isFirst = false | |
if (blur > 0) { | |
url += "?blur=$blur" | |
isFirst = true | |
} | |
if (grayscale) { | |
url += (if (isFirst) "&" else "?") + "grayscale" | |
isFirst = true | |
} | |
if (random) { | |
url += (if (isFirst) "&" else "?") + "random=" + System.currentTimeMillis() | |
isFirst = true | |
} | |
return url | |
} | |
companion object { | |
private const val BASE_URL = "https://picsum.photos" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment