Skip to content

Instantly share code, notes, and snippets.

@LewisRhine
LewisRhine / FragmentDelegates.kt
Created Jun 9, 2017
A Fragment Argument Delegate exmple
View FragmentDelegates.kt
class FragmentDelegateTest : Fragment() {
companion object {
fun newInstance(someId: String) : FragmentDelegateTest {
return FragmentDelegateTest().apply { this.someId = someId }
}
}
private var someId: String? by fragmentArgument()
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
View About.kt
object About : MdlContent {
override val content = content("About") {
setAttribute("style", " background: url('images/whoiam.jpg') center / cover; filter: alpha(opacity=60); padding: 10px;")
grid {
cell(3) {}
cellCard(6) {
title = "About me"
supportingText = "Completely self-taught, I began my love for writing code when I was you kid and found out about QBasic on the family computer. In my day to day life, I enjoy keeping up with new developments within the technology and android community. I am very passionate about clean thought out architecture in the code I write. And believe strongly in testing as much as possible. Even on Android where it's not the easiest to accomplish."
View Content.kt
fun content(title: String, cssClassId: String = "", body: Element.() -> Unit) = Content(title, cssClassId, body)
class Content(val title: String, cssClassId: String = "", body: Element.() -> Unit) : MdlComponent("div", "mdl-layout__content", cssClassId) {
init {
mainElement.body()
}
}
interface MdlContent {
val content: Content
View Main.kt
fun main(args: Array<String>) {
val mdlApp = mdlApp {
navigationLayout(About, "layout") {
header {}
drawer("drawer") {
mainElement.header("drawer-header ${MdlColor.Background.blueGrey(Shade.s300)}") {
img("avatar") { src = "images/roundprofile.png" }
b { textContent = "Lewis Rhine" }
append(document.createTextNode("Android Developer"))
}
View Img.kt
class Img(cssClassId: String = "") : MdlComponent("img", cssClassId) {
var src: String by htmlPram()
var width: Int by htmlPram()
var height: Int by htmlPram()
var border:Int by htmlPram()
var alt: String by htmlPram()
}
View MdlComponent.kt
abstract class MdlComponent(tag: String, classType: String, cssClassId: String = "") {
val mainElement = document.createElement(tag).apply { this classType "$cssClassId $classType" }
var backgroundColor: MdlColor.Background? = null
set(value) {
value?.let { mainElement.setAttribute("class", mainElement.getAttribute("class")?.plus(" $it")!!) }
}
var textColor: MdlColor.Text? = null
set(value) {
View MdlApp.kt
fun mdlApp(init: MdlApp.() -> Unit): MdlApp {
val app = MdlApp()
app.init()
return app
}
class MdlApp() {
private val app = document.getElementById("MdlApp")
init {
@LewisRhine
LewisRhine / whenOnTree.kt
Last active Nov 29, 2016
Running when over restaurantNearby state tree
View whenOnTree.kt
when (restaurantNearby) {
is RestaurantNearby.Loading -> // Tell user the data is loading
is RestaurantNearby.Loading.Done -> {
restaurantNearby.reloadData() // Attach this function to some user feedback.
when (restaurantNearby) {
is RestaurantNearby.Loading.Done.Ready -> restaurantNearby.restaurants // Do something with this list.
is RestaurantNearby.Loading.Done.Empty -> // Tell the user there was no data found.
@LewisRhine
LewisRhine / RestaurantNearby.kt
Created Nov 21, 2016
Example Nearby Restaurant State Tree
View RestaurantNearby.kt
sealed class RestaurantNearby() {
sealed class Loading() : RestaurantNearby() {
//Code for getting Location, and API call
sealed class Done() : Loading() {
fun reloadData() {}
class Ready(val restaurants: List<Restaurant>) : Done()
class Empty() : Done()
class Error(val message: String, val throwable: Throwable?) : Done()
}
}