Modified logic to more consistently show the fetch button if the schedule is empty, and hide it if there are schedule items

This commit is contained in:
denizk0461
2023-05-11 22:10:08 +02:00
parent 1e8cc694a3
commit b88c831530
8 changed files with 27 additions and 55 deletions
@@ -2,7 +2,6 @@ package com.denizk0461.weserplaner.data
import android.app.Application import android.app.Application
import com.denizk0461.weserplaner.db.AppRepository import com.denizk0461.weserplaner.db.AppRepository
import com.denizk0461.weserplaner.model.SettingsPreferences
import com.denizk0461.weserplaner.model.StudIPEvent import com.denizk0461.weserplaner.model.StudIPEvent
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Document import org.jsoup.nodes.Document
@@ -110,9 +109,6 @@ class StudIPParser(application: Application) {
// After fetching has finished, save the list of new items into persistent storage // After fetching has finished, save the list of new items into persistent storage
repo.insertEvents(newEvents) repo.insertEvents(newEvents)
// Set that the user has modified their schedule
repo.setPreference(SettingsPreferences.HAS_MODIFIED_SCHEDULE, true)
// Return the count of elements that were not able to be fetched // Return the count of elements that were not able to be fetched
return elementsNotFetched return elementsNotFetched
} }
@@ -14,12 +14,12 @@ interface AppDAO {
/* --- Stud.IP schedule events --- */ /* --- Stud.IP schedule events --- */
/** /**
* Retrieves all Stud.IP events, ordered by their timeslots, then their IDs. * Retrieves the number of Stud.IP events available in the database.
* *
* @return all Stud.IP events exposed through a LiveData object * @return number of Stud.IP events
*/ */
@get:Query("SELECT * FROM studip_events ORDER BY timeslotId, eventId") @Query("SELECT COUNT(*) FROM studip_events ORDER BY timeslotId, eventId")
val allEvents: LiveData<List<StudIPEvent>> fun getEventCount(): LiveData<Int>
/** /**
* Retrieves Stud.IP events for a specific day, ordered by their timeslots, then their IDs. * Retrieves Stud.IP events for a specific day, ordered by their timeslots, then their IDs.
@@ -52,6 +52,13 @@ class AppRepository(app: Application) {
} }
} }
/**
* Retrieves the number of Stud.IP events available in the database.
*
* @return number of Stud.IP events
*/
fun getEventCount(): LiveData<Int> = dao.getEventCount()
/** /**
* Retrieves Stud.IP events for a specific day, ordered by their timeslots, then their IDs. * Retrieves Stud.IP events for a specific day, ordered by their timeslots, then their IDs.
* *
@@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import com.denizk0461.weserplaner.R import com.denizk0461.weserplaner.R
import com.denizk0461.weserplaner.activity.FetcherActivity import com.denizk0461.weserplaner.activity.FetcherActivity
@@ -87,12 +86,13 @@ class EventFragment : AppFragment() {
startActivity(Intent(context, FetcherActivity::class.java)) startActivity(Intent(context, FetcherActivity::class.java))
} }
/* // Set visibility of the fetch button depending on whether any events have been stored
* Set the fetch button to be visible if the user has not previously modified their schedule viewModel.getEventCount().observe(viewLifecycleOwner) { eventCount ->
* in any way (added an event manually or used the Stud.IP fetcher). binding.buttonFetchSchedule.visibility = if (eventCount == 0) {
*/ View.VISIBLE
if (!viewModel.preferenceHasModifiedSchedule) { } else {
binding.buttonFetchSchedule.visibility = View.VISIBLE View.GONE
}
} }
// Set up button for adding a new event // Set up button for adding a new event
@@ -106,26 +106,12 @@ class EventFragment : AppFragment() {
val bundle = Bundle() val bundle = Bundle()
bundle.putBoolean("isEditing", false) bundle.putBoolean("isEditing", false)
sheet.arguments = bundle sheet.arguments = bundle
/*
* Receive fragment result to update the button visibility.
* TODO doesn't work after configuration change (change into or out of multi
* window)
*/
setFragmentResultListener("eventAdded") { _, _ ->
// Hide the fetch button
binding.buttonFetchSchedule.visibility = View.GONE
}
} }
) )
} }
// Show the user a little message if they're opening the app for the first time // Show the user a little message if they're opening the app for the first time
if (viewModel.preferenceFirstLaunch) { if (viewModel.preferenceFirstLaunch) {
// Show toast
// showToast(context, getString(R.string.toast_first_launch))
// Remember that the app has been launched before // Remember that the app has been launched before
viewModel.preferenceFirstLaunch = false viewModel.preferenceFirstLaunch = false
} }
@@ -63,10 +63,5 @@ enum class SettingsPreferences(val key: String) {
* Whether the user has opened the canteen fragment before. * Whether the user has opened the canteen fragment before.
*/ */
HAS_OPENED_CANTEEN("has_opened_canteen"), HAS_OPENED_CANTEEN("has_opened_canteen"),
/**
* Whether the user has done something to modify their schedule.
*/
HAS_MODIFIED_SCHEDULE("has_modified_schedule"),
; ;
} }
@@ -5,7 +5,6 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.widget.addTextChangedListener import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.transition.TransitionManager import androidx.transition.TransitionManager
import com.denizk0461.weserplaner.R import com.denizk0461.weserplaner.R
@@ -189,12 +188,6 @@ class ScheduleUpdateSheet : AppSheet(R.layout.sheet_schedule_update) {
) )
) )
/*
* Send an empty bundle to tell the parent fragment that it should update its view
* according to the changes made here.
*/
parentFragment?.setFragmentResult("eventAdded", Bundle())
// Dismiss the sheet upon update // Dismiss the sheet upon update
dismiss() dismiss()
} }
@@ -1,6 +1,7 @@
package com.denizk0461.weserplaner.viewmodel package com.denizk0461.weserplaner.viewmodel
import android.app.Application import android.app.Application
import androidx.lifecycle.LiveData
import com.denizk0461.weserplaner.model.SettingsPreferences import com.denizk0461.weserplaner.model.SettingsPreferences
/** /**
@@ -10,6 +11,13 @@ import com.denizk0461.weserplaner.model.SettingsPreferences
*/ */
class EventViewModel(app: Application) : AppViewModel(app) { class EventViewModel(app: Application) : AppViewModel(app) {
/**
* Retrieves the number of Stud.IP events available in the database.
*
* @return number of Stud.IP events
*/
fun getEventCount(): LiveData<Int> = repo.getEventCount()
/** /**
* This value determines whether the user wants their timetable to launch with the current day. * This value determines whether the user wants their timetable to launch with the current day.
*/ */
@@ -28,10 +36,4 @@ class EventViewModel(app: Application) : AppViewModel(app) {
defaultValue = true, defaultValue = true,
) )
set(value) { repo.setPreference(SettingsPreferences.FIRST_LAUNCH, value) } set(value) { repo.setPreference(SettingsPreferences.FIRST_LAUNCH, value) }
/**
* Determines whether the user has done something to modify their schedule.
*/
val preferenceHasModifiedSchedule: Boolean
get() = repo.getBooleanPreference(SettingsPreferences.HAS_MODIFIED_SCHEDULE)
} }
@@ -1,7 +1,6 @@
package com.denizk0461.weserplaner.viewmodel package com.denizk0461.weserplaner.viewmodel
import android.app.Application import android.app.Application
import com.denizk0461.weserplaner.model.SettingsPreferences
import com.denizk0461.weserplaner.model.StudIPEvent import com.denizk0461.weserplaner.model.StudIPEvent
class ScheduleUpdateViewModel(application: Application) : AppViewModel(application) { class ScheduleUpdateViewModel(application: Application) : AppViewModel(application) {
@@ -11,13 +10,7 @@ class ScheduleUpdateViewModel(application: Application) : AppViewModel(applicati
* *
* @param event the event to save * @param event the event to save
*/ */
fun insert(event: StudIPEvent) { fun insert(event: StudIPEvent) { doAsync { repo.insert(event) } }
// Inser the event
doAsync { repo.insert(event) }
// Set that the user has modified their schedule
repo.setPreference(SettingsPreferences.HAS_MODIFIED_SCHEDULE, true)
}
/** /**
* Updates a schedule element. * Updates a schedule element.