Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkumar9t2 committed Feb 23, 2017
0 parents commit 4c16a2a
Show file tree
Hide file tree
Showing 40 changed files with 1,019 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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')
}
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@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());
}
}
26 changes: 26 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.arun.rxgoogleinstant.sample">

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
182 changes: 182 additions & 0 deletions app/src/main/java/com/arun/rxgoogleinstant/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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<TextViewAfterTextChangeEvent, String>() {
@Override
public String call(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) {
return textViewAfterTextChangeEvent.editable().toString();
}
}).filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return !TextUtils.isEmpty(s);
}
}).subscribeOn(AndroidSchedulers.mainThread())
.debounce(300, TimeUnit.MILLISECONDS)
.compose(RxSuggestions.suggestionsTransformer())
.doOnNext(new Action1<List<String>>() {
@Override
public void call(List<String> strings) {
suggestionsAdapter.setSuggestions(strings);
}
}).doOnError(new Action1<Throwable>() {
@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<SuggestionsAdapter.ListItemHolder> {
private final List<String> 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<String> 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<String> newList;
private final List<String> oldList;

SuggestionDiff(@NonNull List<String> oldList, @NonNull List<String> 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));
}
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_clear_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z" />
</vector>
33 changes: 33 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_clear_black_24dp" />

</android.support.design.widget.CoordinatorLayout>
Loading

0 comments on commit 4c16a2a

Please sign in to comment.