support show notes by tag

This commit is contained in:
houxg
2016-11-30 14:40:29 +08:00
parent f22da5f40f
commit 9beba839c1
11 changed files with 311 additions and 49 deletions

View File

@@ -25,7 +25,7 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
private static final int TYPE_NOTEBOOK = 46;
private static final int TYPE_ADD = 735;
private Stack<String> mStack;
private Stack<String> mStack = new Stack<>();
private List<Notebook> mData;
private NotebookAdapterListener mListener;
@@ -33,21 +33,15 @@ public class NotebookAdapter extends RecyclerView.Adapter<NotebookAdapter.Notebo
mListener = listener;
}
public void init() {
mData = AppDataBase.getRootNotebooks(AccountService.getCurrent().getUserId());
mStack = new Stack<>();
notifyDataSetChanged();
}
public void reload() {
public void refresh() {
if (mStack.isEmpty()) {
init();
mData = AppDataBase.getRootNotebooks(AccountService.getCurrent().getUserId());
} else {
Notebook parent = mData.get(0);
mData = AppDataBase.getChildNotebook(mStack.peek(), AccountService.getCurrent().getUserId());
mData.add(0, parent);
notifyDataSetChanged();
}
notifyDataSetChanged();
}
private String getCurrentParentId() {

View File

@@ -0,0 +1,92 @@
package org.houxg.leamonax.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.houxg.leamonax.R;
import org.houxg.leamonax.database.AppDataBase;
import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.service.AccountService;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class TagAdapter extends RecyclerView.Adapter<TagAdapter.TagHolder> {
private List<Tag> mData;
private TagAdapterListener mListener;
public void setListener(TagAdapterListener listener) {
mListener = listener;
}
public void refresh() {
mData = AppDataBase.getAllTags(AccountService.getCurrent().getUserId());
notifyDataSetChanged();
}
public void toggle() {
int size = getItemCount();
if (size == 0) {
mData = AppDataBase.getAllTags(AccountService.getCurrent().getUserId());
int newSize = getItemCount();
if (newSize != 0) {
notifyItemRangeInserted(0, newSize);
} else {
notifyDataSetChanged();
}
} else {
mData = new ArrayList<>();
notifyItemRangeRemoved(0, size);
}
}
@Override
public TagHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tag, parent, false);
return new TagHolder(view);
}
@Override
public void onBindViewHolder(TagHolder holder, int position) {
final Tag tag = mData.get(position);
holder.titleTv.setText(tag.getText());
holder.titleTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onClickedTag(tag);
}
}
});
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
public interface TagAdapterListener {
void onClickedTag(Tag tag);
}
static class TagHolder extends RecyclerView.ViewHolder {
View itemView;
@BindView(R.id.tv_title)
TextView titleTv;
public TagHolder(View itemView) {
super(itemView);
this.itemView = itemView;
ButterKnife.bind(this, itemView);
}
}
}

View File

