From 9087b94e6e4fa791bdd7058b517e730865bc6508 Mon Sep 17 00:00:00 2001 From: marunjar Date: Thu, 26 Oct 2023 13:25:29 +0200 Subject: [PATCH] spinner is spinning - mitigates #2060 - hide spinner if `Searcher` is cancelled - refactor `displayLoader` - cancel running animation if necessary to not interfer with new state - always set proper alpha value --- .../java/fr/neamar/kiss/MainActivity.java | 53 ++++++++++--------- .../fr/neamar/kiss/searcher/Searcher.java | 23 ++++++-- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/fr/neamar/kiss/MainActivity.java b/app/src/main/java/fr/neamar/kiss/MainActivity.java index cfdc92790..a0b96ac46 100644 --- a/app/src/main/java/fr/neamar/kiss/MainActivity.java +++ b/app/src/main/java/fr/neamar/kiss/MainActivity.java @@ -626,32 +626,37 @@ public void displayClearOnInput() { } } - public void displayLoader(Boolean display) { - int animationDuration = getResources().getInteger( - android.R.integer.config_longAnimTime); + public void displayLoader(boolean display) { + if (!display) { + // Do not display animation if launcher button is already visible + if (launcherButton.getVisibility() != View.VISIBLE) { + launcherButton.setVisibility(View.VISIBLE); - // Do not display animation if launcher button is already visible - if (!display && launcherButton.getVisibility() == View.INVISIBLE) { - launcherButton.setVisibility(View.VISIBLE); - - // Animate transition from loader to launch button + int animationDuration = getResources().getInteger( + android.R.integer.config_longAnimTime); + + // Animate transition from loader to launch button + launcherButton.animate() + .alpha(1f) + .setDuration(animationDuration) + .setListener(null); + loaderSpinner.animate() + .alpha(0f) + .setDuration(animationDuration) + .setListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + loaderSpinner.setVisibility(View.GONE); + } + }); + } + } else { + launcherButton.animate().cancel(); launcherButton.setAlpha(0); - launcherButton.animate() - .alpha(1f) - .setDuration(animationDuration) - .setListener(null); - loaderSpinner.animate() - .alpha(0f) - .setDuration(animationDuration) - .setListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - loaderSpinner.setVisibility(View.GONE); - loaderSpinner.setAlpha(1); - } - }); - } else if (display) { launcherButton.setVisibility(View.INVISIBLE); + + loaderSpinner.animate().cancel(); + loaderSpinner.setAlpha(1); loaderSpinner.setVisibility(View.VISIBLE); } } @@ -660,7 +665,7 @@ public void onFavoriteChange() { forwarderManager.onFavoriteChange(); } - public void displayKissBar(Boolean display) { + public void displayKissBar(boolean display) { this.displayKissBar(display, true); } diff --git a/app/src/main/java/fr/neamar/kiss/searcher/Searcher.java b/app/src/main/java/fr/neamar/kiss/searcher/Searcher.java index 9a561c94e..bbc48f197 100644 --- a/app/src/main/java/fr/neamar/kiss/searcher/Searcher.java +++ b/app/src/main/java/fr/neamar/kiss/searcher/Searcher.java @@ -78,7 +78,7 @@ protected void onPreExecute() { displayActivityLoader(); } - void displayActivityLoader() { + protected void displayActivityLoader() { MainActivity activity = activityWeakReference.get(); if (activity == null) return; @@ -86,14 +86,22 @@ void displayActivityLoader() { activity.displayLoader(true); } + private void hideActivityLoader(MainActivity activity) { + // Loader should still be displayed until all the providers have finished loading + activity.displayLoader(!KissApplication.getApplication(activity).getDataHandler().allProvidersHaveLoaded); + } + @Override protected void onPostExecute(Void param) { + if (isCancelled()) { + return; + } + MainActivity activity = activityWeakReference.get(); if (activity == null) return; - // Loader should still be displayed until all the providers have finished loading - activity.displayLoader(!KissApplication.getApplication(activity).getDataHandler().allProvidersHaveLoaded); + hideActivityLoader(activity); if (this.processedPojos.isEmpty()) { activity.adapter.clear(); @@ -120,6 +128,15 @@ protected void onPostExecute(Void param) { Log.v(TAG, "Time to run query `" + query + "` on " + getClass().getSimpleName() + " to completion: " + time + "ms"); } + @Override + protected void onCancelled(Void unused) { + MainActivity activity = activityWeakReference.get(); + if (activity == null) + return; + + hideActivityLoader(activity); + } + public void setRefresh(boolean refresh) { isRefresh = refresh; }