From cf50d0563fcbf0741cd70c927b583de47188987d Mon Sep 17 00:00:00 2001 From: UlrichKu Date: Thu, 30 Mar 2023 21:23:08 +0200 Subject: [PATCH] 3416: Call the list activity when list list empty (#3426) * 3416: Call the list activity when empty; show failure on loading * 3416: Revert include grouping * 3416: Remove faulty include after merge * 3416: Added a list loading progress * 3416: Add proper padding to progress * 3416: Show a text if there are no lists, do not change dialog title * 3416: Center things in layout * 3416: Appease linter (?) * 3416: Do not hide manage lists button * 3416: Use ThemeUtils --- .../tusky/TabPreferenceActivity.kt | 80 ++++++++++++++++--- app/src/main/res/values-de/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 3 files changed, 74 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt b/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt index f02914c91..8ad35fe07 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt @@ -15,11 +15,16 @@ package com.keylesspalace.tusky +import android.content.Intent import android.graphics.Color import android.os.Bundle import android.util.Log +import android.view.Gravity import android.view.View import android.widget.FrameLayout +import android.widget.LinearLayout +import android.widget.ProgressBar +import android.widget.TextView import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.AppCompatEditText @@ -33,6 +38,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.transition.TransitionManager import at.connyduck.calladapter.networkresult.fold import at.connyduck.sparkbutton.helpers.Utils +import com.google.android.material.snackbar.Snackbar import com.google.android.material.transition.MaterialArcMotion import com.google.android.material.transition.MaterialContainerTransform import com.keylesspalace.tusky.adapter.ItemInteractionListener @@ -43,10 +49,16 @@ import com.keylesspalace.tusky.appstore.MainTabsChangedEvent import com.keylesspalace.tusky.databinding.ActivityTabPreferenceBinding import com.keylesspalace.tusky.di.Injectable import com.keylesspalace.tusky.network.MastodonApi +import com.keylesspalace.tusky.util.getDimension +import com.keylesspalace.tusky.util.hide +import com.keylesspalace.tusky.util.show import com.keylesspalace.tusky.util.unsafeLazy import com.keylesspalace.tusky.util.viewBinding import com.keylesspalace.tusky.util.visible +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import java.util.regex.Pattern import javax.inject.Inject @@ -261,19 +273,30 @@ class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListene private fun showSelectListDialog() { val adapter = ListSelectionAdapter(this) - lifecycleScope.launch { - mastodonApi.getLists().fold( - { lists -> - adapter.addAll(lists) - }, - { throwable -> - Log.e("TabPreferenceActivity", "failed to load lists", throwable) - } - ) - } - AlertDialog.Builder(this) + val statusLayout = LinearLayout(this) + statusLayout.gravity = Gravity.CENTER + val progress = ProgressBar(this) + val preferredPadding = getDimension(this, androidx.appcompat.R.attr.dialogPreferredPadding) + progress.setPadding(preferredPadding, 0, preferredPadding, 0) + progress.visible(false) + + val noListsText = TextView(this) + noListsText.setPadding(preferredPadding, 0, preferredPadding, 0) + noListsText.text = getText(R.string.select_list_empty) + noListsText.visible(false) + + statusLayout.addView(progress) + statusLayout.addView(noListsText) + + val dialogBuilder = AlertDialog.Builder(this) .setTitle(R.string.select_list_title) + .setNeutralButton(R.string.select_list_manage) { _, _ -> + val listIntent = Intent(applicationContext, ListsActivity::class.java) + startActivity(listIntent) + } + .setNegativeButton(android.R.string.cancel, null) + .setView(statusLayout) .setAdapter(adapter) { _, position -> val list = adapter.getItem(position) val newTab = createTabDataFromId(LIST, listOf(list!!.id, list.title)) @@ -282,7 +305,40 @@ class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListene updateAvailableTabs() saveTabs() } - .show() + + val showProgressBarJob = getProgressBarJob(progress, 500) + showProgressBarJob.start() + + val dialog = dialogBuilder.show() + + lifecycleScope.launch { + mastodonApi.getLists().fold( + { lists -> + showProgressBarJob.cancel() + adapter.addAll(lists) + if (lists.isEmpty()) { + noListsText.show() + } + }, + { throwable -> + dialog.hide() + Log.e("TabPreferenceActivity", "failed to load lists", throwable) + Snackbar.make(binding.root, R.string.error_list_load, Snackbar.LENGTH_LONG).show() + } + ) + } + } + + private fun getProgressBarJob(progressView: View, delayMs: Long) = this.lifecycleScope.launch( + start = CoroutineStart.LAZY + ) { + try { + delay(delayMs) + progressView.show() + awaitCancellation() + } finally { + progressView.hide() + } } private fun validateHashtag(input: CharSequence?): Boolean { diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 4db251536..1457cfda4 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -431,6 +431,9 @@ Angetrieben durch Tusky Als Lesezeichen gespeichert Liste auswählen + Du hast noch keine Listen + Listen + Fehler beim Laden der Listen Liste Fehler beim Nachschlagen von %s Du hast keine Entwürfe. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 89be8d2c0..8aa90ccaf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -603,6 +603,9 @@ Hashtag without # Hashtags Select list + You have no lists, yet + Manage lists + Error loading lists List Clear Filter