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

Extract view click listeners from Player #8011

Merged
merged 7 commits into from
Mar 16, 2022

Conversation

XiangRongLin
Copy link
Collaborator

What is it?

  • Codebase improvement (dev facing)

Description of the changes in your PR

  • Extract the listeners for views out of the player into their own classes
  • It continues to be tightly coupled, but when they are all extracted it should give a better picture of what is needed and what not. Thus allowing to clean them up a bit.
  • Only did 2 as a sample and ask for feedback before doing all.

Fixes the following issue(s)

Due diligence

@XiangRongLin XiangRongLin added codequality Improvements to the codebase to improve the code quality player Issues related to any player (main, popup and background) labels Mar 10, 2022
@XiangRongLin XiangRongLin changed the title Extract 2 view click listeners from Player Extract view click listeners from Player Mar 10, 2022
@XiangRongLin XiangRongLin marked this pull request as ready for review March 11, 2022 09:35
import org.schabi.newpipe.player.Player
import org.schabi.newpipe.player.helper.PlaybackParameterDialog

class PlaybackSpeedListener(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some documentation would be good in that class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem is I don't know what the listener does. The focus of the PR is extracting the code out of the player. Adding documentation would require more effort, which I don't want to put into it.

import org.schabi.newpipe.player.Player
import org.schabi.newpipe.player.helper.PlaybackParameterDialog

class PlaybackSpeedListener(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename it to PlaybackSpeedClickListener

}

override fun onClick(v: View) {
if (player.binding.qualityTextView.id == v.id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if is useless

import org.schabi.newpipe.extractor.MediaFormat
import org.schabi.newpipe.player.Player

class QualityTextListener(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same statements as above also apply for this class

Comment on lines 31 to 34
val qualityText = (
MediaFormat.getNameById(videoStream.formatId) + " " +
videoStream.resolution
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the ( ) required?

import org.schabi.newpipe.extractor.MediaFormat
import org.schabi.newpipe.player.Player

class QualityTextListener(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rename the class to QualityTextClickListener

@Fs00
Copy link
Contributor

Fs00 commented Mar 15, 2022

In my personal opinion those listeners would look much better as lambdas/method references, like:

binding.qualityTextView.setOnClickListener(this::onQualitySelectorClicked);

What I don't like much about listeners in separate classes, other than the pre-Java 8 kind of verbosity, is that it's being introduced a layer of "public-internal" APIs (like setSomePopupMenuVisible, getBinding) that exposes internal details of the Player to consumers.
Also, this kind of extraction doesn't seem too beneficial to me, since it adds more indirection and verbosity without really making it easier to understand of how the different parts of Player work.

Comment on lines -3708 to -3747
if (v.getId() == binding.qualityTextView.getId()) {
onQualitySelectorClicked();
} else if (v.getId() == binding.playbackSpeed.getId()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I've just noticed now that those listeners can't even be extracted from here since there is the following logic at the end of the onClick method that runs regardless of the item that gets clicked:

if (currentState != STATE_COMPLETED) {
controlsVisibilityHandler.removeCallbacksAndMessages(null);
showHideShadow(true, DEFAULT_CONTROLS_DURATION);
animate(binding.playbackControlRoot, true, DEFAULT_CONTROLS_DURATION,
AnimationType.ALPHA, 0, () -> {
if (currentState == STATE_PLAYING && !isSomePopupMenuVisible) {
if (v.getId() == binding.playPauseButton.getId()
// Hide controls in fullscreen immediately
|| (v.getId() == binding.screenRotationButton.getId()
&& isFullscreen)) {
hideControls(0, 0);
} else {
hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
}
}
});

There are only a few branches in onClick that do an early return and can therefore be safely extracted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that part could be extracted in a player function? Something like maintainControlsShown or something like that (I think there is a particular verb for that in the english language but I cannot remember it rn)

Copy link
Member

@litetex litetex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed problems myself.

@Stypox You may check my implementation of manageControlsAfterOnClick 😄

@sonarcloud
Copy link

sonarcloud bot commented Mar 16, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

@Stypox Stypox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested and it works, thank you :-)

@Stypox Stypox merged commit 102204e into TeamNewPipe:dev Mar 16, 2022
@XiangRongLin XiangRongLin deleted the extract_view_listeners branch March 17, 2022 07:11
@Stypox Stypox mentioned this pull request Apr 16, 2022
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codequality Improvements to the codebase to improve the code quality player Issues related to any player (main, popup and background)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants