diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index 25e5e4e..27cc46a 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -30,4 +30,7 @@ public interface MusicNoteDAO { @Update void updateConcert(Concert concert); + + @Delete + void deleteSong(MusicNote musicNote); } diff --git a/app/src/main/java/core/notevault/ui/home/HomeFragment.java b/app/src/main/java/core/notevault/ui/home/HomeFragment.java index 34d62ac..a034ef6 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/core/notevault/ui/home/HomeFragment.java @@ -4,6 +4,8 @@ import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.provider.OpenableColumns; import android.util.Log; import android.view.LayoutInflater; @@ -50,7 +52,7 @@ public class HomeFragment extends Fragment { importBtn.setOnClickListener(v -> openFileChooser()); RecyclerView recyclerView = root.findViewById(R.id.note_recycler_view); - noteSongAdapter = new NoteSongAdapter(); + noteSongAdapter = new NoteSongAdapter(this::deleteSong); recyclerView.setAdapter(noteSongAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); @@ -66,6 +68,18 @@ public class HomeFragment extends Fragment { return root; } + public void deleteSong(MusicNote musicNote) { + new Thread(() -> { + MusicDatabase musicDatabase = MusicDatabase.getDatabase(getContext()); + MusicNoteDAO musicNoteDAO = musicDatabase.musicNoteDao(); + musicNoteDAO.deleteSong(musicNote); + Log.d("HomeFragment", "Delete Song: " + musicNote.getTitle()); + new Handler(Looper.getMainLooper()).post(()-> { + homeViewModel.deleteSong(musicNote); + }); + }).start(); + } + private class LoadSongTitlesTask extends AsyncTask> { @Override diff --git a/app/src/main/java/core/notevault/ui/home/HomeViewModel.java b/app/src/main/java/core/notevault/ui/home/HomeViewModel.java index 97ace7b..c668877 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeViewModel.java +++ b/app/src/main/java/core/notevault/ui/home/HomeViewModel.java @@ -16,6 +16,9 @@ public class HomeViewModel extends ViewModel { private final MutableLiveData> noteTitles; + + + public HomeViewModel() { noteTitles = new MutableLiveData<>(new ArrayList<>()); } @@ -28,6 +31,7 @@ public class HomeViewModel extends ViewModel { this.noteTitles.setValue(noteTitles); } + public void addSong(MusicNote song) { List songs = noteTitles.getValue(); if(songs != null) { @@ -35,4 +39,12 @@ public class HomeViewModel extends ViewModel { this.noteTitles.setValue(songs); } } + + public void deleteSong(MusicNote musicNote) { + List currentSongs = noteTitles.getValue(); + if(currentSongs != null) { + currentSongs.remove(musicNote); + noteTitles.setValue(currentSongs); + } + } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java index c6ce44f..5d98726 100644 --- a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java +++ b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java @@ -6,6 +6,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageButton; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; @@ -21,12 +22,19 @@ import java.util.List; public class NoteSongAdapter extends RecyclerView.Adapter { private final List noteTitles; + private final OnSongDeleteListener onSongDeleteListener; - public NoteSongAdapter(List noteTitles) { - this.noteTitles = noteTitles; + public interface OnSongDeleteListener { + void onSongDeleted(MusicNote song); } - public NoteSongAdapter() { + public NoteSongAdapter(List noteTitles, OnSongDeleteListener onSongDeleteListener) { + this.noteTitles = noteTitles; + this.onSongDeleteListener = onSongDeleteListener; + } + + public NoteSongAdapter(OnSongDeleteListener onSongDeleteListener) { + this.onSongDeleteListener = onSongDeleteListener; this.noteTitles = new ArrayList<>(); } @@ -43,6 +51,10 @@ public class NoteSongAdapter extends RecyclerView.Adapter { + onSongDeleteListener.onSongDeleted(noteTitles.get(position)); + }); } @Override @@ -61,11 +73,16 @@ public class NoteSongAdapter extends RecyclerView.Adapter { int position = getAdapterPosition(); if(position != RecyclerView.NO_POSITION) { diff --git a/app/src/main/res/layout/item_song.xml b/app/src/main/res/layout/item_song.xml index 6633b08..c93a434 100644 --- a/app/src/main/res/layout/item_song.xml +++ b/app/src/main/res/layout/item_song.xml @@ -56,21 +56,21 @@ android:orientation="horizontal">