From 5b50ac926ecb6a4e25155f30170e70d6cb499650 Mon Sep 17 00:00:00 2001 From: Koen J Date: Fri, 6 Jun 2025 10:15:15 +0200 Subject: [PATCH] Freeze fix when clicking link in description. --- .../others/PlatformLinkMovementMethod.kt | 47 ++++++++++++------- .../views/behavior/NonScrollingTextView.kt | 36 +++++++++----- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/futo/platformplayer/others/PlatformLinkMovementMethod.kt b/app/src/main/java/com/futo/platformplayer/others/PlatformLinkMovementMethod.kt index d524b7cf..76652236 100644 --- a/app/src/main/java/com/futo/platformplayer/others/PlatformLinkMovementMethod.kt +++ b/app/src/main/java/com/futo/platformplayer/others/PlatformLinkMovementMethod.kt @@ -8,11 +8,14 @@ import android.text.method.LinkMovementMethod import android.text.style.URLSpan import android.view.MotionEvent import android.widget.TextView +import androidx.lifecycle.lifecycleScope import com.futo.platformplayer.activities.MainActivity import com.futo.platformplayer.logging.Logger import com.futo.platformplayer.receivers.MediaControlReceiver import com.futo.platformplayer.timestampRegex -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class PlatformLinkMovementMethod(private val _context: Context) : LinkMovementMethod() { @@ -60,31 +63,39 @@ class PlatformLinkMovementMethod(private val _context: Context) : LinkMovementMe val dx = event.x - downX val dy = event.y - downY if (Math.abs(dx) <= touchSlop && Math.abs(dy) <= touchSlop && isTouchInside(widget, event)) { - runBlocking { - for (link in pressedLinks!!) { - Logger.i(TAG) { "Link clicked '${link.url}'." } + for (link in pressedLinks!!) { + Logger.i(TAG) { "Link clicked '${link.url}'." } - if (_context is MainActivity) { - if (_context.handleUrl(link.url)) continue - if (timestampRegex.matches(link.url)) { - val tokens = link.url.split(':') - var time_s = -1L - when (tokens.size) { - 2 -> time_s = tokens[0].toLong() * 60 + tokens[1].toLong() - 3 -> time_s = tokens[0].toLong() * 3600 + - tokens[1].toLong() * 60 + - tokens[2].toLong() - } + val c = _context + if (c is MainActivity) { + c.lifecycleScope.launch(Dispatchers.IO) { + if (c.handleUrl(link.url)) { + return@launch + } + if (timestampRegex.matches(link.url)) { + val tokens = link.url.split(':') + var time_s = -1L + when (tokens.size) { + 2 -> time_s = tokens[0].toLong() * 60 + tokens[1].toLong() + 3 -> time_s = tokens[0].toLong() * 3600 + + tokens[1].toLong() * 60 + + tokens[2].toLong() + } - if (time_s != -1L) { + if (time_s != -1L) { + withContext(Dispatchers.Main) { MediaControlReceiver.onSeekToReceived.emit(time_s * 1000) - continue } + return@launch } } - _context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link.url))) + + withContext(Dispatchers.Main) { + c.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link.url))) + } } } + } pressedLinks = null linkPressed = false return true diff --git a/app/src/main/java/com/futo/platformplayer/views/behavior/NonScrollingTextView.kt b/app/src/main/java/com/futo/platformplayer/views/behavior/NonScrollingTextView.kt index cf599176..6e3a8860 100644 --- a/app/src/main/java/com/futo/platformplayer/views/behavior/NonScrollingTextView.kt +++ b/app/src/main/java/com/futo/platformplayer/views/behavior/NonScrollingTextView.kt @@ -8,12 +8,16 @@ import android.text.Spannable import android.text.style.URLSpan import android.util.AttributeSet import android.view.MotionEvent +import androidx.lifecycle.lifecycleScope import com.futo.platformplayer.activities.MainActivity import com.futo.platformplayer.logging.Logger import com.futo.platformplayer.others.PlatformLinkMovementMethod import com.futo.platformplayer.receivers.MediaControlReceiver +import com.futo.platformplayer.states.StateApp import com.futo.platformplayer.timestampRegex -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class NonScrollingTextView : androidx.appcompat.widget.AppCompatTextView { private var _lastTouchedLinks: Array? = null @@ -77,12 +81,14 @@ class NonScrollingTextView : androidx.appcompat.widget.AppCompatTextView { val dx = event.x - downX val dy = event.y - downY if (Math.abs(dx) <= touchSlop && Math.abs(dy) <= touchSlop && isTouchInside(event)) { - runBlocking { - for (link in _lastTouchedLinks!!) { - Logger.i(PlatformLinkMovementMethod.TAG) { "Link clicked '${link.url}'." } - val c = context - if (c is MainActivity) { - if (c.handleUrl(link.url)) continue + for (link in _lastTouchedLinks!!) { + Logger.i(PlatformLinkMovementMethod.TAG) { "Link clicked '${link.url}'." } + val c = context + if (c is MainActivity) { + c.lifecycleScope.launch(Dispatchers.IO) { + if (c.handleUrl(link.url)) { + return@launch + } if (timestampRegex.matches(link.url)) { val tokens = link.url.split(':') var time_s = -1L @@ -92,13 +98,21 @@ class NonScrollingTextView : androidx.appcompat.widget.AppCompatTextView { tokens[1].toLong() * 60 + tokens[2].toLong() } + if (time_s != -1L) { - MediaControlReceiver.onSeekToReceived.emit(time_s * 1000) - continue + withContext(Dispatchers.Main) { + MediaControlReceiver.onSeekToReceived.emit(time_s * 1000) + } + return@launch } } - c.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link.url))) - } else { + + withContext(Dispatchers.Main) { + c.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link.url))) + } + } + } else { + StateApp.instance.scopeOrNull?.launch(Dispatchers.Main) { c.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link.url))) } }