Skip to content
Open
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
76 changes: 75 additions & 1 deletion app/src/main/java/com/materialnotes/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,51 @@ public void onClick(View view) {
}
});
selectedPositions = new ArrayList<>();
listView.setTextFilterEnabled(true);
setupNotesAdapter();
setupActionModeCallback();
setListOnItemClickListenersWhenNoActionMode();
updateView();
}

/** {@inheritDoc} */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default

SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String query)
{

listAdapter.getFilter().filter(query);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered

return false;
}
};

searchView.setOnCloseListener( new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
listAdapter.getFilter().filter("");
return false;
}
});
searchView.setOnQueryTextListener(textChangeListener);
return true;
}

Expand Down Expand Up @@ -156,6 +191,16 @@ public void onClick(DialogInterface dialog, int which) {
.show();
} else mode.finish();
return true;

case R.id.action_share:
if (!selectedPositions.isEmpty()){
shareNotes(selectedPositions);
}
return true;

case R.id.action_select_all:
selectAll();
return true;
default:
return false;
}
Expand Down Expand Up @@ -193,6 +238,15 @@ private void updateView() {
}
}

/* Method called when select all menu is clicked */
private void selectAll() {
for ( int i=0; i < listAdapter.getCount(); i++) {
selectedPositions.add(i);
notesData.get(i).setSelected(true);
listAdapter.notifyDataSetChanged();
}
}

/**
* Agrega una nota a lista y la fuente de datos.
*
Expand Down Expand Up @@ -245,6 +299,26 @@ private void updateNote(Intent data) {
listAdapter.notifyDataSetChanged();
}

/*
this method is called when the contextual action button of share is pressed
*/
private void shareNotes(ArrayList<Integer> selectedPositions) {
StringBuilder shareBody = new StringBuilder();
// Primero borra de la base de datos
for (int position : selectedPositions) {
NotesAdapter.NoteViewWrapper noteViewWrapper = notesData.get(position);
shareBody.append(noteViewWrapper.getNote().getTitle());
shareBody.append(noteViewWrapper.getNote().getContent());
shreBody.append("\n")
}

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");

sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody.toString());
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
}

/** Reinicia las notas seleccionadas a no seleccionadas y limpia la lista de seleccionados. */
private void resetSelectedListItems() {
for (NotesAdapter.NoteViewWrapper noteViewWrapper : notesData) noteViewWrapper.setSelected(false);
Expand Down Expand Up @@ -309,4 +383,4 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
}
});
}
}
}
55 changes: 50 additions & 5 deletions app/src/main/java/com/materialnotes/widget/NotesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.materialnotes.R;
import com.materialnotes.data.Note;
import android.widget.Filter;
import android.widget.Filterable;

import java.text.DateFormat;
import java.util.List;
Expand All @@ -18,7 +20,7 @@
* @author Daniel Pedraza Arcega
* @see <a href="http://bit.ly/1vZt3ny">Building Layouts with an Adapter</a>
*/
public class NotesAdapter extends BaseAdapter {
public class NotesAdapter extends BaseAdapter implements Filterable {

/** Wrapper para notas. Util para cambiar el fondo de los item seleccionados. */
public static class NoteViewWrapper {
Expand Down Expand Up @@ -47,6 +49,7 @@ public void setSelected(boolean isSelected) {
private static final DateFormat DATETIME_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

private final List<NoteViewWrapper> data;
private List<NoteViewWrapper> filtered_data;

/**
* Constructor.
Expand All @@ -55,12 +58,13 @@ public void setSelected(boolean isSelected) {
*/
public NotesAdapter(List<NoteViewWrapper> data) {
this.data = data;
this.filtered_data = data;
}

/** @return cuantos datos hay en la lista de notas. */
@Override
public int getCount() {
return data.size();
return filtered_data.size();
}

/**
Expand All @@ -69,7 +73,7 @@ public int getCount() {
*/
@Override
public NoteViewWrapper getItem(int position) {
return data.get(position);
return filtered_data.get(position);
}

/**
Expand Down Expand Up @@ -100,7 +104,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
convertView.setTag(holder);
} else holder = (ViewHolder) convertView.getTag(); // ya existe, solo es reciclarlo
// Inicializa la vista con los datos de la nota
NoteViewWrapper noteViewWrapper = data.get(position);
NoteViewWrapper noteViewWrapper = filtered_data.get(position);
holder.noteIdText.setText(String.valueOf(noteViewWrapper.note.getId()));
holder.noteTitleText.setText(noteViewWrapper.note.getTitle());
// Corta la cadena a 80 caracteres y le agrega "..."
Expand Down Expand Up @@ -136,4 +140,45 @@ private ViewHolder(View parent) {
noteDateText = (TextView) parent.findViewById(R.id.note_date);
}
}
}


@Override
public Filter getFilter() {
Filter filter = new Filter(){

@Override
protected FilterResults performFiltering(CharSequence query) {
FilterResults results = new FilterResults();
ArrayList<NoteViewWrapper> filtered = new ArrayList<>();
query = query.toString().toLowerCase();
if(query!=null && query.length()>0){
for(int i=0;i<data.size();i++){
String dataName = data.get(i).getNote().getContent().toLowerCase();
String title = data.get(i).getNote().getTitle().toLowerCase();
if (dataName.contains(query) || title.contain(query)){
filtered.add(data.get(i));
}

}
results.count = filtered.size();
results.values = filtered;

}else{
results.count = data.size();
results.values = data;
}

return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered_data = (ArrayList<NoteViewWrapper>) results.values;
NotesAdapter.this.notifyDataSetChanged();
}
};

return filter;
}

}
13 changes: 12 additions & 1 deletion app/src/main/res/menu/context_note.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@
NotepadApp:showAsAction="ifRoom"
android:icon="@drawable/ic_action_delete"
android:title="@string/action_delete"/>

<item
android:id="@+id/action_share"
NotepadApp:showAsAction="always"
android:icon="@android:drawable/ic_menu_share"
android:title="@string/action_share"
/>
<item
android:id="@+id/action_select_all"
NotepadApp:showAsAction="ifRoom"
android:title="@string/action_select_all"/>

</menu>
</menu>
10 changes: 8 additions & 2 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:NotepadApp="http://schemas.android.com/apk/res-auto">


<item
android:id="@+id/action_search"
NotepadApp:showAsAction="always"
android:title="@string/search_hint"
NotepadApp:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"/>
<item
android:id="@+id/action_about_info"
NotepadApp:showAsAction="never"
Expand All @@ -11,4 +17,4 @@
NotepadApp:showAsAction="never"
android:title="@string/action_licenses_notice" />

</menu>
</menu>
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<string name="action_about_notice">About</string>
<string name="action_licenses_notice">Open source licenses</string>
<string name="delete_notes_alert">Delete %s note(s)?</string>

<string name="search_hint">Search notes</string>
<string name="action_select_all">Select all</string>


<!-- Licenses dialog -->
<string name="dialog_licenses_notice_title">Open source licenses</string>
Expand All @@ -32,4 +36,4 @@
<string name="label_created_at">Created at:</string>
<string name="label_updated_at">Updated at:</string>

</resources>
</resources>