Add progress bars on home screen while data is being fetched.

This commit is contained in:
Antoine POPINEAU 2020-06-21 11:14:31 +02:00 committed by Antoine POPINEAU
parent f7a5d4c2d5
commit 21a545d70a
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
4 changed files with 57 additions and 42 deletions

View File

@ -9,12 +9,12 @@ import com.github.apognu.otter.R
import com.github.apognu.otter.utils.mustNormalizeUrl import com.github.apognu.otter.utils.mustNormalizeUrl
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
import kotlinx.android.synthetic.main.row_dummy.view.* import kotlinx.android.synthetic.main.row_home_media.view.*
class DummyAdapter(val context: Context?, val viewRes: Int = R.layout.row_dummy) : RecyclerView.Adapter<DummyAdapter.ViewHolder>() { class HomeMediaAdapter(val context: Context?, val viewRes: Int = R.layout.row_home_media) : RecyclerView.Adapter<HomeMediaAdapter.ViewHolder>() {
data class DummyItem(val label: String, val cover: String?) data class HomeMediaItem(val label: String, val cover: String?)
var data: List<DummyItem> = listOf() var data: List<HomeMediaItem> = listOf()
override fun getItemCount() = data.size override fun getItemCount() = data.size

View File

@ -7,7 +7,7 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.apognu.otter.R import com.github.apognu.otter.R
import com.github.apognu.otter.adapters.home.DummyAdapter import com.github.apognu.otter.adapters.home.HomeMediaAdapter
import com.github.apognu.otter.repositories.Repository import com.github.apognu.otter.repositories.Repository
import com.github.apognu.otter.repositories.home.RecentlyAddedRepository import com.github.apognu.otter.repositories.home.RecentlyAddedRepository
import com.github.apognu.otter.repositories.home.RecentlyListenedRepository import com.github.apognu.otter.repositories.home.RecentlyListenedRepository
@ -25,10 +25,10 @@ class HomeFragment : Fragment() {
private lateinit var recentlyAddedRepository: RecentlyAddedRepository private lateinit var recentlyAddedRepository: RecentlyAddedRepository
private lateinit var recentlyListenedRepository: RecentlyListenedRepository private lateinit var recentlyListenedRepository: RecentlyListenedRepository
private lateinit var tagsAdapter: DummyAdapter private lateinit var tagsAdapter: HomeMediaAdapter
private lateinit var recentlyAddedAdapter: DummyAdapter private lateinit var recentlyAddedAdapter: HomeMediaAdapter
private lateinit var recentlyListenedAdapter: DummyAdapter private lateinit var recentlyListenedAdapter: HomeMediaAdapter
private lateinit var dummyAdapter: DummyAdapter private lateinit var randomAdapter: HomeMediaAdapter
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -37,10 +37,10 @@ class HomeFragment : Fragment() {
recentlyAddedRepository = RecentlyAddedRepository(context) recentlyAddedRepository = RecentlyAddedRepository(context)
recentlyListenedRepository = RecentlyListenedRepository(context) recentlyListenedRepository = RecentlyListenedRepository(context)
tagsAdapter = DummyAdapter(context, R.layout.row_tag) tagsAdapter = HomeMediaAdapter(context, R.layout.row_tag)
recentlyAddedAdapter = DummyAdapter(context) recentlyAddedAdapter = HomeMediaAdapter(context)
recentlyListenedAdapter = DummyAdapter(context) recentlyListenedAdapter = HomeMediaAdapter(context)
dummyAdapter = DummyAdapter(context) randomAdapter = HomeMediaAdapter(context)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -58,7 +58,7 @@ class HomeFragment : Fragment() {
} }
random.apply { random.apply {
adapter = dummyAdapter adapter = randomAdapter
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
} }
@ -72,33 +72,37 @@ class HomeFragment : Fragment() {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
} }
playlists.apply {
adapter = dummyAdapter
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
}
refresh() refresh()
} }
private fun refresh() { private fun refresh() {
tagsRepository.fetch(Repository.Origin.Network.origin).untilNetwork(IO) {data, _, _ -> tagsRepository.fetch(Repository.Origin.Network.origin).untilNetwork(IO) {data, _, _ ->
GlobalScope.launch(Main) { GlobalScope.launch(Main) {
tagsAdapter.data = data.map { DummyAdapter.DummyItem(it.name, null) } tagsAdapter.data = data.map { HomeMediaAdapter.HomeMediaItem(it.name, null) }
tagsAdapter.notifyDataSetChanged() tagsAdapter.notifyDataSetChanged()
tags_loader.visibility = View.GONE
tags.visibility = View.VISIBLE
} }
} }
recentlyListenedRepository.fetch(Repository.Origin.Network.origin).untilNetwork(IO) { data, _, _ -> recentlyListenedRepository.fetch(Repository.Origin.Network.origin).untilNetwork(IO) { data, _, _ ->
GlobalScope.launch(Main) { GlobalScope.launch(Main) {
recentlyListenedAdapter.data = data.map { DummyAdapter.DummyItem(it.track.title, it.track.album.cover.original) } recentlyListenedAdapter.data = data.map { HomeMediaAdapter.HomeMediaItem(it.track.title, it.track.album.cover.original) }
recentlyListenedAdapter.notifyDataSetChanged() recentlyListenedAdapter.notifyDataSetChanged()
recently_listened_loader.visibility = View.GONE
recently_listened.visibility = View.VISIBLE
} }
} }
recentlyAddedRepository.fetch(Repository.Origin.Network.origin).untilNetwork(IO) { data, _, _ -> recentlyAddedRepository.fetch(Repository.Origin.Network.origin).untilNetwork(IO) { data, _, _ ->
GlobalScope.launch(Main) { GlobalScope.launch(Main) {
recentlyAddedAdapter.data = data.map { DummyAdapter.DummyItem(it.title, it.album.cover.original) } recentlyAddedAdapter.data = data.map { HomeMediaAdapter.HomeMediaItem(it.title, it.album.cover.original) }
recentlyAddedAdapter.notifyDataSetChanged() recentlyAddedAdapter.notifyDataSetChanged()
recently_added_loader.visibility = View.GONE
recently_added.visibility = View.VISIBLE
} }
} }
} }

