Search for files using /search/code endpoint

This commit is contained in:
adi.bk 2017-04-26 09:42:00 -07:00
parent d359b1a56b
commit 1ca6f0bd37
4 changed files with 171 additions and 15 deletions

View File

@ -91,6 +91,8 @@ public class RepoFilePathFragment extends BaseFragment<RepoFilePathMvp.View, Rep
@OnClick(R.id.searchRepoFiles) void onSearchClicked() {
Intent intent = new Intent(getContext(), SearchFileActivity.class);
intent.putExtra(BundleConstant.ID, getPresenter().getRepoId());
intent.putExtra(BundleConstant.EXTRA, getPresenter().getLogin());
startActivity(intent);
}

View File

@ -3,20 +3,33 @@ package com.fastaccess.ui.modules.search.files;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import com.fastaccess.R;
import com.fastaccess.data.dao.model.SearchHistory;
import com.fastaccess.helper.AnimHelper;
import com.fastaccess.helper.AppHelper;
import com.fastaccess.ui.base.BaseActivity;
import com.fastaccess.ui.base.mvp.presenter.BasePresenter;
import net.grandcentrix.thirtyinch.TiPresenter;
import com.fastaccess.ui.modules.search.code.SearchCodeFragment;
import com.fastaccess.ui.widgets.FontAutoCompleteEditText;
import com.fastaccess.ui.widgets.ForegroundImageView;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import butterknife.OnTextChanged;
/**
* Created by adibk on 4/23/17.
*/
public class SearchFileActivity extends BaseActivity<SearchFileMvp.View, SearchFilePresenter> implements SearchFileMvp.View {
public class SearchFileActivity extends BaseActivity {
@BindView(R.id.searchEditText) FontAutoCompleteEditText searchEditText;
@BindView(R.id.clear) ForegroundImageView clear;
private ArrayAdapter adapter;
private SearchCodeFragment searchCodeFragment;
@Override
protected int layout() {
@ -40,16 +53,61 @@ public class SearchFileActivity extends BaseActivity {
@NonNull
@Override
public TiPresenter providePresenter() {
return new BasePresenter();
public SearchFilePresenter providePresenter() {
return new SearchFilePresenter();
}
@OnTextChanged(value = R.id.searchEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void onTextChange(Editable s) {
String text = s.toString();
if (text.length() == 0) {
AnimHelper.animateVisibility(clear, false);
} else {
AnimHelper.animateVisibility(clear, true);
}
}
@OnEditorAction(R.id.searchEditText) boolean onEditor(int actionId, KeyEvent keyEvent) {
if (keyEvent != null && keyEvent.getAction() == KeyEvent.KEYCODE_SEARCH) {
getPresenter().onSearchClicked(searchEditText, searchCodeFragment);
} else if (actionId == EditorInfo.IME_ACTION_SEARCH) {
getPresenter().onSearchClicked(searchEditText, searchCodeFragment);
}
return false;
}
@OnClick(value = {R.id.clear}) void onClear(View view) {
if (view.getId() == R.id.clear) {
AppHelper.hideKeyboard(searchEditText);
searchEditText.setText("");
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchEditText.setAdapter(getAdapter());
searchEditText.setOnItemClickListener((parent, view, position, id) -> getPresenter().onSearchClicked(searchEditText, searchCodeFragment));
getPresenter().onActivityCreated(getIntent().getExtras());
searchCodeFragment = ((SearchCodeFragment) getSupportFragmentManager().findFragmentById(R.id.filesFragment));
}
@OnClick(R.id.back) public void onBackClicked() {
onBackPressed();
}
@OnTextChanged(R.id.searchEditText) public void onSearchTextChanged() {
}
@Override
public void onNotifyAdapter(@Nullable SearchHistory query) {
if (query == null) getAdapter().notifyDataSetChanged();
else getAdapter().add(query);
}
private ArrayAdapter<SearchHistory> getAdapter() {
if (adapter == null) adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getPresenter().getHints());
return adapter;
}
}

View File

@ -0,0 +1,73 @@
package com.fastaccess.ui.modules.search.files;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.AutoCompleteTextView;
import com.annimon.stream.Stream;
import com.fastaccess.R;
import com.fastaccess.data.dao.model.SearchHistory;
import com.fastaccess.helper.AppHelper;
import com.fastaccess.helper.BundleConstant;
import com.fastaccess.helper.InputHelper;
import com.fastaccess.ui.base.mvp.presenter.BasePresenter;
import com.fastaccess.ui.modules.search.code.SearchCodeFragment;
import java.util.ArrayList;
public class SearchFilePresenter extends BasePresenter<SearchFileMvp.View> implements SearchFileMvp.Presenter {
private ArrayList<SearchHistory> hints = new ArrayList<>();
private String repoId;
private String login;
@Override protected void onAttachView(@NonNull SearchFileMvp.View view) {
super.onAttachView(view);
if (hints.isEmpty()) {
manageSubscription(SearchHistory.getHistory()
.subscribe(strings -> {
hints.clear();
if (strings != null) hints.addAll(strings);
view.onNotifyAdapter(null);
}));
}
}
@NonNull
@Override
public ArrayList<SearchHistory> getHints() {
return hints;
}
@Override
public void onSearchClicked(@NonNull AutoCompleteTextView editText, @NonNull SearchCodeFragment searchCodeFragment) {
boolean isEmpty = InputHelper.isEmpty(editText) || InputHelper.toString(editText).length() < 3;
editText.setError(isEmpty ? editText.getResources().getString(R.string.minimum_three_chars) : null);
if (!isEmpty) {
editText.dismissDropDown();
AppHelper.hideKeyboard(editText);
String query = InputHelper.toString(editText);
searchCodeFragment.onSetSearchQuery(modifyQueryForFileSearch(query));
boolean noneMatch = Stream.of(hints).noneMatch(value -> value.getText().equalsIgnoreCase(query));
if (noneMatch) {
SearchHistory searchHistory = new SearchHistory(query);
manageSubscription(searchHistory.save(searchHistory).subscribe());
sendToView(view -> view.onNotifyAdapter(new SearchHistory(query)));
}
}
}
private String modifyQueryForFileSearch(String query) {
//restrict the search to file paths and the current repo user is looking at
return query + "+" + "in:path" + "+" + "repo:" + login + "/" + repoId;
}
@Override
public void onActivityCreated(Bundle extras) {
repoId = extras.getString(BundleConstant.ID);
login = extras.getString(BundleConstant.EXTRA);
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -35,21 +36,43 @@
android:scaleType="centerCrop"
android:src="@drawable/ic_back"/>
<com.fastaccess.ui.widgets.FontEditText
<com.fastaccess.ui.widgets.FontAutoCompleteEditText
android:id="@+id/searchEditText"
style="@style/TextAppearance.AppCompat.Subhead"
style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginEnd="@dimen/spacing_xs_large"
android:layout_weight="1"
android:background="@null"
android:hint="@string/type_here"
android:imeActionLabel="@string/search"
android:background="@color/transparent"
android:completionThreshold="1"
android:hint="@string/search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:padding="@dimen/spacing_xs_large"/>
android:textColorHint="?android:textColorSecondary"/>
<com.fastaccess.ui.widgets.ForegroundImageView
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?selectableItemBackgroundBorderless"
android:contentDescription="@string/clear"
android:padding="@dimen/spacing_normal"
android:scaleType="centerCrop"
android:src="@drawable/ic_clear"
android:visibility="invisible"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<fragment
android:id="@+id/filesFragment"
android:name="com.fastaccess.ui.modules.search.code.SearchCodeFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/spacing_micro"
android:layout_weight="1"
tools:layout="@layout/vertical_refresh_list"/>
</LinearLayout>