Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some simple refactors & beginning of kotlin conversions of the player classes #11965

Open
wants to merge 12 commits into
base: refactor
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,21 @@ public final class VideoDetailFragment
};

@State
protected int serviceId = Constants.NO_SERVICE_ID;
int serviceId = Constants.NO_SERVICE_ID;
@State
@NonNull
protected String title = "";
String title = "";
@State
@Nullable
protected String url = null;
String url = null;
@Nullable
protected PlayQueue playQueue = null;
private PlayQueue playQueue = null;
@State
int bottomSheetState = BottomSheetBehavior.STATE_EXPANDED;
@State
int lastStableBottomSheetState = BottomSheetBehavior.STATE_EXPANDED;
@State
protected boolean autoPlayEnabled = true;
boolean autoPlayEnabled = true;

@Nullable
private StreamInfo currentInfo = null;
Expand Down Expand Up @@ -244,7 +244,7 @@ public void onServiceConnected(final PlayerService connectedPlayerService,
// It will do nothing if the player is not in fullscreen mode
hideSystemUiIfNeeded();

final Optional<MainPlayerUi> playerUi = player.UIs().get(MainPlayerUi.class);
final Optional<MainPlayerUi> playerUi = player.UIs().getOpt(MainPlayerUi.class);
if (!player.videoPlayerSelected() && !playAfterConnect) {
return;
}
Expand Down Expand Up @@ -429,18 +429,15 @@ public void onDestroyView() {
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case ReCaptchaActivity.RECAPTCHA_REQUEST:
if (resultCode == Activity.RESULT_OK) {
NavigationHelper.openVideoDetailFragment(requireContext(), getFM(),
serviceId, url, title, null, false);
} else {
Log.e(TAG, "ReCaptcha failed");
}
break;
default:
Log.e(TAG, "Request code from activity not supported [" + requestCode + "]");
break;
if (requestCode == ReCaptchaActivity.RECAPTCHA_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
NavigationHelper.openVideoDetailFragment(requireContext(), getFM(),
serviceId, url, title, null, false);
} else {
Log.e(TAG, "ReCaptcha failed");
}
} else {
Log.e(TAG, "Request code from activity not supported [" + requestCode + "]");
}
}

