Propagate song changes to UI
This commit is contained in:
		
							parent
							
								
									6768f3f2f4
								
							
						
					
					
						commit
						03ffad650c
					
				@ -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;
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user