Skip to content

Commit

Permalink
sort note by UpdateTime,CreateTime, Title
Browse files Browse the repository at this point in the history
  • Loading branch information
xingstarx committed May 23, 2018
1 parent d25558c commit e7cc74e
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 2 deletions.
66 changes: 65 additions & 1 deletion app/src/main/java/org/houxg/leamonax/model/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public String getMsg() {
return msg;
}

public static class UpdateTimeComparetor implements Comparator<Note> {
public static class UpdateTimeDescComparetor implements Comparator<Note> {
@Override
public int compare(Note lhs, Note rhs) {
long lTime = lhs.getUpdatedTimeVal();
Expand All @@ -356,4 +356,68 @@ public int compare(Note lhs, Note rhs) {
}
}
}

public static class UpdateTimeAscComparetor implements Comparator<Note> {
@Override
public int compare(Note lhs, Note rhs) {
long lTime = lhs.getUpdatedTimeVal();
long rTime = rhs.getUpdatedTimeVal();
if (lTime > rTime) {
return 1;
} else if (lTime < rTime) {
return -1;
} else {
return 0;
}
}
}

public static class CreateTimeDescComparetor implements Comparator<Note> {
@Override
public int compare(Note lhs, Note rhs) {
long lTime = lhs.getCreatedTimeVal();
long rTime = rhs.getCreatedTimeVal();
if (lTime > rTime) {
return -1;
} else if (lTime < rTime) {
return 1;
} else {
return 0;
}
}
}

public static class CreateTimeAscComparetor implements Comparator<Note> {
@Override
public int compare(Note lhs, Note rhs) {
long lTime = lhs.getCreatedTimeVal();
long rTime = rhs.getCreatedTimeVal();
if (lTime > rTime) {
return 1;
} else if (lTime < rTime) {
return -1;
} else {
return 0;
}
}
}

public static class TitleDescComparetor implements Comparator<Note> {
@Override
public int compare(Note lhs, Note rhs) {
String lTitle = lhs.getTitle();
String rTitle = rhs.getTitle();
return lTitle.compareTo(rTitle);
}
}

public static class TitleAscComparetor implements Comparator<Note> {
@Override
public int compare(Note lhs, Note rhs) {
String lTitle = lhs.getTitle();
String rTitle = rhs.getTitle();
return rTitle.compareTo(lTitle);
}
}

}
6 changes: 6 additions & 0 deletions app/src/main/java/org/houxg/leamonax/ui/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public class BaseActivity extends AppCompatActivity {

private Toolbar mToolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -26,8 +27,13 @@ protected void initToolBar(Toolbar toolbar) {
initToolBar(toolbar, false);
}

public Toolbar getToolbar() {
return mToolbar;
}

protected void initToolBar(Toolbar toolbar, boolean hasBackArrow) {
if (toolbar != null) {
mToolbar = toolbar;
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xffFAFAFA);
toolbar.setTitle(getTitle());
Expand Down
49 changes: 48 additions & 1 deletion app/src/main/java/org/houxg/leamonax/ui/NoteFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import org.greenrobot.eventbus.EventBus;
import org.houxg.leamonax.Leamonax;
Expand All @@ -28,9 +30,11 @@
import org.houxg.leamonax.utils.SharedPreferenceUtils;
import org.houxg.leamonax.utils.ToastUtils;
import org.houxg.leamonax.widget.NoteList;
import org.houxg.leamonax.widget.SelectPopupWindow;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
Expand All @@ -57,6 +61,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
NoteList mNoteList;
Mode mCurrentMode;
OnSearchFinishListener mOnSearchFinishListener;
private int mSortType = -1;

public void setOnSearchFinishListener(OnSearchFinishListener onSearchFinishListener) {
this.mOnSearchFinishListener = onSearchFinishListener;
Expand All @@ -72,6 +77,7 @@ public static NoteFragment newInstance() {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSortType = SharedPreferenceUtils.read(SharedPreferenceUtils.CONFIG, SelectPopupWindow.SP_SORT_TYPE, -1);
setHasOptionsMenu(true);
}

Expand All @@ -86,10 +92,26 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_view_type) {
mNoteList.toggleType();
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_VIEW_TYPE, mNoteList.getType());
} else if (item.getItemId() == R.id.action_view_more) {
final SelectPopupWindow popupWindow = new SelectPopupWindow(getContext());
if (getActivity() instanceof BaseActivity) {
Toolbar toolbar = ((BaseActivity) getActivity()).getToolbar();
popupWindow.setOnItemClickListener(new SelectPopupWindow.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int value) {
mSortType = value;
renderNotes();
popupWindow.dismiss();
}
});
popupWindow.showPopWindow(toolbar);
}
}
return super.onOptionsItemSelected(item);
}



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand Down Expand Up @@ -148,13 +170,38 @@ public void setMode(Mode mode) {
notes = new ArrayList<>();
}
mNotes = notes;
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
renderNotes();
}