View File

@ -19,12 +19,19 @@
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:text="Tags" /> android:text="Tags" />
<ProgressBar
android:id="@+id/tags_loader"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_horizontal" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/tags" android:id="@+id/tags"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:orientation="horizontal" /> android:orientation="horizontal"
android:visibility="gone" />
<TextView <TextView
style="@style/AppTheme.Title" style="@style/AppTheme.Title"
@ -36,12 +43,19 @@
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:text="Random picks" /> android:text="Random picks" />
<ProgressBar
android:id="@+id/random_loader"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_horizontal" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/random" android:id="@+id/random"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:orientation="horizontal" /> android:orientation="horizontal"
android:visibility="gone" />
<TextView <TextView
style="@style/AppTheme.Title" style="@style/AppTheme.Title"
@ -53,12 +67,19 @@
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:text="Recently listened" /> android:text="Recently listened" />
<ProgressBar
android:id="@+id/recently_listened_loader"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_horizontal" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recently_listened" android:id="@+id/recently_listened"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:orientation="horizontal" /> android:orientation="horizontal"
android:visibility="gone" />
<TextView <TextView
style="@style/AppTheme.Title" style="@style/AppTheme.Title"
@ -70,26 +91,16 @@
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:text="Newly uploaded" /> android:text="Newly uploaded" />
<ProgressBar
android:id="@+id/recently_added_loader"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_horizontal" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recently_added" android:id="@+id/recently_added"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:visibility="gone"
android:layout_marginBottom="16dp"
android:orientation="horizontal" />
<TextView
style="@style/AppTheme.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Playlists and radios" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlists"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:orientation="horizontal" /> android:orientation="horizontal" />