diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ContactAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ContactAdapter.kt index 7f52bbcc..10ad2473 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ContactAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ContactAdapter.kt @@ -19,8 +19,8 @@ package com.moez.QKSMS.feature.compose import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView import com.moez.QKSMS.R import com.moez.QKSMS.common.base.QkAdapter import com.moez.QKSMS.common.base.QkViewHolder @@ -35,14 +35,23 @@ class ContactAdapter @Inject constructor() : QkAdapter() { val contactSelected: Subject = PublishSubject.create() + private val numbersViewPool = RecyclerView.RecycledViewPool() + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val view = layoutInflater.inflate(R.layout.contact_list_item, parent, false) + + view.addresses.setRecycledViewPool(numbersViewPool) + return QkViewHolder(view).apply { view.primary.setOnClickListener { val contact = getItem(adapterPosition) contactSelected.onNext(copyContact(contact, 0)) } + + view.addresses.adapter = PhoneNumberAdapter { contact, index -> + contactSelected.onNext(copyContact(contact, index + 1)) + } } } @@ -56,17 +65,9 @@ class ContactAdapter @Inject constructor() : QkAdapter() { view.address.text = contact.numbers.first()?.address ?: "" view.type.text = contact.numbers.first()?.type ?: "" - view.addresses.removeAllViews() - contact.numbers.forEachIndexed { index, number -> - if (index != 0) { - val numberView = View.inflate(view.context, R.layout.contact_number_list_item, null) - numberView.setOnClickListener { contactSelected.onNext(copyContact(contact, index)) } - numberView.address.text = number.address - numberView.type.text = number.type - view.addresses.addView(numberView) - } - } - + val adapter = view.addresses.adapter as PhoneNumberAdapter + adapter.contact = contact + adapter.data = contact.numbers.drop(Math.min(contact.numbers.size, 1)) } /** diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/PhoneNumberAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/PhoneNumberAdapter.kt new file mode 100644 index 00000000..17da0d59 --- /dev/null +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/PhoneNumberAdapter.kt @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package com.moez.QKSMS.feature.compose + +import android.view.LayoutInflater +import android.view.ViewGroup +import com.moez.QKSMS.R +import com.moez.QKSMS.common.base.QkAdapter +import com.moez.QKSMS.common.base.QkViewHolder +import com.moez.QKSMS.model.Contact +import com.moez.QKSMS.model.PhoneNumber +import kotlinx.android.synthetic.main.contact_list_item.view.* + +class PhoneNumberAdapter( + private val numberClicked: (Contact, Int) -> Unit +) : QkAdapter() { + + lateinit var contact: Contact + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { + val inflater = LayoutInflater.from(parent.context) + val view = inflater.inflate(R.layout.contact_number_list_item, parent, false) + return QkViewHolder(view) + } + + override fun onBindViewHolder(holder: QkViewHolder, position: Int) { + val number = getItem(position) + val view = holder.itemView + + // Setting this in onCreateViewHolder causes a crash sometimes. [contact] returns the + // contact from a different row, I'm not sure why + view.setOnClickListener { numberClicked(contact, position) } + + view.address.text = number.address + view.type.text = number.type + } + +} \ No newline at end of file diff --git a/presentation/src/main/res/layout/contact_list_item.xml b/presentation/src/main/res/layout/contact_list_item.xml index 4fe19888..9a633a29 100644 --- a/presentation/src/main/res/layout/contact_list_item.xml +++ b/presentation/src/main/res/layout/contact_list_item.xml @@ -39,7 +39,7 @@ android:id="@+id/avatar" android:layout_width="40dp" android:layout_height="40dp" - android:layout_marginRight="16dp" /> + android:layout_marginEnd="16dp" /> + android:layout_height="wrap_content" + android:layout_marginTop="2dp"> - + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + tools:listitem="@layout/contact_number_list_item" /> \ No newline at end of file