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.
* TODO this can be optimised by checking in EventPageFragment.kt and delivering a boolean
*/
private val currentCalendar = Calendar.getInstance()
@@ -128,10 +128,7 @@ class StwParser(application: Application) {
// Parse the HTML using Jsoup to traverse the document
val doc = Jsoup.connect(url).get()
/*
* Retrieve the opening hours for the canteen.
* TODO this formatting sucks and also this doesn't work
*/
// Retrieve the opening hours for the canteen
var openingHours = ""
// Iterate through all wrappers that could contain opening hours
@@ -139,10 +136,7 @@ class StwParser(application: Application) {
// Element is not for providing contact details
if (it.getElementsByClass("contact-person-name").size == 0) {
/*
* Retrieve all opening hours.
* TODO this slows down the fetch SIGNIFICANTLY, but why?
*/
// Retrieve all opening hours.
it.children().forEach { element ->
if (element.tag().toString() == "p") {
openingHours += element.textWithBreaks().trim() + "\n" // Uni-Mensa is not parsed properly
@@ -53,53 +53,18 @@ class CanteenFragment : AppFragment() {
// Set progress circle colours
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
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)
binding.buttonInfo.setOnClickListener {
openBottomSheet(
getTextSheet(
getString(
R.string.canteen_opening_hours,
getCurrentlySelectedCanteenName()
),
openingHours
)
)
}
// binding.buttonInfo.setOnClickListener {
//
// }
// Assign a preference value to every button to filter for dietary preferences
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
binding.fabRefreshOffers.setOnClickListener {
// Display to the user that a refresh is in progress
@@ -224,21 +238,27 @@ class CanteenFragment : AppFragment() {
private fun refresh() {
// Retrieve new offers from the website(s)
viewModel.fetchOffers(viewModel.preferenceCanteen, onFinish = {
/*
* Tell the swipe refresh layout to stop refreshing.
*/
binding.swipeRefreshLayout.isRefreshing = false
// Check if the fragment is still active
if (_binding != null) {
/*
* Tell the swipe refresh layout to stop refreshing.
*/
binding.swipeRefreshLayout.isRefreshing = false
}
}, onError = {
// Tell the user that an error occurred
context.theme?.showErrorSnackBar(
binding.snackbarContainer,
getString(R.string.canteen_fetch_error)
)
/*
// Check if the fragment is still active
if (_binding != null) {
// Tell the user that an error occurred
context.theme?.showErrorSnackBar(
binding.snackbarContainer,
getString(R.string.canteen_fetch_error)
)
/*
* 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
// 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 {
/**
@@ -69,8 +73,13 @@ class AllergenPreferences {
* @return string with preferences inserted
*/
fun deconstruct(): String {
// Create an empty list
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 (hasRye) array.add("a2")
if (hasBarley) array.add("a3")
@@ -98,6 +107,7 @@ class AllergenPreferences {
if (hasSesame) array.add("m")
if (hasMolluscs) array.add("n")
// Join the individual symbols to a single string
return array.joinToString(",")
}
}
@@ -116,6 +126,7 @@ class AllergenPreferences {
* @return instance of AllergenPreferences.Object with preferences inserted
*/
fun construct(input: String): Object {
// Split the string into its individual components
val elements = input.split(",")
return Object(
elements.contains("a1"),