Skip to content

Commit

Permalink
Add more syntactic sugar; update version to 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mareklangiewicz committed Nov 11, 2019
1 parent 78c8599 commit 45a17e6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .idea/kotlinScripting.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions sample/build.gradle

This file was deleted.

20 changes: 20 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
}

android {
compileSdkVersion(Vers.androidCompileSdk)
defaultConfig {
applicationId = "pl.mareklangiewicz.sandboxui"
minSdkVersion(21)
targetSdkVersion(Vers.androidTargetSdk)
versionCode = 1
versionName = "1.0"
}
}

dependencies {
implementation(project(":sandboxui"))
}
26 changes: 16 additions & 10 deletions sample/src/main/java/pl/mareklangiewicz/sandboxui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package pl.mareklangiewicz.sandboxui

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import splitties.toast.toast
import splitties.views.backgroundColor
import splitties.views.dsl.core.*
import kotlin.math.absoluteValue
import kotlin.random.Random

class MainActivity : AppCompatActivity() {
Expand All @@ -19,17 +21,18 @@ class MainActivity : AppCompatActivity() {
add(textView { text = "Text 1 $i" }, lParams())
}
val view2 = verticalLayout {
for (i in 1..7)
add(textView { text = "Text 2 $i" }, lParams())
for (i in 1..7) add(textView { text = "Text 2 $i" }, lParams())
addbox(textView { text = "nested"}) {
action("change nested text") { text = "nested ${randomHash()}"}
}
}
val box1 = SandboxUi(this, view1).apply {
action("xxx") { toast("xxx") }
action("yyy") { toast("yyy") }
val box1 = sandbox(view1) {
action("toast xxx") { toast("xxx") }
action("toast yyy") { toast("yyy") }
action("add random hash") { add(randomHashTextView(), lParams(matchParent)) }
}
val box2 = SandboxUi(this, view2).apply {
action("change color") {
view2.backgroundColor = getRandomColor()
}
val box2 = sandbox(view2) {
action("change color") { backgroundColor = getRandomColor() }
}
val boxes = horizontalLayout {
add(box1.root, lParams(0, weight = 1f))
Expand All @@ -40,3 +43,6 @@ class MainActivity : AppCompatActivity() {
}

private fun getRandomColor() = Color.rgb(Random.nextInt(255), Random.nextInt(255), Random.nextInt(255))

private fun randomHash() = Random.nextInt().hashCode().absoluteValue.toString(16)
private fun View.randomHashTextView() = textView { text = randomHash() }
8 changes: 4 additions & 4 deletions sandboxui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ plugins {
kotlin("android.extensions")
}
group = "com.github.langara.sandboxui"
version = "0.0.1"
version = "0.0.2"

android {
compileSdkVersion(Vers.androidCompileSdk)

defaultConfig {
minSdkVersion(Vers.androidMinSdk)
minSdkVersion(21)
targetSdkVersion(Vers.androidTargetSdk)
versionCode = 100
versionName = "0.0.1"
versionCode = 200
versionName = "0.0.2"
}
}

Expand Down
53 changes: 42 additions & 11 deletions sandboxui/src/main/java/pl/mareklangiewicz/sandboxui/SandboxUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package pl.mareklangiewicz.sandboxui
import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupMenu
import androidx.core.view.isVisible
import splitties.dimensions.dip
import splitties.resources.color
import splitties.resources.drawable
import splitties.views.backgroundColor
import splitties.views.dsl.core.*
import splitties.views.onClick
import splitties.views.padding
import splitties.views.textColorResource

@SuppressLint("SetTextI18n")
class SandboxUi(override val ctx: Context, val content: View, name: String = content::class.simpleName ?: "") : Ui {
class SandboxUi<Content: View>(
override val ctx: Context,
val content: Content,
name: String = content::class.simpleName ?: ""
) : Ui {

private val nameView = label(name)
private val eyeView = label("\u2609")
Expand All @@ -28,27 +31,26 @@ class SandboxUi(override val ctx: Context, val content: View, name: String = con
}

private val contentFrameView = frameLayout {
foreground = drawable(R.drawable.sandbox_ui_border)
background = drawable(R.drawable.sandbox_ui_back)
add(content, lParams(matchParent, matchParent))
}

override val root = verticalLayout {
backgroundColor = color(R.color.amber_500)
foreground = drawable(R.drawable.sandbox_ui_border)
background = drawable(R.drawable.sandbox_ui_back)
padding = dip(4)
add(headerView, lParams(matchParent))
add(contentFrameView, lParams(matchParent, matchParent))
}

private val actions = mutableListOf<Pair<String, () -> Unit>>()
private val actions = mutableListOf<Pair<String, Content.() -> Unit>>()

fun action(name: String, block: () -> Unit) {
fun action(name: String, block: Content.() -> Unit) {
menu.menu.add(0, actions.size, actions.size, name)
actions += name to block
}

private val menu = PopupMenu(ctx, dotsView).apply {
setOnMenuItemClickListener { actions[it.order].second(); true }
setOnMenuItemClickListener { actions[it.order].second.invoke(content); true }
}

init {
Expand All @@ -58,5 +60,34 @@ class SandboxUi(override val ctx: Context, val content: View, name: String = con
}
}

private fun Ui.label(text: String) =
textView { this.text = text; padding = dip(4); textColorResource = android.R.color.black }
fun <Content: View> Context.sandbox(
content: Content,
name: String = content::class.simpleName ?: "",
initBox: SandboxUi<Content>.() -> Unit = {}
) = SandboxUi(this, content, name).apply(initBox)

fun <Content: View> View.sandbox(
content: Content,
name: String = content::class.simpleName ?: "",
initBox: SandboxUi<Content>.() -> Unit = {}
) = context.sandbox(content, name, initBox)

fun <Content: View> Ui.sandbox(
content: Content,
name: String = content::class.simpleName ?: "",
initBox: SandboxUi<Content>.() -> Unit = {}
) = ctx.sandbox(content, name, initBox)

fun <Content: View> LinearLayout.addbox(
content: Content,
name: String = content::class.simpleName ?: "",
initBox: SandboxUi<Content>.() -> Unit = {}
) = sandbox(content, name, initBox).also { add(it.root, lParams(matchParent)) }


private fun Ui.label(text: String) = textView {
this.text = text
isSingleLine = true
padding = dip(4)
textColorResource = android.R.color.black
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<padding
android:left="1dp"
android:right="1dp"
android:top="1dp"
android:bottom="1dp" />

<solid android:color="@color/amber_300" />

<stroke
android:width="1dp"
android:color="@color/grey_800" />

</shape>

0 comments on commit 45a17e6

Please sign in to comment.