Display Concert Songs

This commit is contained in:
sebastian 2024-11-02 10:35:38 +01:00
parent 706cff79bf
commit eac3919710
3 changed files with 109 additions and 1 deletions

View File

@ -1,21 +1,43 @@
package core.notevault.ui.gallery.detail;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.util.ViewInfo;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import core.notevault.data.Concert;
import core.notevault.data.MusicDatabase;
import core.notevault.data.MusicNote;
import core.notevault.data.MusicNoteDAO;
import core.notevault.databinding.FragmentConcertDetailBinding;
import core.notevault.ui.home.NoteSongAdapter;
import java.util.Collections;
import java.util.List;
public class ConcertDetailFragment extends Fragment {
public interface OnConcertSongsLoadedListener {
void onSongsLoaded(List<MusicNote> songs);
}
private FragmentConcertDetailBinding binding;
private Concert concert;
private ConcertDetailViewModel concertDetailViewModel;
private NoteSongAdapter noteSongAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
concertDetailViewModel = new ViewModelProvider(this).get(ConcertDetailViewModel.class);
binding = FragmentConcertDetailBinding.inflate(inflater, container, false);
int concertId = getArguments().getInt("concertID");
String concertTitle = getArguments().getString("concertTitle");
@ -25,6 +47,14 @@ public class ConcertDetailFragment extends Fragment {
binding.textConcertDetailsTitle.setText(concertTitle);
binding.textConcertDetailsDate.setText(concertDate);
RecyclerView recyclerView = binding.noteRecyclerView;
noteSongAdapter = new NoteSongAdapter(this::deleteSongFromConcert);
recyclerView.setAdapter(noteSongAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), LinearLayoutManager.VERTICAL);
recyclerView.addItemDecoration(dividerItemDecoration);
FloatingActionButton fab = binding.addSongConcert;
fab.setOnClickListener(view -> {
ConcertSongSelector concertSongSelector = new ConcertSongSelector();
@ -39,8 +69,40 @@ public class ConcertDetailFragment extends Fragment {
concertSongSelector.show(getParentFragmentManager(), ConcertSongSelector.TAG);
});
concertDetailViewModel.getConcertSongs().observe(getViewLifecycleOwner(), songs -> {
noteSongAdapter.updateSongTitles(songs);
});
new LoadConcertSongsTask(concertId, songs -> {
this.concertDetailViewModel.setConcertSongs(songs);
}).execute();
return binding.getRoot();
}
private void deleteSongFromConcert(MusicNote musicNote) {
this.concertDetailViewModel.deleteConcertSong(musicNote);
}
private class LoadConcertSongsTask extends AsyncTask<Void, Void, List<MusicNote>> {
private final int concertID;
private final OnConcertSongsLoadedListener listener;
public LoadConcertSongsTask(int concertID, OnConcertSongsLoadedListener listener) {
this.concertID = concertID;
this.listener = listener;
}
@Override
protected List<MusicNote> doInBackground(Void... voids) {
MusicDatabase db = MusicDatabase.getDatabase(getContext());
return db.musicNoteDao().getAllMusicNotesOfConcert(concertID);
}
@Override
protected void onPostExecute(List<MusicNote> songs) {
Log.d("ConcertDetailFragment", "Loaded Concertsongs: " + songs.size());
listener.onSongsLoaded(songs);
}
}
}

View File

@ -0,0 +1,41 @@
package core.notevault.ui.gallery.detail;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import core.notevault.data.MusicNote;
import java.util.ArrayList;
import java.util.List;
public class ConcertDetailViewModel extends ViewModel {
private final MutableLiveData<List<MusicNote>> concertSongs;
public ConcertDetailViewModel() {
this.concertSongs = new MutableLiveData<>(new ArrayList<>());
}
public LiveData<List<MusicNote>> getConcertSongs() {
return concertSongs;
}
public void setConcertSongs(List<MusicNote> concertSongs) {
this.concertSongs.setValue(concertSongs);
}
public void deleteConcertSong(MusicNote note) {
List<MusicNote> currentConcertSongs = concertSongs.getValue();
if(currentConcertSongs != null) {
currentConcertSongs.remove(note);
this.concertSongs.setValue(currentConcertSongs);
}
}
public void addConcertSongs(List<MusicNote> concertSongs) {
List<MusicNote> currentConcertSongs = this.concertSongs.getValue();
if(currentConcertSongs != null) {
currentConcertSongs.addAll(concertSongs);
this.concertSongs.setValue(currentConcertSongs);
}
}
}

View File

@ -44,8 +44,13 @@
</androidx.cardview.widget.CardView>
<!-- Hier können weitere Details hinzugefügt werden -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/note_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/add_song_concert"/>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:src="@drawable/add_24dp_e8eaed_fill0_wght400_grad0_opsz24"
android:layout_width="wrap_content"