From 4de3a459e6de45a876d8ba3a77737deb77e6052a Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 1 Nov 2024 12:18:28 +0100 Subject: [PATCH] Update Concerts --- .../java/core/notevault/MainActivity.java | 18 ++++++++++++ .../notevault/ui/gallery/ConcertAdapter.java | 2 ++ .../notevault/ui/gallery/GalleryFragment.java | 29 ++++++++++++++++--- .../ui/gallery/GalleryViewModel.java | 28 ++++++++++++++---- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index a3cfecc..142e9b3 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -6,6 +6,9 @@ import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Menu; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.navigation.fragment.NavHostFragment; import com.google.android.material.snackbar.Snackbar; import com.google.android.material.navigation.NavigationView; import androidx.navigation.NavController; @@ -19,6 +22,7 @@ import core.notevault.data.MusicDatabase; import core.notevault.data.MusicNote; import core.notevault.data.NoteSheet; import core.notevault.databinding.ActivityMainBinding; +import core.notevault.ui.gallery.GalleryFragment; import core.notevault.ui.gallery.editor.ConcertEditorDialog; import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.util.NoteSheetsUtil; @@ -110,6 +114,20 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On 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; + + 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/ui/gallery/ConcertAdapter.java b/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java index 5525bfe..a92698a 100644 --- a/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java +++ b/app/src/main/java/core/notevault/ui/gallery/ConcertAdapter.java @@ -1,5 +1,6 @@ package core.notevault.ui.gallery; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -45,6 +46,7 @@ public class ConcertAdapter extends RecyclerView.Adapter concerts) { + Log.d("ConcertAdapter", "Update Concerts: " + this.concertList.size() + "vs new: " + concerts.size()); this.concertList.clear(); this.concertList.addAll(concerts); notifyDataSetChanged(); diff --git a/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java b/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java index a78edc4..6284c33 100644 --- a/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java +++ b/app/src/main/java/core/notevault/ui/gallery/GalleryFragment.java @@ -2,6 +2,7 @@ package core.notevault.ui.gallery; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -26,10 +27,11 @@ public class GalleryFragment extends Fragment { private FragmentGalleryBinding binding; private ConcertAdapter concertAdapter; + private GalleryViewModel galleryViewModel; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - GalleryViewModel galleryViewModel = + galleryViewModel = new ViewModelProvider(this).get(GalleryViewModel.class); binding = FragmentGalleryBinding.inflate(inflater, container, false); @@ -40,24 +42,39 @@ public class GalleryFragment extends Fragment { recyclerView.setAdapter(concertAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - new LoadConcerts().execute(); + FloatingActionButton addConcertBtn = root.findViewById(R.id.add_concert_btn); addConcertBtn.setOnClickListener(v -> onCreateNewConcert()); + + galleryViewModel.getConcerts().observe(getViewLifecycleOwner(), newConcerts -> { + concertAdapter.updateConcerts(newConcerts); + }); + + new LoadConcerts().execute(); return root; } + public void updateConcerts() { + + } + private class LoadConcerts extends AsyncTask> { @Override protected List doInBackground(Void... voids) { MusicDatabase db = MusicDatabase.getDatabase(getContext()); MusicNoteDAO musicNoteDAO = db.musicNoteDao(); - return musicNoteDAO.getAllConcerts(); + List concerts = musicNoteDAO.getAllConcerts(); + Log.d("GalleryFragment", "Fetching concerts: " + concerts.size()); + return concerts; + } @Override protected void onPostExecute(List concerts) { - concertAdapter.updateConcerts(concerts); // Aktualisiere den Adapter mit den Titeln + Log.d("GalleryFragment", "Concerts size in onPostExecute: " + concerts.size()); + // concertAdapter.updateConcerts(concerts); // Aktualisiere den Adapter mit den Titeln + galleryViewModel.setConcerts(concerts); } } @@ -66,6 +83,10 @@ public class GalleryFragment extends Fragment { concertEditorDialog.show(getParentFragmentManager(), ConcertEditorDialog.TAG); } + public void addConcert(Concert concert) { + this.galleryViewModel.addConcert(concert); + } + @Override public void onDestroyView() { super.onDestroyView(); 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 6d83e44..c253df8 100644 --- a/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java +++ b/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java @@ -1,19 +1,37 @@ package core.notevault.ui.gallery; +import android.util.Log; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import core.notevault.data.Concert; + +import java.util.List; public class GalleryViewModel extends ViewModel { - private final MutableLiveData mText; + private final MutableLiveData> concerts; public GalleryViewModel() { - mText = new MutableLiveData<>(); - mText.setValue("This is gallery fragment"); + this.concerts = new MutableLiveData<>(); } - public LiveData getText() { - return mText; + + public LiveData> getConcerts() { + return concerts; + } + + public void setConcerts(List concerts) { + this.concerts.setValue(concerts); + } + + public void addConcert(Concert concert) { + List currentConcerts = this.concerts.getValue(); + Log.d("GalleryViewModel", "Add Concert"); + if(currentConcerts != null) { + currentConcerts.add(concert); + concerts.setValue(currentConcerts); + } + } } \ No newline at end of file