From d9b59ba07cb74ba18c59731f14bf994f70e48620 Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Sat, 18 Jan 2025 17:11:21 +0100 Subject: [PATCH] Edit Songs --- .../notevault/data/entities/Song.java | 2 +- .../notevault/ui/home/HomeFragment.java | 5 ++- .../notevault/ui/home/HomeViewModel.java | 14 ++++++++ .../ui/songeditor/SongEditorDialog.java | 35 +++++++++++++++++-- 4 files changed, 52 insertions(+), 4 deletions(-) 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 51f67f4..a359e5c 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 @@ -10,7 +10,7 @@ import java.util.Objects; @Entity public class Song { - @PrimaryKey + @PrimaryKey(autoGenerate = true) private int localID; private String serverID; 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 db13d89..aa976fb 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 @@ -112,7 +112,10 @@ public class HomeFragment extends Fragment { } public void onEditSong(Song song) { - + SongEditorDialog songEditorDialog = new SongEditorDialog(); + songEditorDialog.setEditedSong(song); + songEditorDialog.setHomeViewModel(homeViewModel); + songEditorDialog.show(getParentFragmentManager(), "songEditorDialog"); } public void onDeleteSong(Song song) { 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 6a3b78b..33a9deb 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 @@ -63,4 +63,18 @@ public class HomeViewModel extends ViewModel { allSongs.setValue(currentSongs); } } + + public void updateSong(Song editedSong) { + List currentSongs = allSongs.getValue(); + if(currentSongs != null) { + for(int i = 0; i < currentSongs.size(); i++) { + if(currentSongs.get(i).getLocalID() == editedSong.getLocalID()) { + currentSongs.set(i, editedSong); + break; + } + } + songRepository.updateSong(editedSong); + allSongs.setValue(currentSongs); + } + } } \ No newline at end of file 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 0c354d3..328a6fb 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 @@ -17,6 +17,7 @@ import android.view.ViewGroup; import androidx.lifecycle.ViewModelProvider; import com.stormtales.notevault.R; import com.stormtales.notevault.data.entities.Song; +import com.stormtales.notevault.data.sync.SyncStatus; import com.stormtales.notevault.ui.home.HomeViewModel; import org.jetbrains.annotations.NotNull; @@ -27,6 +28,8 @@ public class SongEditorDialog extends DialogFragment { private Uri[] noteSheetFiles; private HomeViewModel homeViewModel; + private Song editedSong; + public SongEditorDialog() { // Required empty public constructor } @@ -38,6 +41,8 @@ public class SongEditorDialog extends DialogFragment { LayoutInflater inflater = LayoutInflater.from(getContext()); View dialogView = inflater.inflate(R.layout.fragment_song_editor_dialog, null); + + dialogView.findViewById(R.id.btnCancel).setOnClickListener(v-> onCancel()); dialogView.findViewById(R.id.btnSave).setOnClickListener(v -> onSave()); @@ -47,9 +52,22 @@ public class SongEditorDialog extends DialogFragment { dialog = builder.create(); dialog.setCanceledOnTouchOutside(false); + + return dialog; } + @Override + public void onStart() { + super.onStart(); + if(editedSong != null) { + ((EditText) dialog.findViewById(R.id.etTitle)).setText(editedSong.getTitle()); + ((EditText) dialog.findViewById(R.id.etComposer)).setText(editedSong.getComposer()); + ((EditText) dialog.findViewById(R.id.etGenre)).setText(editedSong.getGenre()); + ((EditText) dialog.findViewById(R.id.etYear)).setText(String.valueOf(editedSong.getYear())); + } + } + private void onCancel() { dialog.dismiss(); } @@ -62,8 +80,17 @@ public class SongEditorDialog extends DialogFragment { 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.addSong(song); + if(editedSong != null) { + editedSong.setTitle(title); + editedSong.setComposer(composer); + editedSong.setYear(releaseYear); + editedSong.setGenre(genre); + editedSong.setSyncStatus(SyncStatus.MODIFIED); + homeViewModel.updateSong(editedSong); + } else { + Song song = new Song(title, composer, genre, releaseYear); + homeViewModel.addSong(song); + } dialog.dismiss(); @@ -77,4 +104,8 @@ public class SongEditorDialog extends DialogFragment { public void setHomeViewModel(HomeViewModel homeViewModel) { this.homeViewModel = homeViewModel; } + + public void setEditedSong(Song editedSong) { + this.editedSong = editedSong; + } } \ No newline at end of file