Browse Source

refactoring search suggestions adapter

master
Anthony Restaino 8 years ago
parent
commit
284f2c7ff9
  1. 6
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 4
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  3. 3
      app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java
  4. 4
      app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java
  5. 4
      app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java
  6. 34
      app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java
  7. 6
      app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java

6
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -113,7 +113,7 @@ import acr.browser.lightning.fragment.BookmarksFragment; @@ -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 @@ -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 @@ -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);

4
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -17,7 +17,7 @@ import acr.browser.lightning.fragment.DebugSettingsFragment; @@ -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 { @@ -68,6 +68,6 @@ public interface AppComponent {
void inject(DebugSettingsFragment fragment);
void inject(Suggestions suggestions);
void inject(SuggestionsAdapter suggestionsAdapter);
}

3
app/src/main/java/acr/browser/lightning/search/BaseSuggestionsTask.java

@ -32,6 +32,7 @@ abstract class BaseSuggestionsTask { @@ -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 { @@ -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;
}

4
app/src/main/java/acr/browser/lightning/search/DuckSuggestionsTask.java

@ -13,7 +13,7 @@ import acr.browser.lightning.R; @@ -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 { @@ -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;
}
}

4
app/src/main/java/acr/browser/lightning/search/GoogleSuggestionsTask.java

@ -15,7 +15,7 @@ import java.util.List; @@ -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 { @@ -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;
}
}

34
app/src/main/java/acr/browser/lightning/search/Suggestions.java → app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java

@ -43,11 +43,11 @@ import com.anthonycr.bonsai.Subscriber; @@ -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<HistoryItem> mFilteredList = new ArrayList<>(5);
@ -74,7 +74,7 @@ public class Suggestions extends BaseAdapter implements Filterable { @@ -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 { @@ -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 { @@ -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<List<HistoryItem>>() {
@Override
public void onNext(@Nullable List<HistoryItem> 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<List<HistoryItem>>() {
@Override
public void onNext(@Nullable List<HistoryItem> 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<List<HistoryItem>>() {
@Override
public void onNext(@Nullable List<HistoryItem> 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 { @@ -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 { @@ -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;
}

6
app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java

@ -12,7 +12,7 @@ import java.util.List; @@ -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 { @@ -21,11 +21,11 @@ public class SuggestionsManager {
private static volatile boolean sIsTaskExecuting;
public static boolean isRequestInProgress() {
static boolean isRequestInProgress() {
return sIsTaskExecuting;
}
public static Observable<List<HistoryItem>> getObservable(@NonNull final String query, @NonNull final Context context, @NonNull final Source source) {
static Observable<List<HistoryItem>> getObservable(@NonNull final String query, @NonNull final Context context, @NonNull final Source source) {
return Observable.create(new Action<List<HistoryItem>>() {
@Override
public void onSubscribe(@NonNull final Subscriber<List<HistoryItem>> subscriber) {

Loading…
Cancel
Save