Delete Concerts

This commit is contained in:
sebastian 2024-11-01 16:13:13 +01:00
parent d21a7499fe
commit e39b164b10
6 changed files with 63 additions and 8 deletions

View File

@ -6,7 +6,20 @@
<State />
</entry>
<entry key="app">
<State />
<State>
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="adb-R52N50NLGRT-9CB8rW._adb-tls-connect._tcp" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-11-01T14:55:24.387595315Z" />
</State>
</entry>
</value>
</component>

View File

@ -1,6 +1,7 @@
package core.notevault.data;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
@ -26,4 +27,7 @@ public interface MusicNoteDAO {
@Query("SELECT * FROM concerts")
List<Concert> getAllConcerts();
@Delete
void deleteConcert(Concert concert);
}

View File

@ -4,6 +4,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;
@ -18,12 +19,19 @@ import java.util.List;
public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertViewHolder> {
private final List<Concert> concertList;
private final OnConcertDeleteListener deleteListener;
public ConcertAdapter(List<Concert> concertList) {
this.concertList = concertList;
public interface OnConcertDeleteListener {
void onDeleteConcert(Concert concert);
}
public ConcertAdapter() {
public ConcertAdapter(List<Concert> concertList, OnConcertDeleteListener deleteListener) {
this.concertList = concertList;
this.deleteListener = deleteListener;
}
public ConcertAdapter(OnConcertDeleteListener deleteListener) {
this.deleteListener = deleteListener;
this.concertList = new ArrayList<>();
}
@ -31,6 +39,11 @@ public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertV
public void onBindViewHolder(@NonNull ConcertViewHolder holder, int position) {
holder.concertTitleView.setText(concertList.get(position).getTitle());
holder.dateHolder.setText(concertList.get(position).getConcertDate());
holder.deleteButton.setOnClickListener(v ->{
Log.d("ConcertAdapter", "Delete button clicked for: " + concertList.get(position).getTitle());
deleteListener.onDeleteConcert(concertList.get(position));
});
}
@Override
@ -57,12 +70,13 @@ public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertV
TextView concertTitleView;
TextView dateHolder;
ImageButton deleteButton;
public ConcertViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
concertTitleView = itemView.findViewById(R.id.concert_title);
dateHolder = itemView.findViewById(R.id.concert_date);
deleteButton = itemView.findViewById(R.id.delete_concert_button);
}
}
}

View File

@ -2,6 +2,8 @@ package core.notevault.ui.gallery;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -39,7 +41,7 @@ public class GalleryFragment extends Fragment {
View root = binding.getRoot();
RecyclerView recyclerView = root.findViewById(R.id.concert_recycler_view);
concertAdapter = new ConcertAdapter();
concertAdapter = new ConcertAdapter(this::deleteConcert);
recyclerView.setAdapter(concertAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@ -58,6 +60,20 @@ public class GalleryFragment extends Fragment {
return root;
}
public void deleteConcert(Concert concert) {
Log.d("GalleryFragment", "Delete Concert");
new Thread(() -> {
MusicDatabase musicDatabase = MusicDatabase.getDatabase(this.getContext());
MusicNoteDAO musicNoteDAO = musicDatabase.musicNoteDao();
musicNoteDAO.deleteConcert(concert);
new Handler(Looper.getMainLooper()).post(() -> {
galleryViewModel.deleteConcert(concert);
Log.d("GalleryFragment", "Concert deleted successfully and ViewModel updated");
});
}).start();
}
public void updateConcerts() {
}

View File

@ -33,4 +33,12 @@ public class GalleryViewModel extends ViewModel {
concerts.setValue(currentConcerts);
}
}
public void deleteConcert(Concert concert) {
List<Concert> currentConcerts = concerts.getValue();
if(currentConcerts != null) {
currentConcerts.remove(concert);
concerts.setValue(currentConcerts);
}
}
}

View File

@ -49,7 +49,7 @@
android:background="?android:selectableItemBackgroundBorderless" />
<ImageButton
android:id="@+id/delete_button"
android:id="@+id/delete_concert_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/delete"