From 284f2c7ff945b7c644e5c247106618967d392fb3 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sun, 9 Oct 2016 11:36:52 -0400 Subject: [PATCH] refactoring search suggestions adapter --- .../lightning/activity/BrowserActivity.java | 6 ++-- .../browser/lightning/app/AppComponent.java | 4 +-- .../lightning/search/BaseSuggestionsTask.java | 3 +- .../lightning/search/DuckSuggestionsTask.java | 4 +-- .../search/GoogleSuggestionsTask.java | 4 +-- ...ggestions.java => SuggestionsAdapter.java} | 34 +++++++++---------- .../lightning/search/SuggestionsManager.java | 6 ++-- 7 files changed, 31 insertions(+), 30 deletions(-) rename app/src/main/java/acr/browser/lightning/search/{Suggestions.java => SuggestionsAdapter.java} (92%) diff --git a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java index dcf0e95..4a025a4 100644 --- a/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java +++ b/app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java @@ -113,7 +113,7 @@ import acr.browser.lightning.fragment.BookmarksFragment; import acr.browser.lightning.fragment.TabsFragment; import acr.browser.lightning.interpolator.BezierDecelerateInterpolator; import acr.browser.lightning.receiver.NetworkReceiver; -import acr.browser.lightning.search.Suggestions; +import acr.browser.lightning.search.SuggestionsAdapter; import acr.browser.lightning.utils.DrawableUtils; import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.utils.ThemeUtils; @@ -161,7 +161,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements private View mCustomView; // Adapter - private Suggestions mSuggestionsAdapter; + private SuggestionsAdapter mSuggestionsAdapter; // Callback private CustomViewCallback mCustomViewCallback; @@ -1503,7 +1503,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements */ private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) { - mSuggestionsAdapter = new Suggestions(this, mDarkTheme, isIncognito()); + mSuggestionsAdapter = new SuggestionsAdapter(this, mDarkTheme, isIncognito()); getUrl.setThreshold(1); getUrl.setDropDownWidth(-1); diff --git a/app/src/main/java/acr/browser/lightning/app/AppComponent.java b/app/src/main/java/acr/browser/lightning/app/AppComponent.java index 5cec4f2..c28da57 100644 --- a/app/src/main/java/acr/browser/lightning/app/AppComponent.java +++ b/app/src/main/java/acr/browser/lightning/app/AppComponent.java @@ -17,7 +17,7 @@ import acr.browser.lightning.fragment.DebugSettingsFragment; import acr.browser.lightning.fragment.LightningPreferenceFragment; import acr.browser.lightning.fragment.PrivacySettingsFragment; import acr.browser.lightning.fragment.TabsFragment; -import acr.browser.lightning.search.Suggestions; +import acr.browser.lightning.search.SuggestionsAdapter; import acr.browser.lightning.utils.AdBlock; import acr.browser.lightning.utils.ProxyUtils; import acr.browser.lightning.view.LightningView; @@ -68,6 +68,6 @@ public interface AppComponent { void inject(DebugSettingsFragment fragment); - void inject(Suggestions suggestions); + void inject(SuggestionsAdapter suggestionsAdapter); } diff --git a/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java b/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java index 61494e6..67fad33 100644 --- a/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java +++ b/app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java @@ -32,6 +32,7 @@ abstract class BaseSuggestionsTask { private static final String TAG = BaseSuggestionsTask.class.getSimpleName(); + static final int MAX_RESULTS = 5; private static final long INTERVAL_DAY = TimeUnit.DAYS.toMillis(1); private static final String DEFAULT_LANGUAGE = "en"; @Nullable private static String sLanguage; @@ -104,7 +105,7 @@ abstract class BaseSuggestionsTask { @NonNull private File downloadSuggestionsForQuery(@NonNull String query, String language, @NonNull Application app) { String queryUrl = getQueryUrl(query, language); - File cacheFile = new File(app.getCacheDir(), queryUrl.hashCode() + Suggestions.CACHE_FILE_TYPE); + File cacheFile = new File(app.getCacheDir(), queryUrl.hashCode() + SuggestionsAdapter.CACHE_FILE_TYPE); if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) { return cacheFile; } diff --git a/app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java b/app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java index c2b8f02..cec467f 100644 --- a/app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java +++ b/app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java @@ -13,7 +13,7 @@ import acr.browser.lightning.R; import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.utils.FileUtils; -public final class DuckSuggestionsTask extends BaseSuggestionsTask { +final class DuckSuggestionsTask extends BaseSuggestionsTask { private static final String ENCODING = "UTF-8"; @NonNull private final String mSearchSubtitle; @@ -41,7 +41,7 @@ public final class DuckSuggestionsTask extends BaseSuggestionsTask { results.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"', suggestion, R.drawable.ic_search)); counter++; - if (counter >= 5) { + if (counter >= MAX_RESULTS) { break; } } diff --git a/app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java b/app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java index 0e6415c..d68da9a 100644 --- a/app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java +++ b/app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java @@ -15,7 +15,7 @@ import java.util.List; import acr.browser.lightning.R; import acr.browser.lightning.database.HistoryItem; -public class GoogleSuggestionsTask extends BaseSuggestionsTask { +class GoogleSuggestionsTask extends BaseSuggestionsTask { private static final String ENCODING = "ISO-8859-1"; @Nullable private static XmlPullParser sXpp; @@ -47,7 +47,7 @@ public class GoogleSuggestionsTask extends BaseSuggestionsTask { results.add(new HistoryItem(mSearchSubtitle + " \"" + suggestion + '"', suggestion, R.drawable.ic_search)); counter++; - if (counter >= 5) { + if (counter >= MAX_RESULTS) { break; } } diff --git a/app/src/main/java/acr/browser/lightning/search/Suggestions.java b/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java similarity index 92% rename from app/src/main/java/acr/browser/lightning/search/Suggestions.java rename to app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java index d567310..0cac61f 100644 --- a/app/src/main/java/acr/browser/lightning/search/Suggestions.java +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java @@ -43,11 +43,11 @@ import com.anthonycr.bonsai.Subscriber; import acr.browser.lightning.utils.ThemeUtils; -public class Suggestions extends BaseAdapter implements Filterable { +public class SuggestionsAdapter extends BaseAdapter implements Filterable { private static final Scheduler FILTER_SCHEDULER = Schedulers.newSingleThreadedScheduler(); - public static final String CACHE_FILE_TYPE = ".sgg"; + static final String CACHE_FILE_TYPE = ".sgg"; private final List mFilteredList = new ArrayList<>(5); @@ -74,7 +74,7 @@ public class Suggestions extends BaseAdapter implements Filterable { @NonNull private final Context mContext; private PreferenceManager.Suggestion mSuggestionChoice; - public Suggestions(@NonNull Context context, boolean dark, boolean incognito) { + public SuggestionsAdapter(@NonNull Context context, boolean dark, boolean incognito) { super(); BrowserApp.getAppComponent().inject(this); mContext = context; @@ -123,7 +123,7 @@ public class Suggestions extends BaseAdapter implements Filterable { private static class SuggestionHolder { - public SuggestionHolder(@NonNull View view) { + SuggestionHolder(@NonNull View view) { mTitle = (TextView) view.findViewById(R.id.title); mUrl = (TextView) view.findViewById(R.id.url); mImage = (ImageView) view.findViewById(R.id.suggestionIcon); @@ -319,50 +319,50 @@ public class Suggestions extends BaseAdapter implements Filterable { private static class SearchFilter extends Filter { - @NonNull private final Suggestions mSuggestions; + @NonNull private final SuggestionsAdapter mSuggestionsAdapter; - public SearchFilter(@NonNull Suggestions suggestions) { - mSuggestions = suggestions; + SearchFilter(@NonNull SuggestionsAdapter suggestionsAdapter) { + mSuggestionsAdapter = suggestionsAdapter; } @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); if (constraint == null || constraint.length() == 0) { - mSuggestions.clearSuggestions(); + mSuggestionsAdapter.clearSuggestions(); return results; } String query = constraint.toString().toLowerCase(Locale.getDefault()).trim(); - if (mSuggestions.shouldRequestNetwork() && !SuggestionsManager.isRequestInProgress()) { - mSuggestions.getSuggestionsForQuery(query) + if (mSuggestionsAdapter.shouldRequestNetwork() && !SuggestionsManager.isRequestInProgress()) { + mSuggestionsAdapter.getSuggestionsForQuery(query) .subscribeOn(Schedulers.worker()) .observeOn(Schedulers.main()) .subscribe(new OnSubscribe>() { @Override public void onNext(@Nullable List item) { - mSuggestions.combineResults(null, null, item); + mSuggestionsAdapter.combineResults(null, null, item); } }); } - mSuggestions.getBookmarksForQuery(query) + mSuggestionsAdapter.getBookmarksForQuery(query) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.main()) .subscribe(new OnSubscribe>() { @Override public void onNext(@Nullable List item) { - mSuggestions.combineResults(item, null, null); + mSuggestionsAdapter.combineResults(item, null, null); } }); - mSuggestions.getHistoryForQuery(query) + mSuggestionsAdapter.getHistoryForQuery(query) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.main()) .subscribe(new OnSubscribe>() { @Override public void onNext(@Nullable List item) { - mSuggestions.combineResults(null, item, null); + mSuggestionsAdapter.combineResults(null, item, null); } }); results.count = 1; @@ -376,7 +376,7 @@ public class Suggestions extends BaseAdapter implements Filterable { @Override protected void publishResults(CharSequence constraint, FilterResults results) { - mSuggestions.combineResults(null, null, null); + mSuggestionsAdapter.combineResults(null, null, null); } } @@ -385,7 +385,7 @@ public class Suggestions extends BaseAdapter implements Filterable { @NonNull private final Application app; - public ClearCacheRunnable(@NonNull Application app) { + ClearCacheRunnable(@NonNull Application app) { this.app = app; } diff --git a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java index 9913b36..3ea94d7 100644 --- a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java @@ -12,7 +12,7 @@ import java.util.List; import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.database.HistoryItem; -public class SuggestionsManager { +class SuggestionsManager { public enum Source { GOOGLE, @@ -21,11 +21,11 @@ public class SuggestionsManager { private static volatile boolean sIsTaskExecuting; - public static boolean isRequestInProgress() { + static boolean isRequestInProgress() { return sIsTaskExecuting; } - public static Observable> getObservable(@NonNull final String query, @NonNull final Context context, @NonNull final Source source) { + static Observable> getObservable(@NonNull final String query, @NonNull final Context context, @NonNull final Source source) { return Observable.create(new Action>() { @Override public void onSubscribe(@NonNull final Subscriber> subscriber) {