From 7bd430e0f95d760b920411f024abb40e8aff0f47 Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 1 Nov 2024 14:44:35 +0100 Subject: [PATCH] Update Added Notes --- .../main/java/core/notevault/MainActivity.java | 11 +++++++++++ .../notevault/ui/gallery/GalleryViewModel.java | 1 - .../core/notevault/ui/home/HomeFragment.java | 10 ++++++++-- .../core/notevault/ui/home/HomeViewModel.java | 17 +++++++++++------ .../core/notevault/ui/home/NoteSongAdapter.java | 2 +- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index 142e9b3..699b911 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -24,6 +24,7 @@ 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.home.HomeFragment; import core.notevault.ui.metadatadialog.MetaDataDialog; import core.notevault.util.NoteSheetsUtil; @@ -86,6 +87,16 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On NoteSheet noteSheet = new NoteSheet(musicNoteID, filePath); musicDB.musicNoteDao().insertNoteSheet(noteSheet); } + + runOnUiThread(() -> { + NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main); + Fragment currentFragment = navHostFragment != null ? navHostFragment.getChildFragmentManager().getFragments().get(0) : null; + + if(currentFragment instanceof HomeFragment) { + HomeFragment homeFragment = (HomeFragment) currentFragment; + homeFragment.addSong(musicNote); + } + }); }).start(); } 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 c253df8..33b3268 100644 --- a/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java +++ b/app/src/main/java/core/notevault/ui/gallery/GalleryViewModel.java @@ -32,6 +32,5 @@ public class GalleryViewModel extends ViewModel { currentConcerts.add(concert); concerts.setValue(currentConcerts); } - } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/ui/home/HomeFragment.java b/app/src/main/java/core/notevault/ui/home/HomeFragment.java index 72231b4..99cf7dd 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/core/notevault/ui/home/HomeFragment.java @@ -53,6 +53,10 @@ public class HomeFragment extends Fragment { recyclerView.setAdapter(noteSongAdapter); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + homeViewModel.getNoteTitles().observe(getViewLifecycleOwner(), songs -> { + noteSongAdapter.updateSongTitles(songs); + }); + new LoadSongTitlesTask().execute(); return root; @@ -69,7 +73,7 @@ public class HomeFragment extends Fragment { @Override protected void onPostExecute(List songs) { - noteSongAdapter.updateSongTitles(songs); // Aktualisiere den Adapter mit den Titeln + homeViewModel.setNoteTitles(songs); } } @@ -120,5 +124,7 @@ public class HomeFragment extends Fragment { binding = null; } - + public void addSong(MusicNote musicNote) { + this.homeViewModel.addSong(musicNote); + } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/ui/home/HomeViewModel.java b/app/src/main/java/core/notevault/ui/home/HomeViewModel.java index b87b25f..97ace7b 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeViewModel.java +++ b/app/src/main/java/core/notevault/ui/home/HomeViewModel.java @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import core.notevault.MainActivity; import core.notevault.data.MusicDatabase; +import core.notevault.data.MusicNote; import core.notevault.ui.metadatadialog.MetaDataDialog; import java.util.ArrayList; @@ -13,21 +14,25 @@ import java.util.List; public class HomeViewModel extends ViewModel { - private final MutableLiveData> noteTitles; + private final MutableLiveData> noteTitles; public HomeViewModel() { noteTitles = new MutableLiveData<>(new ArrayList<>()); } - public LiveData> getNoteTitles() { + public LiveData> getNoteTitles() { return noteTitles; } - public void setNoteTitles(ArrayList noteTitles) { + public void setNoteTitles(List noteTitles) { this.noteTitles.setValue(noteTitles); } - public void addNote(Uri uri) { - - } + public void addSong(MusicNote song) { + List songs = noteTitles.getValue(); + if(songs != null) { + songs.add(song); + this.noteTitles.setValue(songs); + } + } } \ No newline at end of file diff --git a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java index 964cae2..a7fb6b3 100644 --- a/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java +++ b/app/src/main/java/core/notevault/ui/home/NoteSongAdapter.java @@ -111,7 +111,7 @@ public class NoteSongAdapter extends RecyclerView.Adapter doInBackground(Void... voids) { List sheets = musicNoteDAO.getNoteSheetsForMusicSong(this.musicNoteId); - Log.d("LoadNoteSheetsTask", "Loaded NoteSheets: " + sheets.get(0)); + Log.d("LoadNoteSheetsTask", "Loaded NoteSheets: " + sheets.size()); return sheets; }