Fixed a bug hiding all Stud.IP events from the user's view by... renaming the database table. yeah. i don't even know anymore.

This commit is contained in:
denizk0461
2023-04-28 10:30:30 +02:00
parent ce7b854e8c
commit ee0934f29b
8 changed files with 28 additions and 23 deletions
@@ -75,12 +75,15 @@ class FetcherActivity : Activity() {
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()
}
@@ -6,6 +6,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import com.denizk0461.studip.fragment.CanteenPageFragment
import com.denizk0461.studip.model.CanteenOfferGroup
/**
* Custom ViewPager adapter for managing multiple pages of canteen offers.
*
@@ -19,7 +20,7 @@ import com.denizk0461.studip.model.CanteenOfferGroup
* @param displayAllergens whether the user wants allergens to be marked
*/
class CanteenOfferPageAdapter(
fragmentActivity: FragmentActivity,
val fragmentActivity: FragmentActivity,
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
@@ -18,7 +18,7 @@ interface AppDAO {
*
* @return all Stud.IP events exposed through a LiveData object
*/
@get:Query("SELECT * FROM events ORDER BY timeslotId, id")
@get:Query("SELECT * FROM studipevents ORDER BY timeslotId, id")
val allEvents: LiveData<List<StudIPEvent>>
/**
@@ -54,7 +54,7 @@ interface AppDAO {
/**
* Deletes all Stud.IP events from the database.
*/
@Query("DELETE FROM events")
@Query("DELETE FROM studipevents")
fun nukeEvents()
/* --- canteen offers --- */
@@ -15,9 +15,9 @@ import com.denizk0461.studip.model.*
OfferDate::class,
OfferCanteen::class,
OfferCategory::class,
OfferItem::class
OfferItem::class,
],
version = 14,
version = 16,
)
abstract class AppDatabase : RoomDatabase() {
@@ -137,6 +137,7 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
* flicker.
* TODO implement a more efficient / better-looking method
* TODO order the chips alphabetically
* TODO the refreshing ability stopped working
*/
val index = binding.chipsPreference.indexOfChild(buttonView)
binding.chipsPreference.removeView(buttonView)
@@ -156,9 +157,6 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
// Assign the adapter to the view pager
binding.viewPager.adapter = viewPagerAdapter
// Create and attach the tab mediator for the view pager
// createTabLayoutMediator()
// Set up LiveData observer to refresh the view on update
viewModel.allOffers.observe(viewLifecycleOwner) { offers ->
// Update the element list stored in this fragment
@@ -167,9 +165,7 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
// Group elements by category
val groupedElements = offers.groupElements().distinct()
// Find all dates stored in the database
// val newDates = /*groupedElements.map { it.date }.distinct() TODO */viewModel.getDates()
// Find all dates for which items are available
val newDates = groupedElements.map { it.date }.distinct()
// Update the date count stored in this fragment
@@ -183,7 +179,8 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
binding.swipeRefreshLayout.isRefreshing = false
// createTabLayoutMediator(newDates)
// Create and attach the tab layout for the ViewPager
createTabLayoutMediator(newDates)
}
// Set up functions for when the user swipes to refresh the view
@@ -194,17 +191,17 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
/**
* Create and attach the object mediating the tabs for the view pager.
* TODO this is crash-prone and doesn't refresh when the dates get renewed (when the last update
* was from the day prior)
* TODO WHY DO YOU THROW AN INDEXOUTOFBOUNDSEXCEPTION????
*
* TODO this is error-prone, likes to crash with an IndexOutOfBoundsException, and I have no
* fucking idea why
*
* @param dates titles for the individual tabs
*/
private fun createTabLayoutMediator(dates: List<OfferDate>) {
// Fetch all dates from the database
// val dates = viewModel.getDates()
private fun createTabLayoutMediator(dates: List<String>) {
// Only attach the mediator if the list has items, otherwise an error would occur
if (dates.isNotEmpty()) {
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
tab.text = dates[position].date
tab.text = dates[position]
}.attach()
}
}
@@ -58,7 +58,8 @@ class EventFragment : AppFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
weekdays = resources.getStringArray(R.array.weekdays)
// Get localised weekday names
weekdays = context?.resources?.getStringArray(R.array.weekdays) ?: arrayOf()
// Determine the current day
dayOfWeek = when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
@@ -101,6 +102,9 @@ class EventFragment : AppFragment() {
// Set up LiveData observer to refresh the view on update
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
Log.d("AAAbA?", events.toString())
// Update the item list in the view pager's adapter
viewPagerAdapter.setNewItems(events)
@@ -16,7 +16,7 @@ import androidx.room.PrimaryKey
* @param timeslotId minute the event starts at - used for ordering
* @param colour user-defined colour the event will be shown in - UNIMPLEMENTED
*/
@Entity(tableName = "events")
@Entity(tableName = "studipevents")
data class StudIPEvent(
@PrimaryKey val id: Int,
val title: String,