(re-)added option to launch timetable on the current day; reinstated functionality to launch app on CanteenFragment.kt

This commit is contained in:
denizk0461
2023-05-04 08:32:12 +02:00
parent 9e5277a5f6
commit 3d508dff6c
16 changed files with 183 additions and 25 deletions
@@ -24,6 +24,7 @@ class FetcherActivity : FragmentActivity() {
// View binding
private lateinit var binding: ActivityFetcherBinding
// View model
private lateinit var viewModel: FetcherViewModel
@@ -36,7 +37,9 @@ class FetcherActivity : FragmentActivity() {
super.onCreate(savedInstanceState)
// Instantiate view model
viewModel = ViewModelProvider.AndroidViewModelFactory(this.application).create(FetcherViewModel::class.java)
viewModel = ViewModelProvider.AndroidViewModelFactory(application)
.create(FetcherViewModel::class.java)
// Inflate view binding and bind to this activity
binding = ActivityFetcherBinding.inflate(layoutInflater)
setContentView(binding.root)
@@ -3,10 +3,13 @@ package com.denizk0461.studip.activity
import android.os.Bundle
import androidx.core.view.WindowCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.denizk0461.studip.R
import com.denizk0461.studip.databinding.ActivityMainBinding
import com.denizk0461.studip.viewmodel.MainViewModel
/**
* Main activity that handles all common fragments. This is opened on app launch.
@@ -16,10 +19,17 @@ class MainActivity : FragmentActivity() {
// View binding
private lateinit var binding: ActivityMainBinding
// View model
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
// Instantiate view model
viewModel = ViewModelProvider.AndroidViewModelFactory(application)
.create(MainViewModel::class.java)
// Inflate view binding and bind to this activity
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
@@ -28,5 +38,25 @@ class MainActivity : FragmentActivity() {
binding.navView.setupWithNavController(
binding.navHostFragmentContentMain.getFragment<NavHostFragment>().findNavController()
)
setNavigationGraph()
}
/**
* Sets the navigation graph for the fragments. Also sets the start destination.
*/
private fun setNavigationGraph() {
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment_content_main) as NavHostFragment
val navController = navHostFragment.navController
val navGraph = navController.navInflater.inflate(R.navigation.main_nav_graph)
navGraph.setStartDestination(if (viewModel.preferenceLaunchCanteen) {
R.id.canteen
} else {
R.id.schedule
})
navController.graph = navGraph
}
}
@@ -33,9 +33,6 @@ class EventFragment : AppFragment() {
// View model reference for providing access to the database
private val viewModel: EventViewModel by viewModels()
// Used to determine the current day
private var dayOfWeek: Int = 0
// Titles for the view pager's tabs
private lateinit var weekdays: Array<String>
@@ -51,17 +48,6 @@ class EventFragment : AppFragment() {
// Get localised weekday names
weekdays = context.resources?.getStringArray(R.array.weekdays) ?: arrayOf()
// Determine the current day
dayOfWeek = when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
Calendar.TUESDAY -> 1
Calendar.WEDNESDAY -> 2
Calendar.THURSDAY -> 3
Calendar.FRIDAY -> 4
Calendar.SATURDAY -> 5
Calendar.SUNDAY -> 6
else -> 0 // assume Monday
}
// Set up the view pager's adapter
viewPagerAdapter = StudIPEventPageAdapter(
childFragmentManager,
@@ -76,6 +62,27 @@ class EventFragment : AppFragment() {
tab.text = weekdays[position]
}.attach()
// Set the view pager's current page to the current day, if the user chose this option
if (viewModel.preferenceCurrentDay) {
/*
* Determine the current day and go to the respective page.
* BUG: this selects the correct tab and changes the page, but it doesn't highlight
* the tab that has been selected. binding.viewPager.currentPage encounters the same bug
*/
binding.dayTabLayout.getTabAt(
when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
Calendar.TUESDAY -> 1
Calendar.WEDNESDAY -> 2
Calendar.THURSDAY -> 3
Calendar.FRIDAY -> 4
Calendar.SATURDAY -> 5
Calendar.SUNDAY -> 6
else -> 0 // assume Monday
}
)?.select()
}
binding.fabAddEvent.setOnClickListener {
openBottomSheet(
ScheduleUpdateSheet().also { sheet ->
@@ -58,6 +58,14 @@ class SettingsFragment : AppFragment() {
launchWebView()
}
// Set up switch for highlighting the next course in the schedule
binding.switchCurrentDay.apply {
isChecked = viewModel.preferenceCurrentDay
setOnCheckedChangeListener { _, newValue ->
viewModel.preferenceCurrentDay = newValue
}
}
// Set up switch for highlighting the next course in the schedule
binding.switchHighlight.apply {
isChecked = viewModel.preferenceCourseHighlighting
@@ -18,6 +18,11 @@ enum class SettingsPreferences(val key: String) {
*/
ALLERGEN("setting_allergen"),
/**
* Whether the user wants their timetable on the current day.
*/
CURRENT_DAY("settings_current_day"),
/**
* Whether the user wants the next course in his schedule to be highlighted.
*/
@@ -120,7 +120,6 @@ class DevCodeSheet : AppSheet(R.layout.sheet_dev_code) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))
}
/**
* Decodes Base64 to a regular string.
*
@@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.viewModels
import androidx.transition.TransitionManager
@@ -171,11 +172,20 @@ class ScheduleUpdateSheet : AppSheet(R.layout.sheet_schedule_update), TimePicker
* filter buttons with checkmarks when they're selected maybe?
*/
// Set visibility for academic quarter button based on whether the change applies
binding.buttonAcademicQuarter.visibility = getVisibilityForAcademicQuarter(
timeslotsForAcademicQuarter.contains(timeslotStart),
timeslotsForAcademicQuarter.contains(timeslotEnd),
)
// Set title text field to remove error messages when the user edits the text
binding.editTextTitle.addTextChangedListener {
binding.textLayoutTitle.error = null
}
// Set lecturers text field to remove error messages when the user edits the text
binding.editTextLecturers.addTextChangedListener {
binding.textLayoutLecturers.error = null
}
// Set room text field to remove error messages when the user edits the text
binding.editTextRoom.addTextChangedListener {
binding.textLayoutRoom.error = null
}
// Retrieve new values for the day of the current event
binding.buttonDay.text = getString(when (selectedDay) {
@@ -226,6 +236,12 @@ class ScheduleUpdateSheet : AppSheet(R.layout.sheet_schedule_update), TimePicker
*
* 13:00 - 15:30 -> likely no academic quarter intended
*/
binding.buttonAcademicQuarter.visibility = getVisibilityForAcademicQuarter(
timeslotsForAcademicQuarter.contains(timeslotStart),
timeslotsForAcademicQuarter.contains(timeslotEnd),
)
// Set up academic quarter button
binding.buttonAcademicQuarter.setOnClickListener {
try {
// Retrieve new timestamps with the academic quarter applied, or throw a tantrum
@@ -1,10 +1,21 @@
package com.denizk0461.studip.viewmodel
import android.app.Application
import com.denizk0461.studip.model.SettingsPreferences
/**
* View model for [com.denizk0461.studip.fragment.EventFragment]
*
* @param app reference to the app
*/
class EventViewModel(app: Application) : AppViewModel(app)
class EventViewModel(app: Application) : AppViewModel(app) {
/**
* This value determines whether the user wants their timetable to launch with the current day.
*/
val preferenceCurrentDay: Boolean
get() = repo.getBooleanPreference(
SettingsPreferences.CURRENT_DAY,
defaultValue = true
)
}
@@ -0,0 +1,18 @@
package com.denizk0461.studip.viewmodel
import android.app.Application
import com.denizk0461.studip.model.SettingsPreferences
/**
* View model for [com.denizk0461.studip.activity.MainActivity]
*
* @param application reference to the app
*/
class MainViewModel(application: Application) : AppViewModel(application) {
/**
* This value determines whether the user wants the app to launch with the canteen view.
*/
val preferenceLaunchCanteen: Boolean
get() = repo.getBooleanPreference(SettingsPreferences.LAUNCH_CANTEEN_ON_START)
}
@@ -10,6 +10,15 @@ import com.denizk0461.studip.model.SettingsPreferences
*/
class SettingsViewModel(app: Application) : AppViewModel(app) {
/**
* This value determines whether the user wants their timetable to launch with the current day.
*/
var preferenceCurrentDay: Boolean
get() = repo.getBooleanPreference(
SettingsPreferences.CURRENT_DAY,
)
set(newValue) { repo.setPreference(SettingsPreferences.CURRENT_DAY, newValue) }
/**
* This value determines whether the user wants to have the next course in their schedule
* highlighted.
+1 -1
View File
@@ -13,11 +13,11 @@
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/main_nav_graph"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<!-- app:navGraph="@navigation/main_nav_graph"-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
@@ -94,6 +94,49 @@
</androidx.appcompat.widget.LinearLayoutCompat>
<!-- Highlight course -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_current_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
android:paddingVertical="8dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/switch_current_day"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_bolditalic"
android:text="@string/settings_current_day_title"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_current_day_subtitle"
android:textSize="16sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switch_current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- Highlight course -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_highlight"
+3
View File
@@ -150,6 +150,9 @@
<string name="refresh_schedule_title">Aktualisiere Deinen Stud.IP-Stundenplan</string>
<string name="refresh_schedule_subtitle">Dies wird alle Anpassungen löschen!</string>
<string name="settings_current_day_title">Starte Stundenplan am heutigen Tag</string>
<string name="settings_current_day_subtitle">Ansonsten wird Montag beim Start angezeigt</string>
<string name="settings_highlight_title">Markiere den nächsten Kurs</string>
<string name="settings_highlight_subtitle">Funktioniert nur, wenn Kurse sich nicht überschneiden!</string>
+1
View File
@@ -31,6 +31,7 @@
<item name="colorErrorText">@color/md_theme_dark_error</item>
<item name="android:textColor">@color/md_theme_dark_onSurfaceVariant</item>
<item name="android:textColorHighlight">@color/md_theme_dark_primaryContainer</item>
<item name="colorText">@color/md_theme_dark_onSurfaceVariant</item>
<item name="android:scrollbarThumbVertical">@color/md_theme_dark_primary</item> <!-- md_theme_dark_onSurfaceVariant -->
<item name="colorOutlineVariant">@color/md_theme_dark_outlineVariant</item>
+6 -2
View File
@@ -154,6 +154,9 @@
<string name="refresh_schedule_title">Refresh your Stud.IP schedule</string>
<string name="refresh_schedule_subtitle">This will clear any customisations!</string>
<string name="settings_current_day_title">Launch timetable on current day</string>
<string name="settings_current_day_subtitle">If unselected, Monday will be shown on launch instead</string>
<string name="settings_highlight_title">Highlight next course</string>
<string name="settings_highlight_subtitle">Only works properly if courses don\'t overlap!</string>
@@ -167,10 +170,10 @@
<string name="settings_allergens_subtitle">Also applies to additives</string>
<string name="settings_pref_colour_title">Colour dietary preferences</string>
<string name="settings_pref_colour_subtitle">This colours the icons similar to how they look the website</string> <!-- TODO this text sucks, DE too -->
<string name="settings_pref_colour_subtitle">Icons will be coloured similarly to how they look the website</string> <!-- TODO this text sucks, DE too -->
<string name="settings_launch_canteen_title">Show canteen plan on app launch</string>
<string name="settings_launch_canteen_subtitle">Otherwise your schedule will be shown at launch; CURRENTLY BROKEN</string>
<string name="settings_launch_canteen_subtitle">Otherwise your schedule will be shown at launch</string>
<string name="settings_crashlytics_title">Opt-in to sharing crash data</string>
<string name="settings_crashlytics_subtitle">Data will be anonymised</string> <!-- TODO is this true? -->
@@ -203,5 +206,6 @@
<item>if we only had one more day, what is the last thing you\'d say?</item>
<item>love with your heart, use your head for everything else</item>
<item>trans rights are human rights 🏳️‍⚧️</item>
<item>i wouldn\'t risk any more than six minutes</item>
</string-array>
</resources>
+1
View File
@@ -29,6 +29,7 @@
<item name="colorPrimaryInverse">@color/md_theme_light_inversePrimary</item>
<item name="android:textColor">@color/md_theme_light_onSurfaceVariant</item>
<item name="android:textColorHighlight">@color/md_theme_light_primaryContainer</item>
<item name="colorText">@color/md_theme_light_onSurfaceVariant</item>
<item name="android:scrollbarThumbVertical">@color/md_theme_light_primary</item> <!-- md_theme_light_onSurfaceVariant -->
<item name="colorOutlineVariant">@color/md_theme_light_outlineVariant</item>