Update Concerts
This commit is contained in:
parent
b62ea3bc91
commit
4de3a459e6
@ -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();
|
||||
}
|
||||
}
|
@ -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<ConcertAdapter.ConcertV
|
||||
}
|
||||
|
||||
public void updateConcerts(List<Concert> concerts) {
|
||||
Log.d("ConcertAdapter", "Update Concerts: " + this.concertList.size() + "vs new: " + concerts.size());
|
||||
this.concertList.clear();
|
||||
this.concertList.addAll(concerts);
|
||||
notifyDataSetChanged();
|
||||
|
@ -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<Void, Void, List<Concert>> {
|
||||
|
||||
@Override
|
||||
protected List<Concert> doInBackground(Void... voids) {
|
||||
MusicDatabase db = MusicDatabase.getDatabase(getContext());
|
||||
MusicNoteDAO musicNoteDAO = db.musicNoteDao();
|
||||
return musicNoteDAO.getAllConcerts();
|
||||
List<Concert> concerts = musicNoteDAO.getAllConcerts();
|
||||
Log.d("GalleryFragment", "Fetching concerts: " + concerts.size());
|
||||
return concerts;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
public void addConcert(Concert concert) {
|
||||
this.galleryViewModel.addConcert(concert);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
|
@ -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<String> mText;
|
||||
private final MutableLiveData<List<Concert>> concerts;
|
||||
|
||||
public GalleryViewModel() {
|
||||
mText = new MutableLiveData<>();
|
||||
mText.setValue("This is gallery fragment");
|
||||
this.concerts = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public LiveData<String> getText() {
|
||||
return mText;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user