From bb5c20b20a472272f81ab9b2d49cd421c5fd2796 Mon Sep 17 00:00:00 2001 From: Sergey Variukhin Date: Fri, 30 Oct 2015 13:46:30 +1000 Subject: [PATCH] All done --- .../dummynotes/DummyNotesDbHelper.java | 2 +- .../myitschool/dummynotes/EditFragment.java | 72 +++++++++++- .../myitschool/dummynotes/ListFragment.java | 16 ++- .../myitschool/dummynotes/MainActivity.java | 75 ++++++++----- .../java/ru/myitschool/dummynotes/Note.java | 10 ++ .../myitschool/dummynotes/NotesAdapter.java | 28 +++-- .../myitschool/dummynotes/NotesService.java | 103 ++++++++++++++++-- 7 files changed, 252 insertions(+), 54 deletions(-) diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/DummyNotesDbHelper.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/DummyNotesDbHelper.java index 9a3e0a3..a07b012 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/DummyNotesDbHelper.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/DummyNotesDbHelper.java @@ -21,7 +21,7 @@ public class DummyNotesDbHelper extends SQLiteOpenHelper { NoteEntry._ID + " INTEGER PRIMARY KEY," + NoteEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP + NoteEntry.COLUMN_NAME_TEXT + TEXT_TYPE + COMMA_SEP + - NoteEntry.COLUMN_NAME_DATE + TEXT_TYPE + COMMA_SEP + + NoteEntry.COLUMN_NAME_DATE + TEXT_TYPE + " )"; private static final String SQL_DELETE_ENTRIES = diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/EditFragment.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/EditFragment.java index 80b6dc2..7ba43fd 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/EditFragment.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/EditFragment.java @@ -1,20 +1,90 @@ package ru.myitschool.dummynotes; import android.app.Fragment; +import android.content.Intent; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; /** * Created by student on 20.10.15. */ public class EditFragment extends Fragment { + + private Note note; + private boolean edited = false; + + @Override + public void onStart() { + super.onStart(); + // watch for changes + TextWatcher textWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} + @Override + public void afterTextChanged(Editable s) { + edited = true; + } + }; + ((EditText) getView().findViewById(R.id.nameEditText)).addTextChangedListener(textWatcher); + ((EditText) getView().findViewById(R.id.editText)).addTextChangedListener(textWatcher); + // set toolbar of Activity + ((MainActivity) getActivity()).clearMenu(); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // get note and show its content + NotesService notesService = new NotesService(getActivity()); + long id = getArguments().getLong("id"); + if (id != -1) + note = notesService.getNote(id); + else + note = new Note(); // Inflate the layout for this fragment - return inflater.inflate(R.layout.edit_note, container, false); + View rootView = inflater.inflate(R.layout.edit_note, container, false); + // place info in views + ((EditText) rootView.findViewById(R.id.nameEditText)).setText(note.title); + ((EditText) rootView.findViewById(R.id.editText)).setText(note.text); + ((TextView) rootView.findViewById(R.id.dateTextView)).setText(note.date); + // creating button for sending text + ((Button) rootView.findViewById(R.id.sendButton)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent sendIntent = new Intent(); + sendIntent.setAction(Intent.ACTION_SEND); + updateNoteByEdits(); + sendIntent.putExtra(Intent.EXTRA_TEXT, note.title + "\n" + note.text); + sendIntent.setType("text/plain"); + startActivity(sendIntent); + } + }); + return rootView; + } + + private void updateNoteByEdits(){ + note.title = ((EditText) getView().findViewById(R.id.nameEditText)).getText().toString(); + note.text = ((EditText) getView().findViewById(R.id.editText)).getText().toString(); + } + + @Override + public void onPause() { + super.onPause(); + if (!edited) + return; + updateNoteByEdits(); + NotesService notesService = new NotesService(getActivity()); + notesService.saveNote(note); + edited = false; } } diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/ListFragment.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/ListFragment.java index 0eefdaf..58ad78d 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/ListFragment.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/ListFragment.java @@ -13,6 +13,13 @@ * Created by teacher on 21.10.15. */ public class ListFragment extends Fragment { + @Override + public void onStart() { + super.onStart(); + // set toolbar of Activity + ((MainActivity) getActivity()).fillMenu(); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -20,7 +27,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, View rootView = inflater.inflate(R.layout.list_notes, container, false); // list adapter final ListView listView = (ListView) rootView.findViewById(R.id.listView); - listView.setAdapter(new NotesAdapter(getActivity())); + String sortMode = getArguments().getString("sortMode"); + String searchString = getArguments().getString("searchString"); + if (sortMode.equals("date")) + listView.setAdapter(new NotesAdapter(getActivity(), NotesService.SortMode.DATE, searchString)); + else + listView.setAdapter(new NotesAdapter(getActivity(), NotesService.SortMode.TITLE, searchString)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { @@ -31,7 +43,7 @@ public void onItemClick(AdapterView parent, View view, int position, long id) rootView.findViewById(R.id.add_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Toast.makeText(getActivity(), "Add something!", Toast.LENGTH_SHORT).show(); + ((MainActivity) getActivity()).editNote(-1); } }); return rootView; diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/MainActivity.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/MainActivity.java index 8a0f5ee..ad343d7 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/MainActivity.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/MainActivity.java @@ -26,6 +26,7 @@ public class MainActivity extends AppCompatActivity { private Context mContext = this; private Toolbar mToolbar; private SharedPreferences mPrefs; + private String sortMode = "date"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -33,40 +34,57 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); - - // place list of notes - FragmentManager fragmentManager = getFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - ListFragment listFragment = new ListFragment(); - fragmentTransaction.replace(R.id.controls, listFragment); - fragmentTransaction.commit(); - // Search work + // determine action Intent intent = getIntent(); if (Intent.ACTION_SEARCH.equals(intent.getAction())) { + // Search work String query = intent.getStringExtra(SearchManager.QUERY); - doSearch(query); + mToolbar.setSubtitle("Searching: " + query); + if (savedInstanceState == null) + refreshList(query); + } else { + // place list of notes + if (savedInstanceState == null) + refreshList(""); } } - private void doSearch(String query){ - Toast.makeText(mContext, "Searching: " + query, Toast.LENGTH_SHORT).show(); + public void clearMenu(){ + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + mToolbar.getMenu().clear(); } public void editNote(long id){ FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); EditFragment editFragment = new EditFragment(); + Bundle bundle = new Bundle(); + bundle.putLong("id", id); + editFragment.setArguments(bundle); fragmentTransaction.replace(R.id.controls, editFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - mToolbar.getMenu().clear(); } - public void showList(){ + public void refreshList(String searchString){ FragmentManager fragmentManager = getFragmentManager(); - fragmentManager.popBackStack(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + ListFragment listFragment = new ListFragment(); + Bundle bundle = new Bundle(); + bundle.putString("sortMode", sortMode); + bundle.putString("searchString", searchString); + listFragment.setArguments(bundle); + fragmentTransaction.replace(R.id.controls, listFragment); + fragmentTransaction.commit(); + } + + public void fillMenu(){ + clearMenu(); mToolbar.inflateMenu(R.menu.menu_main); + Menu menu = mToolbar.getMenu(); + final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); + SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); + searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setHomeButtonEnabled(false); // disable the button @@ -75,13 +93,16 @@ public void showList(){ } } + public void showList(){ + FragmentManager fragmentManager = getFragmentManager(); + fragmentManager.popBackStack(); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search)); - SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE); - searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); + FragmentManager fragmentManager = getFragmentManager(); + if(fragmentManager.getBackStackEntryCount() == 0) + fillMenu(); return true; } @@ -94,8 +115,12 @@ public boolean onOptionsItemSelected(MenuItem item) { //noinspection SimplifiableIfStatement if (id == R.id.action_sort){ - Toast.makeText(mContext, "Sort", Toast.LENGTH_SHORT).show(); - return true; + if (sortMode.equals("date")) + sortMode = "title"; + else + sortMode = "date"; + Toast.makeText(mContext, "Sort by " + sortMode, Toast.LENGTH_SHORT).show(); + refreshList(""); } if (id == android.R.id.home) { showList(); @@ -114,10 +139,4 @@ public void onBackPressed() { } } - protected void onSaveInstanceState (Bundle outState){ - super.onSaveInstanceState(outState); - - //FIXME: save current list position, opened note id, sorting mode - } - } diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/Note.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/Note.java index d7270b0..a3afa1a 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/Note.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/Note.java @@ -1,5 +1,8 @@ package ru.myitschool.dummynotes; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * Created by cepreu on 22.10.15. */ @@ -10,6 +13,13 @@ public class Note { String date; String text; + Note(){ + this.id = -1; + this.title = ""; + this.date = null; + this.text = ""; + } + Note(long id, String title, String date, String text){ this.id = id; this.title = title; diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesAdapter.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesAdapter.java index 5443932..1916ee4 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesAdapter.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesAdapter.java @@ -8,49 +8,53 @@ import android.widget.TextView; import android.widget.Toast; +import java.util.List; + /** * Created by teacher on 24.09.15. */ public class NotesAdapter extends BaseAdapter { private Context mContext; - private NotesService notesService; - - String[] names = { "Иван", "Марья", "Петр", "Антон", "Даша", "Борис", - "Костя", "Игорь", "Анна", "Денис", "Андрей" }; + private List notesList; + private String searchString; LayoutInflater inflater; - NotesAdapter(Context context){ + NotesAdapter(Context context, NotesService.SortMode sortMode, String searchString){ inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mContext = context; - notesService = new NotesService(context); + NotesService notesService = new NotesService(context); + notesList = notesService.getNotesList(sortMode, searchString); } @Override public int getCount() { - return names.length; + return notesList.size(); } @Override public Object getItem(int position) { - return names[position]; + return notesList.get(position); } @Override public long getItemId(int position) { - return position; + return notesList.get(position).id; } @Override - public View getView(int position, View convertView, ViewGroup parent) { + public View getView(final int position, View convertView, ViewGroup parent) { View view = inflater.inflate(R.layout.list_item, parent, false); final int mPosition = position; - ((TextView) view.findViewById(R.id.text_data)).setText(names[position]); + ((TextView) view.findViewById(R.id.text_data)).setText(notesList.get(position).title); view.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Toast.makeText(mContext, "Delete the " + mPosition, Toast.LENGTH_SHORT).show(); + NotesService notesService = new NotesService(mContext); + notesService.deleteNote(notesList.get(position).id); + notesList.remove(position); + notifyDataSetChanged(); } }); return view; diff --git a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesService.java b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesService.java index 60ab1f9..a5d80f5 100644 --- a/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesService.java +++ b/DummyNotes/app/src/main/java/ru/myitschool/dummynotes/NotesService.java @@ -1,11 +1,14 @@ package ru.myitschool.dummynotes; +import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import ru.myitschool.dummynotes.DummyNotesContract.NoteEntry; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -21,7 +24,7 @@ public NotesService(Context context){ mDbHelper = new DummyNotesDbHelper(context); } - public List getNotesList(SortMode sortMode){ + public List getNotesList(SortMode sortMode, String searchString){ SQLiteDatabase db = mDbHelper.getReadableDatabase(); String[] projection = { NoteEntry._ID, @@ -32,15 +35,31 @@ public List getNotesList(SortMode sortMode){ if (sortMode == SortMode.TITLE){ sortOrder = NoteEntry.COLUMN_NAME_TITLE + " DESC"; } - Cursor cursor = db.query( - NoteEntry.TABLE_NAME, // The table to query - projection, // The columns to return - null, // The columns for the WHERE clause - null, // The values for the WHERE clause - null, // don't group the rows - null, // don't filter by row groups - sortOrder // The sort order - ); + Cursor cursor = null; + if (searchString.equals("")) + cursor = db.query( + NoteEntry.TABLE_NAME, // The table to query + projection, // The columns to return + null, // The columns for the WHERE clause + null, // The values for the WHERE clause + null, // don't group the rows + null, // don't filter by row groups + sortOrder // The sort order + ); + else { + String selection = NoteEntry.COLUMN_NAME_TEXT + " LIKE ? OR " + + NoteEntry.COLUMN_NAME_TITLE + " LIKE ? "; + String[] selectionArgs = {"%" + searchString + "%", "%" + searchString + "%"}; + cursor = db.query( + NoteEntry.TABLE_NAME, // The table to query + projection, // The columns to return + selection, // The columns for the WHERE clause + selectionArgs, // The values for the WHERE clause + null, // don't group the rows + null, // don't filter by row groups + sortOrder // The sort order + ); + } ArrayList result = new ArrayList<>(); cursor.moveToFirst(); while (!cursor.isAfterLast()){ @@ -53,4 +72,68 @@ public List getNotesList(SortMode sortMode){ return result; } + public Note getNote(long id){ + SQLiteDatabase db = mDbHelper.getReadableDatabase(); + String[] projection = { + NoteEntry.COLUMN_NAME_TITLE, + NoteEntry.COLUMN_NAME_TEXT, + NoteEntry.COLUMN_NAME_DATE, + }; + Cursor cursor = db.query( + NoteEntry.TABLE_NAME, // The table to query + projection, // The columns to return + NoteEntry._ID + " = " + id, // The columns for the WHERE clause + null, // The values for the WHERE clause + null, // don't group the rows + null, // don't filter by row groups + null // The sort order + ); + cursor.moveToFirst(); + String title = cursor.getString(cursor.getColumnIndexOrThrow(NoteEntry.COLUMN_NAME_TITLE)); + String date = cursor.getString(cursor.getColumnIndexOrThrow(NoteEntry.COLUMN_NAME_DATE)); + String text = cursor.getString(cursor.getColumnIndexOrThrow(NoteEntry.COLUMN_NAME_TEXT)); + return new Note(id, title, date, text); + + } + + public long saveNote(Note note){ + // Gets the data repository in write mode + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + // Create a new map of values, where column names are the keys + ContentValues values = new ContentValues(); + values.put(NoteEntry.COLUMN_NAME_TITLE, note.title); + values.put(NoteEntry.COLUMN_NAME_TEXT, note.text); + note.date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()); + values.put(NoteEntry.COLUMN_NAME_DATE, note.date); + // Insert the new row, returning the primary key value of the new row + if (note.id == -1) { + long newRowId; + newRowId = db.insert( + NoteEntry.TABLE_NAME, + null, + values); + note.id = newRowId; + } else { + String selection = NoteEntry._ID + " LIKE ?"; + String[] selectionArgs = { String.valueOf(note.id) }; + db.update( + NoteEntry.TABLE_NAME, + values, + selection, + selectionArgs); + } + return note.id; + } + + public void deleteNote(long id){ + SQLiteDatabase db = mDbHelper.getWritableDatabase(); + // Create a new map of values, where column names are the keys + String selection = NoteEntry._ID + " LIKE ?"; + String[] selectionArgs = {String.valueOf(id)}; + db.delete( + NoteEntry.TABLE_NAME, + selection, + selectionArgs); + } + }