Search in sources :

Example 1 with FavoriteSongCallback

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);
}
Also used : Intent(android.content.Intent) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) FavoriteSongCallback(me.echeung.listenmoeapi.callbacks.FavoriteSongCallback) Snackbar(android.support.design.widget.Snackbar)

Example 2 with FavoriteSongCallback

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);
}
Also used : Song(me.echeung.listenmoeapi.models.Song) Intent(android.content.Intent) FavoriteSongCallback(me.echeung.listenmoeapi.callbacks.FavoriteSongCallback)

Aggregations

Intent (android.content.Intent)2 FavoriteSongCallback (me.echeung.listenmoeapi.callbacks.FavoriteSongCallback)2 Snackbar (android.support.design.widget.Snackbar)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 Song (me.echeung.listenmoeapi.models.Song)1