Skip to content

Commit

Permalink
Merge pull request #1 from Roaim/search
Browse files Browse the repository at this point in the history
search
  • Loading branch information
Roaim authored Aug 1, 2018
2 parents 51ff29c + 5f454a8 commit 85cc402
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 15 deletions.
18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.roaimsapp.findit"
minSdkVersion 19
Expand All @@ -23,18 +23,18 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.1.0'
compile 'com.android.support:appcompat-v7:26.1.0'
testCompile 'junit:junit:4.12'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'

def room_version = "1.1.1"

compile "android.arch.persistence.room:runtime:$room_version"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"

// optional - RxJava support for Room
Expand Down
57 changes: 57 additions & 0 deletions app/src/main/java/com/roaimsapp/findit/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import android.widget.AdapterView;
import android.widget.SearchView;

import com.roaimsapp.findit.adapter.SearchAdapter;
import com.roaimsapp.findit.adapter.SegmentAdapter;
import com.roaimsapp.findit.adapter.SegmentSpinnerAdapter;
import com.roaimsapp.findit.data.NumberDatabase;
import com.roaimsapp.findit.data.dao.NumberDao;
import com.roaimsapp.findit.data.model.Number;
import com.roaimsapp.findit.data.model.Segment;
import com.roaimsapp.findit.databinding.ActivityMainBinding;

