Skip to content

Commit

Permalink
All done
Browse files Browse the repository at this point in the history
  • Loading branch information
cepreu2github committed Oct 30, 2015
1 parent cdcb9b5 commit bb5c20b
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,26 @@
* 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) {
// Inflate the layout for this fragment
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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,65 @@ 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) {
super.onCreate(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
Expand All @@ -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;
}

Expand All @@ -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();
Expand All @@ -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
}

}
10 changes: 10 additions & 0 deletions DummyNotes/app/src/main/java/ru/myitschool/dummynotes/Note.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.myitschool.dummynotes;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Created by cepreu on 22.10.15.
*/
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Note> 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;
Expand Down
Loading

0 comments on commit bb5c20b

Please sign in to comment.