Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.53 KB | None | 0 0
  1.     inner class WebAppInterface {
  2.         @JavascriptInterface
  3.         fun resize(height: Float) {
  4.             val webViewHeight = (height * resources.displayMetrics.density)
  5.             activity?.runOnUiThread {
  6.                 val viewGroup = webViewNotification.layoutParams
  7.                 viewGroup.height = webViewHeight.toInt()
  8.                 webViewNotification.layoutParams = viewGroup
  9.             }
  10.         }
  11.     }
  12.  
  13.     class WebClient : WebViewClient() {
  14.         private val URL = "javascript:AndroidFunction.resize(document.body.scrollHeight)"
  15.         override fun onPageFinished(view: WebView?, url: String?) {
  16.             view?.loadUrl(URL)
  17.         }
  18.     }
  19.  
  20.     override fun onActivityCreated(savedInstanceState: Bundle?) {
  21.         super.onActivityCreated(savedInstanceState)
  22.         webViewNotification.settings.javaScriptEnabled = true
  23.         webViewNotification.addJavascriptInterface(WebAppInterface(), "AndroidFunction")
  24.         webViewNotification.webViewClient = WebClient()
  25.         refreshLayout.setOnRefreshListener {
  26.             mViewModel.loadInbox()
  27.         }
  28.         mViewModel.loaded.observe(viewLifecycleOwner, Observer {
  29.             webViewNotification.loadDataWithBaseURL(
  30.                 it.basePath,
  31.                 it.code,
  32.                 "text/html", "UTF-8", null
  33.             )
  34.             (activity as MainActivity).removeBadge()
  35.         })
  36.         mViewModel.progress.observe(viewLifecycleOwner, Observer { progress ->
  37.             refreshLayout.isRefreshing = progress
  38.         })
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement