Transitioned from a fairly custom solution to using a colour palette generated by the Material Theme Builder to simplify colour management. Also began work on a bottom sheet for displaying allergens and additives in canteen offers.

This commit is contained in:
denizk0461
2023-04-23 17:13:48 +02:00
parent fbad84aa09
commit b957172721
42 changed files with 456 additions and 273 deletions
@@ -8,16 +8,37 @@ import com.denizk0461.studip.databinding.ItemCanteenBinding
import com.denizk0461.studip.databinding.ItemCanteenLineBinding
import com.denizk0461.studip.databinding.ItemIconBinding
import com.denizk0461.studip.model.CanteenOfferGroup
import com.denizk0461.studip.model.CanteenOfferGroupElement
/**
* Custom RecyclerView adapter that lays out the offers of a given canteen on a given day.
*
* @param offers list of all offers filtered by canteen and day, grouped by category
* @param onClickListener used for listening to clicks and long presses
*/
class CanteenOfferItemAdapter(
private val offers: List<CanteenOfferGroup>
private val offers: List<CanteenOfferGroup>,
private val onClickListener: OnClickListener
) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
/**
* Provides a converting function between the index of a dietary preference and its according
* icon.
*/
private val indexToDrawable: Map<Int, Int> = mapOf(
0 to R.drawable.handshake,
1 to R.drawable.fish,
2 to R.drawable.chicken,
3 to R.drawable.sheep,
4 to R.drawable.yoga,
5 to R.drawable.cow,
6 to R.drawable.pig,
7 to R.drawable.leaf,
8 to R.drawable.carrot,
9 to R.drawable.deer,
10 to R.drawable.circle,
)
/**
* View holder class for parent class
*
@@ -86,26 +107,39 @@ class CanteenOfferItemAdapter(
line.imageViewContainer.addView(img.root)
}
// Set up single click listener
line.root.setOnClickListener {
onClickListener.onClick(offer)
}
// Set up long press listener
line.root.setOnLongClickListener {
onClickListener.onLongClick(offer)
}
// Bind the new line to the line container
holder.binding.lineContainer.addView(line.root)
}
}
/**
* Provides a converting function between the index of a dietary preference and its according
* icon.
* Interface used to evaluate clicks and long presses on a given item.
*/
private val indexToDrawable: Map<Int, Int> = mapOf(
0 to R.drawable.handshake,
1 to R.drawable.fish,
2 to R.drawable.chicken,
3 to R.drawable.sheep,
4 to R.drawable.yoga,
5 to R.drawable.cow,
6 to R.drawable.pig,
7 to R.drawable.leaf,
8 to R.drawable.carrot,
9 to R.drawable.deer,
10 to R.drawable.circle,
)
interface OnClickListener {
/**
* Executed when an item has been clicked.
*
* @param offer item that has been clicked
*/
fun onClick(offer: CanteenOfferGroupElement)
/**
* Executed when an item has been long-pressed.
*
* @param offer item that has been long-pressed
* @return whether the long press was successful
*/
fun onLongClick(offer: CanteenOfferGroupElement): Boolean
}
}
@@ -16,10 +16,12 @@ import com.denizk0461.studip.model.CanteenOfferGroup
* @param daysCovered tells for how many days offers are available for.
* Example: if the next two weeks are available, Monday through Friday and
* excluding weekends, this value should be 10.
* @param onClickListener for managing click and long press events
*/
class CanteenOfferPageAdapter(
private var offers: List<CanteenOfferGroup>,
private var daysCovered: Int,
private val onClickListener: CanteenOfferItemAdapter.OnClickListener,
) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
/**
@@ -50,8 +52,12 @@ class CanteenOfferPageAdapter(
/*
* Create new adapter for every page. Attribute position denotes day that will be set up
* by the newly created adapter (0 = Monday, 4 = Friday)
* TODO check if the filtered list is empty, and tell the user if it is
*/
adapter = CanteenOfferItemAdapter(offers.filter { it.dateId == position }) // TODO check if empty
adapter = CanteenOfferItemAdapter(
offers.filter { it.dateId == position },
onClickListener
)
// Animate creation of new page
scheduleLayoutAnimation()
@@ -1,11 +1,8 @@
package com.denizk0461.studip.adapter
import android.content.res.ColorStateList
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.denizk0461.studip.R
import com.denizk0461.studip.model.StudIPEvent
import com.denizk0461.studip.databinding.ItemEventBinding
import java.util.*
@@ -72,42 +69,43 @@ class StudIPEventItemAdapter(
// Use parsed timeslot string as defined in StudIPEvent#timeslot()
holder.binding.textTimeslot.text = currentItem.timeslot()
// TODO highlighting functionality is currently not supported!
// Set up colours used for highlighting and un-highlighting an item.
val colorPrimaryTranslucent = TypedValue()
val colorCardBackground = TypedValue()
val colorTextHintLighter = TypedValue()
// val colorPrimaryTranslucent = TypedValue()
// val colorCardBackground = TypedValue()
// val colorTextHintLighter = TypedValue()
// Resolve themed attributes to get the right values for day and night modes
holder.binding.root.context.theme.apply {
resolveAttribute(R.attr.colorPrimaryTranslucent, colorPrimaryTranslucent, true)
resolveAttribute(R.attr.colorCardBackground, colorCardBackground, true)
resolveAttribute(R.attr.colorTextHintLighter, colorTextHintLighter, true)
}
// holder.binding.root.context.theme.apply {
// resolveAttribute(com.google.android.material.R.attr.colorSecondaryContainer, colorPrimaryTranslucent, true)
// resolveAttribute(R.attr.colorCardBackground, colorCardBackground, true)
// resolveAttribute(R.attr.colorTextHintLighter, colorTextHintLighter, true)
// }
/* Highlight the next upcoming course of the day if the day of the adapter matches the
* current day, and if no other course has been highlighted, to avoid double highlighting.
*/
if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) {
// Ensure that no other course will be highlighted
isAnyCourseHighlighted = true
holder.binding.cardBackground.apply {
// Set the card's background colour to a desaturated shade of the primary colour
backgroundTintList = ColorStateList.valueOf(colorPrimaryTranslucent.data)
// Hide card stroke
strokeColor = context.getColor(android.R.color.transparent)
}
// Otherwise, apply colours to ensure that the item will not be highlighted
} else {
holder.binding.cardBackground.apply {
// Set the card's background colour to its default value
backgroundTintList = ColorStateList.valueOf(colorCardBackground.data)
// Set the card's stroke to its default colour
strokeColor = colorTextHintLighter.data
}
}
// if (!isAnyCourseHighlighted && currentItem.isCurrentCourse(currentCalendar)) {
// // Ensure that no other course will be highlighted
// isAnyCourseHighlighted = true
//
// holder.binding.cardBackground.apply {
// // Set the card's background colour to a desaturated shade of the primary colour
// backgroundTintList = ColorStateList.valueOf(colorPrimaryTranslucent.data)
//
// // Hide card stroke
// strokeColor = context.getColor(android.R.color.transparent)
// }
// // Otherwise, apply colours to ensure that the item will not be highlighted
// } else {
// holder.binding.cardBackground.apply {
// // Set the card's background colour to its default value
// backgroundTintList = ColorStateList.valueOf(colorCardBackground.data)
//
// // Set the card's stroke to its default colour
// strokeColor = colorTextHintLighter.data
// }
// }
// Set up single click listener
holder.binding.cardBackground.setOnClickListener {
@@ -16,7 +16,7 @@ import com.denizk0461.studip.databinding.ItemScrollablePageBinding
*/
class StudIPEventPageAdapter(
private var events: List<StudIPEvent>,
private val onClickListener: StudIPEventItemAdapter.OnClickListener
private val onClickListener: StudIPEventItemAdapter.OnClickListener,
) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
/**
@@ -0,0 +1,49 @@
package com.denizk0461.studip.data
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
// written by Zhuinden (props to them!)
class FragmentViewBindingDelegate<T : ViewBinding>(
val fragment: Fragment,
val viewBindingFactory: (View) -> T
) : ReadOnlyProperty<Fragment, T> {
private var _binding: T? = null
init {
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewLifecycleOwner ->
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
_binding = null
}
})
}
}
})
}
override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
val binding = _binding
if (binding != null) {
return binding
}
val lifecycle = fragment.viewLifecycleOwner.lifecycle
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
throw IllegalStateException("Should not attempt to get bindings when Fragment views are destroyed.")
}
return viewBindingFactory(thisRef.requireView()).also { _binding = it }
}
}
fun <T : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> T) =
FragmentViewBindingDelegate(this, viewBindingFactory)
@@ -209,9 +209,9 @@ class StwParser {
* pointless to the website user. As of now, this information is discarded in the app.
* TODO implement allergen functionality
*/
while (text.contains("<sup>")) {
text = text.substring(0 until text.indexOf("<sup>")) + text.substring(text.indexOf("</sup>")+6 until text.length)
}
// while (text.contains("<sup>")) {
// text = text.substring(0 until text.indexOf("<sup>")) + text.substring(text.indexOf("</sup>")+6 until text.length)
// }
// Return the stripped and updated HTML string
return text
@@ -0,0 +1,21 @@
package com.denizk0461.studip.fragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.denizk0461.studip.sheet.AppSheet
/**
* Fragment super class providing common functionality. Fragments are used for providing the user
* with different views and functionality. All fragments should inherit from this.
*/
open class AppFragment : Fragment() {
/**
* Opens a bottom sheet. Sheet must be a subclass of [com.denizk0461.studip.sheet.AppSheet].
*
* @param sheet element that will be displayed
*/
protected fun openBottomSheet(sheet: AppSheet) {
sheet.show((context as FragmentActivity).supportFragmentManager, sheet.javaClass.simpleName)
}
}
@@ -5,11 +5,12 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.denizk0461.studip.adapter.CanteenOfferItemAdapter
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
import com.denizk0461.studip.databinding.FragmentCanteenBinding
import com.denizk0461.studip.model.*
import com.denizk0461.studip.sheet.AllergenSheet
import com.denizk0461.studip.viewmodel.CanteenViewModel
import com.google.android.material.tabs.TabLayoutMediator
@@ -17,7 +18,7 @@ import com.google.android.material.tabs.TabLayoutMediator
* User-facing fragment view that displays the canteen offers from the website of the
* Studierendenwerk Bremen.
*/
class CanteenFragment : Fragment() {
class CanteenFragment : AppFragment(), CanteenOfferItemAdapter.OnClickListener {
// Nullable view binding reference
private var _binding: FragmentCanteenBinding? = null
@@ -91,7 +92,7 @@ class CanteenFragment : Fragment() {
}
// Set up the view pager's adapter
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0)
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0, this)
// Assign the adapter to the view pager
binding.viewPager.adapter = viewPagerAdapter
@@ -268,4 +269,23 @@ class CanteenFragment : Fragment() {
)
}
}
/**
* Executed when an item has been clicked.
*
* @param offer item that has been clicked
*/
override fun onClick(offer: CanteenOfferGroupElement) {
openBottomSheet(AllergenSheet(offer))
}
/**
* Executed when an item has been long-pressed.
*
* @param offer item that has been long-pressed
* @return whether the long press was successful
*/
override fun onLongClick(offer: CanteenOfferGroupElement): Boolean {
return false
}
}
@@ -1,7 +1,6 @@
package com.denizk0461.studip.fragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -18,7 +17,7 @@ import java.util.*
/**
* User-facing fragment view that displays the user's Stud.IP schedule.
*/
class EventFragment : Fragment() {
class EventFragment : AppFragment() {
// Nullable view binding reference
private var _binding: FragmentEventBinding? = null
@@ -7,7 +7,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.denizk0461.studip.BuildConfig
import com.denizk0461.studip.activity.FetcherActivity
import com.denizk0461.studip.databinding.FragmentSettingsBinding
@@ -15,7 +14,7 @@ import com.denizk0461.studip.databinding.FragmentSettingsBinding
/**
* User-facing fragment view that is used to change app settings.
*/
class SettingsFragment : Fragment() {
class SettingsFragment : AppFragment() {
// Nullable view binding reference
private var _binding: FragmentSettingsBinding? = null
@@ -0,0 +1,22 @@
package com.denizk0461.studip.sheet
import android.os.Bundle
import android.view.View
import com.denizk0461.studip.R
import com.denizk0461.studip.data.viewBinding
import com.denizk0461.studip.databinding.SheetAllergenBinding
import com.denizk0461.studip.model.CanteenOfferGroupElement
class AllergenSheet(private val offer: CanteenOfferGroupElement) : AppSheet(R.layout.sheet_allergen) {
private val binding: SheetAllergenBinding by viewBinding(SheetAllergenBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.apply {
title.text = offer.title
textContent.text = "hello there"
}
}
}
@@ -0,0 +1,33 @@
package com.denizk0461.studip.sheet
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
/**
* Bottom sheet super class providing common functionality. All bottom sheets should inherit from
* this.
*
* @param layoutId ID of the layout of the bottom sheet
*/
open class AppSheet(@LayoutRes private val layoutId: Int) : BottomSheetDialogFragment() {
// Internal context object
private lateinit var _context: Context
// Get non-null context. Only valid after onAttach()
override fun getContext(): Context = _context
override fun onAttach(context: Context) {
super.onAttach(context)
_context = context
}
// Inflate the given layout
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
LayoutInflater.from(context).inflate(layoutId, container, false)
}
@@ -16,7 +16,7 @@ import kotlinx.coroutines.runBlocking
*
* @param app reference to the app
*/
open class TemplateViewModel(app: Application) : AndroidViewModel(app) {
open class AppViewModel(app: Application) : AndroidViewModel(app) {
// Reference to the app's repository for database transactions
protected val repo: AppRepository = Dependencies.repo
@@ -13,7 +13,7 @@ import com.denizk0461.studip.model.OfferDate
*
* @param app reference to the app
*/
class CanteenViewModel(app: Application) : TemplateViewModel(app) {
class CanteenViewModel(app: Application) : AppViewModel(app) {
// Instantiate a parser, should the user want to refresh the canteen offers
private val parser = StwParser()
@@ -9,7 +9,7 @@ import com.denizk0461.studip.model.StudIPEvent
*
* @param app reference to the app
*/
class EventViewModel(app: Application) : TemplateViewModel(app) {
class EventViewModel(app: Application) : AppViewModel(app) {
/**
* Retrieves all Stud.IP events.
@@ -8,7 +8,7 @@ import com.denizk0461.studip.model.StudIPEvent
*
* @param app reference to the app
*/
class FetcherViewModel(app: Application) : TemplateViewModel(app) {
class FetcherViewModel(app: Application) : AppViewModel(app) {
/**
* Save a list of Stud.IP events to persistent storage asynchronously.