Edit Concerts

This commit is contained in:
sebastian 2024-11-01 16:55:19 +01:00
parent e39b164b10
commit a7fcaa0ea7
8 changed files with 127 additions and 39 deletions

View File

@ -122,26 +122,46 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On
} }
@Override @Override
public void onConcertEditorClosed(String concertTitle, String concertDate) { public void onConcertEditorClosed(String concertTitle, String concertDate, int concertID) {
new Thread(() -> { new Thread(() -> {
Concert concert = new Concert(concertTitle, concertDate); if(concertID < 0) {
Log.d("ConcertEditor", "Saved Concert: " + concertTitle + " on " + concertDate); Concert concert = new Concert(concertTitle, concertDate);
musicDB.musicNoteDao().insertConcert(concert); Log.d("ConcertEditor", "Saved Concert: " + concertTitle + " on " + concertDate);
musicDB.musicNoteDao().insertConcert(concert);
runOnUiThread(() -> { runOnUiThread(() -> {
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main); NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
Fragment currentFragment = navHostFragment != null ? navHostFragment.getChildFragmentManager().getFragments().get(0) : null; 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(); }).start();
} }
} }

View File

@ -26,6 +26,13 @@ public class Concert {
public Concert() { public Concert() {
} }
@Ignore
public Concert(int id, String title, String concertDate) {
this.id = id;
this.title = title;
this.concertDate = concertDate;
}
public int getId() { public int getId() {
return id; return id;
} }

View File

@ -1,9 +1,6 @@
package core.notevault.data; package core.notevault.data;
import androidx.room.Dao; import androidx.room.*;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import java.util.List; import java.util.List;
@ -30,4 +27,7 @@ public interface MusicNoteDAO {
@Delete @Delete
void deleteConcert(Concert concert); void deleteConcert(Concert concert);
@Update
void updateConcert(Concert concert);
} }

View File

@ -20,18 +20,25 @@ public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertV
private final List<Concert> concertList; private final List<Concert> concertList;
private final OnConcertDeleteListener deleteListener; private final OnConcertDeleteListener deleteListener;
private final OnOpenConcertEditorListener editorListener;
public interface OnConcertDeleteListener { public interface OnConcertDeleteListener {
void onDeleteConcert(Concert concert); void onDeleteConcert(Concert concert);
} }
public ConcertAdapter(List<Concert> concertList, OnConcertDeleteListener deleteListener) { public interface OnOpenConcertEditorListener {
this.concertList = concertList; void onOpenConcertEditor(Concert concert);
this.deleteListener = deleteListener;
} }
public ConcertAdapter(OnConcertDeleteListener deleteListener) { public ConcertAdapter(List<Concert> concertList, OnConcertDeleteListener deleteListener, OnOpenConcertEditorListener editorListener) {
this.concertList = concertList;
this.deleteListener = deleteListener; this.deleteListener = deleteListener;
this.editorListener = editorListener;
}
public ConcertAdapter(OnConcertDeleteListener deleteListener, OnOpenConcertEditorListener editorListener) {
this.deleteListener = deleteListener;
this.editorListener = editorListener;
this.concertList = new ArrayList<>(); this.concertList = new ArrayList<>();
} }
@ -40,10 +47,9 @@ public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertV
holder.concertTitleView.setText(concertList.get(position).getTitle()); holder.concertTitleView.setText(concertList.get(position).getTitle());
holder.dateHolder.setText(concertList.get(position).getConcertDate()); holder.dateHolder.setText(concertList.get(position).getConcertDate());
holder.deleteButton.setOnClickListener(v ->{ holder.deleteButton.setOnClickListener(v -> deleteListener.onDeleteConcert(concertList.get(position)));
Log.d("ConcertAdapter", "Delete button clicked for: " + concertList.get(position).getTitle()); holder.editButton.setOnClickListener(v -> editorListener.onOpenConcertEditor(concertList.get(position)));
deleteListener.onDeleteConcert(concertList.get(position));
});
} }
@Override @Override
@ -71,12 +77,14 @@ public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertV
TextView dateHolder; TextView dateHolder;
ImageButton deleteButton; ImageButton deleteButton;
ImageButton editButton;
public ConcertViewHolder(@NonNull @NotNull View itemView) { public ConcertViewHolder(@NonNull @NotNull View itemView) {
super(itemView); super(itemView);
concertTitleView = itemView.findViewById(R.id.concert_title); concertTitleView = itemView.findViewById(R.id.concert_title);
dateHolder = itemView.findViewById(R.id.concert_date); dateHolder = itemView.findViewById(R.id.concert_date);
deleteButton = itemView.findViewById(R.id.delete_concert_button); deleteButton = itemView.findViewById(R.id.delete_concert_button);
editButton = itemView.findViewById(R.id.edit_concert_button);
} }
} }
} }

