Adding Note Editing
This commit is contained in:
parent
46419f7d83
commit
22306965ed
@ -47,7 +47,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
notesRecyclerView = (RecyclerView) findViewById(R.id.noteRecyclerView);
|
||||
notesRecyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
noteAdapter = new NotesRecyclerViewAdapter(db.noteDAO());
|
||||
noteAdapter = new NotesRecyclerViewAdapter(db.noteDAO(), this::startActivity);
|
||||
|
||||
notesRecyclerView.setAdapter(noteAdapter);
|
||||
|
||||
|
@ -22,6 +22,8 @@ public class NewNote extends AppCompatActivity {
|
||||
|
||||
private Button saveNote;
|
||||
|
||||
private Note note;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -36,6 +38,16 @@ public class NewNote extends AppCompatActivity {
|
||||
newNoteTitle = (EditText) findViewById(R.id.newNoteTitle);
|
||||
newNoteContent = (EditText) findViewById(R.id.newNoteContent);
|
||||
|
||||
if(getIntent().hasExtra("editableNote")) {
|
||||
note = (Note) getIntent().getSerializableExtra("editableNote");
|
||||
|
||||
newNoteTitle.getText().clear();
|
||||
newNoteTitle.getText().append(note.noteTitle);
|
||||
|
||||
newNoteContent.getText().clear();
|
||||
newNoteContent.getText().append(note.noteContent);
|
||||
}
|
||||
|
||||
saveNote = (Button) findViewById(R.id.saveNote);
|
||||
saveNote.setOnClickListener((v) -> {
|
||||
if(newNoteTitle.getText().toString().isEmpty() || newNoteContent.getText().toString().isEmpty()) {
|
||||
@ -43,11 +55,21 @@ public class NewNote extends AppCompatActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
Note note = new Note();
|
||||
boolean noteWasNull = note == null;
|
||||
|
||||
if(noteWasNull) {
|
||||
note = new Note();
|
||||
}
|
||||
|
||||
note.noteTitle = newNoteTitle.getText().toString();
|
||||
note.noteContent = newNoteContent.getText().toString();
|
||||
|
||||
NoteDB.getDB(getApplicationContext()).noteDAO().insertAll(note);
|
||||
if(noteWasNull) {
|
||||
NoteDB.getDB(getApplicationContext()).noteDAO().insertAll(note);
|
||||
} else {
|
||||
NoteDB.getDB(getApplicationContext()).noteDAO().update(note);
|
||||
}
|
||||
|
||||
finish();
|
||||
});
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.coldlightalchemist.notesapp;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@ -12,9 +13,13 @@ import com.coldlightalchemist.notesapp.db.NoteDB;
|
||||
import com.coldlightalchemist.notesapp.db.dao.NoteDAO;
|
||||
import com.coldlightalchemist.notesapp.db.entities.Note;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecyclerViewAdapter.NoteRecyclerViewHolder> {
|
||||
private final NoteDAO noteSource;
|
||||
|
||||
private final Consumer<Intent> activityStarter;
|
||||
|
||||
public static class NoteRecyclerViewHolder extends RecyclerView.ViewHolder {
|
||||
private final TextView rvNoteTitle;
|
||||
|
||||
@ -37,8 +42,9 @@ public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecycler
|
||||
}
|
||||
}
|
||||
|
||||
public NotesRecyclerViewAdapter(NoteDAO noteSource) {
|
||||
public NotesRecyclerViewAdapter(NoteDAO noteSource, Consumer<Intent> activityStarter) {
|
||||
this.noteSource = noteSource;
|
||||
this.activityStarter = activityStarter;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -47,7 +53,15 @@ public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecycler
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.note_recycler_view_item, parent, false);
|
||||
|
||||
return new NoteRecyclerViewHolder(view);
|
||||
NoteRecyclerViewHolder holder = new NoteRecyclerViewHolder(view);
|
||||
|
||||
view.setOnClickListener((v) -> {
|
||||
Intent intent = new Intent(v.getContext(), NewNote.class);
|
||||
intent.putExtra("editableNote", holder.getNote());
|
||||
activityStarter.accept(intent);
|
||||
});
|
||||
|
||||
return holder;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,6 +4,7 @@ import androidx.room.Dao;
|
||||
import androidx.room.Delete;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
import androidx.room.Update;
|
||||
|
||||
import com.coldlightalchemist.notesapp.db.entities.Note;
|
||||
|
||||
@ -20,6 +21,9 @@ public interface NoteDAO {
|
||||
@Query("SELECT * FROM notes WHERE note_title = :noteTitle")
|
||||
Note getNoteByTitle(String noteTitle);
|
||||
|
||||
@Update(entity = Note.class)
|
||||
void update(Note note);
|
||||
|
||||
@Insert
|
||||
void insertAll(Note... notes);
|
||||
|
||||
|
@ -4,8 +4,10 @@ import androidx.room.ColumnInfo;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Entity(tableName = "notes")
|
||||
public class Note {
|
||||
public class Note implements Serializable {
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
@ColumnInfo(name = "note_id")
|
||||
public int id;
|
||||
|
Loading…
Reference in New Issue
Block a user