Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from kuylar/fix/lateinit_player
Browse files Browse the repository at this point in the history
Fixes the app crashing after staying idle for a while
  • Loading branch information
kuylar authored Jan 6, 2024
2 parents d840866 + 739d0bf commit 90af834
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 21 deletions.
31 changes: 22 additions & 9 deletions app/src/main/java/dev/kuylar/lighttube/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
lateinit var miniplayer: BottomSheetBehavior<View>
private lateinit var miniplayerScene: MotionLayout
lateinit var player: VideoPlayerManager
private lateinit var player: VideoPlayerManager
private lateinit var api: LightTubeApi
private var loadingSuggestions = false
private var fullscreen = false
Expand Down Expand Up @@ -100,14 +100,14 @@ class MainActivity : AppCompatActivity() {
miniplayer = BottomSheetBehavior.from(miniplayerView)
miniplayer.state = BottomSheetBehavior.STATE_HIDDEN

player = VideoPlayerManager(this)
setPlayer()

miniplayer.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
@SuppressLint("SwitchIntDef")
override fun onStateChanged(bottomSheet: View, newState: Int) {
when (newState) {
BottomSheetBehavior.STATE_HIDDEN -> player.stop()
BottomSheetBehavior.STATE_HIDDEN -> getPlayer().stop()

BottomSheetBehavior.STATE_DRAGGING -> {
supportActionBar?.show()
Expand All @@ -120,18 +120,19 @@ class MainActivity : AppCompatActivity() {
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {
val p = getPlayer()
if (slideOffset < 0)
player.setVolume(max(0.1f, 1 + slideOffset * 3))
p.setVolume(max(0.1f, 1 + slideOffset * 3))
else
player.setVolume(1f)
p.setVolume(1f)
miniplayerScene.progress = max(0f, min(1f, slideOffset * 5))

if (slideOffset > .3)
binding.navView.visibility = View.GONE
else
binding.navView.visibility = View.VISIBLE

player.toggleControls(slideOffset * 5 >= 1)
p.toggleControls(slideOffset * 5 >= 1)
}
})

Expand Down Expand Up @@ -162,7 +163,9 @@ class MainActivity : AppCompatActivity() {
var sponsorblockRunnable = Runnable {}
sponsorblockRunnable = Runnable {
try {
player.updateSkipButton(player.getCurrentSegment())
with(getPlayer()) {
updateSkipButton(getCurrentSegment())
}
} catch (e: Exception) {
Log.e("SponsorBlockLoop", "Failed to update SponsorBlock skip button", e)
}
Expand Down Expand Up @@ -248,7 +251,7 @@ class MainActivity : AppCompatActivity() {
}

private fun goBack(closeApp: Boolean): Boolean {
if (!player.closeSheets()) // attempt to close details/comments
if (!getPlayer().closeSheets()) // attempt to close details/comments
if (!tryExitFullscreen()) // attempt to exit fullscreen
if (!minimizePlayer()) // attempt to minimize the player sheet
if (!navController.popBackStack()) { // attempt to go back on the fragment history
Expand All @@ -273,6 +276,16 @@ class MainActivity : AppCompatActivity() {
return api
}

private fun setPlayer() {
player = VideoPlayerManager(this)
}

fun getPlayer(): VideoPlayerManager {
if (!this::player.isInitialized)
setPlayer()
return player
}

private fun minimizePlayer(): Boolean {
return if (miniplayer.state == BottomSheetBehavior.STATE_EXPANDED) {
miniplayer.state = BottomSheetBehavior.STATE_COLLAPSED
Expand Down Expand Up @@ -392,7 +405,7 @@ class MainActivity : AppCompatActivity() {
playerView.findViewById<MaterialButton>(R.id.player_fullscreen).apply {
icon = ResourcesCompat.getDrawable(resources, R.drawable.ic_fullscreen, theme)
setOnClickListener {
enterFullscreen(playerView, player.getAspectRatio() < 1)
enterFullscreen(playerView, getPlayer().getAspectRatio() < 1)
}
}
playerView.findViewById<ViewGroup>(R.id.player_metadata).visibility = View.INVISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LibraryFragment : Fragment() {
binding = FragmentLibraryBinding.inflate(inflater)
(activity as MainActivity).apply {
this@LibraryFragment.api = getApi()
this@LibraryFragment.player = player
this@LibraryFragment.player = getPlayer()
}
return binding.root
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RecyclerViewFragment : Fragment() {
binding = FragmentRecyclerviewBinding.inflate(inflater)
(activity as MainActivity).apply {
this@RecyclerViewFragment.api = getApi()
this@RecyclerViewFragment.player = player
this@RecyclerViewFragment.player = getPlayer()
}
type = arguments?.getString("type")!!
args = arguments?.getString("args")!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SearchFragment : Fragment() {
binding = FragmentSearchBinding.inflate(inflater)
(activity as MainActivity).apply {
this@SearchFragment.api = getApi()
this@SearchFragment.player = player
this@SearchFragment.player = getPlayer()
}
query = arguments?.getString("query") ?: "asdf"
return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SubscriptionsFragment : Fragment() {
binding = FragmentSubscriptionsBinding.inflate(inflater)
(activity as MainActivity).apply {
this@SubscriptionsFragment.api = getApi()
this@SubscriptionsFragment.player = player
this@SubscriptionsFragment.player = getPlayer()
}
return binding.root
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class VideoCommentsFragment : Fragment() {
savedInstanceState: Bundle?
): View {
api = (activity as MainActivity).getApi()
player = (activity as MainActivity).player
player = (activity as MainActivity).getPlayer()
binding = FragmentVideoCommentsBinding.inflate(inflater)
return binding.root
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class VideoInfoFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
api = (requireActivity() as MainActivity).getApi()
player = (requireActivity() as MainActivity).player
player = (requireActivity() as MainActivity).getPlayer()
arguments?.let {
id = it.getString("id")!!
playlistId = it.getString("playlistId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ChannelVideoPlayerRenderer(val binding: RendererVideoBinding) :
binding.root.setOnClickListener {
// bad idea? idk
if (binding.root.context is MainActivity)
(binding.root.context as MainActivity).player.playVideo(item.getAsJsonPrimitive("id").asString)
(binding.root.context as MainActivity).getPlayer().playVideo(item.getAsJsonPrimitive("id").asString)
else
Toast.makeText(binding.root.context, "uhh click :3", Toast.LENGTH_LONG).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class PlaylistInfoRenderer(private val binding: RendererPlaylistInfoBinding) :
.into(binding.playlistThumbnail)

binding.buttonPlayAll.setOnClickListener {
activity.player.playVideo(playlist.videos.first().asJsonObject.getAsJsonPrimitive("id").asString)
activity.getPlayer().playVideo(playlist.videos.first().asJsonObject.getAsJsonPrimitive("id").asString)
}

binding.buttonShuffle.setOnClickListener {
activity.player.playVideo(playlist.videos.first().asJsonObject.getAsJsonPrimitive("id").asString)
activity.getPlayer().playVideo(playlist.videos.first().asJsonObject.getAsJsonPrimitive("id").asString)
}

if (playlist.editable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PlaylistVideoRenderer(val binding: RendererPlaylistVideoBinding) :
binding.root.setOnClickListener {
// bad idea? idk
if (binding.root.context is MainActivity)
(binding.root.context as MainActivity).player.playVideo(item.getAsJsonPrimitive("id").asString)
(binding.root.context as MainActivity).getPlayer().playVideo(item.getAsJsonPrimitive("id").asString)
else
Toast.makeText(binding.root.context, "uhh click :3", Toast.LENGTH_LONG).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ open class SlimVideoInfoRenderer(private val binding: RendererSlimVideoInfoBindi
.into(binding.channelAvatar)

binding.videoDetails.setOnClickListener {
activity.player.setSheets(details = true, comments = false)
activity.getPlayer().setSheets(details = true, comments = false)
}

binding.channelContainer.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class VideoRenderer(val binding: RendererVideoBinding) : RendererViewHolder(bind
binding.root.setOnClickListener {
// bad idea? idk
if (binding.root.context is MainActivity)
(binding.root.context as MainActivity).player.playVideo(item.getAsJsonPrimitive("id").asString)
(binding.root.context as MainActivity).getPlayer().playVideo(item.getAsJsonPrimitive("id").asString)
else
Toast.makeText(binding.root.context, "uhh click :3", Toast.LENGTH_LONG).show()
}
Expand Down

0 comments on commit 90af834

Please sign in to comment.