Skip to content

Commit

Permalink
feat: Added API call to retrieve AlbumID3 details in album page as th…
Browse files Browse the repository at this point in the history
…e object passed from search and album list are different
  • Loading branch information
CappielloAntonio committed Jun 2, 2024
1 parent a67571e commit 2c3aebc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public void onBindViewHolder(ViewHolder holder, int position) {
holder.item.searchResultSongSubtitleTextView.setText(
holder.itemView.getContext().getString(
R.string.song_subtitle_formatter,
this.showAlbum ?
song.getAlbum() :
song.getArtist(),
this.showAlbum ?
song.getAlbum() :
song.getArtist(),
MusicUtil.getReadableDurationString(song.getDuration(), false),
MusicUtil.getReadableAudioQualityString(song)
)
Expand Down Expand Up @@ -100,8 +100,8 @@ public void onBindViewHolder(ViewHolder holder, int position) {
if (album.getDiscTitles() != null) {
Optional<DiscTitle> discTitle = album.getDiscTitles().stream().filter(title -> Objects.equals(title.getDisc(), songs.get(position).getDiscNumber())).findFirst();

if (discTitle.isPresent() && discTitle.get().getTitle() != null) {
holder.item.discTitleTextView.setText(holder.itemView.getContext().getString(R.string.disc_titleless, discTitle.get().getTitle()));
if (discTitle.isPresent() && discTitle.get().getDisc() != null && discTitle.get().getTitle() != null && !discTitle.get().getTitle().isEmpty()) {
holder.item.discTitleTextView.setText(holder.itemView.getContext().getString(R.string.disc_titlefull, discTitle.get().getDisc().toString() , discTitle.get().getTitle()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cappielloantonio.tempo.ui.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
Expand Down Expand Up @@ -34,7 +36,6 @@
import com.cappielloantonio.tempo.util.MappingUtil;
import com.cappielloantonio.tempo.util.MusicUtil;
import com.cappielloantonio.tempo.viewmodel.AlbumPageViewModel;
import com.google.android.material.chip.Chip;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.Collections;
Expand All @@ -46,9 +47,7 @@ public class AlbumPageFragment extends Fragment implements ClickCallback {
private FragmentAlbumPageBinding bind;
private MainActivity activity;
private AlbumPageViewModel albumPageViewModel;

private SongHorizontalAdapter songHorizontalAdapter;

private ListenableFuture<MediaBrowser> mediaBrowserListenableFuture;

@Override
Expand Down Expand Up @@ -117,7 +116,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
}

private void init() {
albumPageViewModel.setAlbum(requireArguments().getParcelable(Constants.ALBUM_OBJECT));
albumPageViewModel.setAlbum(getViewLifecycleOwner(), requireArguments().getParcelable(Constants.ALBUM_OBJECT));
}

private void initAppBar() {
Expand All @@ -126,15 +125,20 @@ private void initAppBar() {
if (activity.getSupportActionBar() != null) {
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
activity.getSupportActionBar().setDisplayShowHomeEnabled(true);

}

bind.animToolbar.setTitle(albumPageViewModel.getAlbum().getName());
albumPageViewModel.getAlbum().observe(getViewLifecycleOwner(), album -> {
if (bind != null && album != null) {
bind.animToolbar.setTitle(album.getName());

bind.albumNameLabel.setText(albumPageViewModel.getAlbum().getName());
bind.albumArtistLabel.setText(albumPageViewModel.getAlbum().getArtist());
bind.albumReleaseYearLabel.setText(albumPageViewModel.getAlbum().getYear() != 0 ? String.valueOf(albumPageViewModel.getAlbum().getYear()) : "");
bind.albumSongCountDurationTextview.setText(getString(R.string.album_page_tracks_count_and_duration, albumPageViewModel.getAlbum().getSongCount(), albumPageViewModel.getAlbum().getDuration() != null ? albumPageViewModel.getAlbum().getDuration() / 60 : 0));
bind.albumGenresTextview.setText(albumPageViewModel.getAlbum().getGenre());
bind.albumNameLabel.setText(album.getName());
bind.albumArtistLabel.setText(album.getArtist());
bind.albumReleaseYearLabel.setText(album.getYear() != 0 ? String.valueOf(album.getYear()) : "");
bind.albumSongCountDurationTextview.setText(getString(R.string.album_page_tracks_count_and_duration, album.getSongCount(), album.getDuration() != null ? album.getDuration() / 60 : 0));
bind.albumGenresTextview.setText(album.getGenre());
}
});

bind.animToolbar.setNavigationOnClickListener(v -> activity.navController.navigateUp());

Expand Down Expand Up @@ -164,8 +168,15 @@ private void initAlbumNotes() {
albumPageViewModel.getAlbumInfo().observe(getViewLifecycleOwner(), albumInfo -> {
if (albumInfo != null) {
if (bind != null) bind.albumNotesTextview.setVisibility(View.VISIBLE);
if (bind != null)
bind.albumNotesTextview.setText(MusicUtil.getReadableString(albumInfo.getNotes()));
if (bind != null) bind.albumNotesTextview.setText(MusicUtil.getReadableString(albumInfo.getNotes()));

if (bind != null && albumInfo.getLastFmUrl() != null && !albumInfo.getLastFmUrl().isEmpty()) {
bind.albumNotesTextview.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(albumInfo.getLastFmUrl()));
startActivity(intent);
});
}
} else {
if (bind != null) bind.albumNotesTextview.setVisibility(View.GONE);
}
Expand Down Expand Up @@ -195,20 +206,28 @@ private void initMusicButton() {
}

private void initBackCover() {
CustomGlideRequest.Builder
.from(requireContext(), albumPageViewModel.getAlbum().getCoverArtId(), CustomGlideRequest.ResourceType.Album)
.build()
.into(bind.albumCoverImageView);
albumPageViewModel.getAlbum().observe(getViewLifecycleOwner(), album -> {
if (bind != null && album != null) {
CustomGlideRequest.Builder
.from(requireContext(), album.getCoverArtId(), CustomGlideRequest.ResourceType.Album)
.build()
.into(bind.albumCoverImageView);
}
});
}

private void initSongsView() {
bind.songRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
bind.songRecyclerView.setHasFixedSize(true);
albumPageViewModel.getAlbum().observe(getViewLifecycleOwner(), album -> {
if (bind != null && album != null) {
bind.songRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
bind.songRecyclerView.setHasFixedSize(true);

songHorizontalAdapter = new SongHorizontalAdapter(this, false, false, albumPageViewModel.getAlbum());
bind.songRecyclerView.setAdapter(songHorizontalAdapter);
songHorizontalAdapter = new SongHorizontalAdapter(this, false, false, album);
bind.songRecyclerView.setAdapter(songHorizontalAdapter);

albumPageViewModel.getAlbumSongLiveList().observe(getViewLifecycleOwner(), songs -> songHorizontalAdapter.setItems(songs));
albumPageViewModel.getAlbumSongLiveList().observe(getViewLifecycleOwner(), songs -> songHorizontalAdapter.setItems(songs));
}
});
}

private void initializeMediaBrowser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.cappielloantonio.tempo.repository.AlbumRepository;
import com.cappielloantonio.tempo.repository.ArtistRepository;
Expand All @@ -18,8 +20,8 @@
public class AlbumPageViewModel extends AndroidViewModel {
private final AlbumRepository albumRepository;
private final ArtistRepository artistRepository;

private AlbumID3 album;
private String albumId;
private final MutableLiveData<AlbumID3> album = new MutableLiveData<>(null);

public AlbumPageViewModel(@NonNull Application application) {
super(application);
Expand All @@ -29,22 +31,27 @@ public AlbumPageViewModel(@NonNull Application application) {
}

public LiveData<List<Child>> getAlbumSongLiveList() {
return albumRepository.getAlbumTracks(album.getId());
return albumRepository.getAlbumTracks(albumId);
}

public AlbumID3 getAlbum() {
public MutableLiveData<AlbumID3> getAlbum() {
return album;
}

public void setAlbum(AlbumID3 album) {
this.album = album;
public void setAlbum(LifecycleOwner owner, AlbumID3 album) {
this.albumId = album.getId();
this.album.postValue(album);

albumRepository.getAlbum(album.getId()).observe(owner, albums -> {
if (albums != null) this.album.setValue(album);
});
}

public LiveData<ArtistID3> getArtist() {
return artistRepository.getArtistInfo(album.getArtistId());
return artistRepository.getArtistInfo(albumId);
}

public LiveData<AlbumInfo> getAlbumInfo() {
return albumRepository.getAlbumInfo(album.getId());
return albumRepository.getAlbumInfo(albumId);
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<string name="delete_download_storage_dialog_summary">Please be aware that continuing with this action will result in the permanent deletion of all saved items downloaded from all servers.</string>
<string name="delete_download_storage_dialog_title">Delete saved items</string>
<string name="description_empty_title">No description available</string>
<string name="disc_titlefull">Disc %1$s - %2$s</string>
<string name="disc_titleless">Disc %1$s</string>
<string name="download_directory_dialog_negative_button">Cancel</string>
<string name="download_directory_dialog_positive_button">Download</string>
Expand Down

0 comments on commit 2c3aebc

Please sign in to comment.