mirror of
https://github.com/leanote/leanote-android.git
synced 2026-01-16 01:00:40 +08:00
support search by title
This commit is contained in:
@@ -48,6 +48,8 @@
|
||||
android:enabled="true"
|
||||
android:exported="false"
|
||||
android:label="NoteSyncService" />
|
||||
|
||||
<activity android:name=".ui.SearchActivity" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -6,11 +6,14 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.raizlabs.android.dbflow.sql.language.SQLite;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.houxg.leamonax.database.AppDataBase;
|
||||
import org.houxg.leamonax.model.Account;
|
||||
import org.houxg.leamonax.model.Note;
|
||||
import org.houxg.leamonax.model.NoteFile;
|
||||
import org.houxg.leamonax.model.Note_Table;
|
||||
import org.houxg.leamonax.model.Notebook;
|
||||
import org.houxg.leamonax.model.UpdateRe;
|
||||
import org.houxg.leamonax.network.ApiProvider;
|
||||
@@ -489,4 +492,15 @@ public class NoteService {
|
||||
RequestBody fileBody = RequestBody.create(MediaType.parse(mimeType), tempFile);
|
||||
return MultipartBody.Part.createFormData(String.format("FileDatas[%s]", noteFile.getLocalId()), tempFile.getName(), fileBody);
|
||||
}
|
||||
|
||||
public static List<Note> searchNoteWithTitle(String keyword) {
|
||||
keyword = String.format(Locale.US, "%%%s%%", keyword);
|
||||
return SQLite.select()
|
||||
.from(Note.class)
|
||||
.where(Note_Table.userId.eq(AccountService.getCurrent().getUserId()))
|
||||
.and(Note_Table.title.like(keyword))
|
||||
.and(Note_Table.isTrash.eq(false))
|
||||
.and(Note_Table.isDeleted.eq(false))
|
||||
.queryList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.text.TextUtils;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.animation.AccelerateDecelerateInterpolator;
|
||||
@@ -116,6 +117,12 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
|
||||
EventBus.getDefault().unregister(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.main, menu);
|
||||
return super.onCreateOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
@@ -125,6 +132,9 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
|
||||
mDrawerLayout.openDrawer(GravityCompat.START, true);
|
||||
}
|
||||
return true;
|
||||
} else if (item.getItemId() == R.id.action_search) {
|
||||
Intent intent = new Intent(this, SearchActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
182
app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java
Normal file
182
app/src/main/java/org/houxg/leamonax/ui/SearchActivity.java
Normal file
@@ -0,0 +1,182 @@
|
||||
package org.houxg.leamonax.ui;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.DefaultItemAnimator;
|
||||
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.TextUtils;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import org.houxg.leamonax.R;
|
||||
import org.houxg.leamonax.adapter.NoteAdapter;
|
||||
import org.houxg.leamonax.model.Note;
|
||||
import org.houxg.leamonax.service.NoteService;
|
||||
import org.houxg.leamonax.utils.DisplayUtils;
|
||||
import org.houxg.leamonax.utils.ToastUtils;
|
||||
import org.houxg.leamonax.widget.DashDividerDecoration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import rx.Observer;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
public class SearchActivity extends BaseActivity implements NoteAdapter.NoteAdapterListener {
|
||||
|
||||
private static final String EXT_SCROLL_POSITION = "ext_scroll_position";
|
||||
|
||||
@BindView(R.id.recycler_view)
|
||||
RecyclerView mNoteListView;
|
||||
@BindView(R.id.toolbar)
|
||||
Toolbar mToolbar;
|
||||
@BindView(R.id.search)
|
||||
SearchView mSearchView;
|
||||
|
||||
List<Note> mNotes = new ArrayList<>();
|
||||
private NoteAdapter mAdapter;
|
||||
|
||||
private float mScrollPosition;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_search);
|
||||
ButterKnife.bind(this);
|
||||
initToolBar(mToolbar, true);
|
||||
setTitle("");
|
||||
|
||||
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
|
||||
mNoteListView.setLayoutManager(layoutManager);
|
||||
mNoteListView.setItemAnimator(new DefaultItemAnimator());
|
||||
|
||||
int dashGap = DisplayUtils.dp2px(this, 4);
|
||||
int dashWidth = DisplayUtils.dp2px(this, 8);
|
||||
int height = DisplayUtils.dp2px(this, 1);
|
||||
mNoteListView.addItemDecoration(new DashDividerDecoration(0xffa0a0a0, dashGap, dashWidth, height));
|
||||
mAdapter = new NoteAdapter(this);
|
||||
mNoteListView.setAdapter(mAdapter);
|
||||
mNoteListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
|
||||
super.onScrollStateChanged(recyclerView, newState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
||||
super.onScrolled(recyclerView, dx, dy);
|
||||
mScrollPosition = dy;
|
||||
}
|
||||
});
|
||||
|
||||
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
|
||||
@Override
|
||||
public boolean onQueryTextSubmit(String query) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onQueryTextChange(String newText) {
|
||||
searchTitle(newText);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
ImageView searchCloseIcon = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
|
||||
searchCloseIcon.setImageResource(R.drawable.ic_clear);
|
||||
ImageView searchIcon = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
|
||||
searchIcon.setImageResource(R.drawable.ic_search);
|
||||
|
||||
SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
|
||||
searchAutoComplete.setHintTextColor(getResources().getColor(R.color.menu_text));
|
||||
searchAutoComplete.setTextColor(getResources().getColor(R.color.menu_text));
|
||||
mSearchView.setIconified(false);
|
||||
mSearchView.setIconifiedByDefault(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
mScrollPosition = savedInstanceState.getFloat(EXT_SCROLL_POSITION, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
mNoteListView.scrollTo(0, (int) mScrollPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putFloat(EXT_SCROLL_POSITION, mScrollPosition);
|
||||
}
|
||||
|
||||
private void searchTitle(String keyword) {
|
||||
if (TextUtils.isEmpty(keyword)) {
|
||||
mNotes = new ArrayList<>();
|
||||
} else {
|
||||
mNotes = NoteService.searchNoteWithTitle(keyword);
|
||||
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
|
||||
}
|
||||
mAdapter.load(mNotes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClickNote(Note note) {
|
||||
startActivity(NotePreviewActivity.getOpenIntent(this, note.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLongClickNote(final Note note) {
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle(R.string.delete_note)
|
||||
.setMessage(String.format(Locale.US, getString(R.string.are_you_sure_to_delete_note), TextUtils.isEmpty(note.getTitle()) ? "this note" : note.getTitle()))
|
||||
.setCancelable(true)
|
||||
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
deleteNote(note);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
private void deleteNote(final Note note) {
|
||||
NoteService.deleteNote(note)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<Void>() {
|
||||
@Override
|
||||
public void onCompleted() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
ToastUtils.show(SearchActivity.this, R.string.delete_note_failed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Void aVoid) {
|
||||
mAdapter.delete(note);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
BIN
app/src/main/res/drawable-xxhdpi/ic_clear.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_clear.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 548 B |
BIN
app/src/main/res/drawable-xxhdpi/ic_search.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_search.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1009 B |
29
app/src/main/res/layout/activity_search.xml
Normal file
29
app/src/main/res/layout/activity_search.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/activity_search"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context="org.houxg.leamonax.ui.SearchActivity">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:background="@color/toolbar"
|
||||
android:elevation="4dp">
|
||||
|
||||
<android.support.v7.widget.SearchView
|
||||
android:id="@+id/search"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</android.support.v7.widget.Toolbar>
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/recycler_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</LinearLayout>
|
||||
10
app/src/main/res/menu/main.xml
Normal file
10
app/src/main/res/menu/main.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_search"
|
||||
android:icon="@drawable/ic_search"
|
||||
android:title="@string/search"
|
||||
app:showAsAction="always"/>
|
||||
</menu>
|
||||
@@ -62,4 +62,5 @@
|
||||
<string name="preview">Preview</string>
|
||||
<string name="change_password_successful">Change password successful</string>
|
||||
<string name="change_user_name_successful">Change user name successful</string>
|
||||
<string name="search">Search</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user