From a7fcaa0ea7aa6d3eed28f061181bba55ef1db1fb Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 1 Nov 2024 16:55:19 +0100 Subject: [PATCH] Edit Concerts --- .../java/core/notevault/MainActivity.java | 54 +++++++++++++------ .../java/core/notevault/data/Concert.java | 7 +++ .../core/notevault/data/MusicNoteDAO.java | 8 +-- .../notevault/ui/gallery/ConcertAdapter.java | 24 ++++++--- .../notevault/ui/gallery/GalleryFragment.java | 17 +++++- .../ui/gallery/GalleryViewModel.java | 14 +++++ .../gallery/editor/ConcertEditorDialog.java | 40 +++++++++++--- app/src/main/res/layout/concert_item.xml | 2 +- 8 files changed, 127 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index 4b5bd55..aae38b7 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -122,26 +122,46 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On } @Override - public void onConcertEditorClosed(String concertTitle, String concertDate) { + public void onConcertEditorClosed(String concertTitle, String concertDate, int concertID) { new Thread(() -> { - Concert concert = new Concert(concertTitle, concertDate); - Log.d("ConcertEditor", "Saved Concert: " + concertTitle + " on " + concertDate); - musicDB.musicNoteDao().insertConcert(concert); + if(concertID < 0) { + Concert concert = new Concert(concertTitle, concertDate); + Log.d("ConcertEditor", "Saved Concert: " + concertTitle + " on " + concertDate); + musicDB.musicNoteDao().insertConcert(concert); - runOnUiThread(() -> { - NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main); - Fragment currentFragment = navHostFragment != null ? navHostFragment.getChildFragmentManager().getFragments().get(0) : null; + runOnUiThread(() -> { + NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main); + Fragment currentFragment = navHostFragment != null ? navHostFragment.getChildFragmentManager().getFragments().get(0) : null; + + Log.d("MainActivity", "Test for GalleryFragment"); + Log.d("MainActivity", "Current Fragment: " + currentFragment.getClass().getSimpleName()); + if(currentFragment instanceof GalleryFragment) { + Log.d("MainActivity", "GalleryFragment Found"); + GalleryFragment galleryFragment = (GalleryFragment) currentFragment; + galleryFragment.addConcert(concert); + } else { + Log.d("MainActivity", "GalleryFragment Not Found"); + } + }); + } else { + Concert concert = new Concert(concertID, concertTitle, concertDate); + musicDB.musicNoteDao().updateConcert(concert); + runOnUiThread(() -> { + NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main); + Fragment currentFragment = navHostFragment != null ? navHostFragment.getChildFragmentManager().getFragments().get(0) : null; + + Log.d("MainActivity", "Test for GalleryFragment"); + Log.d("MainActivity", "Current Fragment: " + currentFragment.getClass().getSimpleName()); + if(currentFragment instanceof GalleryFragment) { + Log.d("MainActivity", "GalleryFragment Found"); + GalleryFragment galleryFragment = (GalleryFragment) currentFragment; + galleryFragment.updateConcert(concert); + } else { + Log.d("MainActivity", "GalleryFragment Not Found"); + } + }); + } - Log.d("MainActivity", "Test for GalleryFragment"); - Log.d("MainActivity", "Current Fragment: " + currentFragment.getClass().getSimpleName()); - if(currentFragment instanceof GalleryFragment) { - Log.d("MainActivity", "GalleryFragment Found"); - GalleryFragment galleryFragment = (GalleryFragment) currentFragment; - galleryFragment.addConcert(concert); - } else { - Log.d("MainActivity", "GalleryFragment Not Found"); - } - }); }).start(); } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/data/Concert.java b/app/src/main/java/core/notevault/data/Concert.java index b4fe639..bf025ae 100644 --- a/app/src/main/java/core/notevault/data/Concert.java +++ b/app/src/main/java/core/notevault/data/Concert.java @@ -26,6 +26,13 @@ public class Concert { public Concert() { } + @Ignore + public Concert(int id, String title, String concertDate) { + this.id = id; + this.title = title; + this.concertDate = concertDate; + } + public int getId() { return id; } diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index 714eb5e..25e5e4e 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -1,9 +1,6 @@ package core.notevault.data; -import androidx.room.Dao; -import androidx.room.Delete; -import androidx.room.Insert; -import androidx.room.Query; +import androidx.room.*; import java.util.List; @@ -30,4 +27,7 @@ public interface MusicNoteDAO { @Delete void deleteConcert(Concert concert); + + @Update + void updateConcert(Concert concert); } diff --git a/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java b/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java index 7aac6a2..38a2b37 100644 --- a/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java +++ b/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java @@ -20,18 +20,25 @@ public class ConcertAdapter extends RecyclerView.Adapter concertList; private final OnConcertDeleteListener deleteListener; + private final OnOpenConcertEditorListener editorListener; public interface OnConcertDeleteListener { void onDeleteConcert(Concert concert); } - public ConcertAdapter(List concertList, OnConcertDeleteListener deleteListener) { - this.concertList = concertList; - this.deleteListener = deleteListener; + public interface OnOpenConcertEditorListener { + void onOpenConcertEditor(Concert concert); } - public ConcertAdapter(OnConcertDeleteListener deleteListener) { + public ConcertAdapter(List concertList, OnConcertDeleteListener deleteListener, OnOpenConcertEditorListener editorListener) { + this.concertList = concertList; this.deleteListener = deleteListener; + this.editorListener = editorListener; + } + + public ConcertAdapter(OnConcertDeleteListener deleteListener, OnOpenConcertEditorListener editorListener) { + this.deleteListener = deleteListener; + this.editorListener = editorListener; this.concertList = new ArrayList<>(); } @@ -40,10 +47,9 @@ public class ConcertAdapter extends RecyclerView.Adapter{ - Log.d("ConcertAdapter", "Delete button clicked for: " + concertList.get(position).getTitle()); - deleteListener.onDeleteConcert(concertList.get(position)); - }); + holder.deleteButton.setOnClickListener(v -> deleteListener.onDeleteConcert(concertList.get(position))); + holder.editButton.setOnClickListener(v -> editorListener.onOpenConcertEditor(concertList.get(position))); + } @Override @@ -71,12 +77,14 @@ public class ConcertAdapter extends RecyclerView.Adapter> { diff --git a/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java b/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java index 4fe9641..48132b8 100644 --- a/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java +++ b/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java @@ -41,4 +41,18 @@ public class GalleryViewModel extends ViewModel { concerts.setValue(currentConcerts); } } + + public void updateConcert(Concert concert) { + List currentConcerts = concerts.getValue(); + if(currentConcerts != null) { + for(int i = 0; i < currentConcerts.size(); i++) { + if(currentConcerts.get(i).getId() == concert.getId()) { + currentConcerts.get(i).setTitle(concert.getTitle()); + currentConcerts.get(i).setConcertDate(concert.getConcertDate()); + break; + } + } + concerts.setValue(currentConcerts); + } + } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/ui/gallery/editor/ConcertEditorDialog.java b/app/src/main/java/core/notevault/ui/gallery/editor/ConcertEditorDialog.java index 0cce15f..3f5e9c5 100644 --- a/app/src/main/java/core/notevault/ui/gallery/editor/ConcertEditorDialog.java +++ b/app/src/main/java/core/notevault/ui/gallery/editor/ConcertEditorDialog.java @@ -12,19 +12,34 @@ import android.widget.EditText; import androidx.annotation.NonNull; import androidx.fragment.app.DialogFragment; import core.notevault.R; +import core.notevault.data.Concert; import core.notevault.ui.metadatadialog.MetaDataDialog; import java.util.Calendar; import java.util.Date; public class ConcertEditorDialog extends DialogFragment { + private Concert concert; - private EditText concert_input; + private EditText concert_date_input; + private EditText concertTitleInput; private OnConcertEditorListener listener; + private int concertID = -1; + + public void setConcert(Concert concert) { + concert_date_input.setText(concert.getConcertDate()); + concertTitleInput.setText(concert.getTitle()); + this.concert = concert; + } + public interface OnConcertEditorListener { - void onConcertEditorClosed(String concertTitle, String concertDate); + void onConcertEditorClosed(String concertTitle, String concertDate, int concertID); + } + + public interface OnConcertEditorEditListener { + void onConcertEditorEditClosed(String concertTitle, String concertDate); } @NonNull @@ -34,18 +49,29 @@ public class ConcertEditorDialog extends DialogFragment { View dialogView = inflater.inflate(R.layout.concert_editor, null); EditText title_input = dialogView.findViewById(R.id.concert_title_input); - concert_input = dialogView.findViewById(R.id.concert_date_input); + concert_date_input = dialogView.findViewById(R.id.concert_date_input); - concert_input.setOnClickListener(v -> showDatePickerDialog()); + concert_date_input.setOnClickListener(v -> showDatePickerDialog()); + + if(getArguments() != null) { + String concertTitle = getArguments().getString("concert_title"); + String concertDate = getArguments().getString("concert_date"); + concertID = getArguments().getInt("concertID"); + title_input.setText(concertTitle); + concert_date_input.setText(concertDate); + + } return new AlertDialog.Builder(requireContext()) .setView(dialogView) .setPositiveButton("Speichern", (dialog, which) -> { String title = title_input.getText().toString(); - String concertDate = concert_input.getText().toString(); + String concertDate = concert_date_input.getText().toString(); Log.d("ConcertEditor", "ConcertDate: " + concertDate); - listener.onConcertEditorClosed(title, concertDate); + listener.onConcertEditorClosed(title, concertDate, concertID); + + }) .setNegativeButton("Abbrechen", (dialog, which) -> {} ) .create(); @@ -73,7 +99,7 @@ public class ConcertEditorDialog extends DialogFragment { DatePickerDialog datePickerDialog = new DatePickerDialog(this.getContext(), (view, selectedYear, selectedMonth, selectedDay) -> { // Setze das ausgewählte Datum in das EditText - concert_input.setText(selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear); + concert_date_input.setText(selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear); }, year, month, day); // Zeige den Dialog an diff --git a/app/src/main/res/layout/concert_item.xml b/app/src/main/res/layout/concert_item.xml index d1a8e0c..657a951 100644 --- a/app/src/main/res/layout/concert_item.xml +++ b/app/src/main/res/layout/concert_item.xml @@ -40,7 +40,7 @@ android:orientation="horizontal">