Archived
Implemented snackbar telling the user that an error has occurred when fetching their schedule through FetcherActivity.kt. Also made some slight additions to the documentation
This commit is contained in:
@@ -3,6 +3,7 @@ package com.denizk0461.studip.activity
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.os.Bundle
|
||||
import android.util.TypedValue
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
@@ -12,6 +13,8 @@ import com.denizk0461.studip.R
|
||||
import com.denizk0461.studip.data.StudIPParser
|
||||
import com.denizk0461.studip.databinding.ActivityFetcherBinding
|
||||
import com.denizk0461.studip.viewmodel.FetcherViewModel
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import java.io.IOException
|
||||
import java.net.URLDecoder
|
||||
|
||||
/**
|
||||
@@ -67,18 +70,43 @@ class FetcherActivity : Activity() {
|
||||
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)
|
||||
// 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()
|
||||
try {
|
||||
// Decode HTML and parse it into a list of StudIPEvent.kt
|
||||
StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
|
||||
// Delete all previously fetched elements
|
||||
viewModel.nukeEvents()
|
||||
// Insert the list into the database
|
||||
viewModel.insertEvents(events)
|
||||
// Notify the user that the fetch was successful
|
||||
Toast.makeText(
|
||||
this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
|
||||
).show()
|
||||
// Close the activity
|
||||
finish()
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
|
||||
// Set up error colours
|
||||
val colorErrorContainer = TypedValue()
|
||||
val colorOnErrorContainer = TypedValue()
|
||||
|
||||
// Resolve themed attributes to get the right values for day and night modes
|
||||
theme.apply {
|
||||
resolveAttribute(R.attr.colorErrorContainer, colorErrorContainer, true)
|
||||
resolveAttribute(R.attr.colorOnErrorContainer, colorOnErrorContainer, true)
|
||||
}
|
||||
|
||||
// Let the user know that an error occurred
|
||||
Snackbar
|
||||
.make(
|
||||
binding.rootView,
|
||||
getString(R.string.fetch_error_snack),
|
||||
Snackbar.LENGTH_SHORT
|
||||
)
|
||||
// Set colours to signify an error
|
||||
.setBackgroundTint(colorErrorContainer.data)
|
||||
.setTextColor(colorOnErrorContainer.data)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.denizk0461.studip.data
|
||||
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
import org.jsoup.Jsoup
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* Parser class used for fetching and collecting scheduled events from a Stud.IP timetable.
|
||||
@@ -11,9 +12,11 @@ class StudIPParser {
|
||||
/**
|
||||
* Parse a given HTML string. HTML must be of a Stud.IP timetable.
|
||||
*
|
||||
* @param html website content of the Stud.IP timetable
|
||||
* @param insert action call to save the contents to persistent storage
|
||||
* @param html website content of the Stud.IP timetable
|
||||
* @param insert action call to save the contents to persistent storage
|
||||
* @throws IOException when an error in fetching or the database transaction occurs
|
||||
*/
|
||||
@kotlin.jvm.Throws(IOException::class)
|
||||
fun parse(html: String, insert: (events: List<StudIPEvent>) -> Unit) {
|
||||
// Primary key value to uniquely identify entries in the database
|
||||
var id = 0
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.denizk0461.studip.sheet
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.TimePicker
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.denizk0461.studip.R
|
||||
import com.denizk0461.studip.data.parseToMinutes
|
||||
@@ -11,6 +10,13 @@ import com.denizk0461.studip.databinding.SheetScheduleUpdateBinding
|
||||
import com.denizk0461.studip.dialog.TimePickerFragment
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
|
||||
/**
|
||||
* Update sheet to allow the user to edit attributes for a specific schedule event.
|
||||
*
|
||||
* @param event event to be updated
|
||||
* @param onUpdate action to execute to update the event
|
||||
* @param onDelete action to execute to delete the event
|
||||
*/
|
||||
class ScheduleUpdateSheet(
|
||||
private val event: StudIPEvent,
|
||||
private val onUpdate: (event: StudIPEvent) -> Unit,
|
||||
@@ -28,24 +34,37 @@ class ScheduleUpdateSheet(
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
// Set editable text fields
|
||||
binding.editTextTitle.setText(event.title)
|
||||
binding.editTextLecturers.setText(event.lecturer)
|
||||
binding.editTextRoom.setText(event.room)
|
||||
|
||||
// Prepare timestamp buttons
|
||||
binding.buttonTimeStart.text = timeslotStart
|
||||
binding.buttonTimeStart.setOnClickListener {
|
||||
TimePickerFragment(binding.buttonTimeStart.text.toString(), this, true).show((context as FragmentActivity).supportFragmentManager, "timePicker")
|
||||
TimePickerFragment(
|
||||
binding.buttonTimeStart.text.toString(),
|
||||
this,
|
||||
true,
|
||||
).show((context as FragmentActivity).supportFragmentManager, "timePicker")
|
||||
}
|
||||
binding.buttonTimeEnd.text = timeslotEnd
|
||||
binding.buttonTimeEnd.setOnClickListener {
|
||||
TimePickerFragment(binding.buttonTimeEnd.text.toString(), this, false).show((context as FragmentActivity).supportFragmentManager, "timePicker")
|
||||
TimePickerFragment(
|
||||
binding.buttonTimeEnd.text.toString(),
|
||||
this,
|
||||
false,
|
||||
).show((context as FragmentActivity).supportFragmentManager, "timePicker")
|
||||
}
|
||||
|
||||
// Prepare delete button
|
||||
binding.buttonDelete.setOnClickListener {
|
||||
onDelete(event)
|
||||
// Dismiss the sheet upon deletion
|
||||
dismiss()
|
||||
}
|
||||
|
||||
// Prepare update/save button
|
||||
binding.buttonSave.setOnClickListener {
|
||||
onUpdate(
|
||||
StudIPEvent(
|
||||
@@ -60,22 +79,28 @@ class ScheduleUpdateSheet(
|
||||
colour = colour,
|
||||
)
|
||||
)
|
||||
// Dismiss the sheet upon update
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks and stores a new timestamp after the user picks one through [TimePickerFragment].
|
||||
*
|
||||
* @param hours newly set hour
|
||||
* @param minutes newly set minute
|
||||
* @param isEventStart whether this timestamp is for the start of the course
|
||||
*/
|
||||
override fun onTimeSet(hours: Int, minutes: Int, isEventStart: Boolean) {
|
||||
|
||||
// Parse the separate ints into a timestamp string
|
||||
val newTimestamp = "$hours:$minutes"
|
||||
|
||||
// If start > end, show an error
|
||||
if ((isEventStart && newTimestamp.parseToMinutes() > timeslotEnd.parseToMinutes()) ||
|
||||
(!isEventStart && newTimestamp.parseToMinutes() < timeslotStart.parseToMinutes())) {
|
||||
// tell user that the timestamp must be set earlier/later
|
||||
} else {
|
||||
|
||||
// If the new timestamp is valid, save it
|
||||
if (isEventStart) {
|
||||
timeslotStart = newTimestamp
|
||||
binding.buttonTimeStart.text = newTimestamp
|
||||
|
||||
Reference in New Issue
Block a user