diff --git a/app/build.gradle b/app/build.gradle index f792b54..21c70cf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { applicationId "xyz.r0r5chach.cpsAssist" - minSdk 24 + minSdk 26 targetSdk 33 versionCode 1 versionName "1.0" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 14a68f7..e55c115 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,6 @@ - { - private final List notes; + private final Notes notes; - public Adapter(List notes) { - this.notes = notes; + public Adapter(File path, String username) { + notes = getStoredNotes(path, username); } @NonNull @@ -30,15 +30,22 @@ public class Adapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(ViewHolder holder, int index) { - File note = notes.get(index); + File note = notes.getNote(index); holder.getFileName().setText(note.getName()); } @Override public int getItemCount() { - return 0; + return notes.getAmount(); } + public Notes getNotes() { + return notes; + } + + private Notes getStoredNotes(File path, String username) { + return new Notes(path, username); + } public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView fileName; diff --git a/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/BtnOnClickListener.java b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/BtnOnClickListener.java new file mode 100644 index 0000000..a459a9f --- /dev/null +++ b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/BtnOnClickListener.java @@ -0,0 +1,32 @@ +package xyz.r0r5chach.cpsAssist.notes; + +import android.view.View; + +import java.util.Locale; + +public class BtnOnClickListener implements View.OnClickListener { + private final Adapter adapter; + + + public BtnOnClickListener(Adapter adapter) { + this.adapter = adapter; + } + + + @Override + public void onClick(View view) { + + String test = view.getResources().getResourceName(view.getId()).split("/")[1]; + switch(ButtonID.valueOf(test.toUpperCase(Locale.ROOT))) { + case ADD_BUTTON: + onAddClick(); + break; + } + } + + private void onAddClick() { + Notes notes = adapter.getNotes(); + notes.createNote(); + adapter.notifyItemInserted(notes.getAmount()); + } +} diff --git a/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/ButtonID.java b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/ButtonID.java new file mode 100644 index 0000000..d91dd58 --- /dev/null +++ b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/ButtonID.java @@ -0,0 +1,5 @@ +package xyz.r0r5chach.cpsAssist.notes; + +public enum ButtonID { + ADD_BUTTON; +} diff --git a/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/Notes.java b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/Notes.java new file mode 100644 index 0000000..f25cdd0 --- /dev/null +++ b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/Notes.java @@ -0,0 +1,51 @@ +package xyz.r0r5chach.cpsAssist.notes; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.file.Files; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +public class Notes { + private final List notes; + private final File rootDir; + private final String username; + + public Notes(File rootDir, String username) { + this.rootDir = rootDir; + this.username = username; + notes = new ArrayList<>(); + } + + public void createNote() { + String fileName = LocalDateTime.now().toString() + "-" + + username; + //Get current username + File note = new File(rootDir, fileName); + notes.add(note); + try { + writeFile(note); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + public File getNote(int index) { + return notes.get(index); + } + + public int getAmount() { + return notes.size(); + } + + private void writeFile(File file) throws IOException { + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(file.toPath())); + outputStreamWriter.write("Hi"); + outputStreamWriter.flush(); + outputStreamWriter.close(); + } +} diff --git a/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/NotesActivity.java b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/NotesActivity.java index 842fa30..e39878f 100644 --- a/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/NotesActivity.java +++ b/app/src/main/java/xyz/r0r5chach/cpsAssist/notes/NotesActivity.java @@ -6,31 +6,23 @@ import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; -import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; - import xyz.r0r5chach.cpsAssist.R; public class NotesActivity extends AppCompatActivity { - private List notes; + private Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notes); - notes = getStoredNotes(); initRecycler(); + findViewById(R.id.add_Button).setOnClickListener(new BtnOnClickListener(adapter)); } private void initRecycler() { - RecyclerView list =findViewById(R.id.notesList); - list.setAdapter(new Adapter(notes)); + RecyclerView list = findViewById(R.id.notesList); + adapter = new Adapter(getFilesDir(), getIntent().getStringExtra("username")); + list.setAdapter(adapter); list.setLayoutManager(new LinearLayoutManager(this)); } - - private List getStoredNotes() { - return Arrays.asList(Objects.requireNonNull(getFilesDir().listFiles())); - } } \ No newline at end of file