diff --git a/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailFragment.java b/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailFragment.java index 2bc4293..91fc984 100644 --- a/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailFragment.java +++ b/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailFragment.java @@ -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 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> { + private final int concertID; + private final OnConcertSongsLoadedListener listener; + + public LoadConcertSongsTask(int concertID, OnConcertSongsLoadedListener listener) { + this.concertID = concertID; + this.listener = listener; + } + + @Override + protected List doInBackground(Void... voids) { + MusicDatabase db = MusicDatabase.getDatabase(getContext()); + return db.musicNoteDao().getAllMusicNotesOfConcert(concertID); + } + + @Override + protected void onPostExecute(List songs) { + Log.d("ConcertDetailFragment", "Loaded Concertsongs: " + songs.size()); + listener.onSongsLoaded(songs); + } + } } diff --git a/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailViewModel.java b/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailViewModel.java new file mode 100644 index 0000000..69920ca --- /dev/null +++ b/app/src/main/java/core/notevault/ui/gallery/detail/ConcertDetailViewModel.java @@ -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> concertSongs; + + public ConcertDetailViewModel() { + this.concertSongs = new MutableLiveData<>(new ArrayList<>()); + } + + public LiveData> getConcertSongs() { + return concertSongs; + } + + public void setConcertSongs(List concertSongs) { + this.concertSongs.setValue(concertSongs); + } + + public void deleteConcertSong(MusicNote note) { + List currentConcertSongs = concertSongs.getValue(); + if(currentConcertSongs != null) { + currentConcertSongs.remove(note); + this.concertSongs.setValue(currentConcertSongs); + } + } + + public void addConcertSongs(List concertSongs) { + List currentConcertSongs = this.concertSongs.getValue(); + if(currentConcertSongs != null) { + currentConcertSongs.addAll(concertSongs); + this.concertSongs.setValue(currentConcertSongs); + } + } +} diff --git a/app/src/main/res/layout/fragment_concert_detail.xml b/app/src/main/res/layout/fragment_concert_detail.xml index be152a3..12156cc 100644 --- a/app/src/main/res/layout/fragment_concert_detail.xml +++ b/app/src/main/res/layout/fragment_concert_detail.xml @@ -44,8 +44,13 @@ - + +