Show hourglass when busy

This commit is contained in:
M66B 2019-06-09 13:11:33 +02:00
parent ed29b6e29b
commit 9382b98a63
4 changed files with 77 additions and 6 deletions

View File

@ -21,9 +21,11 @@ package eu.faircode.email;
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
@ -59,6 +61,7 @@ import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
@ -120,6 +123,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private ImageButton ibHintSwipe;
private ImageButton ibHintSelect;
private TextView tvNoEmail;
private ImageView ivBusy;
private FixedRecyclerView rvMessage;
private SeekBar seekBar;
private ImageButton ibDown;
@ -168,6 +172,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
private boolean manual = false;
private Integer lastUnseen = null;
private boolean swiping = false;
private int busy = 0;
private AdapterMessage adapter;
@ -265,6 +270,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
ibHintSwipe = view.findViewById(R.id.ibHintSwipe);
ibHintSelect = view.findViewById(R.id.ibHintSelect);
tvNoEmail = view.findViewById(R.id.tvNoEmail);
ivBusy = view.findViewById(R.id.ivBusy);
rvMessage = view.findViewById(R.id.rvMessage);
seekBar = view.findViewById(R.id.seekBar);
ibDown = view.findViewById(R.id.ibDown);
@ -749,9 +755,40 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
updateSwipeRefresh();
ivBusy.setVisibility(View.GONE);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
IntentFilter iff = new IntentFilter();
iff.addAction(SimpleTask.ACTION_TASK_COUNT);
lbm.registerReceiver(receiver, iff);
return view;
}
@Override
public void onDestroyView() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
lbm.unregisterReceiver(receiver);
super.onDestroyView();
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
busy = intent.getIntExtra("count", 0);
if (busy == 0)
ivBusy.setVisibility(View.GONE);
else
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (busy > 0)
ivBusy.setVisibility(View.VISIBLE);
}
}, 1500);
}
};
@Override
public void onDestroy() {
super.onDestroy();

View File

@ -20,6 +20,7 @@ package eu.faircode.email;
*/
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
@ -31,6 +32,7 @@ import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.util.ArrayList;
import java.util.List;
@ -47,6 +49,8 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private static final ExecutorService executor = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(), Helper.backgroundThreadFactory);
static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT";
public void execute(Context context, LifecycleOwner owner, @NonNull Bundle args, @NonNull String name) {
run(context, owner, args, name);
}
@ -71,10 +75,15 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
final Handler handler = new Handler();
// prevent garbage collection
int count;
synchronized (tasks) {
tasks.add(this);
count = tasks.size();
}
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", count));
try {
onPreExecute(args);
} catch (Throwable ex) {
@ -104,12 +113,12 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
Lifecycle.State state = owner.getLifecycle().getCurrentState();
if (state.equals(Lifecycle.State.DESTROYED)) {
// No delivery
cleanup();
cleanup(context);
} else if (state.isAtLeast(Lifecycle.State.RESUMED)) {
// Inline delivery
Log.i("Deliver task " + name);
deliver();
cleanup();
cleanup(context);
} else
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
@ -118,7 +127,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
Log.i("Resume task " + name);
owner.getLifecycle().removeObserver(this);
deliver();
cleanup();
cleanup(context);
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
@ -126,7 +135,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
// No delivery
Log.i("Destroy task " + name);
owner.getLifecycle().removeObserver(this);
cleanup();
cleanup(context);
}
});
}
@ -154,11 +163,16 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
});
}
private void cleanup() {
private void cleanup(Context context) {
int count;
synchronized (tasks) {
tasks.remove(this);
Log.i("Remaining tasks=" + tasks.size());
count = tasks.size();
}
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", count));
Log.i("Remaining tasks=" + count);
}
protected void onPreExecute(Bundle args) {

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M6,2v6h0.01L6,8.01 10,12l-4,4 0.01,0.01H6V22h12v-5.99h-0.01L18,16l-4,-4 4,-3.99 -0.01,-0.01H18V2H6z"/>
</vector>

View File

@ -143,6 +143,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorHintSelect" />
<ImageView
android:id="@+id/ivBusy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_hourglass_full_24"
app:layout_constraintBottom_toTopOf="@+id/seekBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparatorHintSelect" />
<eu.faircode.email.FixedRecyclerView
android:id="@+id/rvMessage"
android:layout_width="0dp"