From fc54425aa869d2672a02e475ce3cd547fc157718 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 25 Feb 2019 16:38:10 -0700 Subject: [PATCH] Refactor the static snackbar out of the Domains Activity. https://redmine.stoutner.com/issues/388 --- .../activities/DomainsActivity.java | 25 ++++++++++------- .../fragments/DomainsListFragment.java | 27 ++++++++++++++----- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java index d54499a2..5026e727 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/DomainsActivity.java @@ -59,7 +59,7 @@ import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper; import java.util.Objects; -public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener { +public class DomainsActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener, DomainsListFragment.DismissSnackbarInterface { // `twoPanedMode` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreate()`, `onCreateOptionsMenu()`, and `populateDomainsListView()`. public static boolean twoPanedMode; @@ -69,17 +69,14 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // `deleteMenuItem` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onCreateOptionsMenu()`, `onOptionsItemSelected()`, and `onBackPressed()`. public static MenuItem deleteMenuItem; - // `undoDeleteSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()` and `onBackPressed()`. - public static Snackbar undoDeleteSnackbar; - // `dismissingSnackbar` is public static so it can be accessed from `DomainsListFragment`. It is also used in `onOptionsItemSelected()`. public static boolean dismissingSnackbar; // `closeActivityAfterDismissingSnackbar` is used in `onOptionsItemSelected()`, and `onBackPressed()`. private boolean closeActivityAfterDismissingSnackbar; - // `context` is used in `onCreate()`, `onOptionsItemSelected()`, and `onAddDomain()`. - private Context context; + // The undelete snackbar is used in `onOptionsItemSelected()` and `onBackPressed()`. + private Snackbar undoDeleteSnackbar; // `domainsDatabaseHelper` is used in `onCreate()`, `saveDomainSettings()`, and `onDestroy()`. private static DomainsDatabaseHelper domainsDatabaseHelper; @@ -153,7 +150,6 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo // Populate the class variables. coordinatorLayout = findViewById(R.id.domains_coordinatorlayout); resources = getResources(); - context = this; // `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21. final Toolbar toolbar = findViewById(R.id.domains_toolbar); @@ -169,7 +165,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo actionBar.setDisplayHomeAsUpEnabled(true); // Initialize the database handler. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`. - domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0); + domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0); // Determine if we are in two pane mode. `domain_settings_fragment_container` does not exist on devices with a width less than 900dp. twoPanedMode = (findViewById(R.id.domain_settings_fragment_container) != null); @@ -431,7 +427,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo Cursor undoDeleteDomainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain(); // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`. - CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(context, undoDeleteDomainsCursor, false) { + CursorAdapter undoDeleteDomainsCursorAdapter = new CursorAdapter(getApplicationContext(), undoDeleteDomainsCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate the individual item layout. `false` does not attach it to the root. @@ -759,7 +755,7 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo Cursor domainsCursor = domainsDatabaseHelper.getDomainNameCursorOrderedByDomain(); // Setup `domainsCursorAdapter` with `this` context. `false` disables `autoRequery`. - CursorAdapter domainsCursorAdapter = new CursorAdapter(context, domainsCursor, false) { + CursorAdapter domainsCursorAdapter = new CursorAdapter(getApplicationContext(), domainsCursor, false) { @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // Inflate the individual item layout. `false` does not attach it to the root. @@ -831,6 +827,15 @@ public class DomainsActivity extends AppCompatActivity implements AddDomainDialo } } + @Override + public void dismissSnackbar() { + // Dismiss the undo delete snackbar if it is shown. + if (undoDeleteSnackbar != null && undoDeleteSnackbar.isShown()) { + // Dismiss the snackbar. + undoDeleteSnackbar.dismiss(); + } + } + @Override public void onDestroy() { // Close the domains database helper. diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java index b28289b0..41613170 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/DomainsListFragment.java @@ -19,6 +19,7 @@ package com.stoutner.privacybrowser.fragments; +import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -37,6 +38,22 @@ import com.stoutner.privacybrowser.activities.DomainsActivity; import com.stoutner.privacybrowser.activities.MainWebViewActivity; public class DomainsListFragment extends Fragment { + // Instantiate the dismiss snackbar interface handle. + private DismissSnackbarInterface dismissSnackbarInterface; + + // Define the public dismiss snackbar interface. + public interface DismissSnackbarInterface { + void dismissSnackbar(); + } + + public void onAttach(Context context) { + // Run the default commands. + super.onAttach(context); + + // Get a handle for the dismiss snackbar interface. + dismissSnackbarInterface = (DismissSnackbarInterface) context; + } + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate `domains_list_fragment`. `false` does not attach it to the root `container`. View domainsListFragmentView = inflater.inflate(R.layout.domains_list_fragment, container, false); @@ -47,16 +64,12 @@ public class DomainsListFragment extends Fragment { // Remove the incorrect lint error below that `.getSupportFragmentManager()` might be null. assert getActivity() != null; - // Get a handle for `supportFragmentManager`. + // Get a handle for the support fragment manager. final FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager(); domainsListView.setOnItemClickListener((AdapterView parent, View view, int position, long id) -> { - // Dismiss `undoDeleteSnackbar` if it is currently displayed (because a domain has just been deleted). - if ((DomainsActivity.undoDeleteSnackbar != null) && (DomainsActivity.undoDeleteSnackbar.isShown())) { - DomainsActivity.dismissingSnackbar = true; - - DomainsActivity.undoDeleteSnackbar.dismiss(); - } + // Dismiss the snackbar if it is visible. + dismissSnackbarInterface.dismissSnackbar(); // Save the current domain settings if operating in two-paned mode and a domain is currently selected. if (DomainsActivity.twoPanedMode && DomainsActivity.deleteMenuItem.isEnabled()) { -- 2.45.2