fix: minor refactor of sync album observer

This commit is contained in:
eddyizm 2025-08-30 09:23:44 -07:00
parent 31d91f7215
commit eaf2710054
No known key found for this signature in database
GPG key ID: CF5F671829E8158A

View file

@ -7,6 +7,7 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import com.cappielloantonio.tempo.repository.AlbumRepository; import com.cappielloantonio.tempo.repository.AlbumRepository;
@ -49,19 +50,22 @@ public class StarredAlbumsSyncViewModel extends AndroidViewModel {
CountDownLatch latch = new CountDownLatch(albums.size()); CountDownLatch latch = new CountDownLatch(albums.size());
for (AlbumID3 album : albums) { for (AlbumID3 album : albums) {
albumRepository.getAlbumTracks(album.getId()).observeForever(songs -> { LiveData<List<Child>> albumTracks = albumRepository.getAlbumTracks(album.getId());
if (songs != null) { albumTracks.observeForever(new Observer<List<Child>>() {
allSongs.addAll(songs); @Override
} public void onChanged(List<Child> songs) {
latch.countDown(); if (songs != null) {
allSongs.addAll(songs);
if (latch.getCount() == 0) { }
callback.onSongsCollected(allSongs); latch.countDown();
if (latch.getCount() == 0) {
callback.onSongsCollected(allSongs);
albumTracks.removeObserver(this);
}
} }
}); });
} }
albumRepository.removeObserver(this);
} }
private interface AlbumSongsCallback { private interface AlbumSongsCallback {