Update Concerts

This commit is contained in:
sebastian 2024-11-01 12:18:28 +01:00
parent b62ea3bc91
commit 4de3a459e6
4 changed files with 68 additions and 9 deletions

View File

@ -6,6 +6,9 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.Menu; 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.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController; import androidx.navigation.NavController;
@ -19,6 +22,7 @@ import core.notevault.data.MusicDatabase;
import core.notevault.data.MusicNote; import core.notevault.data.MusicNote;
import core.notevault.data.NoteSheet; import core.notevault.data.NoteSheet;
import core.notevault.databinding.ActivityMainBinding; import core.notevault.databinding.ActivityMainBinding;
import core.notevault.ui.gallery.GalleryFragment;
import core.notevault.ui.gallery.editor.ConcertEditorDialog; import core.notevault.ui.gallery.editor.ConcertEditorDialog;
import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.ui.metadatadialog.MetaDataDialog;
import core.notevault.util.NoteSheetsUtil; 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); Log.d("ConcertEditor", "Saved Concert: " + concertTitle + " on " + concertDate);
musicDB.musicNoteDao().insertConcert(concert); 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(); }).start();
} }
} }

View File

@ -1,5 +1,6 @@
package core.notevault.ui.gallery; package core.notevault.ui.gallery;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -45,6 +46,7 @@ public class ConcertAdapter extends RecyclerView.Adapter<ConcertAdapter.ConcertV
} }
public void updateConcerts(List<Concert> concerts) { public void updateConcerts(List<Concert> concerts) {
Log.d("ConcertAdapter", "Update Concerts: " + this.concertList.size() + "vs new: " + concerts.size());
this.concertList.clear(); this.concertList.clear();
this.concertList.addAll(concerts); this.concertList.addAll(concerts);
notifyDataSetChanged(); notifyDataSetChanged();

View File

@ -2,6 +2,7 @@ package core.notevault.ui.gallery;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -26,10 +27,11 @@ public class GalleryFragment extends Fragment {
private FragmentGalleryBinding binding; private FragmentGalleryBinding binding;
private ConcertAdapter concertAdapter; private ConcertAdapter concertAdapter;
private GalleryViewModel galleryViewModel;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
GalleryViewModel galleryViewModel = galleryViewModel =
new ViewModelProvider(this).get(GalleryViewModel.class); new ViewModelProvider(this).get(GalleryViewModel.class);
binding = FragmentGalleryBinding.inflate(inflater, container, false); binding = FragmentGalleryBinding.inflate(inflater, container, false);
@ -40,24 +42,39 @@ public class GalleryFragment extends Fragment {
recyclerView.setAdapter(concertAdapter); recyclerView.setAdapter(concertAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
new LoadConcerts().execute();
FloatingActionButton addConcertBtn = root.findViewById(R.id.add_concert_btn); FloatingActionButton addConcertBtn = root.findViewById(R.id.add_concert_btn);
addConcertBtn.setOnClickListener(v -> onCreateNewConcert()); addConcertBtn.setOnClickListener(v -> onCreateNewConcert());
galleryViewModel.getConcerts().observe(getViewLifecycleOwner(), newConcerts -> {
concertAdapter.updateConcerts(newConcerts);
});
new LoadConcerts().execute();
return root; return root;
} }
public void updateConcerts() {
}
private class LoadConcerts extends AsyncTask<Void, Void, List<Concert>> { private class LoadConcerts extends AsyncTask<Void, Void, List<Concert>> {
@Override @Override
protected List<Concert> doInBackground(Void... voids) { protected List<Concert> doInBackground(Void... voids) {
MusicDatabase db = MusicDatabase.getDatabase(getContext()); MusicDatabase db = MusicDatabase.getDatabase(getContext());
MusicNoteDAO musicNoteDAO = db.musicNoteDao(); MusicNoteDAO musicNoteDAO = db.musicNoteDao();
return musicNoteDAO.getAllConcerts(); List<Concert> concerts = musicNoteDAO.getAllConcerts();
Log.d("GalleryFragment", "Fetching concerts: " + concerts.size());
return concerts;
} }
@Override @Override
protected void onPostExecute(List<Concert> concerts) { protected void onPostExecute(List<Concert> 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); concertEditorDialog.show(getParentFragmentManager(), ConcertEditorDialog.TAG);
} }
public void addConcert(Concert concert) {
this.galleryViewModel.addConcert(concert);
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();

View File

@ -1,19 +1,37 @@
package core.notevault.ui.gallery; package core.notevault.ui.gallery;
import android.util.Log;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import core.notevault.data.Concert;
import java.util.List;
public class GalleryViewModel extends ViewModel { public class GalleryViewModel extends ViewModel {
private final MutableLiveData<String> mText; private final MutableLiveData<List<Concert>> concerts;
public GalleryViewModel() { public GalleryViewModel() {
mText = new MutableLiveData<>(); this.concerts = new MutableLiveData<>();
mText.setValue("This is gallery fragment");
} }
public LiveData<String> getText() {
return mText; public LiveData<List<Concert>> getConcerts() {
return concerts;
}
public void setConcerts(List<Concert> concerts) {
this.concerts.setValue(concerts);
}
public void addConcert(Concert concert) {
List<Concert> currentConcerts = this.concerts.getValue();
Log.d("GalleryViewModel", "Add Concert");
if(currentConcerts != null) {
currentConcerts.add(concert);
concerts.setValue(currentConcerts);
}
} }
} }