diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..ae523c3 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +SubitoBeers \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 860da66..9596f86 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,14 @@ + + + + diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/BeerRecyclerAdapter.kt b/app/src/main/java/it/danieleverducci/subitobeers/BeerRecyclerAdapter.kt index 0d7e230..c4250cc 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/BeerRecyclerAdapter.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/BeerRecyclerAdapter.kt @@ -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 -) : RecyclerView.Adapter() { +class BeerRecyclerAdapter : RecyclerView.Adapter() { + + private val items: ArrayList = 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) { + 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 } } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/BeersFragment.kt b/app/src/main/java/it/danieleverducci/subitobeers/BeersFragment.kt index 0365ded..5b6b06f 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/BeersFragment.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/BeersFragment.kt @@ -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) { + rvAdapter.addItems(beers) + } + + override fun onFailure() { + Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show() + } + } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt new file mode 100644 index 0000000..5f6caa3 --- /dev/null +++ b/app/src/main/java/it/danieleverducci/subitobeers/BeersRepository.kt @@ -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> { + override fun onResponse(call: Call>, response: Response>) { + if (response.body() != null) + listener?.onBeersObtained(response.body()!!) + else + listener?.onFailure() + } + + override fun onFailure(call: Call>, t: Throwable) { + if (t.message != null) + Log.e(TAG, "Unable to obtain beers: ${t.message!!}") + listener?.onFailure() + } + }) + } + + interface Listener { + fun onBeersObtained(beers: List) + fun onFailure() + } +} \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/MainActivity.kt b/app/src/main/java/it/danieleverducci/subitobeers/MainActivity.kt index fc43fea..3f7be22 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/MainActivity.kt @@ -20,21 +20,13 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - getBeers() } - private fun getBeers() { - val call: Call> = RetrofitProvider.getClient.getBeers() - call.enqueue(object : Callback> { - - override fun onResponse(call: Call>?, response: Response>?) { - Log.d(TAG, response.toString()) - } - - override fun onFailure(call: Call>?, t: Throwable?) { - Toast.makeText(applicationContext, R.string.network_error, Toast.LENGTH_SHORT).show() - } - - }) + fun showBeerDetail(beer: Beer) { + // TODO } + + + + } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt b/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt index bf20371..c26232d 100644 --- a/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt +++ b/app/src/main/java/it/danieleverducci/subitobeers/networking/PunkAPI.kt @@ -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> + @GET("/v2/beers?per_page=10") + fun getBeers( + @Query("page") page: Int + ): Call> } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/subitobeers/placeholder/PlaceholderContent.kt b/app/src/main/java/it/danieleverducci/subitobeers/placeholder/PlaceholderContent.kt deleted file mode 100644 index 3819e3e..0000000 --- a/app/src/main/java/it/danieleverducci/subitobeers/placeholder/PlaceholderContent.kt +++ /dev/null @@ -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 = ArrayList() - - /** - * A map of sample (placeholder) items, by ID. - */ - val ITEM_MAP: MutableMap = 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 - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4fc2444..3158501 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,13 +6,11 @@ android:layout_height="match_parent" tools:context=".MainActivity"> - + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_beers_listitem.xml b/app/src/main/res/layout/fragment_beers_listitem.xml index 1877568..c4116b0 100644 --- a/app/src/main/res/layout/fragment_beers_listitem.xml +++ b/app/src/main/res/layout/fragment_beers_listitem.xml @@ -1,20 +1,54 @@ - + - + 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" /> - \ No newline at end of file + 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" /> + + + + \ No newline at end of file