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
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="KotlinJpsPluginSettings"> <component name="KotlinJpsPluginSettings">
<option name="version" value="1.8.20" /> <option name="version" value="1.8.21" />
</component> </component>
</project> </project>
+1
View File
@@ -59,6 +59,7 @@ dependencies {
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01' implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.2.0-alpha01'
implementation "androidx.room:room-runtime:2.5.1" implementation "androidx.room:room-runtime:2.5.1"
implementation 'androidx.core:core-ktx:1.10.0'
kapt "androidx.room:room-compiler:2.5.1" kapt "androidx.room:room-compiler:2.5.1"
implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.preference:preference-ktx:1.2.0'
@@ -8,7 +8,6 @@ import kotlinx.coroutines.withContext
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import org.jsoup.select.Elements import org.jsoup.select.Elements
import java.io.IOException
import kotlin.jvm.Throws import kotlin.jvm.Throws
/** /**
@@ -162,7 +161,7 @@ class StwParser(application: Application) {
doc.getElementsByClass("field__items")[0] doc.getElementsByClass("field__items")[0]
.getElementsByTag("p")[0] .getElementsByTag("p")[0]
.text() .text()
} catch (e: IOException) { } catch (e: IndexOutOfBoundsException) {
"" ""
} }
@@ -192,6 +192,6 @@ interface AppDAO {
* *
* @return opening hours * @return opening hours
*/ */
@Query("SELECT openingHours FROM offer_canteen LIMIT 1") @Query("SELECT openingHours, news FROM offer_canteen LIMIT 1")
fun getCanteenOpeningHours(): String fun getCanteenInfo(): CanteenOfferTuple
} }
@@ -72,11 +72,11 @@ class AppRepository(app: Application) {
fun nukeEvents() { dao.nukeEvents() } 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. * 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 * Locally saved opening hours to quickly access them without querying the database on every
* opening of the bottom sheet. * 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. * Locally stored dates to populate the TabLayout with.
@@ -67,6 +73,12 @@ class CanteenFragment : AppFragment() {
) )
binding.appTitleBar.isSelected = true 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 // Assign a preference value to every button to filter for dietary preferences
val chipMap = mapOf( val chipMap = mapOf(
@@ -167,7 +179,17 @@ class CanteenFragment : AppFragment() {
// Let the adapter know of the new amount of dates (pages) to display // Let the adapter know of the new amount of dates (pages) to display
viewPagerAdapter.itemCount = dates.size 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 // 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() 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. * 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:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
app:layout_scrollFlags="scroll|exitUntilCollapsed"> app:layout_scrollFlags="scroll|exitUntilCollapsed">
<TextView <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/app_title_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/title_food"
android:textSize="28sp"
android:fontFamily="@font/lato_bolditalic"
android:layout_marginHorizontal="16dp" android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp">
android:singleLine="true"
android:ellipsize="marquee" <TextView
android:marqueeRepeatLimit="marquee_forever" android:id="@+id/app_title_bar"
android:focusable="true" android:layout_width="match_parent"
android:focusableInTouchMode="true" android:layout_height="wrap_content"
android:scrollHorizontally="true"/> 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 <HorizontalScrollView
android:id="@+id/scroll_view_chip" android:id="@+id/scroll_view_chip"
@@ -250,10 +275,4 @@
</androidx.appcompat.widget.LinearLayoutCompat> </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> </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_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_news_sheet_title">Neuigkeiten für diese Mensa</string>
<string name="canteen_fab_opening_hours_content_desc">Zeige Öffnungszeiten</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_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>
+1
View File
@@ -64,6 +64,7 @@
<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_news_sheet_title">News for this canteen</string>
<string name="canteen_fab_opening_hours_content_desc">Show opening hours</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_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>
+1 -1
View File
@@ -2,5 +2,5 @@
plugins { plugins {
id 'com.android.application' version '8.0.1' apply false id 'com.android.application' version '8.0.1' apply false
id 'com.android.library' version '8.0.1' apply false id 'com.android.library' version '8.0.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false id 'org.jetbrains.kotlin.android' version '1.8.21' apply false
} }