diff --git a/delegateadapter/src/main/java/com/livermor/delegateadapter/delegate/KDelegateAdapter.kt b/delegateadapter/src/main/java/com/livermor/delegateadapter/delegate/KDelegateAdapter.kt index 20729a0..b7035b7 100644 --- a/delegateadapter/src/main/java/com/livermor/delegateadapter/delegate/KDelegateAdapter.kt +++ b/delegateadapter/src/main/java/com/livermor/delegateadapter/delegate/KDelegateAdapter.kt @@ -6,8 +6,7 @@ import kotlinx.android.extensions.LayoutContainer /** * @author dumchev on 04.11.17. */ -abstract class KDelegateAdapter - : BaseDelegateAdapter() { +abstract class KDelegateAdapter : BaseDelegateAdapter() { open fun onCreated(view: View) = Unit diff --git a/example/src/main/java/com/example/dumchev/delegateadapters/base/MockDataFactory.java b/example/src/main/java/com/example/dumchev/delegateadapters/base/MockDataFactory.java deleted file mode 100644 index e55253f..0000000 --- a/example/src/main/java/com/example/dumchev/delegateadapters/base/MockDataFactory.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.example.dumchev.delegateadapters.base; - -import com.example.delegateadapter.delegate.diff.IComparableItem; -import com.example.dumchev.delegateadapters.R; -import com.example.dumchev.delegateadapters.base.model.CheckViewModel; -import com.example.dumchev.delegateadapters.base.model.CompositeModel; -import com.example.dumchev.delegateadapters.base.model.IViewModel; -import com.example.dumchev.delegateadapters.base.model.ImageViewModel; -import com.example.dumchev.delegateadapters.base.model.TextViewModel; - -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -/** - * @author dumchev on 05.11.17. - */ -class MockDataFactory { - - private static final int SIZE = 20; - - static List prepareData() { - ArrayList objects = new ArrayList<>(SIZE); - Random random = new Random(); - for (int i = 0; i < SIZE; i++) { - IComparableItem item; - int type = random.nextInt(4); - if (type == 0) { - item = new TextViewModel("Title " + i, "Description " + i); - } else if (type == 1) { - item = new ImageViewModel("Title " + i, R.mipmap.ic_launcher_round); - } else if (type == 2) { - item = new CheckViewModel("You still love this lib", true); - } else { - item = new CompositeModel( - new ImageViewModel("Title " + i, R.mipmap.ic_launcher_round), - new TextViewModel("Title " + i, "Description " + i) - ); - } - objects.add(item); - } - return objects; - } -} diff --git a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/KotlinBaseExampleActivity.kt b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/KotlinBaseExampleActivity.kt index a0800fd..a7d8295 100644 --- a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/KotlinBaseExampleActivity.kt +++ b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/KotlinBaseExampleActivity.kt @@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import com.livermor.delegateadapter.delegate.diff.DiffUtilCompositeAdapter import com.livermor.dumchev.delegateadapters.base.adapter.CheckDelegateAdapter +import com.livermor.dumchev.delegateadapters.base.adapter.CompositeDelegateAdapter import com.livermor.dumchev.delegateadapters.base.adapter.GenerateItemsDelegateAdapter import com.livermor.dumchev.delegateadapters.base.adapter.TextDelegateAdapter import kotlinx.android.synthetic.main.activity_base_example.* @@ -17,6 +18,7 @@ class KotlinBaseExampleActivity : AppCompatActivity() { .add(GenerateItemsDelegateAdapter { generateNewData() }) .add(TextDelegateAdapter()) .add(CheckDelegateAdapter()) + .add(CompositeDelegateAdapter(View.OnClickListener { generateNewData() })) .build() } diff --git a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/MockDataFactory.java b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/MockDataFactory.java index 7ed449f..f5c986a 100644 --- a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/MockDataFactory.java +++ b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/MockDataFactory.java @@ -3,6 +3,7 @@ import com.livermor.delegateadapter.delegate.diff.DiffUtilItem; import com.livermor.dumchev.delegateadapters.R; import com.livermor.dumchev.delegateadapters.base.model.CheckItem; +import com.livermor.dumchev.delegateadapters.base.model.CompositeModel; import com.livermor.dumchev.delegateadapters.base.model.ImageItem; import com.livermor.dumchev.delegateadapters.base.model.TextItem; @@ -22,13 +23,18 @@ static List prepareData() { Random random = new Random(); for (int i = 0; i < SIZE; i++) { DiffUtilItem item; - int type = random.nextInt(3); + int type = random.nextInt(4); if (type == 0) { item = new TextItem("Title " + i, "Description " + i); } else if (type == 1) { item = new ImageItem("Title " + i, R.mipmap.ic_launcher_round); - } else { + } else if (type == 2) { item = new CheckItem("You still love this lib", true); + } else { + item = new CompositeModel( + new ImageItem("Title " + i, R.mipmap.ic_launcher_round), + new TextItem("Title " + i, "Description " + i) + ); } objects.add(item); } diff --git a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/adapter/CompositeDelegateAdapter.kt b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/adapter/CompositeDelegateAdapter.kt index c4c500e..e00ccab 100644 --- a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/adapter/CompositeDelegateAdapter.kt +++ b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/adapter/CompositeDelegateAdapter.kt @@ -1,26 +1,24 @@ -package com.example.dumchev.delegateadapters.base.adapter +package com.livermor.dumchev.delegateadapters.base.adapter import android.view.View -import com.example.delegateadapter.delegate.KDelegateAdapter -import com.example.dumchev.delegateadapters.R -import com.example.dumchev.delegateadapters.base.model.CompositeModel +import com.livermor.delegateadapter.delegate.KDelegateAdapter +import com.livermor.dumchev.delegateadapters.R +import com.livermor.dumchev.delegateadapters.base.model.CompositeModel /** * @author dumchev on 11/12/2018. */ class CompositeDelegateAdapter(clicks: View.OnClickListener) : KDelegateAdapter() { - val textDelegateAdapter by lazy { TxtDelegateAdapter() } - val imageDelegateAdapter by lazy { ImageDelegateAdapter(clicks) } + private val textDelegateAdapter by lazy { TxtDelegateAdapter() } + private val imageDelegateAdapter by lazy { ImageDelegateAdapter(clicks) } - override fun onBind(item: CompositeModel, viewHolder: KViewHolder) { - textDelegateAdapter.onBind(item.textViewModel, viewHolder) - imageDelegateAdapter.onBind(item.imageViewModel, viewHolder) + override fun KViewHolder.onBind(item: CompositeModel) { + with(textDelegateAdapter) { onBind(item.textViewModel) } + with(imageDelegateAdapter) { onBind(item.imageViewModel) } } override fun getLayoutId(): Int = R.layout.composite_item - override fun isForViewType(items: MutableList<*>, position: Int): Boolean { - return items[position] is CompositeModel - } + override fun isForViewType(item: Any): Boolean = item is CompositeModel } \ No newline at end of file diff --git a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/model/CompositeModel.kt b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/model/CompositeModel.kt index f0cbfbf..6c3848f 100644 --- a/example/src/main/java/com/livermor/dumchev/delegateadapters/base/model/CompositeModel.kt +++ b/example/src/main/java/com/livermor/dumchev/delegateadapters/base/model/CompositeModel.kt @@ -1,15 +1,15 @@ -package com.example.dumchev.delegateadapters.base.model +package com.livermor.dumchev.delegateadapters.base.model -import com.example.delegateadapter.delegate.diff.IComparableItem +import com.livermor.delegateadapter.delegate.diff.KDiffUtilItem /** * @author dumchev on 11/12/2018. */ data class CompositeModel( - val imageViewModel: ImageViewModel, - val textViewModel: TextViewModel -) : IComparableItem { + val imageViewModel: ImageItem, + val textViewModel: TextItem +) : KDiffUtilItem { - override fun id(): Any = "" + imageViewModel.id() + textViewModel.id() + override val id: Any = "" + imageViewModel.id() + textViewModel.id() override fun content(): Any = this } \ No newline at end of file