#2929 - refactor common getFeedItem from playable logic in Player

This commit is contained in:
orionlee 2019-09-21 14:25:50 -07:00
parent 0be47a8d19
commit e18d8f3514
1 changed files with 30 additions and 33 deletions

View File

@ -391,29 +391,24 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
return true; return true;
} else { } else {
if (media != null) { if (media != null) {
final FeedItem feedItem = getFeedItem(media); // some options option requires FeedItem
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.add_to_favorites_item: case R.id.add_to_favorites_item:
if(media instanceof FeedMedia) { if(feedItem != null) {
FeedItem feedItem = ((FeedMedia)media).getItem(); DBWriter.addFavoriteItem(feedItem);
if(feedItem != null) { isFavorite = true;
DBWriter.addFavoriteItem(feedItem); invalidateOptionsMenu();
isFavorite = true; Toast.makeText(this, R.string.added_to_favorites, Toast.LENGTH_SHORT)
invalidateOptionsMenu(); .show();
Toast.makeText(this, R.string.added_to_favorites, Toast.LENGTH_SHORT)
.show();
}
} }
break; break;
case R.id.remove_from_favorites_item: case R.id.remove_from_favorites_item:
if(media instanceof FeedMedia) { if(feedItem != null) {
FeedItem feedItem = ((FeedMedia)media).getItem(); DBWriter.removeFavoriteItem(feedItem);
if(feedItem != null) { isFavorite = false;
DBWriter.removeFavoriteItem(feedItem); invalidateOptionsMenu();
isFavorite = false; Toast.makeText(this, R.string.removed_from_favorites, Toast.LENGTH_SHORT)
invalidateOptionsMenu(); .show();
Toast.makeText(this, R.string.removed_from_favorites, Toast.LENGTH_SHORT)
.show();
}
} }
break; break;
case R.id.disable_sleeptimer_item: case R.id.disable_sleeptimer_item:
@ -451,12 +446,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
dialog.show(getSupportFragmentManager(), "playback_controls"); dialog.show(getSupportFragmentManager(), "playback_controls");
break; break;
case R.id.open_feed_item: case R.id.open_feed_item:
if(media instanceof FeedMedia) { if (feedItem != null) {
FeedItem feedItem = ((FeedMedia)media).getItem(); Intent intent = MainActivity.getIntentToOpenFeed(this, feedItem.getFeedId());
if (feedItem != null) { startActivity(intent);
Intent intent = MainActivity.getIntentToOpenFeed(this, feedItem.getFeedId());
startActivity(intent);
}
} }
break; break;
case R.id.visit_website_item: case R.id.visit_website_item:
@ -465,22 +457,22 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
break; break;
case R.id.share_link_item: case R.id.share_link_item:
if (media instanceof FeedMedia) { if (media instanceof FeedMedia) {
ShareUtils.shareFeedItemLink(this, ((FeedMedia) media).getItem()); ShareUtils.shareFeedItemLink(this, feedItem);
} }
break; break;
case R.id.share_download_url_item: case R.id.share_download_url_item:
if (media instanceof FeedMedia) { if (media instanceof FeedMedia) {
ShareUtils.shareFeedItemDownloadLink(this, ((FeedMedia) media).getItem()); ShareUtils.shareFeedItemDownloadLink(this, feedItem);
} }
break; break;
case R.id.share_link_with_position_item: case R.id.share_link_with_position_item:
if (media instanceof FeedMedia) { if (media instanceof FeedMedia) {
ShareUtils.shareFeedItemLink(this, ((FeedMedia) media).getItem(), true); ShareUtils.shareFeedItemLink(this, feedItem, true);
} }
break; break;
case R.id.share_download_url_with_position_item: case R.id.share_download_url_with_position_item:
if (media instanceof FeedMedia) { if (media instanceof FeedMedia) {
ShareUtils.shareFeedItemDownloadLink(this, ((FeedMedia) media).getItem(), true); ShareUtils.shareFeedItemDownloadLink(this, feedItem, true);
} }
break; break;
case R.id.share_file: case R.id.share_file:
@ -824,11 +816,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
} }
private void checkFavorite() { private void checkFavorite() {
Playable playable = controller.getMedia(); FeedItem feedItem = getFeedItem(controller.getMedia());
if (!(playable instanceof FeedMedia)) {
return;
}
FeedItem feedItem = ((FeedMedia) playable).getItem();
if (feedItem == null) { if (feedItem == null) {
return; return;
} }
@ -883,4 +871,13 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
} }
} }
} }
@Nullable
private static FeedItem getFeedItem(@Nullable Playable playable) {
if (playable instanceof FeedMedia) {
return ((FeedMedia)playable).getItem();
} else {
return null;
}
}
} }