diff --git a/app/src/main/java/com/denizk0461/studip/adapter/CanteenOfferItemAdapter.kt b/app/src/main/java/com/denizk0461/studip/adapter/CanteenOfferItemAdapter.kt index 0d2295f..d9dc0fe 100644 --- a/app/src/main/java/com/denizk0461/studip/adapter/CanteenOfferItemAdapter.kt +++ b/app/src/main/java/com/denizk0461/studip/adapter/CanteenOfferItemAdapter.kt @@ -4,6 +4,7 @@ import android.view.LayoutInflater import android.view.ViewGroup import androidx.appcompat.content.res.AppCompatResources import androidx.recyclerview.widget.RecyclerView +import com.denizk0461.studip.R import com.denizk0461.studip.databinding.ItemCanteenBinding import com.denizk0461.studip.databinding.ItemCanteenLineBinding import com.denizk0461.studip.databinding.ItemIconBinding @@ -44,68 +45,102 @@ class CanteenOfferItemAdapter( // Returns the amount of offers for a given canteen and day override fun getItemCount(): Int = offers.size - // TODO inflate a view saying "no events" if none are available for a given day override fun onBindViewHolder(holder: OfferViewHolder, position: Int) { // Retrieve item for current position val currentItem = offers[position] - // Set category text / header of the item - holder.binding.textCategory.text = currentItem.category + // If an item has been added to mark that there are no offers available, tell the user + if (currentItem.category == "NO\$ITEMS") { - // Create a new view (line) for each item displayed within one category - currentItem.offers.forEach { offer -> - // Inflate the new line without binding it to the line container + // Set the text to "No offers" (localised) + holder.binding.textCategory.text = + holder.binding.root.context.getString(R.string.canteen_no_offer_header) + + // Inflate a single line without binding it to the line container val line = ItemCanteenLineBinding.inflate( LayoutInflater.from(holder.binding.root.context), ) - // Set text values - line.textContent.text = offer.title.addAllergenNotice(displayAllergens, offer.allergens) - line.textPrice.text = offer.price + // Set content to tell the user that no offers are available (localised) + line.textContent.text = + holder.binding.root.context.getString(R.string.canteen_no_offer_desc) - /* - * Check which dietary preferences are met by this item. This is used to set visual - * icons on each line. Example: a leaf on vegan items. - */ - val indices = arrayListOf() - offer.dietaryPreferences.filterIndexed { index, char -> - if (char == 't') { - indices.add(index) - true - } else false - } + // Inflate a new icon holder + val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context)) - // If no dietary preferences are met, set the view to display a neutral icon - if (indices.isEmpty()) indices.add(10) - - // Iterate through all icons that need to be displayed - indices.forEach { index -> - // Inflate a new icon holder - val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context)) - - // Set the appropriate icon - img.imageView.setImageDrawable( - AppCompatResources.getDrawable( - holder.binding.root.context, - DietaryPreferences.indexToDrawable[index]!!, - ) + // Set a cross icon + img.imageView.setImageDrawable( + AppCompatResources.getDrawable( + holder.binding.root.context, + DietaryPreferences.indexToDrawable[11]!!, ) + ) - // Bind the new icon to the line view - line.imageViewContainer.addView(img.root) - } + // Bind the new icon to the line view + line.imageViewContainer.addView(img.root) - // Set up single click listener - line.root.setOnClickListener { - onClickListener.onClick(offer, currentItem.category) - } - - // 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) + + } else { // Proceed normally if items are available + // Set category text / header of the item + holder.binding.textCategory.text = currentItem.category + + // Create a new view (line) for each item displayed within one category + currentItem.offers.forEach { offer -> + // Inflate the new line without binding it to the line container + val line = ItemCanteenLineBinding.inflate( + LayoutInflater.from(holder.binding.root.context), + ) + + // Set text values + line.textContent.text = offer.title.addAllergenNotice(displayAllergens, offer.allergens) + line.textPrice.text = offer.price + + /* + * Check which dietary preferences are met by this item. This is used to set visual + * icons on each line. Example: a leaf on vegan items. + */ + val indices = arrayListOf() + offer.dietaryPreferences.filterIndexed { index, char -> + if (char == 't') { + indices.add(index) + true + } else false + } + + // If no dietary preferences are met, set the view to display a neutral icon + if (indices.isEmpty()) indices.add(10) + + // Iterate through all icons that need to be displayed + indices.forEach { index -> + // Inflate a new icon holder + val img = ItemIconBinding.inflate(LayoutInflater.from(holder.binding.root.context)) + + // Set the appropriate icon + img.imageView.setImageDrawable( + AppCompatResources.getDrawable( + holder.binding.root.context, + DietaryPreferences.indexToDrawable[index]!!, + ) + ) + + // Bind the new icon to the line view + line.imageViewContainer.addView(img.root) + } + + // Set up single click listener + line.root.setOnClickListener { + onClickListener.onClick(offer, currentItem.category) + } + + // 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) + } } } diff --git a/app/src/main/java/com/denizk0461/studip/data/StwParser.kt b/app/src/main/java/com/denizk0461/studip/data/StwParser.kt index a064a23..338ca62 100644 --- a/app/src/main/java/com/denizk0461/studip/data/StwParser.kt +++ b/app/src/main/java/com/denizk0461/studip/data/StwParser.kt @@ -1,5 +1,6 @@ package com.denizk0461.studip.data +import android.util.Log import com.denizk0461.studip.db.AppRepository import com.denizk0461.studip.model.* import org.jsoup.Jsoup @@ -24,6 +25,9 @@ class StwParser { // Unique primary key value for the item elements private var itemId = 0 + // Determine whether there are offers for a given date + private var dateHasItems = false + /** * Parses through a list of canteen plans and saves them to persistent storage. * @@ -137,11 +141,18 @@ class StwParser { */ repo.insert(OfferDate(dateId, date)) + dateHasItems = false + // Iterate through all categories offered on a given day dayPlan.getElementsByClass("food-category").forEach { category -> + + dateHasItems = true + // Retrieve the category text val categoryTitle = category.getElementsByClass("category-name")[0].text() + Log.d("AAA?", "$date on $categoryTitle") + // Save the category to persistent storage repo.insert(OfferCategory(categoryId, dateId, canteenId, categoryTitle)) @@ -184,16 +195,38 @@ class StwParser { ) ) - // Increment the item ID to avoid overriding + // Increment the item ID to avoid conflict itemId += 1 } - // Increment the category ID to avoid overriding + // Increment the category ID to avoid conflict categoryId += 1 } - // Increment the date ID to avoid overriding + + /* + * If no items were found for the day, insert items that will tell the user that nothing + * is available. Text will then be handled in the adapter class. + */ + if (!dateHasItems) { + repo.insert(OfferCategory(categoryId, dateId, canteenId, "NO\$ITEMS")) + repo.insert( + OfferItem( + itemId, + categoryId, + title = "NO\$ITEMS", + price = "", + dietaryPreferences = "..........", + allergens = "", + ) + ) + // Increment the IDs to avoid conflict + categoryId += 1 + itemId += 1 + } + + // Increment the date ID to avoid conflict dateId += 1 } - // Increment the canteen ID to avoid overriding + // Increment the canteen ID to avoid conflict canteenId += 1 } diff --git a/app/src/main/java/com/denizk0461/studip/model/DietaryPreferences.kt b/app/src/main/java/com/denizk0461/studip/model/DietaryPreferences.kt index f61a717..1ec5954 100644 --- a/app/src/main/java/com/denizk0461/studip/model/DietaryPreferences.kt +++ b/app/src/main/java/com/denizk0461/studip/model/DietaryPreferences.kt @@ -41,6 +41,7 @@ enum class DietaryPreferences(val value: String) { 8 to R.drawable.carrot, 9 to R.drawable.deer, 10 to R.drawable.circle, + 11 to R.drawable.cross, ) /** diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 862cd2a..bb0d649 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -20,6 +20,9 @@ Dozierende(r) Raum + Kein Angebot + Für diesen Tag sind keine Angebote vorhanden + Lizenzen Dein Stundenplan diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 09537be..81c45cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -21,6 +21,9 @@ Room + No offers + There are no offers available for this day + Licences Android developer resources provided by Google.\nhttps://developer.android.com/\n\nCrash reporting provided by Google.\nhttps://firebase.google.com/products/crashlytics/\n\nDesign guidelines, icons, and colour palette provided by Google.\nhttps://m3.material.io/\n\nAdditional icons provided by Flaticon.\nhttps://www.flaticon.com/\n\nWeb scraping functionality provided by Jonathan Hedley.\nhttps://jhy.io/ | https://jsoup.org/