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 -> StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
// Delete all previously fetched elements // Delete all previously fetched elements
viewModel.nukeEvents() viewModel.nukeEvents()
// Insert the list into the database // Insert the list into the database
viewModel.insertEvents(events) viewModel.insertEvents(events)
// Notify the user that the fetch was successful // Notify the user that the fetch was successful
Toast.makeText( Toast.makeText(
this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT this, R.string.toast_fetch_finished, Toast.LENGTH_SHORT
).show() ).show()
// Close the activity // Close the activity
finish() finish()
} }
@@ -6,6 +6,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import com.denizk0461.studip.fragment.CanteenPageFragment import com.denizk0461.studip.fragment.CanteenPageFragment
import com.denizk0461.studip.model.CanteenOfferGroup import com.denizk0461.studip.model.CanteenOfferGroup
/** /**
* Custom ViewPager adapter for managing multiple pages of canteen offers. * 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 * @param displayAllergens whether the user wants allergens to be marked
*/ */
class CanteenOfferPageAdapter( class CanteenOfferPageAdapter(
fragmentActivity: FragmentActivity, val fragmentActivity: FragmentActivity,
private var offers: List<CanteenOfferGroup>, private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int, private var daysCovered: Int,
private val onClickListener: CanteenOfferItemAdapter.OnClickListener, private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
@@ -18,7 +18,7 @@ interface AppDAO {
* *
* @return all Stud.IP events exposed through a LiveData object * @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>> val allEvents: LiveData<List<StudIPEvent>>
/** /**
@@ -54,7 +54,7 @@ interface AppDAO {
/** /**
* Deletes all Stud.IP events from the database. * Deletes all Stud.IP events from the database.
*/ */
@Query("DELETE FROM events") @Query("DELETE FROM studipevents")
fun nukeEvents() fun nukeEvents()
/* --- canteen offers --- */ /* --- canteen offers --- */
@@ -15,9 +15,9 @@ import com.denizk0461.studip.model.*
OfferDate::class, OfferDate::class,
OfferCanteen::class, OfferCanteen::class,
OfferCategory::class, OfferCategory::class,
OfferItem::class OfferItem::class,
], ],
version = 14, version = 16,
) )
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
@@ -137,6 +137,7 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
* flicker. * flicker.
* TODO implement a more efficient / better-looking method * TODO implement a more efficient / better-looking method
* TODO order the chips alphabetically * TODO order the chips alphabetically
* TODO the refreshing ability stopped working
*/ */
val index = binding.chipsPreference.indexOfChild(buttonView) val index = binding.chipsPreference.indexOfChild(buttonView)
binding.chipsPreference.removeView(buttonView) binding.chipsPreference.removeView(buttonView)
@@ -156,9 +157,6 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
// Assign the adapter to the view pager // Assign the adapter to the view pager
binding.viewPager.adapter = viewPagerAdapter 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 // Set up LiveData observer to refresh the view on update
viewModel.allOffers.observe(viewLifecycleOwner) { offers -> viewModel.allOffers.observe(viewLifecycleOwner) { offers ->
// Update the element list stored in this fragment // Update the element list stored in this fragment
@@ -167,9 +165,7 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
// Group elements by category // Group elements by category
val groupedElements = offers.groupElements().distinct() val groupedElements = offers.groupElements().distinct()
// Find all dates stored in the database // Find all dates for which items are available
// val newDates = /*groupedElements.map { it.date }.distinct() TODO */viewModel.getDates()
val newDates = groupedElements.map { it.date }.distinct() val newDates = groupedElements.map { it.date }.distinct()
// Update the date count stored in this fragment // Update the date count stored in this fragment
@@ -183,7 +179,8 @@ class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
binding.swipeRefreshLayout.isRefreshing = false 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 // 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. * 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 this is error-prone, likes to crash with an IndexOutOfBoundsException, and I have no
* TODO WHY DO YOU THROW AN INDEXOUTOFBOUNDSEXCEPTION???? * fucking idea why
*
* @param dates titles for the individual tabs
*/ */
private fun createTabLayoutMediator(dates: List<OfferDate>) { private fun createTabLayoutMediator(dates: List<String>) {
// Fetch all dates from the database // Only attach the mediator if the list has items, otherwise an error would occur
// val dates = viewModel.getDates()
if (dates.isNotEmpty()) { if (dates.isNotEmpty()) {
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position -> TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
tab.text = dates[position].date tab.text = dates[position]
}.attach() }.attach()
} }
} }
@@ -58,7 +58,8 @@ class EventFragment : AppFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) 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 // Determine the current day
dayOfWeek = when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) { 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 // Set up LiveData observer to refresh the view on update
viewModel.allEvents.observe(viewLifecycleOwner) { events -> viewModel.allEvents.observe(viewLifecycleOwner) { events ->
Log.d("AAAbA?", events.toString())
// Update the item list in the view pager's adapter // Update the item list in the view pager's adapter
viewPagerAdapter.setNewItems(events) viewPagerAdapter.setNewItems(events)
@@ -16,7 +16,7 @@ import androidx.room.PrimaryKey
* @param timeslotId minute the event starts at - used for ordering * @param timeslotId minute the event starts at - used for ordering
* @param colour user-defined colour the event will be shown in - UNIMPLEMENTED * @param colour user-defined colour the event will be shown in - UNIMPLEMENTED
*/ */
@Entity(tableName = "events") @Entity(tableName = "studipevents")
data class StudIPEvent( data class StudIPEvent(
@PrimaryKey val id: Int, @PrimaryKey val id: Int,
val title: String, val title: String,
@@ -15,7 +15,7 @@ class FetcherViewModel(app: Application) : AppViewModel(app) {
* *
* @param events list of events to be saved * @param events list of events to be saved
*/ */
fun insertEvents(events: List<StudIPEvent>) { doAsync { repo.insertEvents(events) } } fun insertEvents(events: List<StudIPEvent>) { doAsync { repo.insertEvents(events) }}
/** /**
* Delete all Stud.IP events from the database asynchronously. * Delete all Stud.IP events from the database asynchronously.