From 417477685da2ff40a1d025288e4b7433e16662c0 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Sat, 18 Jan 2025 16:30:13 +0100 Subject: [PATCH] List Songs in HomeFragment --- .../notevault/data/entities/Song.java | 3 + .../data/repositories/SongRepository.java | 10 ++- .../notevault/ui/home/HomeFragment.java | 44 +++++++++- .../notevault/ui/home/HomeViewModel.java | 38 +++++++-- .../notevault/ui/home/SongAdapter.java | 80 +++++++++++++++++++ .../ui/songeditor/SongEditorDialog.java | 12 ++- app/src/main/res/drawable/delete.xml | 9 +++ ...24dp_e8eaed_fill0_wght400_grad0_opsz24.xml | 9 +++ app/src/main/res/layout/fragment_home.xml | 6 ++ app/src/main/res/layout/item_song.xml | 77 ++++++++++++++++++ 10 files changed, 271 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java create mode 100644 app/src/main/res/drawable/delete.xml create mode 100644 app/src/main/res/drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24.xml create mode 100644 app/src/main/res/layout/item_song.xml diff --git a/app/src/main/java/com/stormtales/notevault/data/entities/Song.java b/app/src/main/java/com/stormtales/notevault/data/entities/Song.java index 239b49a..4fb6039 100644 --- a/app/src/main/java/com/stormtales/notevault/data/entities/Song.java +++ b/app/src/main/java/com/stormtales/notevault/data/entities/Song.java @@ -31,6 +31,9 @@ public class Song { this.year = year; } + public Song() { + } + public int getLocalID() { return localID; } diff --git a/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java b/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java index 3640a39..9081498 100644 --- a/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java +++ b/app/src/main/java/com/stormtales/notevault/data/repositories/SongRepository.java @@ -1,16 +1,18 @@ package com.stormtales.notevault.data.repositories; import android.app.Application; +import android.content.Context; import com.stormtales.notevault.data.MusicDatabase; import com.stormtales.notevault.data.dao.SongDao; import com.stormtales.notevault.data.entities.Song; +import java.util.List; import java.util.concurrent.Executors; public class SongRepository { private SongDao songDao; - public SongRepository(Application application) { - MusicDatabase database = MusicDatabase.getDatabase(application); + public SongRepository(Context context) { + MusicDatabase database = MusicDatabase.getDatabase(context); songDao = database.getSongTable(); } public void insert(Song song) { @@ -18,4 +20,8 @@ public class SongRepository { songDao.insert(song); }); } + + public List getAllSongs() { + return songDao.getAllSongs(); + } } diff --git a/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java b/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java index 9782c48..9c2c37a 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/com/stormtales/notevault/ui/home/HomeFragment.java @@ -16,23 +16,36 @@ import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; 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 com.google.android.material.floatingactionbutton.FloatingActionButton; import com.stormtales.notevault.R; +import com.stormtales.notevault.data.entities.Song; +import com.stormtales.notevault.data.repositories.SongRepository; import com.stormtales.notevault.databinding.FragmentHomeBinding; import com.stormtales.notevault.ui.songeditor.SongEditorDialog; +import java.util.ArrayList; + public class HomeFragment extends Fragment { private FragmentHomeBinding binding; + private RecyclerView recyclerView; + private SongAdapter songAdapter; + private HomeViewModel homeViewModel; private static final int PICK_FILE_REQUEST_CODE = 1; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - HomeViewModel homeViewModel = - new ViewModelProvider(this).get(HomeViewModel.class); + SongRepository songRepository = new SongRepository(this.getContext()); + homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class); + homeViewModel. binding = FragmentHomeBinding.inflate(inflater, container, false); View root = binding.getRoot(); + recyclerView = root.findViewById(R.id.recycler_view_songs); + ActivityResultLauncher launcher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), activityResult -> { @@ -70,8 +83,18 @@ public class HomeFragment extends Fragment { launcher.launch(intent); }); - final TextView textView = binding.textHome; - homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); + // RecyclerView einrichten + songAdapter = new SongAdapter(new ArrayList<>(), new SongEventClickListener()); + recyclerView.setAdapter(songAdapter); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), LinearLayoutManager.VERTICAL); + recyclerView.addItemDecoration(dividerItemDecoration); + + // Beobachte Änderungen in der Song-Liste + homeViewModel.getAllSongsLive().observe(getViewLifecycleOwner(), songs -> { + songAdapter.updateData(songs); + }); + return root; } @@ -84,6 +107,19 @@ public class HomeFragment extends Fragment { private void handleSelectedNoteSheets(Uri... files) { SongEditorDialog songEditorDialog = new SongEditorDialog(); songEditorDialog.setNoteSheetFiles(files); + songEditorDialog.setHomeViewModel(homeViewModel); songEditorDialog.show(getParentFragmentManager(), "songEditorDialog"); } + + private static class SongEventClickListener implements SongAdapter.OnSongEventClickListener { + @Override + public void onEditSong(Song song) { + + } + + @Override + public void onDeleteSong(Song song) { + + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java b/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java index 4458c1d..f8f98ef 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java +++ b/app/src/main/java/com/stormtales/notevault/ui/home/HomeViewModel.java @@ -1,26 +1,48 @@ package com.stormtales.notevault.ui.home; +import android.app.Application; +import android.util.Log; +import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import com.stormtales.notevault.data.entities.Song; import com.stormtales.notevault.data.repositories.SongRepository; +import kotlinx.coroutines.CoroutineScope; + +import java.util.ArrayList; +import java.util.List; public class HomeViewModel extends ViewModel { - private final MutableLiveData mText; + private MutableLiveData> allSongs; private SongRepository songRepository; public HomeViewModel() { - mText = new MutableLiveData<>(); - mText.setValue("This is home fragment"); - } - - public LiveData getText() { - return mText; + /*songRepository = new SongRepository(application); + List requestedSongs = songRepository.getAllSongs();*/ + List currentSongs = new ArrayList<>(); + currentSongs.add(new Song("Test", "Some Test Composer", "Genre", 2022)); + this.allSongs = new MutableLiveData<>(currentSongs); + /*this.allSongs.setValue(songRepository.getAllSongs());*/ } public void addSong(Song song) { - songRepository.insert(song); + //songRepository.insert(song); + List currentSongs = allSongs.getValue(); + if(currentSongs == null) { + currentSongs = new ArrayList<>(); + } + + // Neue Liste erstellen und den Song hinzufügen + List updatedSongs = new ArrayList<>(currentSongs); + updatedSongs.add(song); + // Neue Liste in MutableLiveData setzen + allSongs.setValue(updatedSongs); + Log.d("HomeViewModel", "Song added. Total songs: " + updatedSongs.size()); + } + + public LiveData> getAllSongsLive() { + return allSongs; } } \ No newline at end of file diff --git a/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java b/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java new file mode 100644 index 0000000..2e7487e --- /dev/null +++ b/app/src/main/java/com/stormtales/notevault/ui/home/SongAdapter.java @@ -0,0 +1,80 @@ +package com.stormtales.notevault.ui.home; + +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; +import com.stormtales.notevault.R; +import com.stormtales.notevault.data.entities.Song; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class SongAdapter extends RecyclerView.Adapter { + + private List songList; + private OnSongEventClickListener onSongEventClickListener; + + public SongAdapter(List songList, OnSongEventClickListener onSongEventClickListener) { + this.songList = songList; + this.onSongEventClickListener = onSongEventClickListener; + } + + public void updateData(List songs) { + if (songs == null) { + this.songList = new ArrayList<>(); + } else { + this.songList = new ArrayList<>(songs); + } + notifyDataSetChanged(); + Log.d("SongAdapter", "Data updated: " + this.songList.size()); + } + + public static class SongViewHolder extends RecyclerView.ViewHolder { + TextView textYear, textComposition, textTitle; + ImageButton editButton, deleteButton; + + public SongViewHolder(View itemView) { + super(itemView); + textYear = itemView.findViewById(R.id.text_year); + textComposition = itemView.findViewById(R.id.text_composition); + textTitle = itemView.findViewById(R.id.text_title); + editButton = itemView.findViewById(R.id.edit_song_button); + deleteButton = itemView.findViewById(R.id.delete_song_button); + } + } + + @NonNull + @Override + public @NotNull SongViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int i) { + View itemView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_song, parent, false); + return new SongViewHolder(itemView); + } + + @Override + public void onBindViewHolder(@NonNull @NotNull SongAdapter.SongViewHolder holder, int position) { + Song song = songList.get(position); + holder.textYear.setText(String.valueOf(song.getYear())); + holder.textComposition.setText(song.getComposer()); + holder.textTitle.setText(song.getTitle()); + + holder.editButton.setOnClickListener(v -> onSongEventClickListener.onEditSong(song)); + holder.deleteButton.setOnClickListener(v -> onSongEventClickListener.onDeleteSong(song)); + } + + @Override + public int getItemCount() { + return songList.size(); + } + + public interface OnSongEventClickListener { + void onEditSong(Song song); + void onDeleteSong(Song song); + } +} diff --git a/app/src/main/java/com/stormtales/notevault/ui/songeditor/SongEditorDialog.java b/app/src/main/java/com/stormtales/notevault/ui/songeditor/SongEditorDialog.java index 81d48b1..0c354d3 100644 --- a/app/src/main/java/com/stormtales/notevault/ui/songeditor/SongEditorDialog.java +++ b/app/src/main/java/com/stormtales/notevault/ui/songeditor/SongEditorDialog.java @@ -25,6 +25,7 @@ public class SongEditorDialog extends DialogFragment { Dialog dialog; private Uri[] noteSheetFiles; + private HomeViewModel homeViewModel; public SongEditorDialog() { // Required empty public constructor @@ -56,14 +57,15 @@ public class SongEditorDialog extends DialogFragment { private void onSave() { String title = ((EditText) dialog.findViewById(R.id.etTitle)).getText().toString(); String composer = ((EditText) dialog.findViewById(R.id.etComposer)).getText().toString(); - int releaseYear = Integer.parseInt(((EditText) dialog.findViewById(R.id.etYear)).getText().toString()); + + String year_string = ((EditText) dialog.findViewById(R.id.etYear)).getText().toString(); + int releaseYear = year_string.isBlank()? 0 : Integer.parseInt(year_string); String genre = ((EditText) dialog.findViewById(R.id.etGenre)).getText().toString(); Song song = new Song(title, composer, genre, releaseYear); - - HomeViewModel homeViewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class); homeViewModel.addSong(song); + dialog.dismiss(); } @@ -71,4 +73,8 @@ public class SongEditorDialog extends DialogFragment { public void setNoteSheetFiles(Uri[] noteSheetFiles) { this.noteSheetFiles = noteSheetFiles; } + + public void setHomeViewModel(HomeViewModel homeViewModel) { + this.homeViewModel = homeViewModel; + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/delete.xml b/app/src/main/res/drawable/delete.xml new file mode 100644 index 0000000..0b9c437 --- /dev/null +++ b/app/src/main/res/drawable/delete.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24.xml b/app/src/main/res/drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24.xml new file mode 100644 index 0000000..a2f714d --- /dev/null +++ b/app/src/main/res/drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 65ac7c7..4a69c05 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -28,4 +28,10 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> + \ No newline at end of file diff --git a/app/src/main/res/layout/item_song.xml b/app/src/main/res/layout/item_song.xml new file mode 100644 index 0000000..fda3320 --- /dev/null +++ b/app/src/main/res/layout/item_song.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +