diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/AppRouter.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/AppRouter.kt index 0bd665d7d..2985821cf 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/AppRouter.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/AppRouter.kt @@ -52,6 +52,7 @@ import org.koitharu.kotatsu.core.ui.dialog.buildAlertDialog import org.koitharu.kotatsu.core.util.ext.connectivityManager import org.koitharu.kotatsu.core.util.ext.findActivity import org.koitharu.kotatsu.core.util.ext.getThemeDrawable +import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import org.koitharu.kotatsu.core.util.ext.toFileOrNull import org.koitharu.kotatsu.core.util.ext.toUriOrNull import org.koitharu.kotatsu.core.util.ext.withArgs @@ -614,9 +615,11 @@ class AppRouter private constructor( startActivity(Intent(contextOrNull() ?: return, activityClass)) } - private fun getFragmentManager(): FragmentManager? { - return fragment?.childFragmentManager ?: activity?.supportFragmentManager - } + private fun getFragmentManager(): FragmentManager? = runCatching { + fragment?.childFragmentManager ?: activity?.supportFragmentManager + }.onFailure { exception -> + exception.printStackTraceDebug() + }.getOrNull() private fun shareLink(link: String, title: String) { val context = contextOrNull() ?: return diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/NavUtil.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/NavUtil.kt index 1d0122d1e..642fe8cfa 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/NavUtil.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/nav/NavUtil.kt @@ -7,6 +7,7 @@ import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity import org.koitharu.kotatsu.core.util.ext.isAnimationsEnabled +import org.koitharu.kotatsu.core.util.ext.isOnScreen inline val FragmentActivity.router: AppRouter get() = AppRouter(this) @@ -26,14 +27,15 @@ tailrec fun Fragment.dismissParentDialog(): Boolean { } } -fun scaleUpActivityOptionsOf(view: View): Bundle? = if (view.context.isAnimationsEnabled) { - ActivityOptions.makeScaleUpAnimation( - view, - 0, - 0, - view.width, - view.height, +fun scaleUpActivityOptionsOf(view: View): Bundle? { + if (!view.context.isAnimationsEnabled || !view.isOnScreen()) { + return null + } + return ActivityOptions.makeScaleUpAnimation( + /* source = */ view, + /* startX = */ 0, + /* startY = */ 0, + /* width = */ view.width, + /* height = */ view.height, ).toBundle() -} else { - null } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt index 51b4d2cbb..c1614396d 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/View.kt @@ -1,10 +1,13 @@ package org.koitharu.kotatsu.core.util.ext +import android.content.Context +import android.graphics.Point import android.graphics.Rect import android.os.Build import android.view.View import android.view.View.MeasureSpec import android.view.ViewGroup +import android.view.WindowManager import android.widget.Checkable import androidx.annotation.StringRes import androidx.appcompat.widget.ActionMenuView @@ -200,3 +203,24 @@ fun View.setContentDescriptionAndTooltip(@StringRes resId: Int) { contentDescription = text TooltipCompat.setTooltipText(this, text) } + +fun View.getWindowBounds(): Rect { + val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + wm.currentWindowMetrics.bounds + } else { + val size = Point() + @Suppress("DEPRECATION") + display.getSize(size) + Rect(0, 0, size.x, size.y) + } +} + +fun View.isOnScreen(): Boolean { + if (!isShown) { + return false + } + val actualPosition = Rect() + getGlobalVisibleRect(actualPosition) + return actualPosition.intersect(getWindowBounds()) +}