Added functionality to view news for a canteen; fixed a bug that would prevent the offers from being fetched for canteens without news; BUG: news are still a bit buggy, check for crashes

This commit is contained in:
denizk0461
2023-05-09 12:22:40 +02:00
parent b42118ed94
commit 6cbfe71e62
13 changed files with 93 additions and 33 deletions
@@ -8,7 +8,6 @@ import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import java.io.IOException
import kotlin.jvm.Throws
/**
@@ -162,7 +161,7 @@ class StwParser(application: Application) {
doc.getElementsByClass("field__items")[0]
.getElementsByTag("p")[0]
.text()
} catch (e: IOException) {
} catch (e: IndexOutOfBoundsException) {
""
}
@@ -192,6 +192,6 @@ interface AppDAO {
*
* @return opening hours
*/
@Query("SELECT openingHours FROM offer_canteen LIMIT 1")
fun getCanteenOpeningHours(): String
@Query("SELECT openingHours, news FROM offer_canteen LIMIT 1")
fun getCanteenInfo(): CanteenOfferTuple
}
@@ -72,11 +72,11 @@ class AppRepository(app: Application) {
fun nukeEvents() { dao.nukeEvents() }
/**
* Retrieves the opening hours of the canteen as a string.
* Retrieves the opening hours as well as notifications for the canteen as strings.
*
* @return opening hours
* @return opening hours and notifications
*/
fun getCanteenOpeningHours(): String = dao.getCanteenOpeningHours()
fun getCanteenInfo(): CanteenOfferTuple = dao.getCanteenInfo()
/**
* Retrieves all canteen offers that match given day.
@@ -41,7 +41,13 @@ class CanteenFragment : AppFragment() {
* Locally saved opening hours to quickly access them without querying the database on every
* opening of the bottom sheet.
*/
private var openingHours = ""
private var openingHours: String = ""
/**
* Locally saved news to quickly access them without querying the database on every opening of
* the corresponding bottom sheet.
*/
private var news: String = ""
/**
* Locally stored dates to populate the TabLayout with.
@@ -67,6 +73,12 @@ class CanteenFragment : AppFragment() {
)
binding.appTitleBar.isSelected = true
binding.buttonNotifications.setOnClickListener {
openBottomSheet(getTextSheet(
getString(R.string.canteen_news_sheet_title),
news,
))
}
// Assign a preference value to every button to filter for dietary preferences
val chipMap = mapOf(
@@ -167,7 +179,17 @@ class CanteenFragment : AppFragment() {
// Let the adapter know of the new amount of dates (pages) to display
viewPagerAdapter.itemCount = dates.size
openingHours = viewModel.getCanteenOpeningHours()
// Retrieve further information about this canteen from the database
viewModel.getCanteenInfo().also { info ->
openingHours = info.openingHours
news = info.news
binding.buttonNotifications.visibility = if (news.isBlank()) {
View.GONE
} else {
View.VISIBLE
}
}
}
// Assign the adapter to the view pager
@@ -0,0 +1,12 @@
package com.denizk0461.weserplaner.model
/**
* Serves to retrieve information about a specified canteen from the database using a single query.
*
* @param openingHours opening hours of the canteen
* @param news news that may be available for the canteen
*/
data class CanteenOfferTuple(
val openingHours: String,
val news: String,
)
@@ -26,11 +26,11 @@ class CanteenViewModel(app: Application) : AppViewModel(app) {
fun getDates(): LiveData<List<OfferDate>> = repo.getDates()
/**
* Retrieves the opening hours of the canteen as a string.
* Retrieves the opening hours as well as notifications for the canteen as strings.
*
* @return opening hours
* @return opening hours and notifications
*/
fun getCanteenOpeningHours(): String = returnBlocking { repo.getCanteenOpeningHours() }
fun getCanteenInfo(): CanteenOfferTuple = returnBlocking { repo.getCanteenInfo() }
/**
* Fetch the canteen offers asynchronously. Updates will be provided through a LiveData object.
@@ -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="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/>
</vector>
+38 -19
View File
@@ -17,23 +17,48 @@
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<TextView
android:id="@+id/app_title_bar"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_food"
android:textSize="28sp"
android:fontFamily="@font/lato_bolditalic"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"/>
android:layout_marginBottom="8dp">
<TextView
android:id="@+id/app_title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_food"
android:textSize="28sp"
android:fontFamily="@font/lato_bolditalic"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/button_notifications"/>
<com.google.android.material.button.MaterialButton
style="@style/Widget.Material3.Button.TextButton"
android:id="@+id/button_notifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:layout_marginStart="8dp"
app:icon="@drawable/notification"
app:iconPadding="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:iconTint="?attr/colorOnSurfaceVariant"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<HorizontalScrollView
android:id="@+id/scroll_view_chip"
@@ -250,10 +275,4 @@
</androidx.appcompat.widget.LinearLayoutCompat>
<!-- <androidx.coordinatorlayout.widget.CoordinatorLayout-->
<!-- android:id="@+id/snackbar_container"-->
<!-- android:layout_width="0dp"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:layout_gravity="bottom"/>-->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
+1
View File
@@ -63,6 +63,7 @@
<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_news_sheet_title">Neuigkeiten für diese Mensa</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>
+1
View File
@@ -64,6 +64,7 @@
<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_news_sheet_title">News for this canteen</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>