Skip to content

Commit

Permalink
Add search.
Browse files Browse the repository at this point in the history
Inc version.
  • Loading branch information
kenumir committed Oct 11, 2017
1 parent 6868de8 commit 822155f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ android {
applicationId "com.wt.apkinfo"
minSdkVersion 15
targetSdkVersion 26
versionCode 2
versionName "1.0.1"
versionCode 3
versionName "1.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

resConfigs "en", "pl"
Expand Down Expand Up @@ -117,8 +117,8 @@ dependencies {

compile 'com.github.Hivedi:Console:1.0.3'

compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') { transitive = true; }
compile('com.crashlytics.sdk.android:answers:1.3.13@aar') { transitive = true; }
compile('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }
compile('com.crashlytics.sdk.android:answers:1.4.0@aar') { transitive = true; }
compile 'com.github.anrwatchdog:anrwatchdog:1.3.0'
compile 'com.afollestad.material-dialogs:core:' + versions.materialDialogs
}
Expand Down
80 changes: 45 additions & 35 deletions app/src/main/java/com/wt/apkinfo/fragment/ApplicationsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
Expand Down Expand Up @@ -53,11 +51,11 @@
public class ApplicationsFragment extends Fragment {

@BindView(R2.id.recycler) RecyclerView recycler;
@BindView(R2.id.searchEdit) EditText searchEdit;
@BindView(R2.id.overlayFrame) FrameLayout overlayFrame;
@BindView(R2.id.toolbar) Toolbar toolbar;

private ApplicationsListAdapter adapter;
private String searchText = null;

public ApplicationsFragment() {
// Required empty public constructor
Expand All @@ -67,13 +65,22 @@ public ApplicationsFragment() {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View res = inflater.inflate(R.layout.fragment_applications, container, false);
ButterKnife.bind(this, res);
searchEdit.setText(
ViewModelProviders.of(getActivity())
.get(ApplicationListViewModel.class)
.getFilter()
);
searchEdit.addTextChangedListener(new TextWatcher() {
private String searchText = null;

if (savedInstanceState != null) {
searchText = savedInstanceState.getString("searchText");
} else {
ApplicationListViewModel model = ViewModelProviders.of(getActivity()).get(ApplicationListViewModel.class);
String filter = model.getFilter();
if (filter != null) {
model.search(null);
}
}

Menu menu = toolbar.getMenu();

SearchView search = new SearchView(toolbar.getContext());
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
private Handler handler = new Handler(Looper.getMainLooper());
private Runnable delay = new Runnable() {
@Override
public void run() {
Expand All @@ -87,36 +94,33 @@ public void run() {
}
}
};
private Handler h = new Handler(Looper.getMainLooper());
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
public boolean onQueryTextSubmit(String query) {
searchText = query;
handler.removeCallbacks(delay);
handler.postDelayed(delay, 300);
return false;
}

@Override
public void afterTextChanged(Editable editable) {
if (BuildConfig.DEBUG) {
Console.logd("Text changed: " + editable);
}
searchText = editable.toString();
h.removeCallbacks(delay);
h.postDelayed(delay, 300);
public boolean onQueryTextChange(String newText) {
searchText = newText;
handler.removeCallbacks(delay);
handler.postDelayed(delay, 300);
return false;
}
});
Menu menu = toolbar.getMenu();
/*
menu
.add("Search")

MenuItem searchMenuItem = menu.add(R.string.main_menu_search)
.setIcon(R.drawable.ic_search_white_24dp)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return false;
}
})
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
*/
.setActionView(search);
searchMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

if (searchText != null) {
search.setQuery(searchText, false);
searchMenuItem.expandActionView();
}

menu
.add(R.string.main_menu_about)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
Expand Down Expand Up @@ -190,6 +194,12 @@ public void onChanged(@Nullable List<ApplicationEntity> apps) {
});
}

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("searchText", searchText);
super.onSaveInstanceState(outState);
}

private interface OnItemClick {
void onItemClick(View v, ApplicationEntity item, ApplicationsItemHolder holder);
}
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/res/layout/fragment_applications.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/app_name"/>

<EditText
android:id="@+id/searchEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:padding="8dp"
android:inputType="text"
android:visibility="gone"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_below="@+id/toolbar"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<!-- main activity -->
<string name="main_menu_about">About</string>
<string name="main_menu_search">Search</string>


<!-- about -->
Expand Down

0 comments on commit 822155f

Please sign in to comment.