Propagate song changes to UI

This commit is contained in:
Fawkes100 2025-01-07 16:22:43 +01:00
parent 6768f3f2f4
commit 03ffad650c
3 changed files with 27 additions and 0 deletions

View File

@ -294,5 +294,15 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On
musicDB.musicNoteDao().updateSong(updatedSong);
Log.d("MainActivity", "Updated Song: " + updatedSong.getTitle());
}).start();
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.updateSong(updatedSong);
} else if(currentFragment instanceof GalleryFragment) {
GalleryFragment galleryFragment = (GalleryFragment) currentFragment;
}
}
}

View File

@ -151,4 +151,8 @@ public class HomeFragment extends Fragment {
public void addSong(MusicNote musicNote) {
this.homeViewModel.addSong(musicNote);
}
public void updateSong(MusicNote song) {
this.homeViewModel.updateSong(song);
}
}

View File

@ -47,4 +47,17 @@ public class HomeViewModel extends ViewModel {
noteTitles.setValue(currentSongs);
}
}
public void updateSong(MusicNote updatedSong) {
List<MusicNote> currentSongs = noteTitles.getValue();
if(currentSongs != null) {
for(int i=0; i<currentSongs.size(); i++) {
if(currentSongs.get(i).getMusicNoteId() == updatedSong.getMusicNoteId()) {
currentSongs.set(i, updatedSong);
break;
}
}
noteTitles.setValue(currentSongs);
}
}
}