2023-03-24 23:20:41 +00:00
|
|
|
package xyz.r0r5chach.cpsAssist.notes;
|
|
|
|
|
|
2023-03-26 01:11:08 +00:00
|
|
|
|
2023-03-24 23:20:41 +00:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.ArrayList;
|
2023-03-26 01:11:08 +00:00
|
|
|
import java.util.Arrays;
|
2023-03-24 23:20:41 +00:00
|
|
|
import java.util.List;
|
2023-03-26 01:11:08 +00:00
|
|
|
import java.util.Objects;
|
2023-03-24 23:20:41 +00:00
|
|
|
|
|
|
|
|
public class Notes {
|
|
|
|
|
private final List<File> notes;
|
|
|
|
|
private final File rootDir;
|
|
|
|
|
private final String username;
|
|
|
|
|
|
|
|
|
|
public Notes(File rootDir, String username) {
|
|
|
|
|
this.rootDir = rootDir;
|
|
|
|
|
this.username = username;
|
|
|
|
|
notes = new ArrayList<>();
|
2023-03-26 01:11:08 +00:00
|
|
|
getStoredNotes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void getStoredNotes() {
|
|
|
|
|
notes.addAll(Arrays.asList(Objects.requireNonNull(rootDir.listFiles())));
|
2023-03-24 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void createNote() {
|
2023-03-26 01:11:08 +00:00
|
|
|
String fileName = LocalDateTime.now().toString() + "-" + username;
|
2023-03-24 23:20:41 +00:00
|
|
|
//Get current username
|
|
|
|
|
File note = new File(rootDir, fileName);
|
|
|
|
|
notes.add(note);
|
|
|
|
|
try {
|
2023-03-26 01:11:08 +00:00
|
|
|
writeFile(note, "");
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int deleteNote(String path) {
|
|
|
|
|
File tmp = new File(rootDir.getAbsolutePath() + "/" + path);
|
|
|
|
|
int index = getNoteIndex(tmp);
|
|
|
|
|
notes.remove(index);
|
|
|
|
|
if (tmp.delete()) {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateNote(File note, String content) {
|
|
|
|
|
try {
|
|
|
|
|
writeFile(note, content);
|
2023-03-24 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public File getNote(int index) {
|
|
|
|
|
return notes.get(index);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 01:11:08 +00:00
|
|
|
public int getNoteIndex(File note) {
|
|
|
|
|
return notes.indexOf(note);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 23:20:41 +00:00
|
|
|
public int getAmount() {
|
|
|
|
|
return notes.size();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 01:11:08 +00:00
|
|
|
private void writeFile(File file, String content) throws IOException {
|
|
|
|
|
OutputStreamWriter fileWriter = new OutputStreamWriter(Files.newOutputStream(file.toPath()));
|
|
|
|
|
fileWriter.write(content);
|
|
|
|
|
fileWriter.flush();
|
|
|
|
|
fileWriter.close();
|
2023-03-24 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
}
|