NotificationsLoadStateViewHolder.kt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2023 Tusky Contributors
  3. *
  4. * This file is a part of Tusky.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under the terms of the
  7. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  11. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. * Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  15. * see <http://www.gnu.org/licenses>.
  16. */
  17. package com.keylesspalace.tusky.components.notifications
  18. import android.view.LayoutInflater
  19. import android.view.ViewGroup
  20. import androidx.core.view.isVisible
  21. import androidx.paging.LoadState
  22. import androidx.recyclerview.widget.RecyclerView
  23. import com.keylesspalace.tusky.R
  24. import com.keylesspalace.tusky.databinding.ItemNotificationsLoadStateFooterViewBinding
  25. import java.net.SocketTimeoutException
  26. /**
  27. * Display the header/footer loading state to the user.
  28. *
  29. * Either:
  30. *
  31. * 1. A page is being loaded, display a progress view, or
  32. * 2. An error occurred, display an error message with a "retry" button
  33. *
  34. * @param retry function to invoke if the user clicks the "retry" button
  35. */
  36. class NotificationsLoadStateViewHolder(
  37. private val binding: ItemNotificationsLoadStateFooterViewBinding,
  38. retry: () -> Unit
  39. ) : RecyclerView.ViewHolder(binding.root) {
  40. init {
  41. binding.retryButton.setOnClickListener { retry.invoke() }
  42. }
  43. fun bind(loadState: LoadState) {
  44. if (loadState is LoadState.Error) {
  45. val ctx = binding.root.context
  46. binding.errorMsg.text = when (loadState.error) {
  47. is SocketTimeoutException -> ctx.getString(R.string.socket_timeout_exception)
  48. // Other exceptions to consider:
  49. // - UnknownHostException, default text is:
  50. // Unable to resolve "%s": No address associated with hostname
  51. else -> loadState.error.localizedMessage
  52. }
  53. }
  54. binding.progressBar.isVisible = loadState is LoadState.Loading
  55. binding.retryButton.isVisible = loadState is LoadState.Error
  56. binding.errorMsg.isVisible = loadState is LoadState.Error
  57. }
  58. companion object {
  59. fun create(parent: ViewGroup, retry: () -> Unit): NotificationsLoadStateViewHolder {
  60. val binding = ItemNotificationsLoadStateFooterViewBinding.inflate(
  61. LayoutInflater.from(parent.context),
  62. parent,
  63. false
  64. )
  65. return NotificationsLoadStateViewHolder(binding, retry)
  66. }
  67. }
  68. }