Implemented button to change dates for an event to reflect the academic quarter

This commit is contained in:
denizk0461
2023-04-27 08:20:56 +02:00
parent 540a2a7b9a
commit a6a1309bce
12 changed files with 119 additions and 42 deletions
@@ -1,7 +1,9 @@
package com.denizk0461.studip.data
import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import android.widget.Toast
import androidx.annotation.AttrRes
import com.denizk0461.studip.R
@@ -30,24 +32,52 @@ fun Resources.Theme.getThemedColor(@AttrRes id: Int): Int = TypedValue().also {
resolveAttribute(id, value, true)
}.data
object Misc {
/**
* Present the user with a toast.
*
* @param text text to display
*/
fun showToast(context: Context, text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
// private val timeslotAcademicQuarter = mapOf(
// "6:00" to "6:15",
// "8:00" to "7:45",
// "8:00" to "8:15",
// "10:00" to "9:45",
// "10:00" to "10:15",
// "12:00" to "11:45",
// "12:00" to "12:15",
// "14:00" to "13:45",
// "14:00" to "14:15",
// "16:00" to "15:45",
// "16:00" to "16:15",
// "18:00" to "17:45",
// "18:00" to "18:15",
// "20:00" to "19:45",
// "20:00" to "20:15",
// "22:00" to "21:45",
// )
}
/**
* Provides a conversion method between a timestamp ending in a full hour, and one with an academic
* quarter applied.
*/
val timeslotsAcademicQuarter = listOf(
"4:00",
"6:00",
"8:00",
"10:00",
"12:00",
"14:00",
"16:00",
"18:00",
"20:00",
"22:00",
)
val timeslotsAcademicQuarterStart = mapOf(
"4:00" to "4:15",
"6:00" to "6:15",
"8:00" to "8:15",
"10:00" to "10:15",
"12:00" to "12:15",
"14:00" to "14:15",
"16:00" to "16:15",
"18:00" to "18:15",
"20:00" to "20:15",
)
val timeslotsAcademicQuarterEnd = mapOf(
"6:00" to "5:45",
"8:00" to "7:45",
"10:00" to "9:45",
"12:00" to "11:45",
"14:00" to "13:45",
"16:00" to "15:45",
"18:00" to "17:45",
"20:00" to "19:45",
"22:00" to "21:45",
)
@@ -197,7 +197,8 @@ class AppRepository(app: Application) {
* @param pref preference to retrieve
* @return whether the user set this preference
*/
fun getBooleanPreference(pref: SettingsPreferences): Boolean = prefs.getBoolean(pref.key, false)
fun getBooleanPreference(pref: SettingsPreferences, defaultValue: Boolean = false): Boolean =
prefs.getBoolean(pref.key, defaultValue)
/**
* Retrieves ta user-set integer preference.
@@ -7,6 +7,7 @@ import android.util.Base64
import android.view.View
import android.widget.Toast
import com.denizk0461.studip.R
import com.denizk0461.studip.data.showToast
import com.denizk0461.studip.data.viewBinding
import com.denizk0461.studip.databinding.SheetDevCodeBinding
import java.nio.charset.StandardCharsets
@@ -60,43 +61,34 @@ class DevCodeSheet(
"QkJVT0s/".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS90Rm1PMm1TY0tHNA==".d())
"NEVENT" -> { // nuke events
nukeEvents()
showToast("Nuked all events")
showToast(context, "Nuked all events")
}
"NCANTE" -> { // nuke canteens
nukeOfferCanteens()
showToast("Nuked all canteens")
showToast(context, "Nuked all canteens")
}
"NDATES" -> { // nuke dates
nukeOfferDates()
showToast("Nuked all dates")
showToast(context, "Nuked all dates")
}
"NCATEG" -> { // nuke categories
nukeOfferCategories()
showToast("Nuked all categories")
showToast(context, "Nuked all categories")
}
"NITEMS" -> { // nuke items
nukeOfferItems()
showToast("Nuked all items")
showToast(context, "Nuked all items")
}
"NEVERY" -> { // nuke everything
nukeEverything()
showToast("Nuked everything")
showToast(context, "Nuked everything")
}
else -> showToast("code is invalid")
else -> showToast(context, "code is invalid")
}
dismiss()
}
}
/**
* Present the user with a toast.
*
* @param text text to display
*/
private fun showToast(text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
/**
* Launches a web link.
*
@@ -104,7 +96,7 @@ class DevCodeSheet(
*/
private fun launchLink(link: String) {
// Let the user know they accomplished something with their life
showToast("")
showToast(context, "")
// Give the user a slight dopamine boost
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))
@@ -1,11 +1,17 @@
package com.denizk0461.studip.sheet
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.transition.TransitionManager
import com.denizk0461.studip.R
import com.denizk0461.studip.data.parseToMinutes
import com.denizk0461.studip.data.showToast
import com.denizk0461.studip.data.timeslotsAcademicQuarter
import com.denizk0461.studip.data.timeslotsAcademicQuarterEnd
import com.denizk0461.studip.data.timeslotsAcademicQuarterStart
import com.denizk0461.studip.data.viewBinding
import com.denizk0461.studip.databinding.SheetScheduleUpdateBinding
import com.denizk0461.studip.dialog.TimePickerFragment
@@ -40,6 +46,34 @@ class ScheduleUpdateSheet(
binding.editTextLecturers.setText(event.lecturer)
binding.editTextRoom.setText(event.room)
// Show academic quarter suggestion if it is applicable
binding.buttonAcademicQuarter.visibility = if (
timeslotsAcademicQuarter.contains(timeslotStart) &&
timeslotsAcademicQuarter.contains(timeslotEnd)
) {
binding.buttonAcademicQuarter.setOnClickListener {
try {
val newStart = timeslotsAcademicQuarterStart[timeslotStart]
?: throw NullPointerException()
val newEnd = timeslotsAcademicQuarterEnd[timeslotEnd]
?: throw NullPointerException()
timeslotStart = newStart
binding.buttonTimeStart.text = newStart
timeslotEnd = newEnd
binding.buttonTimeEnd.text = newEnd
TransitionManager.beginDelayedTransition(binding.sheet)
binding.buttonAcademicQuarter.visibility = View.GONE
} catch (e: NullPointerException) {
showToast(context, getString(R.string.sheet_schedule_update_hint_quarter_error))
}
}
View.VISIBLE
} else {
View.GONE
}
// Prepare timestamp buttons
binding.buttonTimeStart.text = timeslotStart
binding.buttonTimeStart.setOnClickListener {
@@ -70,7 +70,7 @@ class CanteenViewModel(app: Application) : AppViewModel(app) {
* This value determines whether the user wants to have allergens marked.
*/
val preferenceAllergen: Boolean
get() = repo.getBooleanPreference(SettingsPreferences.ALLERGEN)
get() = repo.getBooleanPreference(SettingsPreferences.ALLERGEN, defaultValue = true)
var preferenceCanteen: Int
get() = repo.getIntPreference(SettingsPreferences.CANTEEN)
@@ -60,7 +60,7 @@ class SettingsViewModel(app: Application) : AppViewModel(app) {
* This value determines whether the user wants to have allergens marked.
*/
var preferenceAllergen: Boolean
get() = repo.getBooleanPreference(SettingsPreferences.ALLERGEN)
get() = repo.getBooleanPreference(SettingsPreferences.ALLERGEN, defaultValue = true)
set(newValue) { repo.setPreference(SettingsPreferences.ALLERGEN, newValue) }
/**