Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not for merge #1186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.airbnb.epoxy.kotlinsample

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
Expand All @@ -9,16 +8,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.airbnb.epoxy.EpoxyRecyclerView
import com.airbnb.epoxy.EpoxyVisibilityTracker
import com.airbnb.epoxy.epoxyView
import com.airbnb.epoxy.group
import com.airbnb.epoxy.kotlinsample.helpers.carouselNoSnapBuilder
import com.airbnb.epoxy.kotlinsample.models.ItemDataClass
import com.airbnb.epoxy.kotlinsample.models.ItemViewBindingDataClass
import com.airbnb.epoxy.kotlinsample.models.carouselItemCustomView
import com.airbnb.epoxy.kotlinsample.models.coloredSquareView
import com.airbnb.epoxy.kotlinsample.models.decoratedLinearGroup
import com.airbnb.epoxy.kotlinsample.models.itemCustomView
import com.airbnb.epoxy.kotlinsample.models.itemEpoxyHolder
import com.airbnb.epoxy.kotlinsample.models.itemViewBindingEpoxyHolder

class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: EpoxyRecyclerView
Expand Down Expand Up @@ -55,125 +45,7 @@ class MainActivity : AppCompatActivity() {
epoxyVisibilityTracker.partialImpressionThresholdPercentage = 75
epoxyVisibilityTracker.attach(recyclerView)

recyclerView.withModels {

group {
id("epoxyModelGroupDsl")
layout(R.layout.vertical_linear_group)

coloredSquareView {
id("coloredSquareView 1")
color(Color.DKGRAY)
onVisibilityStateChanged { model, _, visibilityState ->
Log.d(TAG, "$model -> $visibilityState")
}
}

coloredSquareView {
id("coloredSquareView 2")
color(Color.GRAY)
}

coloredSquareView {
id("coloredSquareView 3")
color(Color.LTGRAY)
}

carouselNoSnapBuilder {
id("nested carousel")
val lastPage = 10
for (i in 0 until lastPage) {
carouselItemCustomView {
id("nested carousel $i")
title("Page $i / $lastPage")
onVisibilityStateChanged { model, _, visibilityState ->
Log.d(TAG, "pos: $i ${model.javaClass} -> $visibilityState")
}
}
}
}
}

decoratedLinearGroup {
id("epoxyModelGroupWithLayoutDsl")

coloredSquareView {
id("coloredSquareView 1")
color(Color.DKGRAY)
}

coloredSquareView {
id("coloredSquareView 2")
color(Color.GRAY)
}

coloredSquareView {
id("coloredSquareView 3")
color(Color.LTGRAY)
}
}

for (i in 0 until 100) {
dataBindingItem {
id("data binding $i")
text("this is a data binding model2")
onClick { _ ->
Toast.makeText(this@MainActivity, "clicked", Toast.LENGTH_LONG).show()
}
onVisibilityStateChanged { model, view, visibilityState ->
Log.d(TAG, "$model -> $visibilityState")
}
}

itemCustomView {
id("custom view $i")
color(Color.GREEN)
title("Open sticky header activity")
listener { _ ->
Toast.makeText(this@MainActivity, "clicked", Toast.LENGTH_LONG).show()
startActivity(Intent(this@MainActivity, StickyHeaderActivity::class.java))
}
}

itemEpoxyHolder {
id("view holder $i")
title("this is a View Holder item")
listener {
Toast.makeText(this@MainActivity, "clicked", Toast.LENGTH_LONG)
.show()
}
}

itemViewBindingEpoxyHolder {
id("view binding $i")
title("This is a ViewBinding item")
listener {
Toast.makeText(this@MainActivity, "clicked", Toast.LENGTH_LONG)
.show()
}
}

carouselNoSnapBuilder {
id("carousel $i")
val lastPage = 10
for (j in 0 until lastPage) {
carouselItemCustomView {
id("carousel $i-$j")
title("Page $j / $lastPage")
}
}
}

// Since data classes do not use code generation, there's no extension generated here
ItemDataClass("this is a Data Class Item")
.id("data class $i")
.addTo(this)

ItemViewBindingDataClass("This is a Data Class Item using ViewBinding")
.id("data class view binding $i")
.addTo(this)
}
}
recyclerView.adapter = MainController(this).adapter
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.airbnb.epoxy.kotlinsample

import android.content.Intent
import android.graphics.Color
import android.util.Log
import android.widget.Toast
import com.airbnb.epoxy.EpoxyController
import com.airbnb.epoxy.EpoxyModel
import com.airbnb.epoxy.ModelCollector
import com.airbnb.epoxy.group
import com.airbnb.epoxy.kotlinsample.helpers.carouselNoSnapBuilder
import com.airbnb.epoxy.kotlinsample.models.ItemDataClass
import com.airbnb.epoxy.kotlinsample.models.ItemViewBindingDataClass
import com.airbnb.epoxy.kotlinsample.models.carouselItemCustomView
import com.airbnb.epoxy.kotlinsample.models.coloredSquareView
import com.airbnb.epoxy.kotlinsample.models.decoratedLinearGroup
import com.airbnb.epoxy.kotlinsample.models.itemCustomView
import com.airbnb.epoxy.kotlinsample.models.itemEpoxyHolder
import com.airbnb.epoxy.kotlinsample.models.itemViewBindingEpoxyHolder

class MainController(
private val mainActivity: MainActivity
) : EpoxyController() {

override fun buildModels() {
group {
id("epoxyModelGroupDsl")
layout(R.layout.vertical_linear_group)

coloredSquareView {
id("coloredSquareView 1")
color(Color.DKGRAY)
onVisibilityStateChanged { model, _, visibilityState ->
Log.d(TAG, "$model -> $visibilityState")
}
}

coloredSquareView {
id("coloredSquareView 2")
color(Color.GRAY)
}

coloredSquareView {
id("coloredSquareView 3")
color(Color.LTGRAY)
}

carouselNoSnapBuilder {
id("nested carousel")
val lastPage = 10
for (i in 0 until lastPage) {
carouselItemCustomView {
id("nested carousel $i")
title("Page $i / $lastPage")
onVisibilityStateChanged { model, _, visibilityState ->
[email protected](model, visibilityState, i)
}
}
}
}
}

decoratedLinearGroup {
id("epoxyModelGroupWithLayoutDsl")

[email protected](Color.DKGRAY)
[email protected](Color.GRAY)
[email protected](Color.LTGRAY)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is here :

e: kotlinsample/src/main/java/com/airbnb/epoxy/kotlinsample/MainController.kt: (66, 33): 'fun ModelCollector.addColoredSquareView(color: Int): Unit' can't be called in this context by implicit receiver. Use the explicit one if necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work with

        decoratedLinearGroup {
            val group = this
            id("epoxyModelGroupWithLayoutDsl")
            with (this@MainController) {
                group.addColoredSquareView(Color.DKGRAY)
                group.addColoredSquareView(Color.GRAY)
                group.addColoredSquareView(Color.LTGRAY)
            }
        }

but this is not ideal.

}

for (i in 0 until 100) {
dataBindingItem {
id("data binding $i")
text("this is a data binding model2")
onClick { _ ->
Toast.makeText([email protected], "clicked", Toast.LENGTH_LONG).show()
}
onVisibilityStateChanged { model, _, visibilityState ->
[email protected](model, visibilityState)
}
}

itemCustomView {
id("custom view $i")
color(Color.GREEN)
title("Open sticky header activity")
listener { _ ->
Toast.makeText([email protected], "clicked", Toast.LENGTH_LONG).show()
[email protected](Intent([email protected], StickyHeaderActivity::class.java))
}
}

itemEpoxyHolder {
id("view holder $i")
title("this is a View Holder item")
listener {
Toast.makeText([email protected], "clicked", Toast.LENGTH_LONG)
.show()
}
}

itemViewBindingEpoxyHolder {
id("view binding $i")
title("This is a ViewBinding item")
listener {
Toast.makeText([email protected], "clicked", Toast.LENGTH_LONG)
.show()
}
}

carouselNoSnapBuilder {
id("carousel $i")
val lastPage = 10
for (j in 0 until lastPage) {
carouselItemCustomView {
id("carousel $i-$j")
title("Page $j / $lastPage")
}
}
}

// Since data classes do not use code generation, there's no extension generated here
ItemDataClass("this is a Data Class Item")
.id("data class $i")
.addTo(this)

ItemViewBindingDataClass("This is a Data Class Item using ViewBinding")
.id("data class view binding $i")
.addTo(this)
}
}

private fun ModelCollector.addColoredSquareView(color: Int) {
coloredSquareView {
id("coloredSquareView-$color")
color(Color.DKGRAY)
onVisibilityStateChanged { model, _, visibilityState ->
[email protected](model, visibilityState)
}
}
}

private fun logVisibilityState(model: EpoxyModel<*>, visibilityState: Int, position: Int? = null) {
Log.d(TAG, "$model -> $visibilityState position=$position")
}

companion object {
private const val TAG = "MainController"
}
}