private void renderNotes() {
Collections.sort(mNotes, getComparatorBySortType(mSortType));
mNoteList.render(mNotes);
if (mNotes.size() == 0 && mOnSearchFinishListener != null) {
mOnSearchFinishListener.doSearchFinish();
}
}

private Comparator<Note> getComparatorBySortType(int sortType) {
switch (sortType) {
case 1:
return new Note.CreateTimeAscComparetor();
case 2:
return new Note.CreateTimeDescComparetor();
case 3:
return new Note.UpdateTimeAscComparetor();
case 4:
return new Note.UpdateTimeDescComparetor();
case 5:
return new Note.TitleAscComparetor();
case 6:
return new Note.TitleDescComparetor();
default:
return new Note.UpdateTimeDescComparetor();
}

}


@Override
public void onClickNote(Note note) {
if (mActionModeHandler.isActionMode()) {
Expand Down
115 changes: 115 additions & 0 deletions app/src/main/java/org/houxg/leamonax/widget/SelectPopupWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.houxg.leamonax.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.ListPopupWindow;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.houxg.leamonax.R;
import org.houxg.leamonax.utils.DisplayUtils;
import org.houxg.leamonax.utils.SharedPreferenceUtils;

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

public class SelectPopupWindow extends ListPopupWindow {
public static final String SP_SORT_TYPE = "sp_sort_type";
private Context mContext;
private OnItemClickListener mOnItemClickListener;
private Adapter mAdapter;
private int mChecked = -1;

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}

public SelectPopupWindow(Context context) {
super(context);
this.mContext = context;
}

@SuppressLint("RestrictedApi")
public void showPopWindow(View anchorView) {
String[] selectKey = mContext.getResources().getStringArray(R.array.select_key);
final String[] selectValue = mContext.getResources().getStringArray(R.array.select_value);
mAdapter = new Adapter();
mAdapter.setDatas(new ArrayList<>(Arrays.asList(selectKey)));
mChecked = SharedPreferenceUtils.read(SharedPreferenceUtils.CONFIG, SP_SORT_TYPE, mChecked);
mAdapter.setChecked(mChecked);
setAdapter(mAdapter);
setWidth(DisplayUtils.dp2px(180));
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setDropDownGravity(Gravity.END);
setAnchorView(anchorView);
setOverlapAnchor(true);
setModal(true);
setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int value = Integer.valueOf(selectValue[position]);
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_SORT_TYPE, value);
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(parent, view, value);
}
}
});
show();
}

class Adapter extends BaseAdapter {
List<String> datas = new ArrayList<>();
private int checked = -1;

void setDatas(List<String> datas) {
this.datas = datas;
}

@Override
public int getCount() {
return datas.size();
}

@Override
public String getItem(int position) {
return datas.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_item_1, parent, false);
}
ImageView checkView = convertView.findViewById(R.id.check);
TextView textView = convertView.findViewById(android.R.id.text1);
if (checked == (position + 1)) {
checkView.setVisibility(View.VISIBLE);
} else {
checkView.setVisibility(View.INVISIBLE);
}
textView.setText(getItem(position));
return convertView;
}

public void setChecked(int checked) {
this.checked = checked;
}
}

public interface OnItemClickListener {
void onItemClick(AdapterView<?> parent, View view, int value);
}

}
Binary file added app/src/main/res/drawable-xxhdpi/ic_menu_more.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_pop_checked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions app/src/main/res/layout/simple_list_item_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/check"
android:layout_width="36dp"
android:scaleType="center"
android:src="@drawable/ic_pop_checked"
android:layout_height="match_parent" />

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="36dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="?android:attr/textAppearanceListItemSmall" />

</LinearLayout>


6 changes: 6 additions & 0 deletions app/src/main/res/menu/note.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
android:icon="@drawable/ic_view_type"
android:title="@string/view_type"
app:showAsAction="always"/>

<item
android:id="@+id/action_view_more"
android:icon="@drawable/ic_menu_more"
app:showAsAction="always"
android:title="" />
</menu>
20 changes: 20 additions & 0 deletions app/src/main/res/values-zh/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="select_key">
<item>按创建时间-升序</item>
<item>按创建时间-降序</item>
<item>按更新时间-升序</item>
<item>按更新时间-降序</item>
<item>按标题-升序</item>
<item>按标题-降序</item>
</string-array>

<string-array name="select_value">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
</resources>
Loading

0 comments on commit e7cc74e

Please sign in to comment.