From 840c8f1db73693c4bebaae0ea080b1851c6e5bbe Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Tue, 25 Apr 2023 12:02:25 +0200 Subject: [PATCH] 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 --- .../studip/activity/FetcherActivity.kt | 52 ++++++++++++++----- .../denizk0461/studip/data/StudIPParser.kt | 7 ++- .../studip/sheet/ScheduleUpdateSheet.kt | 35 +++++++++++-- app/src/main/res/layout/activity_fetcher.xml | 8 +-- app/src/main/res/values-de/strings.xml | 6 ++- app/src/main/res/values/attrs.xml | 3 ++ app/src/main/res/values/strings.xml | 4 +- 7 files changed, 89 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/denizk0461/studip/activity/FetcherActivity.kt b/app/src/main/java/com/denizk0461/studip/activity/FetcherActivity.kt index 2395237..06ef3ed 100644 --- a/app/src/main/java/com/denizk0461/studip/activity/FetcherActivity.kt +++ b/app/src/main/java/com/denizk0461/studip/activity/FetcherActivity.kt @@ -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() } } } diff --git a/app/src/main/java/com/denizk0461/studip/data/StudIPParser.kt b/app/src/main/java/com/denizk0461/studip/data/StudIPParser.kt index d10b82e..adb576e 100644 --- a/app/src/main/java/com/denizk0461/studip/data/StudIPParser.kt +++ b/app/src/main/java/com/denizk0461/studip/data/StudIPParser.kt @@ -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) -> Unit) { // Primary key value to uniquely identify entries in the database var id = 0 diff --git a/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt b/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt index fee7af4..19c9eb0 100644 --- a/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt +++ b/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt @@ -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 diff --git a/app/src/main/res/layout/activity_fetcher.xml b/app/src/main/res/layout/activity_fetcher.xml index d10388d..8d9002f 100644 --- a/app/src/main/res/layout/activity_fetcher.xml +++ b/app/src/main/res/layout/activity_fetcher.xml @@ -1,6 +1,7 @@ - @@ -17,10 +18,9 @@ android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" - app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toEndOf="parent" + android:layout_gravity="bottom|end" android:layout_marginEnd="@dimen/fab_margin" android:layout_marginBottom="@dimen/fab_margin" app:srcCompat="@drawable/check" /> - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index f1d4c13..3113a4a 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -9,6 +9,8 @@ Samstag Sonntag + Ein Fehler ist aufgetreten! + Speichern Löschen Veranstaltung bearbeiten @@ -118,9 +120,9 @@ Was passiert mit meinen Daten? Stiehlst du meinen Stud.IP-Login? TL;DR: nein, aber Google könnte mir Bescheid geben, falls die App mal abstürzt. - Die App funktioniert standalone, was bedeutet, dass sie sich niemals mit einem Server verbindet, der mir gehört. Sie ist ein Web-Scraper, also ein Programm, welches eine Internetseite öffnet und dann all die Informationen dieser Webseite herunterlädt und analysiert. In diesem Fall sind es die Webseiten der Mensen und Cafeterien des Studierendenwerks Bremen (stw-bremen.de) sowie die Stud.IP-Seite (elearning.uni-bremen.de).\n\nDies funktioniert relativ einfach für die Essenspläne, da diese Pläne öffentlich zugänglich sind. Um jedoch Deinen persönlichen Stud.IP-Stundenplan herunterzuladen, braucht die App Zugriff auf diese Webseite während Dein Account eingeloggt ist. Dies erfolgt, indem die App Dich mit deinem Account einloggen lässt - dies geschieht, wenn Du den Knopf zum Aktualisieren des Plans drückst und die App Dir die Login-Seite zeigt - und Du dann den Knopf klickst, um den Inhalt, den HTML-Quellcode, herunterzuladen. Der HTML-Quellcode beinhält Informationen darüber, was auf einer Webseite angezeigt wird und wie es aussehen soll, aber er kann niemals persönliche Details beinhalten, wie zum Beispiel Passwörter oder andere Daten, die auf Deinem Gerät vorhanden sind.\n\nDer Quellcode wird dann lokal, also auf Deinem Gerät, zu den Karten verarbeitet, die Du dann in der App sehen kannst, ohne, dass jegliche Daten irgendwohin geschickt werden müssten.\n\nEinige Daten könnten jedoch dennoch nach außen geschickt werden. Diese App nutzt \"Crashlytics\" von Google. Dies ist ein Tool, welches Absturzreporte an Google-Server schickt, wenn in der App etwas schief läuft. Diese werden genutzt, um Probleme in der App zu lösen.\n\nDies ist jedoch gemäß europäischer Datenschutz-Grundverordnung Opt-In, was bedeutet, dass Du zustimmen musst, bevor Daten nach außen geschickt werden. Du wurdest danach gefragt, als Du die App zum ersten Mal gestartet hast, und du kannst jederzeit deine Meinung ändern, indem Du den Schalter hierfür in den Einstellungen umstellst. Solltest Du dich entscheiden, keine Daten verschicken zu lassen, wird auch nichts mit Google (und mir) geteilt.\n\nSolltest du dich versichern wollen, dass wirklich keine Daten an irgendwelche Server verschickt wird (abgesehen von den Absturzreports an Google), kannst Du jederzeit das Projekt erkunden, da es Open-Source und auf meinen GitHub-Profil öffentlich zugänglich ist:\nhttps://github.com/denizk0461/studip-timetable/ + Die App funktioniert standalone, was bedeutet, dass sie sich niemals mit einem Server verbindet, der mir gehört. Sie ist ein Web-Scraper, also ein Programm, welches eine Internetseite öffnet und dann all die Informationen dieser Webseite herunterlädt und analysiert. In diesem Fall sind es die Webseiten der Mensen und Cafeterien des Studierendenwerks Bremen (stw-bremen.de) sowie die Stud.IP-Seite (elearning.uni-bremen.de).\n\nDies funktioniert relativ einfach für die Essenspläne, da diese Pläne öffentlich zugänglich sind. Um jedoch Deinen persönlichen Stud.IP-Stundenplan herunterzuladen, braucht die App Zugriff auf diese Webseite während Dein Account eingeloggt ist. Dies erfolgt, indem die App Dich mit deinem Account einloggen lässt - dies geschieht, wenn Du den Knopf zum Aktualisieren des Plans drückst und die App Dir die Login-Seite zeigt - und Du dann den Knopf klickst, um den Inhalt, den HTML-Quellcode, herunterzuladen. Der HTML-Quellcode beinhält Informationen darüber, was auf einer Webseite angezeigt wird und wie es aussehen soll, aber er kann niemals persönliche Details beinhalten, wie zum Beispiel Passwörter oder andere Daten, die auf Deinem Gerät vorhanden sind.\n\nDer Quellcode wird dann lokal, also auf Deinem Gerät, zu den Karten verarbeitet, die Du dann in der App sehen kannst, ohne, dass jegliche Daten irgendwohin geschickt werden müssten.\n\nEinige Daten könnten jedoch dennoch nach außen geschickt werden. Diese App nutzt \"Crashlytics\" von Google. Dies ist ein Tool, welches Absturzreporte an Google-Server schickt, wenn in der App etwas schief läuft. Diese werden genutzt, um Probleme in der App zu lösen.\n\nDies ist jedoch gemäß europäischer Datenschutz-Grundverordnung Opt-In, was bedeutet, dass Du zustimmen musst, bevor Daten nach außen geschickt werden. Du wurdest danach gefragt, als Du die App zum ersten Mal gestartet hast, und du kannst jederzeit deine Meinung ändern, indem Du den Schalter hierfür in den Einstellungen umstellst. Solltest Du dich entscheiden, keine Daten verschicken zu lassen, wird auch nichts mit Google (und mir) geteilt.\n\nSolltest du dich versichern wollen, dass wirklich keine Daten an irgendwelche Server verschickt wird (abgesehen von den Absturzreports an Google), kannst Du jederzeit das Projekt erkunden, da es Open-Source und auf meinen GitHub-Profil öffentlich zugänglich ist:\nhttps://github.com/denizk0461/studip-timetable/\n\nSchreib mir ansonsten gerne eine E-Mail:\ndenizk0461+kyzil@gmail.com - App-Verison + App-Version mit ♡️ aus Bremen-Osterholz\von denizk0461 \\__* \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 3256fe9..16e5b88 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -1,6 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f1d1c15..b9a0470 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,6 +8,8 @@ Friday Saturday Sunday + + Something went wrong! Save Delete @@ -121,7 +123,7 @@ What happens with my data? Will you steal my Stud.IP login? TL;DR: no, but Google may let me know if the app crashes on your device. - This app works standalone, which is to say, it doesn\'t ever connect to a server I own. Instead, it is a web scraper, which means that it opens a website and then downloads and analyses all the data from that website by itself. In this case, those websites are the canteen websites of the Studierendenwerk Bremen (stw-bremen.de) and the Stud.IP page (elearning.uni-bremen.de).\n\nThis works fairly easily for the canteen offers, since the plans are public and anyone can access them. However, to access your personal Stud.IP schedule, the app needs access to that website, with your account logged in. This is done by letting you log into the website by yourself (which happens when you click the button to refresh the schedule and the app opens the login page) and then clicking a button to download the content, the HTML source code, of the website. The HTML source code contains information about which elements are shown on the website and how they look, but it can never contain your personal details such as passwords, or any other sensitive data you may store on your device.\n\nThe HTML source is then processed locally on your device, which means that your device will handle converting the HTML source to the cards you will be shown in the app by itself, without needing to send any data anywhere.\n\nHowever, some data may still be sent outside of your device. This app uses \"Crashlytics\" by Google, which is a tool that sends crash reports to Google servers if something breaks. These are used to fix problems that occur in the app.\n\nThis, in compliance with the European General Data Protection Regulation, is of course opt-in, which means that you need to accept this before any data is sent outside of your device. You have been prompted for this when you first started the app, and you can always change your mind in the app settings by clicking the corresponding switch. Should you choose to not share crash data with Google (and me), no data will be sent outside of your device.\n\nShould you want to reassure yourself that no data is sent to any servers (outside of the crash reports from Google), feel free to explore the project, since it is open-source and documented on my GitHub profile:\nhttps://github.com/denizk0461/studip-timetable/ + This app works standalone, which is to say, it doesn\'t ever connect to a server I own. Instead, it is a web scraper, which means that it opens a website and then downloads and analyses all the data from that website by itself. In this case, those websites are the canteen websites of the Studierendenwerk Bremen (stw-bremen.de) and the Stud.IP page (elearning.uni-bremen.de).\n\nThis works fairly easily for the canteen offers, since the plans are public and anyone can access them. However, to access your personal Stud.IP schedule, the app needs access to that website, with your account logged in. This is done by letting you log into the website by yourself (which happens when you click the button to refresh the schedule and the app opens the login page) and then clicking a button to download the content, the HTML source code, of the website. The HTML source code contains information about which elements are shown on the website and how they look, but it can never contain your personal details such as passwords, or any other sensitive data you may store on your device.\n\nThe HTML source is then processed locally on your device, which means that your device will handle converting the HTML source to the cards you will be shown in the app by itself, without needing to send any data anywhere.\n\nHowever, some data may still be sent outside of your device. This app uses \"Crashlytics\" by Google, which is a tool that sends crash reports to Google servers if something breaks. These are used to fix problems that occur in the app.\n\nThis, in compliance with the European General Data Protection Regulation, is of course opt-in, which means that you need to accept this before any data is sent outside of your device. You have been prompted for this when you first started the app, and you can always change your mind in the app settings by clicking the corresponding switch. Should you choose to not share crash data with Google (and me), no data will be sent outside of your device.\n\nShould you want to reassure yourself that no data is sent to any servers (outside of the crash reports from Google), feel free to explore the project, since it is open-source and documented on my GitHub profile:\nhttps://github.com/denizk0461/studip-timetable/\n\nYou\'re also welcome to send me an email:\ndenizk0461+kyzil@gmail.com App Version