Archived
Added comments and cleaned up code in activities and StudIPEvent.kt
This commit is contained in:
@@ -14,9 +14,15 @@ import com.denizk0461.studip.databinding.ActivityFetcherBinding
|
||||
import com.denizk0461.studip.viewmodel.FetcherViewModel
|
||||
import java.net.URLDecoder
|
||||
|
||||
/**
|
||||
* Activity for allowing the user to log in to access and fetch their schedule from their Stud.IP
|
||||
* profile. Launched through a button in the app's settings.
|
||||
*/
|
||||
class FetcherActivity : Activity() {
|
||||
|
||||
// View binding
|
||||
private lateinit var binding: ActivityFetcherBinding
|
||||
// View model
|
||||
private lateinit var viewModel: FetcherViewModel
|
||||
|
||||
/*
|
||||
@@ -27,39 +33,51 @@ class FetcherActivity : Activity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Instantiate view model
|
||||
viewModel = ViewModelProvider.AndroidViewModelFactory(this.application).create(FetcherViewModel::class.java)
|
||||
|
||||
// Inflate view binding and bind to this activity
|
||||
binding = ActivityFetcherBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
// Enabling JavaScript. See comment above lint suppression for reason why this is done.
|
||||
binding.webview.settings.apply {
|
||||
javaScriptEnabled = true
|
||||
}
|
||||
binding.webview.webViewClient = object : WebViewClient() {
|
||||
// Disallow redirecting to prevent the app from launching Chrome after the user logs in
|
||||
override fun shouldOverrideUrlLoading(
|
||||
view: WebView,
|
||||
request: WebResourceRequest
|
||||
): Boolean {
|
||||
return false
|
||||
}
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
binding.webview.loadUrl(
|
||||
"javascript:window.HtmlViewer.showHTML" +
|
||||
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Attempt to load the user-set Stud.IP front page. Redirects to login page if user is
|
||||
* not logged in.
|
||||
*/
|
||||
binding.webview.loadUrl("https://elearning.uni-bremen.de/index.php?again=yes")
|
||||
|
||||
binding.fab.setOnClickListener { view ->
|
||||
/*
|
||||
* Retrieve encoded HTML via JavaScript function. Encoding is done as otherwise not all
|
||||
* symbols will be accurately fetched.
|
||||
*/
|
||||
binding.webview.evaluateJavascript(
|
||||
"(function(){return encodeURI(document.getElementsByTagName('html')[0].innerHTML)})();"
|
||||
) { p0 ->
|
||||
// Delete all previously fetched elements
|
||||
viewModel.nukeEvents()
|
||||
// Decode HTML and parse it into a list of StudIPEvent.kt
|
||||
StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
|
||||
// Insert the list into the database
|
||||
viewModel.insertEvents(events)
|
||||
Toast.makeText(this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT).show()
|
||||
// Notify the user that the fetch was successful. TODO: notify on error
|
||||
Toast.makeText(
|
||||
this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
|
||||
).show()
|
||||
// Close the activity
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user