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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <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"> <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" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </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.databinding.FragmentBeersListitemBinding
import it.danieleverducci.subitobeers.entities.Beer import it.danieleverducci.subitobeers.entities.Beer
class BeerRecyclerAdapter( class BeerRecyclerAdapter : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() {
private val values: List<Beer>
) : RecyclerView.Adapter<BeerRecyclerAdapter.ViewHolder>() { private val items: ArrayList<Beer> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -25,20 +25,21 @@ class BeerRecyclerAdapter(
} }
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = values[position] val item = items[position]
holder.idView.text = item.name holder.name.text = item.name
holder.contentView.text = item.tagline 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) { inner class ViewHolder(binding: FragmentBeersListitemBinding) : RecyclerView.ViewHolder(binding.root) {
val idView: TextView = binding.itemNumber val name: TextView = binding.beerItemName
val contentView: TextView = binding.content val descr: TextView = binding.beerItemDescr
override fun toString(): String {
return super.toString() + " '" + contentView.text + "'"
}
} }
} }

View File

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

View File

@ -3,8 +3,11 @@ package it.danieleverducci.subitobeers.networking
import it.danieleverducci.subitobeers.entities.Beer import it.danieleverducci.subitobeers.entities.Beer
import retrofit2.Call import retrofit2.Call
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.Query
interface PunkAPI { interface PunkAPI {
@GET("/v2/beers") @GET("/v2/beers?per_page=10")
fun getBeers(): Call<List<Beer>> 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" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView <androidx.fragment.app.FragmentContainerView
android:layout_width="wrap_content" android:id="@+id/fragment_container"
android:layout_height="wrap_content" android:name="it.danieleverducci.subitobeers.BeersFragment"
android:text="Hello World!" android:layout_width="match_parent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_height="match_parent">
app:layout_constraintLeft_toLeftOf="parent" </androidx.fragment.app.FragmentContainerView>
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,20 +1,54 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <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">
<ImageView
android:id="@+id/beer_item_pic"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"> 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 <TextView
android:id="@+id/item_number" android:id="@+id/beer_item_name"
android:layout_width="wrap_content" style="@style/Widget.AppCompat.TextView"
android:layout_height="wrap_content" android:layout_width="0dp"
android:layout_margin="@dimen/text_margin" android:layout_height="19dp"
android:textAppearance="?attr/textAppearanceListItem" /> 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 <TextView
android:id="@+id/content" android:id="@+id/beer_item_descr"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="21dp"
android:layout_margin="@dimen/text_margin" android:layout_marginStart="20dp"
android:textAppearance="?attr/textAppearanceListItem" /> android:layout_marginEnd="10dp"
</LinearLayout> 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>