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

Reusing adapters with concatenating models (example) #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .idea/vcs.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import kotlinx.android.extensions.LayoutContainer
/**
* @author dumchev on 04.11.17.
*/
abstract class KDelegateAdapter<T>
: BaseDelegateAdapter<KDelegateAdapter.KViewHolder, T>() {
abstract class KDelegateAdapter<T> : BaseDelegateAdapter<KDelegateAdapter.KViewHolder, T>() {

open fun onCreated(view: View) = Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.ConcatenatingAdapter
import com.livermor.dumchev.delegateadapters.base.adapter.GenerateItemsDelegateAdapter
import com.livermor.dumchev.delegateadapters.base.adapter.TextDelegateAdapter
import kotlinx.android.synthetic.main.activity_base_example.*
Expand All @@ -17,6 +18,7 @@ class KotlinBaseExampleActivity : AppCompatActivity() {
.add(GenerateItemsDelegateAdapter { generateNewData() })
.add(TextDelegateAdapter())
.add(CheckDelegateAdapter())
.add(ConcatenatingAdapter(View.OnClickListener { generateNewData() }))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.ConcatenetedModel;
import com.livermor.dumchev.delegateadapters.base.model.ImageItem;
import com.livermor.dumchev.delegateadapters.base.model.TextItem;

Expand All @@ -22,13 +23,18 @@ static List<DiffUtilItem> 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 ConcatenetedModel(
new ImageItem("Title " + i, R.mipmap.ic_launcher_round),
new TextItem("Title " + i, "Description " + i)
);
}
objects.add(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.livermor.dumchev.delegateadapters.base.adapter

import android.view.View
import com.livermor.delegateadapter.delegate.KDelegateAdapter
import com.livermor.dumchev.delegateadapters.R
import com.livermor.dumchev.delegateadapters.base.model.ConcatenetedModel

/**
* @author dumchev on 11/12/2018.
*/
class ConcatenatingAdapter(clicks: View.OnClickListener) : KDelegateAdapter<ConcatenetedModel>() {

private val textDelegateAdapter by lazy { TxtDelegateAdapter() }
private val imageDelegateAdapter by lazy { ImageDelegateAdapter(clicks) }

override fun KViewHolder.onBind(item: ConcatenetedModel) {
with(textDelegateAdapter) { onBind(item.textViewModel) }
with(imageDelegateAdapter) { onBind(item.imageViewModel) }
}

override fun getLayoutId(): Int = R.layout.composite_item

override fun isForViewType(item: Any): Boolean = item is ConcatenetedModel
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.livermor.dumchev.delegateadapters.base.model

import com.livermor.delegateadapter.delegate.diff.KDiffUtilItem

/**
* @author dumchev on 11/12/2018.
*/
data class ConcatenetedModel(
val imageViewModel: ImageItem,
val textViewModel: TextItem
) : KDiffUtilItem {
override val id: Any = "" + imageViewModel.id() + textViewModel.id()
}
12 changes: 12 additions & 0 deletions example/src/main/res/layout/composite_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellowBg"
android:orientation="vertical">

<include layout="@layout/image_item"/>

<include layout="@layout/text_item"/>

</LinearLayout>
1 change: 1 addition & 0 deletions example/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="yellowBg">#fffdb9</color>
</resources>