use of me.echeung.listenmoeapi.callbacks.FavoriteSongCallback in project android-app by LISTEN-moe.
the class SongActionsUtil method toggleFavorite.
/**
* Updates the favorite status of a song.
*
* @param song The song to update the favorite status of.
*/
public static void toggleFavorite(final Activity activity, final RecyclerView.Adapter adapter, final Song song) {
final int songId = song.getId();
final boolean isCurrentlyFavorite = song.isFavorite();
final FavoriteSongCallback callback = new FavoriteSongCallback() {
@Override
public void onSuccess() {
if (App.getRadioViewModel().getCurrentSong().getId() == songId) {
App.getRadioViewModel().setIsFavorited(!isCurrentlyFavorite);
}
if (activity == null)
return;
activity.runOnUiThread(() -> {
song.setFavorite(!isCurrentlyFavorite);
adapter.notifyDataSetChanged();
// Broadcast event
final Intent favIntent = new Intent(SongActionsUtil.FAVORITE_EVENT);
activity.sendBroadcast(favIntent);
if (isCurrentlyFavorite) {
// Undo action
final View coordinatorLayout = activity.findViewById(R.id.coordinator_layout);
if (coordinatorLayout != null) {
final Snackbar undoBar = Snackbar.make(coordinatorLayout, String.format(activity.getString(R.string.unfavorited), song.toString()), Snackbar.LENGTH_LONG);
undoBar.setAction(R.string.action_undo, (v) -> toggleFavorite(activity, adapter, song));
undoBar.show();
}
}
});
}
@Override
public void onFailure(final String message) {
if (activity == null)
return;
activity.runOnUiThread(() -> Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_SHORT).show());
}
};
App.getApiClient().toggleFavorite(String.valueOf(songId), isCurrentlyFavorite, callback);
}
use of me.echeung.listenmoeapi.callbacks.FavoriteSongCallback in project android-app by LISTEN-moe.
the class RadioService method favoriteCurrentSong.
private void favoriteCurrentSong() {
final Song currentSong = App.getRadioViewModel().getCurrentSong();
if (currentSong == null)
return;
final int songId = currentSong.getId();
if (songId == -1)
return;
if (!App.getAuthUtil().isAuthenticated()) {
showLoginRequiredToast();
return;
}
final boolean isCurrentlyFavorite = currentSong.isFavorite();
final FavoriteSongCallback callback = new FavoriteSongCallback() {
@Override
public void onSuccess() {
final Song currentSong = App.getRadioViewModel().getCurrentSong();
if (currentSong.getId() == songId) {
App.getRadioViewModel().setIsFavorited(!isCurrentlyFavorite);
}
final Intent favIntent = new Intent(SongActionsUtil.FAVORITE_EVENT);
sendBroadcast(favIntent);
updateNotification();
updateMediaSessionPlaybackState();
}
@Override
public void onFailure(final String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
};
App.getApiClient().toggleFavorite(String.valueOf(songId), isCurrentlyFavorite, callback);
}
Aggregations