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