Skip to content

Commit 6f6ece9

Browse files
committed
refactor: extract WebChromeClient setup to a separate class
1 parent 1223e1b commit 6f6ece9

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/previewer/CardViewerFragment.kt

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ abstract class CardViewerFragment(
143143
protected open fun onCreateWebViewClient(savedInstanceState: Bundle?): CardViewerWebViewClient =
144144
CardViewerWebViewClient(savedInstanceState)
145145

146+
protected open fun onCreateWebChromeClient() = CardViewerWebChromeClient()
147+
146148
override fun onWebViewRecreated(webView: WebView) {
147149
setupWebView(null)
148150
}
@@ -236,43 +238,42 @@ abstract class CardViewerFragment(
236238
}
237239
}
238240

239-
private fun onCreateWebChromeClient(): WebChromeClient =
240-
object : WebChromeClient() {
241-
private lateinit var customView: View
241+
open inner class CardViewerWebChromeClient : WebChromeClient() {
242+
protected lateinit var paramView: View
242243

243-
// used for displaying `<video>` in fullscreen.
244-
// This implementation requires configChanges="orientation" in the manifest
245-
// to avoid destroying the View if the device is rotated
246-
override fun onShowCustomView(
247-
paramView: View,
248-
paramCustomViewCallback: CustomViewCallback?,
249-
) {
250-
customView = paramView
251-
val window = requireActivity().window
252-
(window.decorView as FrameLayout).addView(
253-
customView,
254-
FrameLayout.LayoutParams(
255-
FrameLayout.LayoutParams.MATCH_PARENT,
256-
FrameLayout.LayoutParams.MATCH_PARENT,
257-
),
258-
)
259-
// hide system bars
260-
with(WindowInsetsControllerCompat(window, window.decorView)) {
261-
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
262-
hide(WindowInsetsCompat.Type.systemBars())
263-
}
244+
// used for displaying `<video>` in fullscreen.
245+
// This implementation requires configChanges="orientation" in the manifest
246+
// to avoid destroying the View if the device is rotated
247+
override fun onShowCustomView(
248+
paramView: View,
249+
paramCustomViewCallback: CustomViewCallback?,
250+
) {
251+
this@CardViewerWebChromeClient.paramView = paramView
252+
val window = requireActivity().window
253+
(window.decorView as FrameLayout).addView(
254+
paramView,
255+
FrameLayout.LayoutParams(
256+
FrameLayout.LayoutParams.MATCH_PARENT,
257+
FrameLayout.LayoutParams.MATCH_PARENT,
258+
),
259+
)
260+
// hide system bars
261+
with(WindowInsetsControllerCompat(window, window.decorView)) {
262+
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
263+
hide(WindowInsetsCompat.Type.systemBars())
264264
}
265+
}
265266

266-
override fun onHideCustomView() {
267-
val window = requireActivity().window
268-
(window.decorView as FrameLayout).removeView(customView)
269-
// show system bars back
270-
with(WindowInsetsControllerCompat(window, window.decorView)) {
271-
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_DEFAULT
272-
show(WindowInsetsCompat.Type.systemBars())
273-
}
267+
override fun onHideCustomView() {
268+
val window = requireActivity().window
269+
(window.decorView as FrameLayout).removeView(paramView)
270+
// show system bars back
271+
with(WindowInsetsControllerCompat(window, window.decorView)) {
272+
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_DEFAULT
273+
show(WindowInsetsCompat.Type.systemBars())
274274
}
275275
}
276+
}
276277

277278
private fun showMediaErrorSnackbar(filename: String) {
278279
showSnackbar(getString(R.string.card_viewer_could_not_find_image, filename)) {

0 commit comments

Comments
 (0)