Notes finished

This commit is contained in:
r0r-5chach 2023-03-26 14:51:08 +01:00
parent b40dbec724
commit da306d6270
3 changed files with 78 additions and 34 deletions

View File

@ -1,17 +1,19 @@
package xyz.r0r5chach.cpsAssist.notes;
import android.app.AlertDialog;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Locale;
import java.util.Scanner;
import xyz.r0r5chach.cpsAssist.R;
@ -52,15 +54,11 @@ public class BtnOnClickListener implements View.OnClickListener {
private void onEditClick(String path, View v) {
File tmp = new File(v.getContext().getExternalFilesDir(null) + "/" + path); //load file into var
FileReader r = null;
AlertDialog.Builder dialog = new AlertDialog.Builder(v.getContext());
dialog.setTitle("Edit Note");
dialog.setView(R.layout.edit_dialog);
dialog.create().show();
//TODO: get text from dialog and save to file
//TODO: set text in dialog to what is in note
adapter.getNotes().updateNote(tmp, "");
adapter.notifyItemChanged(adapter.getNotes().getNoteIndex(tmp));
EditText editField = new EditText(v.getContext());
editField.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
AlertDialog dialog = editDialog(v, tmp, editField);
initNoteText(editField, tmp);
dialog.show();
}
private void onDeleteClick(String path) {
@ -73,4 +71,36 @@ public class BtnOnClickListener implements View.OnClickListener {
return name.getText().toString();
}
private AlertDialog editDialog(View v, File note, TextView editField) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Edit Note");
builder.setView(editField);
builder.setPositiveButton("Save", new DialogOnClickListener(adapter, note, editField));
builder.setNegativeButton("Cancel", new DialogOnClickListener());
return builder.create();
}
private AlertDialog viewDialog(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(v.getContext());
dialog.setTitle("View Note");
dialog.setMessage("note"); //TODO: Get selected file's note
return dialog.create();
}
private void initNoteText(TextView editField, File note) {
try {
Scanner r = new Scanner(note);
StringBuilder text = new StringBuilder();
while (r.hasNextLine()) {
text.append(r.nextLine());
}
editField.setText(text.toString());
r.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,36 @@
package xyz.r0r5chach.cpsAssist.notes;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.TextView;
import java.io.File;
import xyz.r0r5chach.cpsAssist.R;
public class DialogOnClickListener implements DialogInterface.OnClickListener {
private Adapter adapter;
private File note;
private TextView editField;
public DialogOnClickListener() {
}
public DialogOnClickListener(Adapter adapter, File note, TextView editField) {
this.adapter = adapter;
this.note = note;
this.editField = editField;
}
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog d = (AlertDialog) dialog;
if (which == DialogInterface.BUTTON_POSITIVE) {
adapter.getNotes().updateNote(note, editField.getText().toString());
adapter.notifyItemChanged(adapter.getNotes().getNoteIndex(note));
}
d.dismiss();
}
}

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editTextTextMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:autofillHints=""
android:hint="@string/note_edit_hint"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/saveButtonText" />
</LinearLayout>