From 4389cf99857e1e30879c45d6137d068ebab41fde Mon Sep 17 00:00:00 2001 From: Mohit Sarveiya Date: Mon, 26 Jun 2017 21:55:04 -0400 Subject: [PATCH] Added Kotlin to project and converted SearchSuggestionManager to Kotlin --- app/build.gradle | 4 ++ .../lightning/search/SuggestionsAdapter.java | 6 +- .../lightning/search/SuggestionsManager.java | 70 ------------------- .../lightning/search/SuggestionsManager.kt | 43 ++++++++++++ build.gradle | 2 + 5 files changed, 52 insertions(+), 73 deletions(-) delete mode 100644 app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java create mode 100644 app/src/main/java/acr/browser/lightning/search/SuggestionsManager.kt diff --git a/app/build.gradle b/app/build.gradle index be9f680..beb699b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' apply plugin: 'com.getkeepsafe.dexcount' android { @@ -108,4 +109,7 @@ dependencies { def leakCanaryVersion = '1.5.1' debugCompile "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion" + + // Kotlin + compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" } diff --git a/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java b/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java index ccb4281..54c24b6 100644 --- a/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsAdapter.java @@ -302,11 +302,11 @@ public class SuggestionsAdapter extends BaseAdapter implements Filterable { @NonNull private Single> getSuggestionsForQuery(@NonNull final String query) { if (mSuggestionChoice == PreferenceManager.Suggestion.SUGGESTION_GOOGLE) { - return SuggestionsManager.createGoogleQueryObservable(query, mApplication); + return SuggestionsManager.INSTANCE.createGoogleQueryObservable(query, mApplication); } else if (mSuggestionChoice == PreferenceManager.Suggestion.SUGGESTION_DUCK) { - return SuggestionsManager.createDuckQueryObservable(query, mApplication); + return SuggestionsManager.INSTANCE.createDuckQueryObservable(query, mApplication); } else if (mSuggestionChoice == PreferenceManager.Suggestion.SUGGESTION_BAIDU) { - return SuggestionsManager.createBaiduQueryObservable(query, mApplication); + return SuggestionsManager.INSTANCE.createBaiduQueryObservable(query, mApplication); } else { return Single.empty(); } diff --git a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java deleted file mode 100644 index 90bbf6b..0000000 --- a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.java +++ /dev/null @@ -1,70 +0,0 @@ -package acr.browser.lightning.search; - -import android.app.Application; -import android.support.annotation.NonNull; - -import com.anthonycr.bonsai.Single; -import com.anthonycr.bonsai.SingleAction; -import com.anthonycr.bonsai.SingleSubscriber; - -import java.util.List; - -import acr.browser.lightning.database.HistoryItem; -import acr.browser.lightning.search.suggestions.BaiduSuggestionsModel; -import acr.browser.lightning.search.suggestions.DuckSuggestionsModel; -import acr.browser.lightning.search.suggestions.GoogleSuggestionsModel; - -class SuggestionsManager { - - private static volatile boolean sIsTaskExecuting; - - static boolean isRequestInProgress() { - return sIsTaskExecuting; - } - - @NonNull - static Single> createGoogleQueryObservable(@NonNull final String query, - @NonNull final Application application) { - return Single.create(new SingleAction>() { - @Override - public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { - sIsTaskExecuting = true; - List results = new GoogleSuggestionsModel(application).fetchResults(query); - subscriber.onItem(results); - subscriber.onComplete(); - sIsTaskExecuting = false; - } - }); - } - - @NonNull - static Single> createBaiduQueryObservable(@NonNull final String query, - @NonNull final Application application) { - return Single.create(new SingleAction>() { - @Override - public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { - sIsTaskExecuting = true; - List results = new BaiduSuggestionsModel(application).fetchResults(query); - subscriber.onItem(results); - subscriber.onComplete(); - sIsTaskExecuting = false; - } - }); - } - - @NonNull - static Single> createDuckQueryObservable(@NonNull final String query, - @NonNull final Application application) { - return Single.create(new SingleAction>() { - @Override - public void onSubscribe(@NonNull final SingleSubscriber> subscriber) { - sIsTaskExecuting = true; - List results = new DuckSuggestionsModel(application).fetchResults(query); - subscriber.onItem(results); - subscriber.onComplete(); - sIsTaskExecuting = false; - } - }); - } - -} diff --git a/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.kt b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.kt new file mode 100644 index 0000000..3a8edff --- /dev/null +++ b/app/src/main/java/acr/browser/lightning/search/SuggestionsManager.kt @@ -0,0 +1,43 @@ +package acr.browser.lightning.search + +import acr.browser.lightning.database.HistoryItem +import acr.browser.lightning.search.suggestions.BaiduSuggestionsModel +import acr.browser.lightning.search.suggestions.DuckSuggestionsModel +import acr.browser.lightning.search.suggestions.GoogleSuggestionsModel +import android.app.Application +import com.anthonycr.bonsai.Single +import com.anthonycr.bonsai.SingleAction + +internal object SuggestionsManager { + + @JvmStatic + @Volatile var isRequestInProgress: Boolean = false + + fun createGoogleQueryObservable(query: String, application: Application) = + Single.create(SingleAction> { subscriber -> + isRequestInProgress = true + val results = GoogleSuggestionsModel(application).fetchResults(query) + subscriber.onItem(results) + subscriber.onComplete() + isRequestInProgress = false + }) + + fun createBaiduQueryObservable(query: String, application: Application) = + Single.create(SingleAction> { subscriber -> + isRequestInProgress = true + val results = BaiduSuggestionsModel(application).fetchResults(query) + subscriber.onItem(results) + subscriber.onComplete() + isRequestInProgress = false + }) + + fun createDuckQueryObservable(query: String, application: Application) = + Single.create(SingleAction> { subscriber -> + isRequestInProgress = true + val results = DuckSuggestionsModel(application).fetchResults(query) + subscriber.onItem(results) + subscriber.onComplete() + isRequestInProgress = false + }) + +} diff --git a/build.gradle b/build.gradle index 6e6a447..b262873 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,12 @@ buildscript { + ext.kotlin_version = '1.1.3' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.3' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }