From 852b2d557947aff0ddcbd4a468d6857e9c687ce9 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 23 Sep 2019 12:07:52 +0200 Subject: [PATCH] Added swipe refresh to unified accounts --- .../eu/faircode/email/FragmentAccounts.java | 83 ++++++++++++++++++- app/src/main/res/layout/fragment_accounts.xml | 66 ++++++++------- 2 files changed, 118 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAccounts.java b/app/src/main/java/eu/faircode/email/FragmentAccounts.java index 48b88051a5..1f9d7c8ddc 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccounts.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccounts.java @@ -24,6 +24,7 @@ import android.animation.ValueAnimator; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; @@ -44,8 +45,10 @@ import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.google.android.material.snackbar.Snackbar; import java.util.ArrayList; import java.util.List; @@ -55,6 +58,8 @@ public class FragmentAccounts extends FragmentBase { private boolean cards; + private ViewGroup view; + private SwipeRefreshLayout swipeRefresh; private RecyclerView rvAccount; private ContentLoadingProgressBar pbWait; private Group grpReady; @@ -82,9 +87,10 @@ public class FragmentAccounts extends FragmentBase { setSubtitle(R.string.title_list_accounts); setHasOptionsMenu(true); - View view = inflater.inflate(R.layout.fragment_accounts, container, false); + view = (ViewGroup) inflater.inflate(R.layout.fragment_accounts, container, false); // Get controls + swipeRefresh = view.findViewById(R.id.swipeRefresh); rvAccount = view.findViewById(R.id.rvAccount); pbWait = view.findViewById(R.id.pbWait); grpReady = view.findViewById(R.id.grpReady); @@ -93,6 +99,17 @@ public class FragmentAccounts extends FragmentBase { // Wire controls + int colorPrimary = Helper.resolveColor(getContext(), R.attr.colorPrimary); + swipeRefresh.setColorSchemeColors(Color.WHITE, Color.WHITE, Color.WHITE); + swipeRefresh.setProgressBackgroundColorSchemeColor(colorPrimary); + swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + onSwipeRefresh(); + } + }); + swipeRefresh.setEnabled(!settings); + rvAccount.setHasFixedSize(false); LinearLayoutManager llm = new LinearLayoutManager(getContext()); rvAccount.setLayoutManager(llm); @@ -279,4 +296,68 @@ public class FragmentAccounts extends FragmentBase { super.onPrepareOptionsMenu(menu); } + + private void onSwipeRefresh() { + Bundle args = new Bundle(); + + new SimpleTask() { + @Override + protected void onPostExecute(Bundle args) { + swipeRefresh.setRefreshing(false); + } + + @Override + protected Void onExecute(Context context, Bundle args) { + if (!ConnectionHelper.getNetworkState(context).isSuitable()) + throw new IllegalStateException(context.getString(R.string.title_no_internet)); + + boolean now = true; + + DB db = DB.getInstance(context); + try { + db.beginTransaction(); + + // Unified inbox + List folders = db.folder().getFoldersSynchronizingUnified(null); + for (EntityFolder folder : folders) { + EntityOperation.sync(context, folder.id, true); + + if (folder.account != null) { + EntityAccount account = db.account().getAccount(folder.account); + if (account != null && !"connected".equals(account.state)) + now = false; + } + } + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + if (!now) + throw new IllegalArgumentException(context.getString(R.string.title_no_connection)); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + if (ex instanceof IllegalStateException) { + Snackbar snackbar = Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG); + snackbar.setAction(R.string.title_fix, new View.OnClickListener() { + @Override + public void onClick(View view) { + getContext().startActivity( + new Intent(getContext(), ActivitySetup.class) + .putExtra("tab", "connection")); + } + }); + snackbar.show(); + } else if (ex instanceof IllegalArgumentException) + Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); + else + Helper.unexpectedError(getFragmentManager(), ex); + } + }.execute(this, args, "folders:refresh"); + } } diff --git a/app/src/main/res/layout/fragment_accounts.xml b/app/src/main/res/layout/fragment_accounts.xml index 38d21c1088..e5ed180c62 100644 --- a/app/src/main/res/layout/fragment_accounts.xml +++ b/app/src/main/res/layout/fragment_accounts.xml @@ -6,41 +6,47 @@ android:layout_height="match_parent" tools:context=".ActivitySetup"> - - + - + - - + + + + +