This repository has been archived on 2026-05-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
weserplaner/app/src/main/java/com/denizk0461/studip/fragment/ParserFragment.kt
T

87 lines
2.8 KiB
Kotlin
Raw Normal View History

2023-04-08 11:41:02 +02:00
package com.denizk0461.studip.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2023-04-10 14:50:57 +02:00
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.denizk0461.studip.data.StudIPParser
2023-04-08 11:41:02 +02:00
import com.denizk0461.studip.databinding.FragmentSecondBinding
import com.denizk0461.studip.viewmodel.ParserViewModel
import java.net.URLDecoder
2023-04-08 11:41:02 +02:00
/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class ParserFragment : Fragment() {
2023-04-08 11:41:02 +02:00
private var _binding: FragmentSecondBinding? = null
private var html = "" // temporary storage for the website HTML
2023-04-08 11:41:02 +02:00
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
private val viewModel: ParserViewModel by viewModels()
2023-04-08 11:41:02 +02:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
2023-04-08 18:53:51 +02:00
): View {
2023-04-08 11:41:02 +02:00
_binding = FragmentSecondBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2023-04-08 18:53:51 +02:00
// binding.buttonSecond.setOnClickListener {
// findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
// }
binding.webview.settings.apply {
javaScriptEnabled = true
}
2023-04-10 14:50:57 +02:00
binding.webview.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView,
request: WebResourceRequest
2023-04-10 14:50:57 +02:00
): Boolean {
return false
}
override fun onPageFinished(view: WebView, url: String) {
binding.webview.loadUrl(
"javascript:window.HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"
)
}
2023-04-10 14:50:57 +02:00
}
2023-04-08 18:53:51 +02:00
binding.webview.loadUrl("https://elearning.uni-bremen.de/index.php?again=yes")
2023-04-10 14:50:57 +02:00
binding.fab.setOnClickListener { view ->
binding.webview.evaluateJavascript(
"(function(){return encodeURI(document.getElementsByTagName('html')[0].innerHTML)})();"
2023-04-10 14:50:57 +02:00
) { p0 ->
viewModel.nukeEvents()
StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
viewModel.insertEvents(events)
}
2023-04-10 14:50:57 +02:00
}
// html.chunked(3000).forEach {
// Log.d("HELLO", it)
// }
2023-04-10 14:50:57 +02:00
}
2023-04-08 11:41:02 +02:00
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}