List Songs in HomeFragment

This commit is contained in:
Fawkes100 2025-01-18 16:30:13 +01:00
parent 6a6a7bfa90
commit 417477685d
10 changed files with 271 additions and 17 deletions

View File

@ -31,6 +31,9 @@ public class Song {
this.year = year;
}
public Song() {
}
public int getLocalID() {
return localID;
}

View File

@ -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<Song> getAllSongs() {
return songDao.getAllSongs();
}
}

View File

@ -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<Intent> 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) {
}
}
}

View File

@ -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<String> mText;
private MutableLiveData<List<Song>> allSongs;
private SongRepository songRepository;
public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}
public LiveData<String> getText() {
return mText;
/*songRepository = new SongRepository(application);
List<Song> requestedSongs = songRepository.getAllSongs();*/
List<Song> 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<Song> currentSongs = allSongs.getValue();
if(currentSongs == null) {
currentSongs = new ArrayList<>();
}
// Neue Liste erstellen und den Song hinzufügen
List<Song> 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<List<Song>> getAllSongsLive() {
return allSongs;
}
}

View File

@ -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<SongAdapter.SongViewHolder> {
private List<Song> songList;
private OnSongEventClickListener onSongEventClickListener;
public SongAdapter(List<Song> songList, OnSongEventClickListener onSongEventClickListener) {
this.songList = songList;
this.onSongEventClickListener = onSongEventClickListener;
}
public void updateData(List<Song> 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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M280,840q-33,0 -56.5,-23.5T200,760v-520h-40v-80h200v-40h240v40h200v80h-40v520q0,33 -23.5,56.5T680,840L280,840ZM680,240L280,240v520h400v-520ZM360,680h80v-360h-80v360ZM520,680h80v-360h-80v360ZM280,240v520,-520Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M200,760h57l391,-391 -57,-57 -391,391v57ZM120,840v-170l528,-527q12,-11 26.5,-17t30.5,-6q16,0 31,6t26,18l55,56q12,11 17.5,26t5.5,30q0,16 -5.5,30.5T817,313L290,840L120,840ZM760,256 L704,200 760,256ZM619,341 L591,312 648,369 619,341Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@ -28,4 +28,10 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_songs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:padding="8dp"
android:gravity="center_vertical">
<!-- Text Section -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<!-- First Row: Year and Composition -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
<TextView
android:id="@+id/text_composition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Composition"
android:textSize="14sp"
android:layout_marginStart="8dp"
android:textColor="@android:color/darker_gray" />
</LinearLayout>
<!-- Second Row: Title -->
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="16sp"
android:textColor="@android:color/black"
android:paddingTop="4dp"/>
</LinearLayout>
<!-- Rechte Seite mit den Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
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 Song"
android:background="?android:selectableItemBackgroundBorderless" />
<ImageButton
android:id="@+id/delete_song_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/delete"
app:tint="#FF0000"
android:contentDescription="Delete Song"
android:background="?android:selectableItemBackgroundBorderless"/>
</LinearLayout>
</LinearLayout>