Expand Down Expand Up @@ -520,7 +517,7 @@ private void setOnClickListeners() {
binding.overlayPlayPauseButton.setOnClickListener(v -> {
if (playerIsNotStopped()) {
player.playPause();
player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0));
showSystemUi();
} else {
autoPlayEnabled = true; // forcefully start playing
Expand Down Expand Up @@ -679,7 +676,7 @@ protected void initListeners() {
@Override
public boolean onKeyDown(final int keyCode) {
return isPlayerAvailable()
&& player.UIs().get(VideoPlayerUi.class)
&& player.UIs().getOpt(VideoPlayerUi.class)
.map(playerUi -> playerUi.onKeyDown(keyCode)).orElse(false);
}

Expand Down Expand Up @@ -806,25 +803,17 @@ private void prepareAndHandleInfo(final StreamInfo info, final boolean scrollToT

}

protected void prepareAndLoadInfo() {
private void prepareAndLoadInfo() {
scrollToTop();
startLoading(false);
}

@Override
public void startLoading(final boolean forceLoad) {
super.startLoading(forceLoad);

initTabs();
currentInfo = null;
if (currentWorker != null) {
currentWorker.dispose();
}

runWorker(forceLoad, stack.isEmpty());
startLoading(forceLoad, null);
}

private void startLoading(final boolean forceLoad, final boolean addToBackStack) {
private void startLoading(final boolean forceLoad, final @Nullable Boolean addToBackStack) {
super.startLoading(forceLoad);

initTabs();
Expand All @@ -833,7 +822,7 @@ private void startLoading(final boolean forceLoad, final boolean addToBackStack)
currentWorker.dispose();
}

runWorker(forceLoad, addToBackStack);
runWorker(forceLoad, addToBackStack != null ? addToBackStack : stack.isEmpty());
}

private void runWorker(final boolean forceLoad, final boolean addToBackStack) {
Expand Down Expand Up @@ -1019,7 +1008,7 @@ private void toggleFullscreenIfInFullscreenMode() {
// If a user watched video inside fullscreen mode and than chose another player
// return to non-fullscreen mode
if (isPlayerAvailable()) {
player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
player.UIs().getOpt(MainPlayerUi.class).ifPresent(playerUi -> {
if (playerUi.isFullscreen()) {
playerUi.toggleFullscreen();
}
Expand Down Expand Up @@ -1129,7 +1118,7 @@ private void openNormalBackgroundPlayer(final boolean append) {
}

private void openMainPlayer() {
if (!isPlayerServiceAvailable()) {
if (noPlayerServiceAvailable()) {
playerHolder.startService(autoPlayEnabled, this, this);
return;
}
Expand All @@ -1154,7 +1143,7 @@ private void openMainPlayer() {
*/
private void hideMainPlayerOnLoadingNewStream() {
final var root = getRoot();
if (!isPlayerServiceAvailable() || root.isEmpty() || !player.videoPlayerSelected()) {
if (noPlayerServiceAvailable() || root.isEmpty() || !player.videoPlayerSelected()) {
return;
}

Expand Down Expand Up @@ -1235,7 +1224,7 @@ private void tryAddVideoPlayerView() {
// setup the surface view height, so that it fits the video correctly
setHeightThumbnail();

player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
player.UIs().getOpt(MainPlayerUi.class).ifPresent(playerUi -> {
// sometimes binding would be null here, even though getView() != null above u.u
if (binding != null) {
// prevent from re-adding a view multiple times
Expand All @@ -1251,7 +1240,7 @@ private void removeVideoPlayerView() {
makeDefaultHeightForVideoPlaceholder();

if (player != null) {
player.UIs().get(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(VideoPlayerUi::removeViewFromParent);
}
}

Expand Down Expand Up @@ -1318,7 +1307,7 @@ private void setHeightThumbnail(final int newHeight, final DisplayMetrics metric
binding.detailThumbnailImageView.setMinimumHeight(newHeight);
if (isPlayerAvailable()) {
final int maxHeight = (int) (metrics.heightPixels * MAX_PLAYER_HEIGHT);
player.UIs().get(VideoPlayerUi.class).ifPresent(ui ->
player.UIs().getOpt(VideoPlayerUi.class).ifPresent(ui ->
ui.getBinding().surfaceView.setHeights(newHeight,
ui.isFullscreen() ? newHeight : maxHeight));
}
Expand All @@ -1328,31 +1317,31 @@ private void showContent() {
binding.detailContentRootHiding.setVisibility(View.VISIBLE);
}

protected void setInitialData(final int newServiceId,
@Nullable final String newUrl,
@NonNull final String newTitle,
@Nullable final PlayQueue newPlayQueue) {
private void setInitialData(final int newServiceId,
@Nullable final String newUrl,
@NonNull final String newTitle,
@Nullable final PlayQueue newPlayQueue) {
this.serviceId = newServiceId;
this.url = newUrl;
this.title = newTitle;
this.playQueue = newPlayQueue;
}

private void setErrorImage(final int imageResource) {
private void setErrorImage() {
if (binding == null || activity == null) {
return;
}

binding.detailThumbnailImageView.setImageDrawable(
AppCompatResources.getDrawable(requireContext(), imageResource));
AppCompatResources.getDrawable(requireContext(), R.drawable.not_available_monkey));
animate(binding.detailThumbnailImageView, false, 0, AnimationType.ALPHA,
0, () -> animate(binding.detailThumbnailImageView, true, 500));
}

@Override
public void handleError() {
super.handleError();
setErrorImage(R.drawable.not_available_monkey);
setErrorImage();

if (binding.relatedItemsLayout != null) { // hide related streams for tablets
binding.relatedItemsLayout.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -1769,16 +1758,14 @@ public void onPlaybackUpdate(final int state,
final PlaybackParameters parameters) {
setOverlayPlayPauseImage(player != null && player.isPlaying());

switch (state) {
case Player.STATE_PLAYING:
if (binding.positionView.getAlpha() != 1.0f
&& player.getPlayQueue() != null
&& player.getPlayQueue().getItem() != null
&& player.getPlayQueue().getItem().getUrl().equals(url)) {
animate(binding.positionView, true, 100);
animate(binding.detailPositionView, true, 100);
}
break;
if (state == Player.STATE_PLAYING) {
if (binding.positionView.getAlpha() != 1.0f
&& player.getPlayQueue() != null
&& player.getPlayQueue().getItem() != null
&& player.getPlayQueue().getItem().getUrl().equals(url)) {
animate(binding.positionView, true, 100);
animate(binding.detailPositionView, true, 100);
}
}
}

Expand Down Expand Up @@ -1851,7 +1838,7 @@ public void onServiceStopped() {
public void onFullscreenStateChanged(final boolean fullscreen) {
setupBrightness();
if (!isPlayerAndPlayerServiceAvailable()
|| player.UIs().get(MainPlayerUi.class).isEmpty()
|| player.UIs().getOpt(MainPlayerUi.class).isEmpty()
|| getRoot().map(View::getParent).isEmpty()) {
return;
}
Expand Down Expand Up @@ -1880,7 +1867,7 @@ public void onScreenRotationButtonClicked() {
final boolean isLandscape = DeviceUtils.isLandscape(requireContext());
if (DeviceUtils.isTablet(activity)
&& (!globalScreenOrientationLocked(activity) || isLandscape)) {
player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::toggleFullscreen);
return;
}

Expand Down Expand Up @@ -1980,7 +1967,7 @@ public void hideSystemUiIfNeeded() {
}

private boolean isFullscreen() {
return isPlayerAvailable() && player.UIs().get(VideoPlayerUi.class)
return isPlayerAvailable() && player.UIs().getOpt(VideoPlayerUi.class)
.map(VideoPlayerUi::isFullscreen).orElse(false);
}

Expand Down Expand Up @@ -2057,7 +2044,7 @@ private void checkLandscape() {
setAutoPlay(true);
}

player.UIs().get(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
player.UIs().getOpt(MainPlayerUi.class).ifPresent(MainPlayerUi::checkLandscape);
// Let's give a user time to look at video information page if video is not playing
if (globalScreenOrientationLocked(activity) && !player.isPlaying()) {
player.play();
Expand Down Expand Up @@ -2322,7 +2309,7 @@ && isPlayerAvailable()
&& player.isPlaying()
&& !isFullscreen()
&& !DeviceUtils.isTablet(activity)) {
player.UIs().get(MainPlayerUi.class)
player.UIs().getOpt(MainPlayerUi.class)
.ifPresent(MainPlayerUi::toggleFullscreen);
}
setOverlayLook(binding.appBarLayout, behavior, 1);
Expand All @@ -2336,7 +2323,7 @@ && isPlayerAvailable()
// Re-enable clicks
setOverlayElementsClickable(true);
if (isPlayerAvailable()) {
player.UIs().get(MainPlayerUi.class)
player.UIs().getOpt(MainPlayerUi.class)
.ifPresent(MainPlayerUi::closeItemsList);
}
setOverlayLook(binding.appBarLayout, behavior, 0);
Expand All @@ -2347,7 +2334,7 @@ && isPlayerAvailable()
showSystemUi();
}
if (isPlayerAvailable()) {
player.UIs().get(MainPlayerUi.class).ifPresent(ui -> {
player.UIs().getOpt(MainPlayerUi.class).ifPresent(ui -> {
if (ui.isControlsVisible()) {
ui.hideControls(0, 0);
}
Expand Down Expand Up @@ -2434,8 +2421,8 @@ boolean isPlayerAvailable() {
return player != null;
}

boolean isPlayerServiceAvailable() {
return playerService != null;
boolean noPlayerServiceAvailable() {
return playerService == null;
}

boolean isPlayerAndPlayerServiceAvailable() {
Expand All @@ -2444,7 +2431,7 @@ boolean isPlayerAndPlayerServiceAvailable() {

public Optional<View> getRoot() {
return Optional.ofNullable(player)
.flatMap(player1 -> player1.UIs().get(VideoPlayerUi.class))
.flatMap(player1 -> player1.UIs().getOpt(VideoPlayerUi.class))
.map(playerUi -> playerUi.getBinding().getRoot());
}

Expand Down
Loading
Loading