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 com.denizk0461.weserplaner.db.AppRepository
import com.denizk0461.weserplaner.model.SettingsPreferences
import com.denizk0461.weserplaner.model.StudIPEvent
import org.jsoup.Jsoup
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
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 elementsNotFetched
}
@@ -14,12 +14,12 @@ interface AppDAO {
/* --- 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")
val allEvents: LiveData<List<StudIPEvent>>
@Query("SELECT COUNT(*) FROM studip_events ORDER BY timeslotId, eventId")
fun getEventCount(): LiveData<Int>
/**
* 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.
*
@@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels
import com.denizk0461.weserplaner.R
import com.denizk0461.weserplaner.activity.FetcherActivity
@@ -87,12 +86,13 @@ class EventFragment : AppFragment() {
startActivity(Intent(context, FetcherActivity::class.java))
}
/*
* Set the fetch button to be visible if the user has not previously modified their schedule
* in any way (added an event manually or used the Stud.IP fetcher).
*/
if (!viewModel.preferenceHasModifiedSchedule) {
binding.buttonFetchSchedule.visibility = View.VISIBLE
// Set visibility of the fetch button depending on whether any events have been stored
viewModel.getEventCount().observe(viewLifecycleOwner) { eventCount ->
binding.buttonFetchSchedule.visibility = if (eventCount == 0) {
View.VISIBLE
} else {
View.GONE
}
}
// Set up button for adding a new event
@@ -106,26 +106,12 @@ class EventFragment : AppFragment() {
val bundle = Bundle()
bundle.putBoolean("isEditing", false)
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
if (viewModel.preferenceFirstLaunch) {
// Show toast
// showToast(context, getString(R.string.toast_first_launch))
// Remember that the app has been launched before
viewModel.preferenceFirstLaunch = false
}
@@ -63,10 +63,5 @@ enum class SettingsPreferences(val key: String) {
* Whether the user has opened the canteen fragment before.
*/
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 androidx.appcompat.widget.PopupMenu
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
import androidx.transition.TransitionManager
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()
}
@@ -1,6 +1,7 @@
package com.denizk0461.weserplaner.viewmodel
import android.app.Application
import androidx.lifecycle.LiveData
import com.denizk0461.weserplaner.model.SettingsPreferences
/**
@@ -10,6 +11,13 @@ import com.denizk0461.weserplaner.model.SettingsPreferences
*/
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.
*/
@@ -28,10 +36,4 @@ class EventViewModel(app: Application) : AppViewModel(app) {
defaultValue = true,
)
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
import android.app.Application
import com.denizk0461.weserplaner.model.SettingsPreferences
import com.denizk0461.weserplaner.model.StudIPEvent
class ScheduleUpdateViewModel(application: Application) : AppViewModel(application) {
@@ -11,13 +10,7 @@ class ScheduleUpdateViewModel(application: Application) : AppViewModel(applicati
*
* @param event the event to save
*/
fun insert(event: StudIPEvent) {
// Inser the event
doAsync { repo.insert(event) }
// Set that the user has modified their schedule
repo.setPreference(SettingsPreferences.HAS_MODIFIED_SCHEDULE, true)
}
fun insert(event: StudIPEvent) { doAsync { repo.insert(event) } }
/**
* Updates a schedule element.