Skip to content

Instantly share code, notes, and snippets.

View LewisRhine's full-sized avatar

Lewis Rhine LewisRhine

View GitHub Profile
@LewisRhine
LewisRhine / FragmentDelegates.kt
Created June 9, 2017 20:05
A Fragment Argument Delegate exmple
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?) {
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."
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
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"))
}
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()
}
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) {
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 November 29, 2016 02:35
Running when over restaurantNearby state tree
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 November 21, 2016 21:05
Example Nearby Restaurant State Tree
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()
}
}