Skip to content

Commit

Permalink
Task #101: replace click interface with function as arg
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandr7035 committed Jul 10, 2022
1 parent 58857f8 commit 9ae3a3a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import by.alexandr7035.affinidi_id.domain.model.credentials.stored_credentials.C
import by.alexandr7035.affinidi_id.presentation.common.credentials.credential_card.CredentialCardUi
import java.lang.IllegalStateException

class CredentialsAdapter(private val credentialClickListener: CredentialClickListener) :
class CredentialsAdapter(private val credentialClickCallback: (credentialId: String) -> Unit) :
RecyclerView.Adapter<CredentialsAdapter.CredentialViewHolder>() {

private var items: List<CredentialCardUi> = emptyList()
Expand All @@ -22,10 +22,10 @@ class CredentialsAdapter(private val credentialClickListener: CredentialClickLis

return when (viewType) {
ACTIVE_CREDENTIAL -> {
CredentialViewHolder.NormalVCViewHolder(binding, credentialClickListener)
CredentialViewHolder.NormalVCViewHolder(binding, credentialClickCallback)
}
NON_ACTIVE_CREDENTIAL -> {
CredentialViewHolder.ExpiredVCViewHolder(binding, credentialClickListener)
CredentialViewHolder.ExpiredVCViewHolder(binding, credentialClickCallback)
}

else -> throw IllegalStateException("View type not implemented")
Expand All @@ -43,8 +43,7 @@ class CredentialsAdapter(private val credentialClickListener: CredentialClickLis
override fun getItemViewType(position: Int): Int {
return if (items[position].credentialStatusUi.domainStatus == CredentialStatus.ACTIVE) {
ACTIVE_CREDENTIAL
}
else {
} else {
NON_ACTIVE_CREDENTIAL
}
}
Expand All @@ -61,30 +60,30 @@ class CredentialsAdapter(private val credentialClickListener: CredentialClickLis

class NormalVCViewHolder(
override val binding: ViewCredentialItemBinding,
private val credentialClickListener: CredentialClickListener
private val credentialClickCallback: (credentialId: String) -> Unit
) : CredentialViewHolder(binding) {
override fun bind(item: CredentialCardUi) {
binding.credentialId.text = item.id
binding.credentialTypeView.text = item.credentialTypeText
binding.issuanceDate.text = item.issuanceDateText
binding.root.setOnClickListener {
credentialClickListener.onCredentialClicked(item.id)
credentialClickCallback.invoke(item.id)
}
}
}


class ExpiredVCViewHolder(
override val binding: ViewCredentialItemBinding,
private val credentialClickListener: CredentialClickListener
private val credentialClickCallback: (credentialId: String) -> Unit
) : CredentialViewHolder(binding) {
override fun bind(item: CredentialCardUi) {
binding.credentialId.text = item.id
binding.credentialTypeView.text = item.credentialTypeText
binding.issuanceDate.text = item.issuanceDateText

binding.root.setOnClickListener {
credentialClickListener.onCredentialClicked(item.id)
credentialClickCallback.invoke(item.id)
}

// Set other background
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import by.alexandr7035.affinidi_id.R
import by.alexandr7035.affinidi_id.core.extensions.debug
import by.alexandr7035.affinidi_id.core.extensions.navigateSafe
import by.alexandr7035.affinidi_id.databinding.FragmentCredentialsListBinding
import by.alexandr7035.affinidi_id.presentation.credentials_list.model.CredentialListUiModel
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber


@AndroidEntryPoint
class CredentialsListFragment : Fragment(), CredentialClickListener {
class CredentialsListFragment : Fragment() {

private val binding by viewBinding(FragmentCredentialsListBinding::bind)
private val viewModel by viewModels<CredentialsListViewModel>()
Expand All @@ -36,7 +37,12 @@ class CredentialsListFragment : Fragment(), CredentialClickListener {

Timber.debug("onViewCreated creds list")

val adapter = CredentialsAdapter(credentialClickListener = this)
val adapter = CredentialsAdapter(credentialClickCallback = { credentialId ->
findNavController().navigateSafe(CredentialsListFragmentDirections
.actionCredentialsListFragmentToCredentialDetailsFragment(credentialId))
})


val layoutManager = LinearLayoutManager(requireContext())
binding.recycler.layoutManager = layoutManager
binding.recycler.adapter = adapter
Expand Down Expand Up @@ -97,9 +103,4 @@ class CredentialsListFragment : Fragment(), CredentialClickListener {
binding.errorView.root.isVisible = false
viewModel.load()
}

override fun onCredentialClicked(credentialId: String) {
findNavController().navigateSafe(CredentialsListFragmentDirections
.actionCredentialsListFragmentToCredentialDetailsFragment(credentialId))
}
}

0 comments on commit 9ae3a3a

Please sign in to comment.