Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: set a note as favorite (star/unstar) #151

Merged
merged 1 commit into from
Nov 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class NoteTest extends TestCase {

public void testMarkDownStrip() {
OwnCloudNote note = new OwnCloudNote(0, Calendar.getInstance(), "#Title", "");
OwnCloudNote note = new OwnCloudNote(0, Calendar.getInstance(), "#Title", "", false);
assertTrue("Title".equals(note.getTitle()));
note.setTitle("* Aufzählung");
assertTrue("Aufzählung".equals(note.getTitle()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem itemFavorite = menu.findItem(R.id.menu_favorite);
itemFavorite.setIcon(note.isFavorite() ? R.drawable.ic_star_white_24dp : R.drawable.ic_star_outline_white_24dp);
itemFavorite.setChecked(note.isFavorite());
return super.onPrepareOptionsMenu(menu);
}

/**
* Main-Menu-Handler
*/
Expand All @@ -146,6 +154,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
setResult(RESULT_FIRST_USER, data);
finish();
return true;
case R.id.menu_favorite:
db.toggleFavorite(note, null);
invalidateOptionsMenu();
return true;
case R.id.menu_preview:
saveData(null);
Intent previewIntent = new Intent(getApplicationContext(), NoteActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.SearchManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.MenuItemCompat;
Expand Down Expand Up @@ -61,8 +62,7 @@ public void onFinish() {
if (mActionMode != null) {
mActionMode.finish();
}
// adapter.checkForUpdates(db.getNotes()); // FIXME deactivated, since it doesn't remove remotely deleted notes
setListView(db.getNotes());
refreshList();
swipeRefreshLayout.setRefreshing(false);
}
};
Expand All @@ -82,7 +82,8 @@ protected void onCreate(Bundle savedInstanceState) {

// Display Data
db = new NoteSQLiteOpenHelper(this);
setListView(db.getNotes());
initList();
refreshList();

// Pull to Refresh
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout);
Expand Down Expand Up @@ -144,6 +145,8 @@ protected void onResume() {
checkNotificationSetting();
if (db.getNoteServerSyncHelper().isSyncPossible()) {
synchronize();
} else {
refreshList();
}
super.onResume();
}
Expand All @@ -162,79 +165,110 @@ public void onClick(View v) {
}

/**
* Allows other classes to set a List of Notes.
*
* @param noteList List<Note>
* Allows other classes to refresh the List of Notes. Starts an AsyncTask which loads the data in the background.
*/
@SuppressWarnings("WeakerAccess")
public void setListView(List<DBNote> noteList) {
final List<Item> itemList = new ArrayList<>();
// #12 Create Sections depending on Time
// TODO Move to ItemAdapter?
boolean todaySet, yesterdaySet, weekSet, monthSet, earlierSet;
todaySet = yesterdaySet = weekSet = monthSet = earlierSet = false;
Calendar recent = Calendar.getInstance();
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar yesterday = Calendar.getInstance();
yesterday.set(Calendar.DAY_OF_YEAR, yesterday.get(Calendar.DAY_OF_YEAR) - 1);
yesterday.set(Calendar.HOUR_OF_DAY, 0);
yesterday.set(Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
yesterday.set(Calendar.MILLISECOND, 0);
Calendar week = Calendar.getInstance();
week.set(Calendar.DAY_OF_WEEK, week.getFirstDayOfWeek());
week.set(Calendar.HOUR_OF_DAY, 0);
week.set(Calendar.MINUTE, 0);
week.set(Calendar.SECOND, 0);
week.set(Calendar.MILLISECOND, 0);
Calendar month = Calendar.getInstance();
month.set(Calendar.DAY_OF_MONTH, 0);
month.set(Calendar.HOUR_OF_DAY, 0);
month.set(Calendar.MINUTE, 0);
month.set(Calendar.SECOND, 0);
month.set(Calendar.MILLISECOND, 0);
for (int i = 0; i < noteList.size(); i++) {
DBNote currentNote = noteList.get(i);
if (!todaySet && recent.getTimeInMillis() - currentNote.getModified().getTimeInMillis() >= 600000 && currentNote.getModified().getTimeInMillis() >= today.getTimeInMillis()) {
// < 10 minutes but after 00:00 today
//if (i > 0) {
//itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_today)));
//}
todaySet = true;
} else if (!yesterdaySet && currentNote.getModified().getTimeInMillis() < today.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= yesterday.getTimeInMillis()) {
// between today 00:00 and yesterday 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_yesterday)));
}
yesterdaySet = true;
} else if (!weekSet && currentNote.getModified().getTimeInMillis() < yesterday.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= week.getTimeInMillis()) {
// between yesterday 00:00 and start of the week 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_this_week)));
}
weekSet = true;
} else if (!monthSet && currentNote.getModified().getTimeInMillis() < week.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= month.getTimeInMillis()) {
// between start of the week 00:00 and start of the month 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_this_month)));
}
monthSet = true;
} else if (!earlierSet && currentNote.getModified().getTimeInMillis() < month.getTimeInMillis()) {
// before start of the month 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_earlier)));
public void refreshList() {
new RefreshListTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

private class RefreshListTask extends AsyncTask<Void, Void, List<Item>> {

private CharSequence query = null;

@Override
protected void onPreExecute() {
if(searchView != null && !searchView.isIconified() && searchView.getQuery().length() != 0) {
query = searchView.getQuery();
}
}

@Override
protected List<Item> doInBackground(Void... voids) {
List<DBNote> noteList;
if (query==null) {
noteList = db.getNotes();
} else {
noteList = db.searchNotes(query);
}

final List<Item> itemList = new ArrayList<>();
// #12 Create Sections depending on Time
// TODO Move to ItemAdapter?
boolean todaySet, yesterdaySet, weekSet, monthSet, earlierSet;
todaySet = yesterdaySet = weekSet = monthSet = earlierSet = false;
Calendar recent = Calendar.getInstance();
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar yesterday = Calendar.getInstance();
yesterday.set(Calendar.DAY_OF_YEAR, yesterday.get(Calendar.DAY_OF_YEAR) - 1);
yesterday.set(Calendar.HOUR_OF_DAY, 0);
yesterday.set(Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
yesterday.set(Calendar.MILLISECOND, 0);
Calendar week = Calendar.getInstance();
week.set(Calendar.DAY_OF_WEEK, week.getFirstDayOfWeek());
week.set(Calendar.HOUR_OF_DAY, 0);
week.set(Calendar.MINUTE, 0);
week.set(Calendar.SECOND, 0);
week.set(Calendar.MILLISECOND, 0);
Calendar month = Calendar.getInstance();
month.set(Calendar.DAY_OF_MONTH, 0);
month.set(Calendar.HOUR_OF_DAY, 0);
month.set(Calendar.MINUTE, 0);
month.set(Calendar.SECOND, 0);
month.set(Calendar.MILLISECOND, 0);
for (int i = 0; i < noteList.size(); i++) {
DBNote currentNote = noteList.get(i);
if (currentNote.isFavorite()) {
// don't show as new section
} else if (!todaySet && currentNote.getModified().getTimeInMillis() >= today.getTimeInMillis()) {
// after 00:00 today
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_today)));
}
todaySet = true;
} else if (!yesterdaySet && currentNote.getModified().getTimeInMillis() < today.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= yesterday.getTimeInMillis()) {
// between today 00:00 and yesterday 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_yesterday)));
}
yesterdaySet = true;
} else if (!weekSet && currentNote.getModified().getTimeInMillis() < yesterday.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= week.getTimeInMillis()) {
// between yesterday 00:00 and start of the week 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_this_week)));
}
weekSet = true;
} else if (!monthSet && currentNote.getModified().getTimeInMillis() < week.getTimeInMillis() && currentNote.getModified().getTimeInMillis() >= month.getTimeInMillis()) {
// between start of the week 00:00 and start of the month 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_this_month)));
}
monthSet = true;
} else if (!earlierSet && currentNote.getModified().getTimeInMillis() < month.getTimeInMillis()) {
// before start of the month 00:00
if (i > 0) {
itemList.add(new SectionItem(getResources().getString(R.string.listview_updated_earlier)));
}
earlierSet = true;
}
earlierSet = true;
itemList.add(currentNote);
}
itemList.add(currentNote);

return itemList;
}