View File

@ -41,7 +41,7 @@ public class GalleryFragment extends Fragment {
View root = binding.getRoot(); View root = binding.getRoot();
RecyclerView recyclerView = root.findViewById(R.id.concert_recycler_view); RecyclerView recyclerView = root.findViewById(R.id.concert_recycler_view);
concertAdapter = new ConcertAdapter(this::deleteConcert); concertAdapter = new ConcertAdapter(this::deleteConcert, this::editConcert);
recyclerView.setAdapter(concertAdapter); recyclerView.setAdapter(concertAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
@ -74,8 +74,21 @@ public class GalleryFragment extends Fragment {
}).start(); }).start();
} }
public void updateConcerts() { public void editConcert(Concert concert) {
ConcertEditorDialog concertEditorDialog = new ConcertEditorDialog();
Bundle args = new Bundle();
args.putString("concert_title", concert.getTitle());
args.putString("concert_date", concert.getConcertDate());
args.putInt("concertID", concert.getId());
concertEditorDialog.setArguments(args);
concertEditorDialog.show(getParentFragmentManager(), ConcertEditorDialog.TAG);
}
public void updateConcert(Concert concert) {
galleryViewModel.updateConcert(concert);
} }
private class LoadConcerts extends AsyncTask<Void, Void, List<Concert>> { private class LoadConcerts extends AsyncTask<Void, Void, List<Concert>> {

View File

@ -41,4 +41,18 @@ public class GalleryViewModel extends ViewModel {
concerts.setValue(currentConcerts); concerts.setValue(currentConcerts);
} }
} }
public void updateConcert(Concert concert) {
List<Concert> 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);
}
}
} }

View File

@ -12,19 +12,34 @@ import android.widget.EditText;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import core.notevault.R; import core.notevault.R;
import core.notevault.data.Concert;
import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.MetaDataDialog;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
public class ConcertEditorDialog extends DialogFragment { public class ConcertEditorDialog extends DialogFragment {
private Concert concert;
private EditText concert_input; private EditText concert_date_input;
private EditText concertTitleInput;
private OnConcertEditorListener listener; 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 { 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 @NonNull
@ -34,18 +49,29 @@ public class ConcertEditorDialog extends DialogFragment {
View dialogView = inflater.inflate(R.layout.concert_editor, null); View dialogView = inflater.inflate(R.layout.concert_editor, null);
EditText title_input = dialogView.findViewById(R.id.concert_title_input); 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()) return new AlertDialog.Builder(requireContext())
.setView(dialogView) .setView(dialogView)
.setPositiveButton("Speichern", (dialog, which) -> { .setPositiveButton("Speichern", (dialog, which) -> {
String title = title_input.getText().toString(); String title = title_input.getText().toString();
String concertDate = concert_input.getText().toString(); String concertDate = concert_date_input.getText().toString();
Log.d("ConcertEditor", "ConcertDate: " + concertDate); Log.d("ConcertEditor", "ConcertDate: " + concertDate);
listener.onConcertEditorClosed(title, concertDate); listener.onConcertEditorClosed(title, concertDate, concertID);
}) })
.setNegativeButton("Abbrechen", (dialog, which) -> {} ) .setNegativeButton("Abbrechen", (dialog, which) -> {} )
.create(); .create();
@ -73,7 +99,7 @@ public class ConcertEditorDialog extends DialogFragment {
DatePickerDialog datePickerDialog = new DatePickerDialog(this.getContext(), DatePickerDialog datePickerDialog = new DatePickerDialog(this.getContext(),
(view, selectedYear, selectedMonth, selectedDay) -> { (view, selectedYear, selectedMonth, selectedDay) -> {
// Setze das ausgewählte Datum in das EditText // 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); }, year, month, day);
// Zeige den Dialog an // Zeige den Dialog an

View File

@ -40,7 +40,7 @@
android:orientation="horizontal"> android:orientation="horizontal">
<ImageButton <ImageButton
android:id="@+id/edit_button" android:id="@+id/edit_concert_button"
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
android:src="@drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24" android:src="@drawable/edit_24dp_e8eaed_fill0_wght400_grad0_opsz24"