From 450ce92dca651e24afee6ba14cc92505137c4b43 Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Wed, 26 Apr 2023 18:36:32 +0200 Subject: [PATCH] Fixed ScheduleUpdateSheet.kt not updating and deleting events... somehow. Also implemented cheat codes hehe --- .idea/kotlinc.xml | 6 + .../com/denizk0461/studip/db/AppRepository.kt | 28 +++- .../studip/fragment/SettingsFragment.kt | 9 +- .../denizk0461/studip/sheet/DevCodeSheet.kt | 122 +++++++++++++++++- .../studip/sheet/ScheduleUpdateSheet.kt | 5 +- .../studip/viewmodel/SettingsViewModel.kt | 38 ++++++ app/src/main/res/drawable/engineering.xml | 9 ++ app/src/main/res/layout/sheet_dev_code.xml | 83 ++++-------- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 10 files changed, 233 insertions(+), 69 deletions(-) create mode 100644 .idea/kotlinc.xml create mode 100644 app/src/main/res/drawable/engineering.xml diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..69e8615 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/denizk0461/studip/db/AppRepository.kt b/app/src/main/java/com/denizk0461/studip/db/AppRepository.kt index ba2a887..1b371da 100644 --- a/app/src/main/java/com/denizk0461/studip/db/AppRepository.kt +++ b/app/src/main/java/com/denizk0461/studip/db/AppRepository.kt @@ -135,12 +135,32 @@ class AppRepository(app: Application) { * Deletes all canteen offers from the database. */ fun nukeOffers() { - dao.nukeOfferItems() - dao.nukeOfferCategories() - dao.nukeOfferCanteens() - dao.nukeOfferDates() + nukeOfferItems() + nukeOfferCategories() + nukeOfferCanteens() + nukeOfferDates() } + /** + * Deletes all canteen items from the database. + */ + fun nukeOfferItems() { dao.nukeOfferItems() } + + /** + * Deletes all canteen categories from the database. + */ + fun nukeOfferCategories() { dao.nukeOfferCategories() } + + /** + * Deletes all canteens from the database. + */ + fun nukeOfferCanteens() { dao.nukeOfferCanteens() } + + /** + * Deletes all canteen dates from the database. + */ + fun nukeOfferDates() { dao.nukeOfferDates() } + /** * Inserts a date into the database. * diff --git a/app/src/main/java/com/denizk0461/studip/fragment/SettingsFragment.kt b/app/src/main/java/com/denizk0461/studip/fragment/SettingsFragment.kt index abdf4c7..f3d0556 100644 --- a/app/src/main/java/com/denizk0461/studip/fragment/SettingsFragment.kt +++ b/app/src/main/java/com/denizk0461/studip/fragment/SettingsFragment.kt @@ -107,7 +107,14 @@ class SettingsFragment : AppFragment() { // Set long click listener for licences button to open dev code sheet binding.buttonLicences.setOnLongClickListener { - openBottomSheet(DevCodeSheet()) + openBottomSheet(DevCodeSheet( + viewModel::nukeEvents, + viewModel::nukeOfferItems, + viewModel::nukeOfferCategories, + viewModel::nukeOfferCanteens, + viewModel::nukeOfferDates, + viewModel::nukeEverything, + )) true } diff --git a/app/src/main/java/com/denizk0461/studip/sheet/DevCodeSheet.kt b/app/src/main/java/com/denizk0461/studip/sheet/DevCodeSheet.kt index a2314fa..b2a3f5c 100644 --- a/app/src/main/java/com/denizk0461/studip/sheet/DevCodeSheet.kt +++ b/app/src/main/java/com/denizk0461/studip/sheet/DevCodeSheet.kt @@ -1,10 +1,130 @@ package com.denizk0461.studip.sheet +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import android.util.Base64 +import android.view.View +import android.widget.Toast import com.denizk0461.studip.R +import com.denizk0461.studip.data.viewBinding +import com.denizk0461.studip.databinding.SheetDevCodeBinding +import java.nio.charset.StandardCharsets /** * Sheet that presents the user / developer with a text field to input developer codes / cheat * codes. + * + * @param nukeEvents deletes all events + * @param nukeOfferItems deletes all canteen items + * @param nukeOfferCategories deletes all canteen categories + * @param nukeOfferCanteens deletes all canteens + * @param nukeOfferDates deletes all canteen dates + * @param nukeEverything deletes everything */ -class DevCodeSheet : AppSheet(R.layout.sheet_dev_code) { +class DevCodeSheet( + private val nukeEvents: () -> Unit, + private val nukeOfferItems: () -> Unit, + private val nukeOfferCategories: () -> Unit, + private val nukeOfferCanteens: () -> Unit, + private val nukeOfferDates: () -> Unit, + private val nukeEverything: () -> Unit, +) : AppSheet(R.layout.sheet_dev_code) { + + // View binding + private val binding: SheetDevCodeBinding by viewBinding(SheetDevCodeBinding::bind) + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + // Execute an action based on the input + binding.buttonConfirm.setOnClickListener { + /* + * Codes are implemented here. + * + * About the inks; they are safe (which is suspicious of me to say), just encoded so + * people can't just look at the file and figure out all the codes! Of course, they're + * easily decoded, but who would go for that? You would have to be a pretty big nerd to + * do that. Then again, I was the one who encoded all of these links in the first place, + * so I suppose the joke's on me... + * + * If you're reading this – why? + */ + when (binding.input.text.toString().uppercase()) { + "U1RST0JF".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS90S2k5Wi1mNnFYNA==".d()) + "U1BJUklU".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS9XN25lMzlEU05TZw==".d()) + "Q0xPVURT".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS9rbUJ6YTRhNTB2dw==".d()) + "NjQxODJL".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS9EbTFyZ285UHBUcw==".d()) + "U0FJTE9S".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS9QUHlDYXZ6OG5NWQ==".d()) + "SE9ORVNU".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS9DSEg0OE5LUEFraw==".d()) + "QkJVT0s/".d() -> launchLink("aHR0cHM6Ly95b3V0dS5iZS90Rm1PMm1TY0tHNA==".d()) + "NEVENT" -> { // nuke events + nukeEvents() + showToast("Nuked all events") + } + "NCANTE" -> { // nuke canteens + nukeOfferCanteens() + showToast("Nuked all canteens") + } + "NDATES" -> { // nuke dates + nukeOfferDates() + showToast("Nuked all dates") + } + "NCATEG" -> { // nuke categories + nukeOfferCategories() + showToast("Nuked all categories") + } + "NITEMS" -> { // nuke items + nukeOfferItems() + showToast("Nuked all items") + } + "NEVERY" -> { // nuke everything + nukeEverything() + showToast("Nuked everything") + } + else -> showToast("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. + * + * @param link link to open + */ + private fun launchLink(link: String) { + // Let the user know they accomplished something with their life + showToast("✨") + + // Give the user a slight dopamine boost + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link))) + } + + /** + * Encodes a string to Base64. + * + * @return the encoded string + */ + private fun String.e(): String = Base64.encodeToString(this.toByteArray(), Base64.DEFAULT) + + + /** + * Decodes Base64 to a regular string. + * + * @return the decoded string + */ + private fun String.d(): String = String( + Base64.decode(this, Base64.DEFAULT), + StandardCharsets.UTF_8 + ) } \ No newline at end of file diff --git a/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt b/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt index 4a68447..9d69f09 100644 --- a/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt +++ b/app/src/main/java/com/denizk0461/studip/sheet/ScheduleUpdateSheet.kt @@ -1,6 +1,7 @@ package com.denizk0461.studip.sheet import android.os.Bundle +import android.util.Log import android.view.View import androidx.fragment.app.FragmentActivity import com.denizk0461.studip.R @@ -104,8 +105,8 @@ class ScheduleUpdateSheet( * @param isEventStart whether this timestamp is for the start of the course */ override fun onTimeSet(hours: Int, minutes: Int, isEventStart: Boolean) { - // Parse the separate ints into a timestamp string - val newTimestamp = "$hours:$minutes" + // Parse the separate ints into a timestamp string. Add a leading zero if necessary. + val newTimestamp = "$hours:${String.format("%02d", minutes)}" // If start > end, show an error if ((isEventStart && newTimestamp.parseToMinutes() > timeslotEnd.parseToMinutes()) || diff --git a/app/src/main/java/com/denizk0461/studip/viewmodel/SettingsViewModel.kt b/app/src/main/java/com/denizk0461/studip/viewmodel/SettingsViewModel.kt index a7c2104..24caf91 100644 --- a/app/src/main/java/com/denizk0461/studip/viewmodel/SettingsViewModel.kt +++ b/app/src/main/java/com/denizk0461/studip/viewmodel/SettingsViewModel.kt @@ -10,6 +10,44 @@ import com.denizk0461.studip.model.SettingsPreferences */ class SettingsViewModel(app: Application) : AppViewModel(app) { + + + /** + * Deletes everything from the database. + */ + fun nukeEverything() { + nukeEvents() + nukeOfferItems() + nukeOfferCategories() + nukeOfferCanteens() + nukeOfferDates() + } + + /** + * Deletes all Stud.IP events from the database. + */ + fun nukeEvents() { doAsync { repo.nukeEvents() } } + + /** + * Deletes all canteen items from the database. + */ + fun nukeOfferItems() { doAsync { repo.nukeOfferItems() } } + + /** + * Deletes all canteen categories from the database. + */ + fun nukeOfferCategories() { doAsync { repo.nukeOfferCategories() } } + + /** + * Deletes all canteens from the database. + */ + fun nukeOfferCanteens() { doAsync { repo.nukeOfferCanteens() } } + + /** + * Deletes all canteen dates from the database. + */ + fun nukeOfferDates() { doAsync { repo.nukeOfferDates() } } + /** * This value determines whether the user wants to have the next course in their schedule * highlighted. diff --git a/app/src/main/res/drawable/engineering.xml b/app/src/main/res/drawable/engineering.xml new file mode 100644 index 0000000..68fa52f --- /dev/null +++ b/app/src/main/res/drawable/engineering.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/app/src/main/res/layout/sheet_dev_code.xml b/app/src/main/res/layout/sheet_dev_code.xml index 7c3d378..8e9f91d 100644 --- a/app/src/main/res/layout/sheet_dev_code.xml +++ b/app/src/main/res/layout/sheet_dev_code.xml @@ -21,75 +21,36 @@ android:layout_marginBottom="8dp" android:text="@string/sheet_cheat_header"/> - + android:paddingHorizontal="24dp"> + android:gravity="center" + android:fontFamily="serif-monospace" + android:inputType="textNoSuggestions" + android:textSize="24sp"/> - + - - - - - - - - - + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 9347197..862cd2a 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -14,6 +14,7 @@ Speichern Löschen Abbrechen + Bestätigen Veranstaltung bearbeiten Veranstaltungstitel Dozierende(r) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 783903d..09537be 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,7 @@ Save Delete Cancel + Confirm Edit Event Event title Lecturer(s)