Italian translation, loading indicator and empty list message

This commit is contained in:
Daniele 2021-09-05 18:05:16 +02:00
parent 387ea890de
commit 359a78e237
4 changed files with 66 additions and 15 deletions

View File

@ -32,7 +32,7 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
private val rvAdapter = BeerRecyclerAdapter() private val rvAdapter = BeerRecyclerAdapter()
private val repo = BeersRepository() private val repo = BeersRepository()
private var binding: FragmentBeersListBinding? = null private lateinit var binding: FragmentBeersListBinding
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -40,7 +40,7 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
): View? { ): View? {
setHasOptionsMenu(true); setHasOptionsMenu(true);
if (binding == null) { if (!this::binding.isInitialized) {
// Inflate layout // Inflate layout
binding = FragmentBeersListBinding.inflate( binding = FragmentBeersListBinding.inflate(
LayoutInflater.from(container!!.context), LayoutInflater.from(container!!.context),
@ -49,12 +49,12 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
) )
// Register filter buttons listeners // Register filter buttons listeners
binding!!.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) } binding.listFilterSinceBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.SINCE) }
binding!!.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) } binding.listFilterToBt.setOnClickListener { onFilterButtonClicked(FILTERTYPE.TO) }
binding!!.listFilterClear.setOnClickListener { onFilterButtonClicked(FILTERTYPE.CLEAR) } binding.listFilterClear.setOnClickListener { onFilterButtonClicked(FILTERTYPE.CLEAR) }
// Set the adapter // Set the adapter
with(binding!!.list) { with(binding.list) {
layoutManager = LinearLayoutManager(context) layoutManager = LinearLayoutManager(context)
adapter = rvAdapter adapter = rvAdapter
} }
@ -68,18 +68,18 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
// Restore state // Restore state
repo.filter.brewedBefore = savedInstanceState?.getString(INSTANCESTATE_FILTER_TO) repo.filter.brewedBefore = savedInstanceState?.getString(INSTANCESTATE_FILTER_TO)
repo.filter.brewedAfter = savedInstanceState?.getString(INSTANCESTATE_FILTER_SINCE) repo.filter.brewedAfter = savedInstanceState?.getString(INSTANCESTATE_FILTER_SINCE)
binding!!.listFilterDropdown.visibility = binding.listFilterDropdown.visibility =
if (savedInstanceState?.getBoolean(INSTANCESTATE_FILTER_OPEN) == true) View.VISIBLE else View.GONE if (savedInstanceState?.getBoolean(INSTANCESTATE_FILTER_OPEN) == true) View.VISIBLE else View.GONE
// Load first page // Load first page
loadBeers(true) loadBeers(true)
} }
return binding!!.root return binding.root
} }
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
outState.putBoolean(INSTANCESTATE_FILTER_OPEN, binding!!.listFilterDropdown.visibility == View.VISIBLE) outState.putBoolean(INSTANCESTATE_FILTER_OPEN, binding.listFilterDropdown.visibility == View.VISIBLE)
outState.putString(INSTANCESTATE_FILTER_SINCE, repo.filter.brewedAfter) outState.putString(INSTANCESTATE_FILTER_SINCE, repo.filter.brewedAfter)
outState.putString(INSTANCESTATE_FILTER_TO, repo.filter.brewedBefore) outState.putString(INSTANCESTATE_FILTER_TO, repo.filter.brewedBefore)
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
@ -97,10 +97,22 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
/**
* Beers obtained from services callback
* @param beers List of beers
* @param more Whether there are more beers to retrieve
*/
override fun onBeersObtained(beers: List<Beer>, more: Boolean) { override fun onBeersObtained(beers: List<Beer>, more: Boolean) {
binding.listProgress.visibility = View.GONE
rvAdapter.addItems(beers, more) rvAdapter.addItems(beers, more)
binding.listEmpty.visibility =
if (beers.isEmpty() && repo.page == 1) View.VISIBLE else View.GONE
} }
/**
* Unable to obtain beers from services
*/
override fun onFailure() { override fun onFailure() {
Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show()
} }
@ -123,8 +135,8 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
if (type == FILTERTYPE.CLEAR) { if (type == FILTERTYPE.CLEAR) {
repo.filter.clear() repo.filter.clear()
loadBeers(true) loadBeers(true)
binding!!.listFilterSinceBt.setText(R.string.list_filter_brewed_after) binding.listFilterSinceBt.setText(R.string.list_filter_brewed_after)
binding!!.listFilterToBt.setText(R.string.list_filter_brewed_before) binding.listFilterToBt.setText(R.string.list_filter_brewed_before)
return return
} }
@ -133,10 +145,10 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
if (type == FILTERTYPE.SINCE) { if (type == FILTERTYPE.SINCE) {
repo.filter.setBrewedAfter(selMonth, selYear) repo.filter.setBrewedAfter(selMonth, selYear)
binding!!.listFilterSinceBt.setText(repo.filter.brewedAfter) binding.listFilterSinceBt.setText(repo.filter.brewedAfter)
} else { } else {
repo.filter.setBrewedBefore(selMonth, selYear) repo.filter.setBrewedBefore(selMonth, selYear)
binding!!.listFilterToBt.setText(repo.filter.brewedBefore) binding.listFilterToBt.setText(repo.filter.brewedBefore)
} }
// Update list // Update list
@ -168,8 +180,8 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
* Toggles beer filter visibility * Toggles beer filter visibility
*/ */
private fun toggleFilter() { private fun toggleFilter() {
binding!!.listFilterDropdown.visibility = binding.listFilterDropdown.visibility =
if (binding!!.listFilterDropdown.visibility == View.VISIBLE) View.GONE else View.VISIBLE if (binding.listFilterDropdown.visibility == View.VISIBLE) View.GONE else View.VISIBLE
} }
/** /**
@ -178,6 +190,7 @@ class BeersFragment : Fragment(), BeersRepository.Listener, BeerRecyclerAdapter.
*/ */
private fun loadBeers(clear: Boolean) { private fun loadBeers(clear: Boolean) {
if (clear) { if (clear) {
binding.listProgress.visibility = View.VISIBLE
rvAdapter.clear() rvAdapter.clear()
repo.reset() repo.reset()
} }

View File

@ -49,6 +49,22 @@
</LinearLayout> </LinearLayout>
<ProgressBar
android:id="@+id/list_progress"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:indeterminate="true"
android:visibility="gone"/>
<TextView
android:id="@+id/list_empty"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="@string/list_empty"
android:visibility="gone"/>
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/list" android:id="@+id/list"
android:name="it.danieleverducci.subitobeers.BeersFragment" android:name="it.danieleverducci.subitobeers.BeersFragment"

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SubitoBeers</string>
<!-- Beers list -->
<string name="network_error">Impossibile ottenere le birre</string>
<string name="list_filter_brewed_after">Prod. dopo</string>
<string name="list_filter_brewed_before">Prod. prima</string>
<string name="list_empty">Nessuna birra trovata</string>
<!-- Beers detail -->
<!-- Generic placeholders -->
<string name="placeholder_beertitle">Nome della birra</string>
<string name="placeholder_tagline">Lorem ipsum dolor sit amet</string>
<string name="placeholder_firstbrewed">00/0000</string>
<string name="placeholder_description">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tristique consectetur felis vel pretium. Praesent tincidunt vestibulum lacus, molestie rhoncus lacus fermentum vel. Morbi vel pulvinar sapien, sed ultrices arcu. Vivamus gravida nulla vel ex varius, vel consequat dolor tincidunt. Fusce maximus quis mi eget tristique. Aenean sed facilisis libero. Aliquam volutpat posuere fringilla. Praesent in lorem at elit ornare congue et quis quam. Maecenas tempus et purus ac rhoncus. Donec in pulvinar ex. Aliquam ac vehicula dolor. Nam vitae tortor id nibh congue sollicitudin id in nisi. Proin lorem eros, congue at euismod et, malesuada porta nunc. Proin laoreet massa ac mi lacinia, eget tincidunt odio vestibulum. Vestibulum fermentum nunc nunc, eget ultrices nisi tempor non. Curabitur lorem leo, fringilla et pellentesque a, pharetra id magna. </string>
<!-- Menu -->
<string name="action_filter">Filtra</string>
</resources>

View File

@ -5,6 +5,7 @@
<string name="network_error">Unable to retrieve beers from network</string> <string name="network_error">Unable to retrieve beers from network</string>
<string name="list_filter_brewed_after">Brewed after</string> <string name="list_filter_brewed_after">Brewed after</string>
<string name="list_filter_brewed_before">Brewed before</string> <string name="list_filter_brewed_before">Brewed before</string>
<string name="list_empty">No beers found</string>
<!-- Beers detail --> <!-- Beers detail -->