Add: presentation layer

This commit is contained in:
Carlos Martinez
2021-06-17 13:26:48 -04:00
parent 4e53225a2f
commit 0e1a9850ed
13 changed files with 317 additions and 10 deletions

View File

@@ -1,4 +1,90 @@
package dev.carlos.shortform.feature
class ShortformFragment {
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import dev.carlos.core.domain.network.RequestError
import dev.carlos.core.domain.network.RequestState
import dev.carlos.core.extensions.gone
import dev.carlos.core.extensions.observeNonNull
import dev.carlos.core.extensions.visible
import dev.carlos.shortform.R
import dev.carlos.shortform.data.models.ShortformModel
import dev.carlos.shortform.databinding.FragmentShortformDefinitionBinding
import dev.carlos.shortform.viewmodels.ShortformViewmodel
import dev.carlos.shortform.widgets.LongformAdapter
import org.koin.androidx.viewmodel.ext.android.viewModel
class ShortformFragment : Fragment() {
private val viewModel: ShortformViewmodel by viewModel()
private val listAdapter by lazy { LongformAdapter() }
private lateinit var binding: FragmentShortformDefinitionBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.fragment_shortform_definition, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setBinding(view)
setRecycler()
setObservers()
}
private fun setBinding(view: View) {
binding = FragmentShortformDefinitionBinding.bind(view)
}
private fun setObservers() {
viewModel.acronymDefinition.observeNonNull(this) {
handleResult(it)
}
}
private fun handleResult(state: RequestState) {
when (state) {
is RequestState.Success<*> -> handleSuccess(state.data as ShortformModel)
is RequestState.Empty -> handleEmpty()
is RequestState.Error -> handleError(state.type)
is RequestState.Loading -> handleLoading()
}
}
private fun handleLoading() {
binding.shortformDefinitionLoading.visible()
binding.shortformDefinitionError.gone()
}
private fun handleSuccess(shortform: ShortformModel) {
binding.shortformDefinitionError.gone()
binding.shortformDefinitionLoading.gone()
listAdapter.addAll(shortform.results)
}
private fun handleEmpty() {
binding.shortformDefinitionErrorLabel.text = getString(R.string.shortform_definition_no_definition)
binding.shortformDefinitionError.visible()
binding.shortformDefinitionLoading.gone()
}
private fun handleError(error: RequestError) {
binding.shortformDefinitionErrorLabel.text = getString(error.message)
binding.shortformDefinitionError.visible()
binding.shortformDefinitionLoading.gone()
}
private fun setRecycler() {
binding.shortformDefinitionRecycler.apply {
layoutManager = LinearLayoutManager(context)
adapter = listAdapter.apply {
onItemClick = {
Toast.makeText(context, it.value, Toast.LENGTH_SHORT).show()
}
}
}
}
}

View File

@@ -0,0 +1,60 @@
package dev.carlos.shortform.widgets
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import dev.carlos.core.extensions.autoNotify
import dev.carlos.core.extensions.capitalize
import dev.carlos.shortform.R
import dev.carlos.shortform.data.models.ShortformModel
import dev.carlos.shortform.databinding.ItemLongformCardBinding
import kotlin.properties.Delegates
class LongformAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var items by Delegates.observable(emptyList<ShortformModel.LongformModel>()) { _, oldList, newList ->
autoNotify(oldList, newList) { old, new -> old.value == new.value }
notifyDataSetChanged()
}
var onItemClick: (ShortformModel.LongformModel) -> Unit = { }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =
LongformViewHolder(
LayoutInflater
.from(parent.context)
.inflate(R.layout.item_longform_card, parent, false),
onItemClick
)
fun getItem(position: Int) = items[position]
fun addAll(list: List<ShortformModel.LongformModel>) {
items = list
}
override fun getItemCount() = items.size
override fun onBindViewHolder(viewholder: RecyclerView.ViewHolder, position: Int) =
when (viewholder) {
is LongformViewHolder -> viewholder.bind(items[position])
else -> throw NoWhenBranchMatchedException("Undefined viewholder")
}
}
class LongformViewHolder(
private val view: View,
private val onItemClick: (ShortformModel.LongformModel) -> Unit
) : RecyclerView.ViewHolder(view) {
private val binding = ItemLongformCardBinding.bind(view)
fun bind(longform: ShortformModel.LongformModel) = with(view) {
binding.itemLongformName.text = longform.value.capitalize()
binding.itemLongformSince.text = view.resources.getString(R.string.item_longform_since, longform.since)
binding.itemLongformContainer.setOnClickListener {
onItemClick(longform)
}
}
}

View File

@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="@string/shortform_definition_search"
android:textSize="18sp"
android:autofillHints="no"
android:id="@+id/shortform_definition_search_field"
android:layout_weight="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/shortform_definition_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/shortform_definition_search_field"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/shortform_definition_error"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@id/shortform_definition_search_field"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<TextView
android:id="@+id/shortform_definition_error_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Error"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:visibility="gone"
android:id="@+id/shortform_definition_loading"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/white"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@id/shortform_definition_search_field"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<ProgressBar
android:id="@+id/shortform_definition_loading_bar"
android:indeterminate="true"
android:indeterminateBehavior="repeat"
android:layout_width="36dp"
android:layout_height="36dp"
/>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_longform_container"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/bg_card"
android:elevation="2dp"
android:orientation="vertical">
<TextView
android:id="@+id/item_longform_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:textSize="18sp"
android:textStyle="bold"
android:maxLines="1"
android:ellipsize="end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Adenovirus receptor" />
<TextView
android:id="@+id/item_longform_since"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@+id/item_longform_name"
app:layout_constraintTop_toBottomOf="@+id/item_longform_name"
tools:text="Since: 1997" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/shortform_navigation"
app:startDestination="@id/shortformFragment"
>
<fragment
android:id="@+id/shortformFragment"
android:name="dev.carlos.shortform.feature.ShortformFragment"
tools:layout="@layout/fragment_shortform_definition"
android:label="Acronyms"
/>
</navigation>

View File

@@ -1,3 +1,5 @@
<resources>
<string name="app_name">acronyms</string>
<string name="item_longform_since">Desde %1$d</string>
<string name="shortform_definition_search">Buscar acrónimo</string>
<string name="shortform_definition_no_definition">No existe una definición</string>
</resources>