diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fbdc37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# files for the dex VM +*.dex + +# Java class files +*.class + +# generated files +bin/ +gen/ +out/ +build/ +.gradle/ +.ART +# Project files +*.iml +.idea +# .idea/workspace.xml + +# Local configuration file (sdk path, etc) +local.properties +keystore.properties + +# Values file +values.properties + +# Windows thumbnail db +.DS_Store + +# Idea non-crucial project fileS +*.iws + +# Sandbox stuff +_sandbox \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..b6294be --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,35 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.2" + defaultConfig { + applicationId "com.arun.rxgoogleinstant.sample" + minSdkVersion 16 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + vectorDrawables.useSupportLibrary = true; + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:25.1.1' + compile 'com.jakewharton.rxbinding:rxbinding:1.0.0' + compile 'com.jakewharton:butterknife:8.5.1' + annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' + compile 'com.android.support:design:25.1.1' + testCompile 'junit:junit:4.12' + compile project(path: ':library') +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..2dcbc5f --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\arunk\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/arun/rxgoogleinstant/sample/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/arun/rxgoogleinstant/sample/ExampleInstrumentedTest.java new file mode 100644 index 0000000..a83daed --- /dev/null +++ b/app/src/androidTest/java/com/arun/rxgoogleinstant/sample/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.arun.rxgoogleinstant.sample; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.arun.rxgoogleinstant", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..13a1936 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/arun/rxgoogleinstant/sample/MainActivity.java b/app/src/main/java/com/arun/rxgoogleinstant/sample/MainActivity.java new file mode 100644 index 0000000..c14761d --- /dev/null +++ b/app/src/main/java/com/arun/rxgoogleinstant/sample/MainActivity.java @@ -0,0 +1,182 @@ +package com.arun.rxgoogleinstant.sample; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.design.widget.FloatingActionButton; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.util.DiffUtil; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; +import android.text.TextUtils; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.TextView; + +import com.arun.rxgoogleinstant.RxSuggestions; +import com.jakewharton.rxbinding.widget.RxTextView; +import com.jakewharton.rxbinding.widget.TextViewAfterTextChangeEvent; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action1; +import rx.functions.Func1; +import rx.subscriptions.CompositeSubscription; + +public class MainActivity extends AppCompatActivity { + private static final String TAG = MainActivity.class.getSimpleName(); + @BindView(R.id.toolbar) + Toolbar toolbar; + @BindView(R.id.search_box) + EditText searchBox; + @BindView(R.id.recycler_view) + RecyclerView recyclerView; + @BindView(R.id.fab) + FloatingActionButton fab; + + private final CompositeSubscription subscription = new CompositeSubscription(); + private SuggestionsAdapter suggestionsAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + ButterKnife.bind(this); + setSupportActionBar(toolbar); + + suggestionsAdapter = new SuggestionsAdapter(); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + recyclerView.setAdapter(suggestionsAdapter); + } + + @Override + protected void onResume() { + super.onResume(); + subscription.add(RxTextView.afterTextChangeEvents(searchBox) + .map(new Func1() { + @Override + public String call(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) { + return textViewAfterTextChangeEvent.editable().toString(); + } + }).filter(new Func1() { + @Override + public Boolean call(String s) { + return !TextUtils.isEmpty(s); + } + }).subscribeOn(AndroidSchedulers.mainThread()) + .debounce(300, TimeUnit.MILLISECONDS) + .compose(RxSuggestions.suggestionsTransformer()) + .doOnNext(new Action1>() { + @Override + public void call(List strings) { + suggestionsAdapter.setSuggestions(strings); + } + }).doOnError(new Action1() { + @Override + public void call(Throwable throwable) { + Log.e(TAG, throwable.toString()); + } + }).subscribe()); + } + + @Override + protected void onPause() { + super.onPause(); + subscription.clear(); + } + + @OnClick(R.id.fab) + public void onClick() { + searchBox.setText(""); + } + + static class SuggestionsAdapter extends RecyclerView.Adapter { + private final List strings = new ArrayList<>(); + + SuggestionsAdapter() { + setHasStableIds(true); + } + + @Override + public ListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new ListItemHolder(LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false)); + } + + @Override + public void onBindViewHolder(ListItemHolder holder, int position) { + if (holder.itemView instanceof TextView) { + ((TextView) holder.itemView).setText(strings.get(position)); + ((TextView) holder.itemView).setTextColor(Color.BLACK); + } + } + + @Override + public long getItemId(int position) { + return strings.get(position).hashCode(); + } + + @Override + public int getItemCount() { + return strings.size(); + } + + public void setSuggestions(@NonNull List newStrings) { + final SuggestionDiff suggestionDiff = new SuggestionDiff(strings, newStrings); + final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(suggestionDiff, true); + strings.clear(); + strings.addAll(newStrings); + diffResult.dispatchUpdatesTo(this); + } + + static class ListItemHolder extends RecyclerView.ViewHolder { + ListItemHolder(View itemView) { + super(itemView); + } + } + + private static class SuggestionDiff extends DiffUtil.Callback { + + private final List newList; + private final List oldList; + + SuggestionDiff(@NonNull List oldList, @NonNull List newList) { + this.oldList = oldList; + this.newList = newList; + } + + @Override + public int getOldListSize() { + return oldList.size(); + } + + @Override + public int getNewListSize() { + return newList.size(); + } + + @Override + public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { + return caseInsensitiveComparison(oldItemPosition, newItemPosition); + } + + @Override + public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { + return caseInsensitiveComparison(oldItemPosition, newItemPosition); + } + + private boolean caseInsensitiveComparison(int oldItemPosition, int newItemPosition) { + return oldList.get(oldItemPosition).equalsIgnoreCase(newList.get(newItemPosition)); + } + } + } +} diff --git a/app/src/main/res/drawable/ic_clear_black_24dp.xml b/app/src/main/res/drawable/ic_clear_black_24dp.xml new file mode 100644 index 0000000..56da961 --- /dev/null +++ b/app/src/main/res/drawable/ic_clear_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..0dc2679 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml new file mode 100644 index 0000000..5663800 --- /dev/null +++ b/app/src/main/res/layout/content_main.xml @@ -0,0 +1,27 @@ + + + + + + + + diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/app/src/main/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png new file mode 100644 index 0000000..9a078e3 Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/app/src/main/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png new file mode 100644 index 0000000..efc028a Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/app/src/main/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png new file mode 100644 index 0000000..3af2608 Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..9bec2e6 Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png new file mode 100644 index 0000000..34947cd Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png differ diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml new file mode 100644 index 0000000..d453dc6 --- /dev/null +++ b/app/src/main/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #4CAF50 + #2E7D32 + #FFD740 + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..59a0b0c --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,3 @@ + + 16dp + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml new file mode 100644 index 0000000..eef2311 --- /dev/null +++ b/app/src/main/res/values/strings.xml @@ -0,0 +1,3 @@ + + RxGoogleInstant + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml new file mode 100644 index 0000000..545b9c6 --- /dev/null +++ b/app/src/main/res/values/styles.xml @@ -0,0 +1,20 @@ + + + + + + + +