Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
class MainActivity : AppCompatActivity() {
private var toolBar: Toolbar? = null
private var container: ViewGroup? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
coordinatorLayout {
fitsSystemWindows = true
appBarLayout {
interface ViewBinder<in T> {
fun bind(t: T) : View
fun unbind(t: T)
}
class MainLayout : ViewBinder<MainActivity> {
override fun bind(mainActivity: MainActivity): View =
mainActivity.UI {
coordinatorLayout {
fitsSystemWindows = true
appBarLayout {
mainActivity.toolBar = toolbar {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) elevation = 4f
configuration(orientation = Orientation.LANDSCAPE, smallestWidth = 700) {
recyclerView {
init()
}.lparams(width = widthProcent(50), height = matchParent)
frameLayout().lparams(width = matchParent, height = matchParent)
}
fun <T : View> T.widthProcent(procent: Int): Int =
getAppUseableScreenSize().x.toFloat().times(procent.toFloat() / 100).toInt()
public class MainActivity extends AppCompatActivity {
LinearLayout container;
RecyclerView recycView;
FrameLayout detailContainer;
private MainLayout mainLayout = new MainLayout();
@Override
protected void onCreate(Bundle savedInstanceState) {
@nomisRev
nomisRev / MathExpression.kt
Last active July 10, 2017 05:49
Modelling data in Kotlin
sealed class MathExpression
data class Add(val left: MathExpression, val right: MathExpression) : MathExpression()
data class Sub(val left: MathExpression, val right: MathExpression) : MathExpression()
data class Number(val value: Int): MathExpression()
sealed class MathExpression {
abstract fun eval(): Int
}
data class Addition(val left: MathExpression, val right: MathExpression) : MathExpression() {
override fun eval(): Int = left.eval() + right.eval()
}
data class Subtraction(val left: MathExpression, val right: MathExpression) : MathExpression() {
override fun eval(): Int = left.eval() - right.eval()
sealed class MathExpression {
fun eval(): Int = when(this) {
is Addition -> left.eval() + right.eval()
is Subtraction -> left.eval() - right.eval()
is Number -> value
}
}
data class Addition(val left: MathExpression, val right: MathExpression) : MathExpression()
data class Subtraction(val left: MathExpression, val right: MathExpression) : MathExpression()
╔═══════════════════╦═════════════════════════╦═════════════════════════╗
║ ║ Add new method ║ Add new data ║
╠═══════════════════╬═════════════════════════╬═════════════════════════╣
║ Polymorpish ║ Change existing code ║ Existing code unchanged ║
║ Pattern matching ║ Existing code unchanged ║ Change existing code ║
╚═══════════════════╩═════════════════════════╩═════════════════════════╝
sealed class MathExpression {
fun eval(): Int = when (this) {
is Addition -> left.eval() + right.eval()
is Subtraction -> left.eval() - right.eval()
is Division -> left.eval() / right.eval()
is Number -> value
}
}
data class Addition(val left: MathExpression, val right: MathExpression) : MathExpression()