Delete Songs

This commit is contained in:
sebastian 2024-11-01 17:39:45 +01:00
parent c724905027
commit 22ad507745
5 changed files with 54 additions and 8 deletions

View File

@ -30,4 +30,7 @@ public interface MusicNoteDAO {
@Update
void updateConcert(Concert concert);
@Delete
void deleteSong(MusicNote musicNote);
}

View File

@ -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<Void, Void, List<MusicNote>> {
@Override

View File

@ -16,6 +16,9 @@ public class HomeViewModel extends ViewModel {
private final MutableLiveData<List<MusicNote>> 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<MusicNote> 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<MusicNote> currentSongs = noteTitles.getValue();
if(currentSongs != null) {
currentSongs.remove(musicNote);
noteTitles.setValue(currentSongs);
}
}
}

View File

@ -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<NoteSongAdapter.NoteViewHolder> {
private final List<MusicNote> noteTitles;
private final OnSongDeleteListener onSongDeleteListener;
public NoteSongAdapter(List<MusicNote> noteTitles) {
this.noteTitles = noteTitles;
public interface OnSongDeleteListener {
void onSongDeleted(MusicNote song);
}
public NoteSongAdapter() {
public NoteSongAdapter(List<MusicNote> 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<NoteSongAdapter.NoteVi
holder.titleTextView.setText(noteTitles.get(position).getTitle());
holder.yearTextView.setText(String.valueOf(noteTitles.get(position).getYear()));
holder.compositionTextView.setText(noteTitles.get(position).getComposer());
holder.deleteButton.setOnClickListener(v -> {
onSongDeleteListener.onSongDeleted(noteTitles.get(position));
});
}
@Override
@ -61,11 +73,16 @@ public class NoteSongAdapter extends RecyclerView.Adapter<NoteSongAdapter.NoteVi
TextView yearTextView;
TextView compositionTextView;
ImageButton deleteButton;
NoteViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.text_title);
yearTextView = itemView.findViewById(R.id.text_year);
compositionTextView = itemView.findViewById(R.id.text_composition);
deleteButton = itemView.findViewById(R.id.delete_song_button);
titleTextView.setOnClickListener(v -> {
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION) {

View File

@ -56,21 +56,21 @@
android:orientation="horizontal">
<ImageButton
android:id="@+id/edit_concert_button"
android:id="@+id/edit_song_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24"
app:tint="@color/teal_700"
android:contentDescription="Edit Concert"
android:contentDescription="Edit Song"
android:background="?android:selectableItemBackgroundBorderless" />
<ImageButton
android:id="@+id/delete_concert_button"
android:id="@+id/delete_song_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/delete"
app:tint="@color/red"
android:contentDescription="Delete Concert"
android:contentDescription="Delete Song"
android:background="?android:selectableItemBackgroundBorderless" />
</LinearLayout>