List with first page

This commit is contained in:
Daniele Verducci su MatissePenguin 2021-08-31 20:20:40 +02:00
parent 25c8cb8a5d
commit eaff3a4965
11 changed files with 152 additions and 147 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
SubitoBeers

View File

@ -1,5 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DesignSurface">
<option name="filePathToZoomLevelMap">
<map>
<entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" />
<entry key="app/src/main/res/layout/fragment_beers_listitem.xml" value="0.5307291666666667" />
</map>
</option>
</component>
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -8,9 +8,9 @@ import android.widget.TextView
import it.danieleverducci.subitobeers.databinding.FragmentBeersListitemBinding
import it.danieleverducci.subitobeers.entities.Beer
class BeerRecyclerAdapter(
private val values: List<Beer>
) : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
private val items: ArrayList<Beer> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -25,20 +25,21 @@ class BeerRecyclerAdapter(
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = values[position]
holder.idView.text = item.name
holder.contentView.text = item.tagline
val item = items[position]
holder.name.text = item.name
holder.descr.text = item.tagline
}
override fun getItemCount(): Int = values.size
fun addItems(ni: List<Beer>) {
items.addAll(ni);
notifyDataSetChanged();
}
override fun getItemCount(): Int = items.size
inner class ViewHolder(binding: FragmentBeersListitemBinding) : RecyclerView.ViewHolder(binding.root) {
val idView: TextView = binding.itemNumber
val contentView: TextView = binding.content
override fun toString(): String {
return super.toString() + " '" + contentView.text + "'"
}
val name: TextView = binding.beerItemName
val descr: TextView = binding.beerItemDescr
}
}

View File

@ -2,28 +2,20 @@ package it.danieleverducci.subitobeers
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import it.danieleverducci.subitobeers.placeholder.PlaceholderContent
import android.widget.Toast
import it.danieleverducci.subitobeers.entities.Beer
/**
* A fragment representing a list of Items.
*/
class BeersFragment : Fragment() {
class BeersFragment : Fragment(), BeersRepository.Listener {
private var columnCount = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
columnCount = it.getInt(ARG_COLUMN_COUNT)
}
}
private val rvAdapter = BeerRecyclerAdapter()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
@ -34,28 +26,28 @@ class BeersFragment : Fragment() {
// Set the adapter
if (view is RecyclerView) {
with(view) {
layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
adapter = BeerRecyclerAdapter(PlaceholderContent.ITEMS)
layoutManager = LinearLayoutManager(context)
adapter = rvAdapter
}
}
return view
}
companion object {
override fun onStart() {
super.onStart()
// TODO: Customize parameter argument names
const val ARG_COLUMN_COUNT = "column-count"
// TODO: Customize parameter initialization
@JvmStatic
fun newInstance(columnCount: Int) =
BeersFragment().apply {
arguments = Bundle().apply {
putInt(ARG_COLUMN_COUNT, columnCount)
}
}
// Load beers from network
val repo = BeersRepository()
repo.listener = this
repo.getBeers(1)
}
override fun onBeersObtained(beers: List<Beer>) {
rvAdapter.addItems(beers)
}
override fun onFailure() {
Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show()
}
}

View File

@ -0,0 +1,42 @@
package it.danieleverducci.subitobeers
import android.util.Log
import android.widget.Toast
import it.danieleverducci.subitobeers.entities.Beer
import it.danieleverducci.subitobeers.networking.RetrofitProvider
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class BeersRepository {
companion object {
val TAG = "BeersRepository"
}
var listener: Listener? = null
fun getBeers(page: Int) {
// Obtain beers
val call = RetrofitProvider.getClient.getBeers(page)
call.enqueue(object: Callback<List<Beer>> {
override fun onResponse(call: Call<List<Beer>>, response: Response<List<Beer>>) {
if (response.body() != null)
listener?.onBeersObtained(response.body()!!)
else
listener?.onFailure()
}
override fun onFailure(call: Call<List<Beer>>, t: Throwable) {
if (t.message != null)
Log.e(TAG, "Unable to obtain beers: ${t.message!!}")
listener?.onFailure()
}
})
}
interface Listener {
fun onBeersObtained(beers: List<Beer>)
fun onFailure()
}
}

View File

@ -20,21 +20,13 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getBeers()
}
private fun getBeers() {
val call: Call<List<Beer>> = RetrofitProvider.getClient.getBeers()
call.enqueue(object : Callback<List<Beer>> {
override fun onResponse(call: Call<List<Beer>>?, response: Response<List<Beer>>?) {
Log.d(TAG, response.toString())
}
override fun onFailure(call: Call<List<Beer>>?, t: Throwable?) {
Toast.makeText(applicationContext, R.string.network_error, Toast.LENGTH_SHORT).show()
}
})
fun showBeerDetail(beer: Beer) {
// TODO
}
}

View File

@ -3,8 +3,11 @@ package it.danieleverducci.subitobeers.networking
import it.danieleverducci.subitobeers.entities.Beer
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query
interface PunkAPI {
@GET("/v2/beers")
fun getBeers(): Call<List<Beer>>
@GET("/v2/beers?per_page=10")
fun getBeers(
@Query("page") page: Int
): Call<List<Beer>>
}

View File

@ -1,57 +0,0 @@
package it.danieleverducci.subitobeers.placeholder
import java.util.ArrayList
import java.util.HashMap
/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
*
* TODO: Replace all uses of this class before publishing your app.
*/
object PlaceholderContent {
/**
* An array of sample (placeholder) items.
*/
val ITEMS: MutableList<PlaceholderItem> = ArrayList()
/**
* A map of sample (placeholder) items, by ID.
*/
val ITEM_MAP: MutableMap<String, PlaceholderItem> = HashMap()
private val COUNT = 25
init {
// Add some sample items.
for (i in 1..COUNT) {
addItem(createPlaceholderItem(i))
}
}
private fun addItem(item: PlaceholderItem) {
ITEMS.add(item)
ITEM_MAP.put(item.id, item)
}
private fun createPlaceholderItem(position: Int): PlaceholderItem {
return PlaceholderItem(position.toString(), "Item " + position, makeDetails(position))
}
private fun makeDetails(position: Int): String {
val builder = StringBuilder()
builder.append("Details about Item: ").append(position)
for (i in 0..position - 1) {
builder.append("\nMore details information here.")
}
return builder.toString()
}
/**
* A placeholder item representing a piece of content.
*/
data class PlaceholderItem(val id: String, val content: String, val details: String) {
override fun toString(): String = content
}
}

View File

@ -6,13 +6,11 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:name="it.danieleverducci.subitobeers.BeersFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.fragment.app.FragmentContainerView>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,20 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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="wrap_content">
<TextView
android:id="@+id/item_number"
<ImageView
android:id="@+id/beer_item_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
android:id="@+id/beer_item_name"
style="@style/Widget.AppCompat.TextView"
android:layout_width="0dp"
android:layout_height="19dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:text="Beer name"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/beer_item_descr"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/beer_item_pic"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/beer_item_descr"
android:layout_width="0dp"
android:layout_height="21dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
android:ellipsize="end"
android:lines="1"
android:maxLines="1"
android:text="Beer description lorem ipsum dolor sit amet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/beer_item_pic"
app:layout_constraintTop_toBottomOf="@+id/beer_item_name" />
</androidx.constraintlayout.widget.ConstraintLayout>