Expand All @@ -24,6 +27,7 @@ public class SearchActivity extends AppCompatActivity implements AdapterView.OnI
ActivityMainBinding binding;
private NumberDatabase database;
private Segment mSegment;
private SearchAdapter searchAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -33,7 +37,12 @@ protected void onCreate(Bundle savedInstanceState) {

database = NumberDatabase.getDatabase(this);

searchAdapter = new SearchAdapter();
binding.include.recyclerView.setLayoutManager(new LinearLayoutManager(this));
binding.include.recyclerView.setAdapter(searchAdapter);

Tasker.start(null, new Tasker.Callback<Void, List<Segment>>() {

@Override
public List<Segment> onBackground(Void input) {
return database.segmentDao().getAll();
Expand All @@ -55,6 +64,51 @@ public void onUi(List<Segment> output) {
@Override
public boolean onQueryTextSubmit(String s) {
Log.d(TAG, "onQueryTextSubmit() called with: s = [" + s + "]");
String[] split = new String[1];
if (s.contains(" ")) {
split = s.split(" ");
} else if (!s.isEmpty()){
split[0] = s;
}
Tasker.start(split, new Tasker.Callback<String[], List<Number>>() {
@Override
public List<Number> onBackground(String[] input) {
int length = input.length;
Log.d(TAG, "onBackground: length: " + length);
if (length > 3) return null;
searchAdapter.setLength(length);
NumberDao numberDao = database.numberDao();
List<Number> numbers = null;
if (length == 1) {
if (mSegment != null) {
numbers = numberDao.getNumbers(input[0], mSegment.getId());
} else {
numbers = numberDao.getNumbers(input[0]);
}
} else if (length == 2) {
if (mSegment != null) {
numbers = numberDao.getNumbers(input[0], input[1], mSegment.getId());
} else {
numbers = numberDao.getNumbers(input[0], input[1]);
}
} else if (length == 3) {
if (mSegment != null) {
numbers = numberDao.getNumbers(input[0], input[1], input[2], mSegment.getId());
} else {
numbers = numberDao.getNumbers(input[0], input[1], input[2]);
}
}
return numbers;
}

@Override
public void onUi(List<Number> output) {
Log.d(TAG, "onQueryTextSubmit::onUi() called with: output = [" + output + "]");
if (output != null) {
searchAdapter.addAll(output);
}
}
}, binding.include.progressBar);
return false;
}

Expand All @@ -63,6 +117,7 @@ public boolean onQueryTextChange(String s) {
Log.d(TAG, "onQueryTextChange() called with: s = [" + s + "]");
if (s.isEmpty()) {
prev = "";
searchAdapter.clear();
return true;
}
if (s.startsWith(" ")) {
Expand Down Expand Up @@ -107,6 +162,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d(TAG, "onItemSelected() called with: i = [" + i + "], l = [" + l + "]");
if (adapterView.getAdapter() instanceof SegmentSpinnerAdapter) {
Segment item = (Segment) adapterView.getItemAtPosition(i);
if (item.getId()==-1) mSegment = null;
Expand All @@ -116,6 +172,7 @@ public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.d(TAG, "onNothingSelected() called");
mSegment = null;
}
}
105 changes: 105 additions & 0 deletions app/src/main/java/com/roaimsapp/findit/adapter/SearchAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.roaimsapp.findit.adapter;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.roaimsapp.findit.R;
import com.roaimsapp.findit.adapter.viewholder.NumberViewHolder;
import com.roaimsapp.findit.adapter.viewholder.SearchViewHolder;
import com.roaimsapp.findit.data.model.Number;
import com.roaimsapp.findit.databinding.ItemNumberBinding;
import com.roaimsapp.findit.databinding.ItemSearchBinding;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Roaim on 21-Jul-18.
*/

public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {
private List<Number> mList;
private int length;

public void setLength(int length) {
this.length = length;
}

public SearchAdapter() {
mList = new ArrayList<>();
}

@Override
public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ItemSearchBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_search, parent, false);
return new SearchViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
Number number = getNumber(position);
holder.bind(number, length);
}

@Override
public int getItemCount() {
return mList.size();
}

public Number getNumber(int position) {
return mList.get(position);
}

public int getIndexOf(Number number) {
return mList.indexOf(number);
}

public void update(Number number, int index) {
mList.set(index, number);
}

public void update(int startIndex, Number... numbers) {
for (int i = 0; i < numbers.length; i++) {
int index = startIndex - 1 - i;
mList.set(index, numbers[i]);
}
}

public void update(Number... numbers) {
for (int i = 0; i < numbers.length; i++) {
int index = mList.size() - 1 - i;
mList.set(index, numbers[i]);
}
}

public void addAll(List<Number> list) {
clear();
mList.addAll(list);
notifyDataSetChanged();
}

public void addNumber(Number number) {
mList.add(number);
}

public void removeNumber(Number number) {
mList.remove(number);
}

@Override
public void onViewDetachedFromWindow(SearchViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.unBind();
}

public void clear() {
if (!mList.isEmpty()) {
mList.removeAll(mList);
}
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.roaimsapp.findit.adapter.viewholder;

import com.roaimsapp.findit.data.model.Number;
import com.roaimsapp.findit.databinding.ItemSearchBinding;

/**
* Created by Roaim on 22-Jul-18.
*/

public class SearchViewHolder extends BaseViewHolder<ItemSearchBinding> {

public SearchViewHolder(ItemSearchBinding binding) {
super(binding);
}

public void bind(Number number, int length) {
// binding.setNumber(number);
binding.setPrev(number.getPrev());
switch (length) {
case 1:
binding.setSearch(number.getNumber());
binding.setNext(number.getNext1());
break;
case 2:
binding.setSearch(number.getNumber() + " " + number.getNext1());
binding.setNext(number.getNext2());
break;
case 3:
binding.setSearch(number.getNumber() + " " + number.getNext1() + " " + number.getNext2());
binding.setNext(number.getNext3());
}
binding.executePendingBindings();
}

}
61 changes: 58 additions & 3 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,75 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner" />

<LinearLayout
android:id="@+id/linearLayout"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="#efefef"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Previous"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Searched"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Next"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView"
app:layout_constraintVertical_bias="0.0" />
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="@+id/recyclerView"
app:layout_constraintStart_toStartOf="@+id/recyclerView"
app:layout_constraintTop_toTopOf="@+id/recyclerView" />

</android.support.constraint.ConstraintLayout>

</layout>
Loading

0 comments on commit 85cc402

Please sign in to comment.