CanteenFragment.kt: replaced buttons for opening hours and canteen switch embedded in the app bar with floating action buttons; implemented a possible fix for the app crashing after refreshing the canteen plan. This may have been caused by accessing the view/context of the fragment to let the user know that the refresh succeeded or encountered an error. Reference will be null by the time the fragment is inactive, causing a NullPointerException

This commit is contained in:
denizk0461
2023-05-04 15:09:10 +02:00
parent 5ca5788899
commit e17a81e604
14 changed files with 165 additions and 142 deletions
@@ -35,7 +35,6 @@ class StudIPEventItemAdapter(
/** /**
* Retrieve an instance of Calendar to check whether the adapter's day matches the current day. * Retrieve an instance of Calendar to check whether the adapter's day matches the current day.
* TODO this can be optimised by checking in EventPageFragment.kt and delivering a boolean
*/ */
private val currentCalendar = Calendar.getInstance() private val currentCalendar = Calendar.getInstance()
@@ -128,10 +128,7 @@ class StwParser(application: Application) {
// Parse the HTML using Jsoup to traverse the document // Parse the HTML using Jsoup to traverse the document
val doc = Jsoup.connect(url).get() val doc = Jsoup.connect(url).get()
/* // Retrieve the opening hours for the canteen
* Retrieve the opening hours for the canteen.
* TODO this formatting sucks and also this doesn't work
*/
var openingHours = "" var openingHours = ""
// Iterate through all wrappers that could contain opening hours // Iterate through all wrappers that could contain opening hours
@@ -139,10 +136,7 @@ class StwParser(application: Application) {
// Element is not for providing contact details // Element is not for providing contact details
if (it.getElementsByClass("contact-person-name").size == 0) { if (it.getElementsByClass("contact-person-name").size == 0) {
/* // Retrieve all opening hours.
* Retrieve all opening hours.
* TODO this slows down the fetch SIGNIFICANTLY, but why?
*/
it.children().forEach { element -> it.children().forEach { element ->
if (element.tag().toString() == "p") { if (element.tag().toString() == "p") {
openingHours += element.textWithBreaks().trim() + "\n" // Uni-Mensa is not parsed properly openingHours += element.textWithBreaks().trim() + "\n" // Uni-Mensa is not parsed properly
@@ -53,53 +53,18 @@ class CanteenFragment : AppFragment() {
// Set progress circle colours // Set progress circle colours
binding.swipeRefreshLayout.setRainbowProgressCircle() binding.swipeRefreshLayout.setRainbowProgressCircle()
// Open the menu for the canteen picker button
binding.buttonCanteenPicker.setOnClickListener {
PopupMenu(binding.root.context, binding.buttonCanteenPicker).apply {
setOnMenuItemClickListener { item ->
viewModel.preferenceCanteen = when (item?.itemId) {
R.id.mensa_uni -> 0
R.id.cafe_central -> 1
R.id.mensa_nw1 -> 2
R.id.cafeteria_gw2 -> 3
R.id.mensa_neustadt -> 4
R.id.mensa_werder -> 5
R.id.mensa_airport -> 6
R.id.mensa_bhv -> 7
R.id.cafeteria_bhv -> 8
R.id.mensa_hfk -> 9
else -> 0
}
// Set newly selected canteen to the button
binding.buttonCanteenPicker.text = getCurrentlySelectedCanteenName()
// Display to the user that the canteen plan will be refreshed
binding.swipeRefreshLayout.isRefreshing = true
// Refresh the canteen menu for the newly selected canteen
refresh()
true
}
inflate(R.menu.menu_canteens)
show()
}
}
// Set currently selected canteen to the button // Set currently selected canteen to the button
binding.buttonCanteenPicker.text = getCurrentlySelectedCanteenName() // binding.buttonCanteenPicker.text = getCurrentlySelectedCanteenName()
binding.appTitleBar.text = getString(
R.string.title_canteen_template,
getCurrentlySelectedCanteenName(),
)
binding.appTitleBar.isSelected = true
// Set up button to display info (most likely opening hours) // Set up button to display info (most likely opening hours)
binding.buttonInfo.setOnClickListener { // binding.buttonInfo.setOnClickListener {
openBottomSheet( //
getTextSheet( // }
getString(
R.string.canteen_opening_hours,
getCurrentlySelectedCanteenName()
),
openingHours
)
)
}
// Assign a preference value to every button to filter for dietary preferences // Assign a preference value to every button to filter for dietary preferences
val chipMap = mapOf( val chipMap = mapOf(
@@ -127,6 +92,55 @@ class CanteenFragment : AppFragment() {
} }
} }
// Set up floating action button for viewing the opening hours
binding.fabOpeningHours.setOnClickListener {
openBottomSheet(
getTextSheet(
getString(
R.string.canteen_opening_hours,
getCurrentlySelectedCanteenName()
),
openingHours
)
)
}
// Set up floating action button for switching the canteen
binding.fabSwitchCanteen.setOnClickListener {
PopupMenu(binding.root.context, binding.fabSwitchCanteen).apply {
setOnMenuItemClickListener { item ->
viewModel.preferenceCanteen = when (item?.itemId) {
R.id.mensa_uni -> 0
R.id.cafe_central -> 1
R.id.mensa_nw1 -> 2
R.id.cafeteria_gw2 -> 3
R.id.mensa_neustadt -> 4
R.id.mensa_werder -> 5
R.id.mensa_airport -> 6
R.id.mensa_bhv -> 7
R.id.cafeteria_bhv -> 8
R.id.mensa_hfk -> 9
else -> 0
}
// Set newly selected canteen to the button
// binding.buttonCanteenPicker.text = getCurrentlySelectedCanteenName()
binding.appTitleBar.text = getString(
R.string.title_canteen_template,
getCurrentlySelectedCanteenName(),
)
// Display to the user that the canteen plan will be refreshed
binding.swipeRefreshLayout.isRefreshing = true
// Refresh the canteen menu for the newly selected canteen
refresh()
true
}
inflate(R.menu.menu_canteens)
show()
}
}
// Set up floating action button for refreshing the canteen offers // Set up floating action button for refreshing the canteen offers
binding.fabRefreshOffers.setOnClickListener { binding.fabRefreshOffers.setOnClickListener {
// Display to the user that a refresh is in progress // Display to the user that a refresh is in progress
@@ -224,12 +238,17 @@ class CanteenFragment : AppFragment() {
private fun refresh() { private fun refresh() {
// Retrieve new offers from the website(s) // Retrieve new offers from the website(s)
viewModel.fetchOffers(viewModel.preferenceCanteen, onFinish = { viewModel.fetchOffers(viewModel.preferenceCanteen, onFinish = {
// Check if the fragment is still active
if (_binding != null) {
/* /*
* Tell the swipe refresh layout to stop refreshing. * Tell the swipe refresh layout to stop refreshing.
*/ */
binding.swipeRefreshLayout.isRefreshing = false binding.swipeRefreshLayout.isRefreshing = false
}
}, onError = { }, onError = {
// Check if the fragment is still active
if (_binding != null) {
// Tell the user that an error occurred // Tell the user that an error occurred
context.theme?.showErrorSnackBar( context.theme?.showErrorSnackBar(
binding.snackbarContainer, binding.snackbarContainer,
@@ -239,6 +258,7 @@ class CanteenFragment : AppFragment() {
* Tell the swipe refresh layout to stop refreshing. * Tell the swipe refresh layout to stop refreshing.
*/ */
binding.swipeRefreshLayout.isRefreshing = false binding.swipeRefreshLayout.isRefreshing = false
}
}) })
} }
} }
@@ -1,6 +1,10 @@
package com.denizk0461.studip.model package com.denizk0461.studip.model
// TODO KDoc /**
* Class for managing allergen preferences. The user can tell the app which substances they are
* allergic against, and the functions in this class can be used to filter out offers containing
* allergens that could kill them.
*/
class AllergenPreferences { class AllergenPreferences {
/** /**
@@ -69,8 +73,13 @@ class AllergenPreferences {
* @return string with preferences inserted * @return string with preferences inserted
*/ */
fun deconstruct(): String { fun deconstruct(): String {
// Create an empty list
val array = arrayListOf<String>() val array = arrayListOf<String>()
/*
* Add the symbols for the allergens to the list. Allergen symbols derived from the
* allergen list at stw-bremen.de
*/
if (hasWheat) array.add("a1") if (hasWheat) array.add("a1")
if (hasRye) array.add("a2") if (hasRye) array.add("a2")
if (hasBarley) array.add("a3") if (hasBarley) array.add("a3")
@@ -98,6 +107,7 @@ class AllergenPreferences {
if (hasSesame) array.add("m") if (hasSesame) array.add("m")
if (hasMolluscs) array.add("n") if (hasMolluscs) array.add("n")
// Join the individual symbols to a single string
return array.joinToString(",") return array.joinToString(",")
} }
} }
@@ -116,6 +126,7 @@ class AllergenPreferences {
* @return instance of AllergenPreferences.Object with preferences inserted * @return instance of AllergenPreferences.Object with preferences inserted
*/ */
fun construct(input: String): Object { fun construct(input: String): Object {
// Split the string into its individual components
val elements = input.split(",") val elements = input.split(",")
return Object( return Object(
elements.contains("a1"), elements.contains("a1"),
+5
View File
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M7.41,8.59L12,13.17l4.59,-4.58L18,10l-6,6 -6,-6 1.41,-1.41z"/>
</vector>
+6
View File
@@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
<path android:fillColor="@android:color/white" android:pathData="M12.5,7H11v6l5.25,3.15 0.75,-1.23 -4.5,-2.67z"/>
</vector>
@@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M18,12l4,-4l-4,-4l0,3l-15,0l0,2l15,0z"/>
<path android:fillColor="@android:color/white" android:pathData="M6,12l-4,4l4,4l0,-3l15,0l0,-2l-15,0z"/>
</vector>
+47 -41
View File
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/snackbar_container" android:id="@+id/snackbar_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@@ -17,48 +18,21 @@
android:orientation="vertical" android:orientation="vertical"
app:layout_scrollFlags="scroll|exitUntilCollapsed"> app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_canteen_picker"
style="@style/Widget.Material3.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="start"
android:layout_marginStart="8dp"
android:fontFamily="@font/lato_bolditalic"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp">
<TextView <TextView
android:id="@+id/app_title_bar" android:id="@+id/app_title_bar"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_food" android:text="@string/title_food"
android:textSize="32sp" android:textSize="28sp"
android:fontFamily="@font/lato_bolditalic" android:fontFamily="@font/lato_bolditalic"
app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginHorizontal="16dp"
app:layout_constraintStart_toStartOf="parent" android:singleLine="true"
app:layout_constraintEnd_toStartOf="@id/button_info"/> android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
<com.google.android.material.button.MaterialButton android:focusable="true"
style="@style/Widget.Material3.Button.TextButton" android:focusableInTouchMode="true"
android:id="@+id/button_info" android:scrollHorizontally="true"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
app:icon="@drawable/info"
app:iconPadding="0dp"
android:layout_gravity="center_vertical|end"
app:iconTint="?attr/colorOnSurfaceVariant"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<HorizontalScrollView <HorizontalScrollView
android:id="@+id/scroll_view_chip" android:id="@+id/scroll_view_chip"
@@ -222,16 +196,48 @@
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="@dimen/fab_margin"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior">
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="?attr/floatingActionButtonSmallStyle"
android:id="@+id/fab_opening_hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/clock"
android:contentDescription="@string/canteen_fab_opening_hours_content_desc"
android:layout_gravity="center_horizontal"
android:tooltipText="@string/canteen_fab_opening_hours_content_desc"
tools:ignore="UnusedAttribute"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
style="?attr/floatingActionButtonSmallStyle"
android:id="@+id/fab_switch_canteen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/switch_arrows"
android:contentDescription="@string/canteen_fab_switch_canteen_content_desc"
android:layout_marginBottom="8dp"
android:layout_gravity="center_horizontal"
android:tooltipText="@string/canteen_fab_switch_canteen_content_desc"
tools:ignore="UnusedAttribute"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_refresh_offers" android:id="@+id/fab_refresh_offers"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:srcCompat="@drawable/refresh" app:srcCompat="@drawable/refresh"
android:contentDescription="@string/canteen_fab_refresh_content_desc" android:contentDescription="@string/canteen_fab_refresh_content_desc"
android:layout_gravity="bottom|end" android:tooltipText="@string/canteen_fab_refresh_content_desc"
android:layout_marginEnd="@dimen/fab_margin" tools:ignore="UnusedAttribute"/>
android:layout_marginBottom="@dimen/fab_margin"
app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"/> </androidx.appcompat.widget.LinearLayoutCompat>
<!-- <androidx.coordinatorlayout.widget.CoordinatorLayout--> <!-- <androidx.coordinatorlayout.widget.CoordinatorLayout-->
<!-- android:id="@+id/snackbar_container"--> <!-- android:id="@+id/snackbar_container"-->
+1 -1
View File
@@ -17,7 +17,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_schedule" android:text="@string/title_schedule"
android:textSize="32sp" android:textSize="28sp"
android:fontFamily="@font/lato_bolditalic" android:fontFamily="@font/lato_bolditalic"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:layout_marginHorizontal="16dp" android:layout_marginHorizontal="16dp"
@@ -22,7 +22,7 @@
android:fontFamily="@font/lato_bolditalic" android:fontFamily="@font/lato_bolditalic"
android:text="@string/title_settings" android:text="@string/title_settings"
app:layout_scrollFlags="scroll|exitUntilCollapsed" app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:textSize="32sp"/> android:textSize="28sp"/>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
+3
View File
@@ -58,12 +58,15 @@
<string name="canteen_no_offer_header">Kein Angebot</string> <string name="canteen_no_offer_header">Kein Angebot</string>
<string name="canteen_no_offer_desc">Für diesen Tag sind keine Angebote vorhanden</string> <string name="canteen_no_offer_desc">Für diesen Tag sind keine Angebote vorhanden</string>
<string name="canteen_fab_opening_hours_content_desc">Zeige Öffnungszeiten</string>
<string name="canteen_fab_switch_canteen_content_desc">Wechsle Mensa</string>
<string name="canteen_fab_refresh_content_desc">Aktualisiere Angebote</string> <string name="canteen_fab_refresh_content_desc">Aktualisiere Angebote</string>
<string name="sheet_licences_header">Lizenzen</string> <string name="sheet_licences_header">Lizenzen</string>
<string name="title_schedule">Dein Stundenplan</string> <string name="title_schedule">Dein Stundenplan</string>
<string name="title_food">Mensenangebote</string> <string name="title_food">Mensenangebote</string>
<string name="title_canteen_template">%s Angebote</string>
<string name="title_settings">App-Einstellungen</string> <string name="title_settings">App-Einstellungen</string>
<string name="canteen_opening_hours">%s Öffnungszeiten</string> <string name="canteen_opening_hours">%s Öffnungszeiten</string>
-15
View File
@@ -80,24 +80,9 @@
<item name="colorOnPrimaryGame">@color/game_dark_onPrimary</item> <item name="colorOnPrimaryGame">@color/game_dark_onPrimary</item>
<item name="colorContainerGame">@color/game_dark_container</item> <item name="colorContainerGame">@color/game_dark_container</item>
<item name="colorOnContainerGame">@color/game_dark_onContainer</item> <item name="colorOnContainerGame">@color/game_dark_onContainer</item>
<item name="materialTimePickerTheme">@style/TimeDialogTheme</item>
</style> </style>
<style name="DividerTheme" parent="Widget.Material3.MaterialDivider"> <style name="DividerTheme" parent="Widget.Material3.MaterialDivider">
<item name="dividerColor">@color/md_theme_dark_outlineVariant</item> <item name="dividerColor">@color/md_theme_dark_outlineVariant</item>
</style> </style>
<style name="TimeDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog">
<item name="android:colorAccent">@color/md_theme_dark_primary</item>
<item name="android:colorPrimary">@color/md_theme_dark_primary</item>
<item name="android:colorPrimaryDark">@color/md_theme_dark_secondary</item>
<!-- <item name="buttonBarPositiveButtonStyle">@style/TextButton</item>-->
<!-- <item name="buttonBarNegativeButtonStyle">@style/TextButton</item>-->
</style>
<style name="TextButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/md_theme_dark_primary</item>
<item name="backgroundTint">@color/md_theme_dark_primary</item>
</style>
</resources> </resources>
+3
View File
@@ -59,6 +59,8 @@
<string name="canteen_no_offer_header">No offers</string> <string name="canteen_no_offer_header">No offers</string>
<string name="canteen_no_offer_desc">There are no offers available for this day</string> <string name="canteen_no_offer_desc">There are no offers available for this day</string>
<string name="canteen_fab_opening_hours_content_desc">Show opening hours</string>
<string name="canteen_fab_switch_canteen_content_desc">Switch canteen</string>
<string name="canteen_fab_refresh_content_desc">Refresh offers</string> <string name="canteen_fab_refresh_content_desc">Refresh offers</string>
<string name="sheet_licences_header">Licences</string> <string name="sheet_licences_header">Licences</string>
@@ -66,6 +68,7 @@
<string name="title_schedule">Your Schedule</string> <string name="title_schedule">Your Schedule</string>
<string name="title_food">Canteen Offers</string> <string name="title_food">Canteen Offers</string>
<string name="title_canteen_template">%s Offers</string>
<string name="title_settings">App Settings</string> <string name="title_settings">App Settings</string>
<string name="canteen_opening_hours">%s Opening Hours</string> <string name="canteen_opening_hours">%s Opening Hours</string>
-15
View File
@@ -80,8 +80,6 @@
<item name="colorOnPrimaryGame">@color/game_light_onPrimary</item> <item name="colorOnPrimaryGame">@color/game_light_onPrimary</item>
<item name="colorContainerGame">@color/game_light_container</item> <item name="colorContainerGame">@color/game_light_container</item>
<item name="colorOnContainerGame">@color/game_light_onContainer</item> <item name="colorOnContainerGame">@color/game_light_onContainer</item>
<item name="materialTimePickerTheme">@style/TimeDialogTheme</item> <!-- TimePicker dialog -->
</style> </style>
<style name="Theme.StudIPTimetable" parent="Base.Theme.StudIPTimetable" /> <style name="Theme.StudIPTimetable" parent="Base.Theme.StudIPTimetable" />
@@ -94,17 +92,4 @@
<item name="checkedIconEnabled">false</item> <item name="checkedIconEnabled">false</item>
<item name="checkedIcon">@null</item> <item name="checkedIcon">@null</item>
</style> </style>
<style name="TimeDialogTheme" parent="ThemeOverlay.MaterialComponents.TimePicker">
<item name="android:colorAccent">@color/md_theme_light_primary</item>
<item name="android:colorPrimary">@color/md_theme_light_primary</item>
<item name="android:colorPrimaryDark">@color/md_theme_light_secondary</item>
<item name="buttonBarPositiveButtonStyle">@style/TextButton</item>
<item name="buttonBarNegativeButtonStyle">@style/TextButton</item>
</style>
<style name="TextButton" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/md_theme_light_primary</item>
<item name="backgroundTint">@color/md_theme_light_primary</item>
</style>
</resources> </resources>