adapter = new ItemAdapter(itemList);
ItemAdapter.setNoteClickListener(this);
@Override
protected void onPostExecute(List<Item> items) {
adapter.setItemList(items);
}
}

public void initList() {
adapter = new ItemAdapter(this);
listView = (RecyclerView) findViewById(R.id.recycler_view);
listView.setAdapter(adapter);
listView.setLayoutManager(new LinearLayoutManager(this));
Expand Down Expand Up @@ -296,31 +330,17 @@ public boolean onQueryTextSubmit(String query) {

@Override
public boolean onQueryTextChange(String newText) {
search(newText.trim());
refreshList();
return true;
}
});
return true;
}

private void search(final String query) {
new Thread() {
@Override
public void run() {
if (query.length() > 0) {
setListView(db.searchNotes(query));
} else {
setListView(db.getNotes());
}
listView.scrollToPosition(0);
}
}.run();
}

@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
search(intent.getStringExtra(SearchManager.QUERY));
searchView.setQuery(intent.getStringExtra(SearchManager.QUERY), true);
}
super.onNewIntent(intent);
}
Expand Down Expand Up @@ -377,12 +397,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
if (resultCode == RESULT_OK) {
DBNote editedNote = (DBNote) data.getExtras().getSerializable(EditNoteActivity.PARAM_NOTE);
if(oldItem instanceof DBNote && !editedNote.getModified().after(((DBNote)oldItem).getModified())) {
adapter.replace(editedNote, notePosition);
} else {
adapter.remove(oldItem);
adapter.add(editedNote);
}
adapter.replace(editedNote, notePosition);
refreshList();
}
}
} else if (requestCode == server_settings) {
Expand Down Expand Up @@ -435,6 +451,15 @@ public void onNoteClick(int position, View v) {
}
}

