Skip to content

Commit

Permalink
fix: most app crashes (#10)
Browse files Browse the repository at this point in the history
- fix: app crashing when activity gets reloaded
- fix: recyclerviews making the app crash if items are added during they change
- fix: storyboards making the app crash on short videos (again)
  • Loading branch information
kuylar authored Aug 23, 2023
2 parents c7036f1 + aeb8e42 commit 1084b12
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 41 deletions.
34 changes: 7 additions & 27 deletions app/src/main/java/dev/kuylar/lighttube/ui/VideoPlayerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import android.content.pm.ActivityInfo
import android.os.Handler
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.github.vkay94.dtpv.DoubleTapPlayerView
import com.github.vkay94.dtpv.youtube.YouTubeOverlay
import com.github.vkay94.timebar.LibTimeBar
import com.github.vkay94.timebar.YouTubeChapter
import com.github.vkay94.timebar.YouTubeSegment
import com.github.vkay94.timebar.YouTubeTimeBar
import com.github.vkay94.timebar.YouTubeTimeBarPreview
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
Expand All @@ -44,7 +40,7 @@ import java.io.IOException
import kotlin.concurrent.thread

class VideoPlayerManager(private val activity: MainActivity) : Player.Listener,
YouTubeTimeBarPreview.Listener, LibTimeBar.SegmentListener {
LibTimeBar.SegmentListener {
private var videoTracks: Tracks? = null
private val playerHandler: Handler
private val exoplayerView: DoubleTapPlayerView = activity.findViewById(R.id.player)
Expand All @@ -56,7 +52,7 @@ class VideoPlayerManager(private val activity: MainActivity) : Player.Listener,
private val player: ExoPlayer = ExoPlayer.Builder(activity).apply {
setHandleAudioBecomingNoisy(true)
}.build()
private val api: LightTubeApi = activity.api
private lateinit var api: LightTubeApi
private val fragmentManager = activity.supportFragmentManager

private val miniplayerTitle: TextView = activity.findViewById(R.id.miniplayer_video_title)
Expand All @@ -82,6 +78,8 @@ class VideoPlayerManager(private val activity: MainActivity) : Player.Listener,
)
playerHandler = Handler(player.applicationLooper)

api = activity.getApi()

// im sorry for this monstrosity
var r = Runnable {}
r = Runnable {
Expand Down Expand Up @@ -129,10 +127,6 @@ class VideoPlayerManager(private val activity: MainActivity) : Player.Listener,
val timeBar =
view.findViewById<YouTubeTimeBar>(com.google.android.exoplayer2.ui.R.id.exo_progress)
timeBar.addSegmentListener(this)
val preview = view.findViewById<YouTubeTimeBarPreview>(R.id.player_preview)
view.findViewById<YouTubeTimeBar>(com.google.android.exoplayer2.ui.R.id.exo_progress)
.timeBarPreview(preview)
preview.previewListener(this)

if (isFullscreen) {
fullscreenDoubleTapView
Expand Down Expand Up @@ -334,8 +328,9 @@ class VideoPlayerManager(private val activity: MainActivity) : Player.Listener,
}

private fun setStoryboards(levels: String?, recommendedLevel: String?, length: Long?) {
if (levels == null || length == null || recommendedLevel == null) storyboard = null
storyboard = StoryboardInfo(levels!!, recommendedLevel!!, length!!)
return //FIXME: disabled until i write a working storyboard view
// if (levels == null || length == null || recommendedLevel == null) storyboard = null
// storyboard = StoryboardInfo(levels!!, recommendedLevel!!, length!!)
}

private fun setSponsors(videoId: String) {
Expand Down Expand Up @@ -470,21 +465,6 @@ class VideoPlayerManager(private val activity: MainActivity) : Player.Listener,
}
}

override fun loadThumbnail(imageView: ImageView, position: Long) {
try {
storyboard!!.throttle(position) // the library fails throttling, so we do it ourselves
Glide.with(activity)
.load(storyboard!!.getImageUrl(position))
.transform(storyboard!!.getTransformation(position), CenterCrop())
.into(imageView)
} catch (e: Exception) {
if (e.message != "throttle")
Glide.with(activity)
.load("https://i.ytimg.com/vi/${player.currentMediaItem?.mediaId}/maxresdefault.jpg")
.into(imageView)
}
}

override fun onChapterChanged(timeBar: LibTimeBar, newChapter: YouTubeChapter, drag: Boolean) {
// todo: show current chapter on UI
}
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/dev/kuylar/lighttube/ui/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MainActivity : AppCompatActivity() {
lateinit var miniplayer: BottomSheetBehavior<View>
private lateinit var miniplayerScene: MotionLayout
lateinit var player: VideoPlayerManager
lateinit var api: LightTubeApi
private lateinit var api: LightTubeApi
private var loadingSuggestions = false

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -58,7 +58,7 @@ class MainActivity : AppCompatActivity() {
return
}

api = LightTubeApi(this)
setApi()

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Expand Down Expand Up @@ -151,6 +151,16 @@ class MainActivity : AppCompatActivity() {
}
}

private fun setApi() {
api = LightTubeApi(this)
}

fun getApi(): LightTubeApi {
if (!this::api.isInitialized)
setApi()
return api
}

private fun minimizePlayer(): Boolean {
return if (miniplayer.state == BottomSheetBehavior.STATE_EXPANDED) {
miniplayer.state = BottomSheetBehavior.STATE_COLLAPSED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ChannelFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
(activity as MainActivity).apply {
thread {
val channel = api.getChannel(id, "about")
val channel = getApi().getChannel(id, "about")
val channelAdapter =
ChannelAdapter(childFragmentManager, id, ArrayList(channel.data!!.enabledTabs.filter { it.lowercase() != "search" }))
val viewPager = binding.channelPager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HomeFragment : Fragment() {
a.setLoading(true)
thread {
try {
val info = a.api.getInstanceInfo()
val info = a.getApi().getInstanceInfo()
a.runOnUiThread {
a.setLoading(false)
binding.homeMotd.text = info.motd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class LibraryFragment : Fragment() {
val adapter = RendererRecyclerAdapter(items)
binding.recyclerLibrary.layoutManager = LinearLayoutManager(context)
binding.recyclerLibrary.adapter = adapter
binding.recyclerLibrary.itemAnimator = null
if (items.size == 0)
loadData()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PlaylistFragment : Fragment() {
val adapter = RendererRecyclerAdapter(items)
binding.recyclerPlaylist.layoutManager = LinearLayoutManager(context)
binding.recyclerPlaylist.adapter = adapter
binding.recyclerPlaylist.itemAnimator = null
binding.recyclerPlaylist.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class SearchFragment : Fragment() {
val adapter = RendererRecyclerAdapter(items)
binding.recyclerSearch.layoutManager = LinearLayoutManager(context)
binding.recyclerSearch.adapter = adapter
binding.recyclerSearch.itemAnimator = null
binding.recyclerSearch.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class VideoCommentsFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
api = (activity as MainActivity).api
api = (activity as MainActivity).getApi()
binding = FragmentVideoCommentsBinding.inflate(inflater)
return binding.root
}
Expand All @@ -46,6 +46,7 @@ class VideoCommentsFragment : Fragment() {
val adapter = RendererRecyclerAdapter(items)
binding.recyclerComments.layoutManager = LinearLayoutManager(context)
binding.recyclerComments.adapter = adapter
binding.recyclerComments.itemAnimator = null
binding.recyclerComments.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class VideoInfoFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
api = (requireActivity() as MainActivity).api
api = (requireActivity() as MainActivity).getApi()
player = (requireActivity() as MainActivity).player
arguments?.let {
id = it.getString("id")!!
Expand Down Expand Up @@ -93,10 +93,10 @@ class VideoInfoFragment : Fragment() {
val adapter = RendererRecyclerAdapter(items)
binding.recyclerRecommended.layoutManager = LinearLayoutManager(context)
binding.recyclerRecommended.adapter = adapter
binding.recyclerRecommended.itemAnimator = null

requireActivity().supportFragmentManager.beginTransaction().apply {
replace(R.id.video_info_fragment, VideoDetailsFragment::class.java, bundleOf(Pair("video", Gson().toJson(video))))

replace(R.id.comments_fragment, VideoCommentsFragment::class.java, bundleOf(Pair("commentsContinuation", video.commentsContinuation)))
}.commit()

Expand Down
7 changes: 0 additions & 7 deletions app/src/main/res/layout/exo_playback_control_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.github.vkay94.timebar.YouTubeTimeBarPreview
android:id="@+id/player_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp" />

</FrameLayout>

<com.github.vkay94.timebar.YouTubeTimeBar
Expand Down

0 comments on commit 1084b12

Please sign in to comment.