@@ -9,6 +9,7 @@ import com.raizlabs.android.dbflow.annotation.Migration;
import com.raizlabs.android.dbflow.sql.language.Join;
import com.raizlabs.android.dbflow.sql.language.NameAlias;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.sql.language.property.IProperty;
import com.raizlabs.android.dbflow.sql.migration.BaseMigration;
import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper;
@@ -115,6 +116,14 @@ public class AppDataBase {
.queryList();
}
public static List<Note> getNotesByTagText(String tagText, String userId) {
Tag tag = getTagByText(tagText, userId);
if (tag == null) {
return new ArrayList<>();
}
return getNotesByTagId(tag.getId());
}
public static List<Note> getAllNotes(String userId) {
return SQLite.select()
.from(Note.class)
@@ -225,6 +234,21 @@ public class AppDataBase {
.queryList();
}
public static List<Note> getNotesByTagId(long tagId) {
IProperty[] properties = Note_Table.ALL_COLUMN_PROPERTIES;
NameAlias nameAlias = NameAlias.builder("N").build();
for (int i = 0; i < properties.length; i++) {
properties[i] = properties[i].withTable(nameAlias);
}
return SQLite.select(properties)
.from(Note.class).as("N")
.join(RelationshipOfNoteTag.class, Join.JoinType.INNER).as("R")
.on(Tag_Table.id.withTable(NameAlias.builder("N").build())
.eq(RelationshipOfNoteTag_Table.noteLocalId.withTable(NameAlias.builder("R").build())))
.where(RelationshipOfNoteTag_Table.tagLocalId.withTable(NameAlias.builder("R").build()).eq(tagId))
.queryList();
}
public static RelationshipOfNoteTag getRelationShip(long noteLocalId, long tagId, String userId) {
return SQLite.select()
.from(RelationshipOfNoteTag.class)

View File

@@ -29,10 +29,12 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.houxg.leamonax.R;
import org.houxg.leamonax.adapter.NotebookAdapter;
import org.houxg.leamonax.adapter.TagAdapter;
import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.Notebook;
import org.houxg.leamonax.model.SyncEvent;
import org.houxg.leamonax.model.Tag;
import org.houxg.leamonax.model.User;
import org.houxg.leamonax.service.AccountService;
import org.houxg.leamonax.service.NotebookService;
@@ -48,7 +50,7 @@ import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class MainActivity extends BaseActivity implements NotebookAdapter.NotebookAdapterListener {
public class MainActivity extends BaseActivity implements NotebookAdapter.NotebookAdapterListener, TagAdapter.TagAdapterListener {
private static final String EXT_SHOULD_RELOAD = "ext_should_reload";
private static final String TAG_NOTE_FRAGMENT = "tag_note_fragment";
@@ -69,6 +71,10 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
View mNotebookTriangle;
@BindView(R.id.rl_notebook_list)
View mNotebookPanel;
@BindView(R.id.rv_tag)
RecyclerView mTagRv;
@BindView(R.id.iv_tag_triangle)
View mTagTriangle;
public static Intent getOpenIntent(Context context, boolean shouldReload) {
Intent intent = new Intent(context, MainActivity.class);
@@ -91,23 +97,36 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
} else {
mNoteFragment = (NoteFragment) getFragmentManager().findFragmentByTag(TAG_NOTE_FRAGMENT);
}
mNotebookRv.setLayoutManager(new LinearLayoutManager(this));
NotebookAdapter adapter = new NotebookAdapter();
adapter.setListener(this);
mNotebookRv.setAdapter(adapter);
adapter.init();
mEmailTv.setText(AccountService.getCurrent().getEmail());
mNotebookTriangle.setTag(false);
initNotebookPanel();
initTagPanel();
refreshInfo();
fetchInfo();
EventBus.getDefault().register(this);
}
private void initTagPanel() {
mTagRv.setLayoutManager(new LinearLayoutManager(this));
TagAdapter tagAdapter = new TagAdapter();
tagAdapter.setListener(this);
mTagRv.setAdapter(tagAdapter);
mTagTriangle.setTag(false);
}
private void initNotebookPanel() {
mNotebookRv.setLayoutManager(new LinearLayoutManager(this));
NotebookAdapter notebookAdapter = new NotebookAdapter();
notebookAdapter.setListener(this);
mNotebookRv.setAdapter(notebookAdapter);
notebookAdapter.refresh();
mNotebookTriangle.setTag(false);
}
@Override
protected void onResume() {
super.onResume();
((NotebookAdapter) mNotebookRv.getAdapter()).reload();
((NotebookAdapter) mNotebookRv.getAdapter()).refresh();
}
@Override
@@ -185,7 +204,7 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
@Override
public void onClickedNotebook(Notebook notebook) {
mNoteFragment.loadNoteFromLocal(notebook.getId());
mNoteFragment.loadFromNotebook(notebook.getId());
mDrawerLayout.closeDrawer(GravityCompat.START, true);
}
@@ -233,7 +252,7 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
@Override
public void onNext(Void isSucceed) {
((NotebookAdapter) mNotebookRv.getAdapter()).reload();
((NotebookAdapter) mNotebookRv.getAdapter()).refresh();
}
});
}
@@ -251,7 +270,7 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
@OnClick(R.id.rl_recent_notes)
void showRecentNote() {
mNoteFragment.loadNoteFromLocal(NoteFragment.RECENT_NOTES);
mNoteFragment.loadRecentNotes();
mDrawerLayout.closeDrawer(GravityCompat.START, true);
}
@@ -271,25 +290,44 @@ public class MainActivity extends BaseActivity implements NotebookAdapter.Notebo
void toggleNotebook() {
boolean shouldShowNotebook = (boolean) mNotebookTriangle.getTag();
shouldShowNotebook = !shouldShowNotebook;
if (shouldShowNotebook) {
mNotebookTriangle.animate()
.rotation(180)
animateTriangle(mNotebookTriangle, shouldShowNotebook);
mNotebookPanel.setVisibility(shouldShowNotebook ? View.VISIBLE : View.GONE);
mNotebookTriangle.setTag(shouldShowNotebook);
}
@OnClick(R.id.rl_tag)
void toggleTag() {
boolean shouldShowTag = (boolean) mTagTriangle.getTag();
shouldShowTag = !shouldShowTag;
animateTriangle(mTagTriangle, shouldShowTag);
((TagAdapter) mTagRv.getAdapter()).toggle();
mTagTriangle.setTag(shouldShowTag);
}
private void animateTriangle(View triangle, boolean isOpen) {
if (isOpen) {
triangle.animate()
.rotation(-180)
.setDuration(200)
.setInterpolator(new AccelerateDecelerateInterpolator())
.start();
} else {
mNotebookTriangle.animate()
triangle.animate()
.rotation(0)
.setDuration(200)
.setInterpolator(new AccelerateDecelerateInterpolator())
.start();
}
mNotebookPanel.setVisibility(shouldShowNotebook ? View.VISIBLE : View.GONE);
mNotebookTriangle.setTag(shouldShowNotebook);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(SyncEvent event) {
((NotebookAdapter) mNotebookRv.getAdapter()).reload();
((NotebookAdapter) mNotebookRv.getAdapter()).refresh();
}
@Override
public void onClickedTag(Tag tag) {
mNoteFragment.loadFromTag(tag.getText());
mDrawerLayout.closeDrawer(GravityCompat.START, true);
}
}

View File

@@ -48,7 +48,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
private static final String EXT_SCROLL_POSITION = "ext_scroll_position";
private static final String EXT_SHOULD_FETCH_NOTES = "ext_should_fetch_notes";
public static final int RECENT_NOTES = -1;
Mode mCurrentMode = Mode.RECENT_NOTES;
@BindView(R.id.recycler_view)
RecyclerView mNoteListView;
@@ -58,7 +58,6 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
List<Note> mNotes;
private NoteAdapter mAdapter;
private long mCurrentNotebookId = RECENT_NOTES;
private float mScrollPosition;
public NoteFragment() {
@@ -123,7 +122,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
super.onActivityCreated(savedInstanceState);
EventBus.getDefault().register(this);
if (savedInstanceState == null) {
loadNoteFromLocal(RECENT_NOTES);
refreshNotes();
if (getArguments().getBoolean(EXT_SHOULD_FETCH_NOTES, false)) {
mSwipeRefresh.postDelayed(new Runnable() {
@Override
@@ -144,7 +143,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
public void onResume() {
super.onResume();
mNoteListView.scrollTo(0, (int) mScrollPosition);
loadNoteFromLocal(mCurrentNotebookId);
refreshNotes();
}
@Override
@@ -159,13 +158,33 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
EventBus.getDefault().unregister(this);
}
public void loadNoteFromLocal(long notebookLocalId) {
if (notebookLocalId < 0) {
mCurrentNotebookId = RECENT_NOTES;
mNotes = AppDataBase.getAllNotes(AccountService.getCurrent().getUserId());
} else {
mCurrentNotebookId = notebookLocalId;
mNotes = AppDataBase.getNotesFromNotebook(AccountService.getCurrent().getUserId(), notebookLocalId);
public void loadRecentNotes() {
mCurrentMode = Mode.RECENT_NOTES;
refreshNotes();
}
public void loadFromNotebook(long notebookId) {
mCurrentMode = Mode.NOTEBOOK;
mCurrentMode.notebookId = notebookId;
refreshNotes();
}
public void loadFromTag(String tagText) {
mCurrentMode = Mode.TAG;
mCurrentMode.tagText = tagText;
refreshNotes();
}
private void refreshNotes() {
switch (mCurrentMode) {
case RECENT_NOTES:
mNotes = AppDataBase.getAllNotes(AccountService.getCurrent().getUserId());
break;
case NOTEBOOK:
mNotes = AppDataBase.getNotesFromNotebook(AccountService.getCurrent().getUserId(), mCurrentMode.notebookId);
break;
case TAG:
mNotes = AppDataBase.getNotesByTagText(mCurrentMode.tagText, AccountService.getCurrent().getUserId());
}
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
mAdapter.load(mNotes);
@@ -225,10 +244,27 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
Log.i(TAG, "RequestNotes rcv: isSucceed=" + event.isSucceed());
if (isAdded()) {
mSwipeRefresh.setRefreshing(false);
loadNoteFromLocal(mCurrentNotebookId);
refreshNotes();
if (!event.isSucceed()) {
ToastUtils.show(getActivity(), R.string.sync_notes_failed);
}
}
}
private enum Mode {
RECENT_NOTES,
NOTEBOOK,
TAG;
long notebookId;
String tagText;
public void setNotebookId(long notebookId) {
this.notebookId = notebookId;
}
public void setTagText(String tagText) {
this.tagText = tagText;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="@+id/navigator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:src="@drawable/ic_tag" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="56dp"
android:fontFamily="sans-serif-medium"
android:gravity="start|center_vertical"
android:textColor="@color/primary_text_light"
android:textSize="14sp"
tools:text="NotebookTitle" />
</RelativeLayout>

View File

@@ -1,8 +1,8 @@
<?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:layout_width="240dp"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="start"
android:background="@color/navigation"
android:clickable="true"
@@ -32,19 +32,19 @@
<TextView
android:id="@+id/tv_user_name"
tools:text="exercitation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/secondary_text_light"
android:textSize="14sp" />
android:textSize="14sp"
tools:text="exercitation" />
<TextView
android:id="@+id/tv_email"
tools:text="quis@incididunt.com"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/secondary_text_light"
android:textSize="14sp" />
android:textSize="14sp"
tools:text="quis@incididunt.com" />
</LinearLayout>
@@ -89,16 +89,17 @@
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_folder" />
android:src="@drawable/ic_folder"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="48dp"
android:layout_marginStart="0dp"
android:fontFamily="sans-serif-medium"
android:text="Notebooks"
android:text="@string/notebooks"
android:textColor="@color/hint_text_light"
android:textSize="14sp" />
@@ -126,10 +127,57 @@
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_tag_book"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="0dp"
android:fontFamily="sans-serif-medium"
android:text="@string/tags"
android:textColor="@color/hint_text_light"
android:textSize="14sp" />
<ImageView
android:id="@+id/iv_tag_triangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:padding="8dp"
android:src="@drawable/ic_triangle" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_tag"
android:layout_width="240dp"
android:layout_height="wrap_content" />
<View
android:layout_width="wrap_content"
android:layout_height="8dp" />
<include layout="@layout/divider" />
<RelativeLayout
android:id="@+id/rl_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="16dp">
<ImageView

View File

@@ -63,4 +63,5 @@
<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>
<string name="notebooks">Notebooks</string>
</resources>