@Override
public void onNoteFavoriteClick(int position, View view) {
DBNote note = (DBNote) adapter.getItem(position);
NoteSQLiteOpenHelper db = new NoteSQLiteOpenHelper(view.getContext());
db.toggleFavorite(note, syncCallBack);
adapter.notifyItemChanged(position);
refreshList();
}

@Override
public boolean onNoteLongClick(int position, View v) {
boolean selected = adapter.select(position);
Expand Down Expand Up @@ -464,8 +489,7 @@ private void synchronize() {
/**
* Handler for the MultiSelect Actions
*/
private class MultiSelectedActionModeCallback implements
ActionMode.Callback {
private class MultiSelectedActionModeCallback implements ActionMode.Callback {

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
Expand Down Expand Up @@ -499,7 +523,7 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
mode.finish(); // Action picked, so close the CAB
//after delete selection has to be cleared
searchView.setIconified(true);
setListView(db.getNotes());
refreshList();
return true;
default:
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public void onCreate(Bundle savedInstanceState) {
private void setListView(List<DBNote> noteList) {
List<Item> itemList = new ArrayList<>();
itemList.addAll(noteList);
adapter = new ItemAdapter(itemList);
adapter = new ItemAdapter(this);
adapter.setItemList(itemList);
listView = (RecyclerView) findViewById(R.id.select_single_note_list_view);
listView.setAdapter(adapter);
listView.setLayoutManager(new LinearLayoutManager(this));
ItemAdapter.setNoteClickListener(this);
}

@Override
Expand All @@ -87,6 +87,10 @@ public void onNoteClick(int position, View v) {
finish();
}

@Override
public void onNoteFavoriteClick(int position, View v) {
}

@Override
public boolean onNoteLongClick(int position, View v) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public StackRemoteViewsFactory(Context context, Intent intent) {
NoteSQLiteOpenHelper db = new NoteSQLiteOpenHelper(mContext);
db.getNoteServerSyncHelper().scheduleSync(false);
mWidgetItems = db.getNotes();
mWidgetItems.add(new DBNote(0, 0, Calendar.getInstance(), "Test-Titel", "Test-Beschreibung", DBStatus.VOID));
mWidgetItems.add(new DBNote(0, 0, Calendar.getInstance(), "Test-Titel", "Test-Beschreibung", false, DBStatus.VOID));
}

public void onCreate() {
mWidgetItems.add(new DBNote(0, 0, Calendar.getInstance(), "Test-Titel", "Test-Beschreibung", DBStatus.VOID));
mWidgetItems.add(new DBNote(0, 0, Calendar.getInstance(), "Test-Titel", "Test-Beschreibung", false, DBStatus.VOID));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class DBNote extends OwnCloudNote implements Item, Serializable {
private DBStatus status;
private String excerpt = "";

public DBNote(long id, long remoteId, Calendar modified, String title, String content, DBStatus status) {
super(remoteId, modified, title, content);
public DBNote(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, DBStatus status) {
super(remoteId, modified, title, content, favorite);
this.id = id;
setExcerpt(content);
this.status = status;
Expand Down Expand Up @@ -60,6 +60,6 @@ public boolean isSection() {

@Override
public String toString() {
return "#" + getId() + "/R"+getRemoteId()+" " + getTitle() + " (" + getModified(NoteSQLiteOpenHelper.DATE_FORMAT) + ") " + getStatus();
return "#" + getId() + "/R"+getRemoteId()+" " + (isFavorite() ? " (*) " : " ") + getTitle() + " (" + getModified(NoteSQLiteOpenHelper.DATE_FORMAT) + ") " + getStatus();
}
}
Loading