dodane usuwanie i edycja notatek
This commit is contained in:
parent
44b13e4377
commit
06e5e3c578
@ -1,4 +1,5 @@
|
|||||||
package com.example.bsm_notatnik;
|
package com.example.bsm_notatnik;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -28,8 +29,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private static final String SHARED_NAME_CREDENTIALS = "Credentials";
|
private static final String SHARED_NAME_CREDENTIALS = "Credentials";
|
||||||
private static final String SHARED_NOTES_NAME = "Notes";
|
private static final String SHARED_NOTES_NAME = "Notes";
|
||||||
|
|
||||||
private static final String KEY_NOTE_COUNT = "NoteCount";
|
|
||||||
|
|
||||||
private static String HASHED_EMAIL = "";
|
private static String HASHED_EMAIL = "";
|
||||||
|
|
||||||
private List<Note> noteList;
|
private List<Note> noteList;
|
||||||
@ -50,31 +49,20 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
loadNotesFromPreferences();
|
loadNotesFromPreferences();
|
||||||
displayNotes();
|
displayNotes();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
buttonLogout = findViewById(R.id.btn_logout);
|
buttonLogout = findViewById(R.id.btn_logout);
|
||||||
buttonChangePassword = findViewById(R.id.btn_change_password);
|
buttonChangePassword = findViewById(R.id.btn_change_password);
|
||||||
buttonAddNewNote = findViewById(R.id.btn_add_note);
|
buttonAddNewNote = findViewById(R.id.btn_add_note);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
buttonLogout.setOnClickListener(view -> logOut());
|
buttonLogout.setOnClickListener(view -> logOut());
|
||||||
|
|
||||||
buttonChangePassword.setOnClickListener(view -> showPasswordChangeDialog(current_username_hashed));
|
buttonChangePassword.setOnClickListener(view -> showPasswordChangeDialog(current_username_hashed));
|
||||||
|
|
||||||
buttonAddNewNote.setOnClickListener(view -> {
|
buttonAddNewNote.setOnClickListener(view -> showAddNewNoteDialog());
|
||||||
showAddNewNoteDialog();
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void logOut(){
|
private void logOut(){
|
||||||
Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show();
|
Toast.makeText(getApplicationContext(), "Logout Successful!", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
@ -84,8 +72,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void showPasswordChangeDialog(String hashedEmail){
|
private void showPasswordChangeDialog(String hashedEmail){
|
||||||
// Inflate the dialog layout
|
// Inflate the dialog layout
|
||||||
LayoutInflater inflater = getLayoutInflater();
|
LayoutInflater inflater = getLayoutInflater();
|
||||||
@ -158,9 +144,10 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
note.setTitle(title);
|
note.setTitle(title);
|
||||||
note.setContent(content);
|
note.setContent(content);
|
||||||
|
|
||||||
|
|
||||||
noteList.add(note);
|
noteList.add(note);
|
||||||
|
|
||||||
saveNotesToPreferences();
|
saveNotesToPreferences("add");
|
||||||
createNoteView(note);
|
createNoteView(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,6 +161,43 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void showEditNoteDialog(Note note){
|
||||||
|
LayoutInflater inflater = getLayoutInflater();
|
||||||
|
View dialogView = inflater.inflate(R.layout.create_note_dialog, null);
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||||
|
builder.setView(dialogView);
|
||||||
|
builder.setTitle("Edit note");
|
||||||
|
|
||||||
|
builder.setPositiveButton("Save", (dialogInterface, i) -> {
|
||||||
|
EditText noteTitleEditText = dialogView.findViewById(R.id.noteTitleEditText);
|
||||||
|
EditText noteContentEditText = dialogView.findViewById(R.id.noteContentEditText);
|
||||||
|
|
||||||
|
String title = noteTitleEditText.getText().toString();
|
||||||
|
String content = noteContentEditText.getText().toString();
|
||||||
|
|
||||||
|
if (!title.isEmpty() && !content.isEmpty()){
|
||||||
|
deleteNoteAndRefresh(note);
|
||||||
|
|
||||||
|
note.setTitle(title);
|
||||||
|
note.setContent(content);
|
||||||
|
|
||||||
|
|
||||||
|
noteList.add(note);
|
||||||
|
|
||||||
|
saveNotesToPreferences("add");
|
||||||
|
createNoteView(note);
|
||||||
|
}
|
||||||
|
|
||||||
|
Toast.makeText(MainActivity.this, "Note Edited!", Toast.LENGTH_SHORT).show();
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.setNegativeButton("Cancel", (dialogInterface, i) -> dialogInterface.dismiss());
|
||||||
|
|
||||||
|
AlertDialog alertDialog = builder.create();
|
||||||
|
alertDialog.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -220,28 +244,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
return sharedPreferences.getString("user_" + hashedEmail, "err");
|
return sharedPreferences.getString("user_" + hashedEmail, "err");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveNote(){
|
|
||||||
EditText noteTitleEditText = findViewById(R.id.noteTitleEditText);
|
|
||||||
EditText noteContentEditText = findViewById(R.id.noteContentEditText);
|
|
||||||
|
|
||||||
String title = noteTitleEditText.getText().toString();
|
private void saveNotesToPreferences(String mode){
|
||||||
String content = noteContentEditText.getText().toString();
|
|
||||||
|
|
||||||
if (!title.isEmpty() && !content.isEmpty()){
|
|
||||||
Note note = new Note();
|
|
||||||
note.setTitle(title);
|
|
||||||
note.setContent(content);
|
|
||||||
|
|
||||||
noteList.add(note);
|
|
||||||
saveNotesToPreferences();
|
|
||||||
|
|
||||||
//createNoteView(note);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void saveNotesToPreferences(){
|
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE);
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
|
if (mode.equals("del")){
|
||||||
|
editor.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
editor.putInt("notecount_" + HASHED_EMAIL, noteList.size());
|
editor.putInt("notecount_" + HASHED_EMAIL, noteList.size());
|
||||||
for(int i=0; i<noteList.size(); i++){
|
for(int i=0; i<noteList.size(); i++){
|
||||||
@ -252,6 +262,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void loadNotesFromPreferences(){
|
private void loadNotesFromPreferences(){
|
||||||
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE);
|
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_NOTES_NAME, MODE_PRIVATE);
|
||||||
int noteCount = sharedPreferences.getInt("notecount_" + HASHED_EMAIL, 0);
|
int noteCount = sharedPreferences.getInt("notecount_" + HASHED_EMAIL, 0);
|
||||||
@ -268,16 +279,54 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void createNoteView(final Note note){
|
private void createNoteView(final Note note){
|
||||||
View noteView = getLayoutInflater().inflate(R.layout.note_item, null);
|
View noteView = getLayoutInflater().inflate(R.layout.note_item, null);
|
||||||
TextView noteTitleTextView = noteView.findViewById(R.id.noteTitleTextView);
|
TextView noteTitleTextView = noteView.findViewById(R.id.noteTitleTextView);
|
||||||
TextView noteContentTextView = noteView.findViewById(R.id.noteContentTextView);
|
TextView noteContentTextView = noteView.findViewById(R.id.noteContentTextView);
|
||||||
|
Button deleteNoteDutton = noteView.findViewById(R.id.btnDeleteNote);
|
||||||
|
|
||||||
noteTitleTextView.setText(note.getTitle());
|
noteTitleTextView.setText(note.getTitle());
|
||||||
noteContentTextView.setText(note.getContent());
|
noteContentTextView.setText(note.getContent());
|
||||||
|
|
||||||
|
deleteNoteDutton.setOnClickListener(view -> {
|
||||||
|
showDeleteDialog(note);
|
||||||
|
});
|
||||||
|
|
||||||
|
noteView.setOnLongClickListener(new View.OnLongClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onLongClick(View view) {
|
||||||
|
|
||||||
|
showEditNoteDialog(note);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
notesContainer.addView(noteView);
|
notesContainer.addView(noteView);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showDeleteDialog(final Note note){
|
||||||
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||||
|
builder.setTitle("Delete this note");
|
||||||
|
builder.setMessage("Are you sure you want to delete it?");
|
||||||
|
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
|
deleteNoteAndRefresh(note);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
builder.setNegativeButton("Cancel", null);
|
||||||
|
builder.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteNoteAndRefresh(Note note){
|
||||||
|
noteList.remove(note);
|
||||||
|
saveNotesToPreferences("del");
|
||||||
|
refreshNotesView();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshNotesView(){
|
private void refreshNotesView(){
|
||||||
@ -291,7 +340,5 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,11 +4,13 @@ public class Note {
|
|||||||
private String title;
|
private String title;
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
public Note(){
|
public Note(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Note(String title, String content){
|
public Note(String title, String content, int id){
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
@ -28,4 +30,5 @@ public class Note {
|
|||||||
public void setContent(String content) {
|
public void setContent(String content) {
|
||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,24 +2,40 @@
|
|||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical"
|
android:orientation="horizontal"
|
||||||
android:padding="8dp">
|
android:padding="8dp">
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
android:id="@+id/noteTitleTextView"
|
android:layout_width="0dp"
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:textSize="18sp"
|
android:layout_weight="1"
|
||||||
android:textStyle="bold"
|
android:orientation="vertical">
|
||||||
android:text="Note Title"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/noteContentTextView"
|
android:id="@+id/noteTitleTextView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="18sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:text="Note Title"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/noteContentTextView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:text="Note Content"
|
||||||
|
/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<!-- Delete Button -->
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnDeleteNote"
|
||||||
|
android:layout_width="40dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="4dp"
|
android:padding="0dp"
|
||||||
android:text="Note Content"
|
android:text="X"
|
||||||
|
android:layout_marginLeft="10dp"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
Reference in New Issue
Block a user