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

Pull different steps in the codelab into modules #63

Merged
merged 1 commit into from
Jul 8, 2021

Conversation

tunjid
Copy link
Contributor

@tunjid tunjid commented Jun 25, 2021

A single PR against a copy of master with all the commits squashed; closes out #60, #61 & #62

Commit summary:

Initial kotlin conversion
Update kotlin to be more idiomatic
More idiomatic kotlin

Copy link
Contributor

@dturner dturner left a comment

Choose a reason for hiding this comment

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

This looks great. I tried pretty hard to find issues with the code but could only find one thing! I've asked borrelli@ to take a look as well.

Please don't merge until the codelab itself has been updated as well.

app/build.gradle Outdated
@@ -24,5 +25,6 @@ android {
}

dependencies {
implementation project(":player-lib")
// Switch out the code lab step to jump to different points in the code lab
implementation project(':exoplayer-codelab-04')
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
implementation project(':exoplayer-codelab-04')
implementation project(':exoplayer-codelab-00')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and force pushed.

@tunjid tunjid force-pushed the kotlin-squashed branch from 40f5423 to f7a2982 Compare June 28, 2021 18:18
Copy link
Contributor

@nic0lette nic0lette left a comment

Choose a reason for hiding this comment

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

Looks good =D I made some comments about using some of the more helpful parts of AndroidX. Other stuff is much smaller.

private fun initializePlayer() {
player = SimpleExoPlayer.Builder(this)
.build()
.also {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider an explicit name for it or even just getting rid of the also block, since this doesn't all need to be in one statement.

Copy link
Contributor Author

@tunjid tunjid Jun 29, 2021

Choose a reason for hiding this comment

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

Opted for it based on conversations here to be more idiomatic: #62 as the more explicit names can be a bit noisy. It could also just be that I didn't choose very good explicit names lmao.

What would you suggest naming it more explicitly as?

Copy link
Contributor

@dturner dturner Jul 5, 2021

Choose a reason for hiding this comment

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

Some alternatives:

  1. Use apply instead of also. This would avoid using it.
  2. Rename it to exoPlayer since it is the ExoPlayer implementation of the Player interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opted for apply { -> exoPlayer } as the semantics of this got hard to discern because of the playWhenReady var .

It would have required:

apply {
    playWhenReady = this@PlayerActivity.playWhenReady
}

}

private fun releasePlayer() {
player?.let {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to the above, providing a specific name for it

import com.google.android.exoplayer2.util.MimeTypes
import com.google.android.exoplayer2.util.Util

private val TAG = PlayerActivity::class.java.name
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest just putting in a static string here, which would allow making this a private const val

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
}
private val viewBinding by lazy { ActivityPlayerBinding.inflate(layoutInflater) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider moving this to be a local variable in onCreate -- I know it's used beyond onCreate in subsequent steps, so I'll explain more there.

public override fun onStart() {
super.onStart()
if (Util.SDK_INT > 23) {
initializePlayer()
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of calling these inline like this, consider creating a LifecycleObserver. It could be initialized in onCreate and added as an observer to the activity with getLifecycle().addObserver(...).

If I were building it, I might consider having two parameters to the observer -- the 'init' and 'release' lifecycle states. Then, in the observer, check the state and call the appropriate method. (This way there wouldn't have to be duplicate code to init/release the player in onStart/onResume and onPause/onStop)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered this, however my major prompt was to keep it super simple so users can copy paste, and also to keep it as close to the original java codelab as possible as this is a first pass kotlin conversion.

After the convergence implementation, I can then explore using androidx dependencies like in lifecycle observers, and then even plausibly move the exoplayer instance to a ViewModel.

I can't do that at the moment as it would require extra dependencies, and even more edits to the codelab doc.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a good suggestion but agree that we might want to do this in a follow up PR. I would file a separate issue on this repo so that this suggestion doesn't get forgotten.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #65

delete rootProject.buildDir
}
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.exoplayer:exoplayer-core:2.12.0'
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe consider moving 2.12.0 to a gradle variable at the top level, since these are used in each of the modules, and they're all the same version (unless this might change in the future) -- even then, though, it might be useful to have a core, dash, and ui version in that case, so they only have to be updated in one place.

Copy link
Contributor Author

@tunjid tunjid Jun 29, 2021

Choose a reason for hiding this comment

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

Will this affect the user's ability to copy paste? I considered doing this as well as I do it with the kotlin version, but as the user may be jumping in between modules, I decided to err on the side of copy pasting convenience.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't get too caught up on copypastability. Maintainability and readability is also important. In this instance I agree with @nic0lette that moving to gradle variables is the better solution since it avoids duplicating the same version string throughout the code, although this could also be done in a follow up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will create an issue as an action item.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #66

}

private fun releasePlayer() {
player?.let {
Copy link
Contributor

Choose a reason for hiding this comment

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

        player?.run {
            playbackPosition = this.currentPosition
            currentWindow = this.currentWindowIndex
            playWhenReady = this.playWhenReady
            removeListener(playbackStateListener)
            release()
        }

Copy link
Contributor

Choose a reason for hiding this comment

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

Using run is marginally better here as it avoids it

@dturner
Copy link
Contributor

dturner commented Jul 8, 2021

@tunjid Thanks for all your hard work on this. Please rebase and merge as soon as the codelab has been updated.

@tunjid tunjid force-pushed the kotlin-squashed branch from 5c3c25a to 823f1b2 Compare July 8, 2021 13:09
@tunjid tunjid merged commit 474f624 into android:kotlin-refresh Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants