Archived
Canteen content can now be displayed
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.denizk0461.studip.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.denizk0461.studip.databinding.ItemCanteenBinding
|
||||
import com.denizk0461.studip.model.CanteenOffer
|
||||
|
||||
class CanteenOfferItemAdapter(private val offers: List<CanteenOffer>) : RecyclerView.Adapter<CanteenOfferItemAdapter.OfferViewHolder>() {
|
||||
|
||||
class OfferViewHolder(val binding: ItemCanteenBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OfferViewHolder {
|
||||
return OfferViewHolder(
|
||||
ItemCanteenBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = offers.size
|
||||
|
||||
override fun onBindViewHolder(holder: OfferViewHolder, position: Int) {
|
||||
val currentItem = offers[position]
|
||||
|
||||
holder.binding.textCategory.text = currentItem.category
|
||||
holder.binding.tempContent.text = "${currentItem.title} • ${currentItem.price}"
|
||||
|
||||
// TODO inflate view saying "no offers!" or sth
|
||||
}
|
||||
|
||||
// interface OnClickListener {
|
||||
// fun onClick(event: StudIPEvent)
|
||||
// fun onLongClick(event: StudIPEvent)
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.denizk0461.studip.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
|
||||
import com.denizk0461.studip.model.CanteenOffer
|
||||
|
||||
class CanteenOfferPageAdapter(private var offers: List<CanteenOffer>, private var daysCovered: Int) : RecyclerView.Adapter<CanteenOfferPageAdapter.CanteenOfferPageViewHolder>() {
|
||||
|
||||
class CanteenOfferPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CanteenOfferPageViewHolder =
|
||||
CanteenOfferPageViewHolder(
|
||||
ItemScrollablePageBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
|
||||
override fun getItemCount(): Int = daysCovered
|
||||
|
||||
override fun onBindViewHolder(holder: CanteenOfferPageViewHolder, position: Int) {
|
||||
|
||||
holder.binding.pageRecyclerView.apply {
|
||||
|
||||
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
|
||||
|
||||
val filtered = offers.filter { it.dateId == position + 1 }
|
||||
adapter = CanteenOfferItemAdapter(filtered) // TODO check if empty
|
||||
scheduleLayoutAnimation()
|
||||
}
|
||||
}
|
||||
|
||||
fun setNewItems(items: List<CanteenOffer>, daysCovered: Int) {
|
||||
this.daysCovered = daysCovered
|
||||
offers = items
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -9,9 +9,9 @@ import com.denizk0461.studip.model.StudIPEvent
|
||||
import com.denizk0461.studip.databinding.ItemEventBinding
|
||||
import java.util.*
|
||||
|
||||
class StudIPEventAdapter(
|
||||
class StudIPEventItemAdapter(
|
||||
events: List<StudIPEvent>, private val currentDay: Int, private val onClickListener: OnClickListener
|
||||
) : RecyclerView.Adapter<StudIPEventAdapter.EventViewHolder>() {
|
||||
) : RecyclerView.Adapter<StudIPEventItemAdapter.EventViewHolder>() {
|
||||
|
||||
private val filteredEvents: List<StudIPEvent> = events.filter { it.day == currentDay }
|
||||
private val currentCalendar = Calendar.getInstance()
|
||||
@@ -5,14 +5,14 @@ import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
import com.denizk0461.studip.databinding.ItemEventPageBinding
|
||||
import com.denizk0461.studip.databinding.ItemScrollablePageBinding
|
||||
|
||||
class StudIPEventPageAdapter(private var events: List<StudIPEvent>, private val onClickListener: StudIPEventAdapter.OnClickListener) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
|
||||
class StudIPEventPageAdapter(private var events: List<StudIPEvent>, private val onClickListener: StudIPEventItemAdapter.OnClickListener) : RecyclerView.Adapter<StudIPEventPageAdapter.EventPageViewHolder>() {
|
||||
|
||||
class EventPageViewHolder(val binding: ItemEventPageBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
class EventPageViewHolder(val binding: ItemScrollablePageBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventPageViewHolder = EventPageViewHolder(
|
||||
ItemEventPageBinding.inflate(
|
||||
ItemScrollablePageBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
@@ -25,7 +25,7 @@ class StudIPEventPageAdapter(private var events: List<StudIPEvent>, private val
|
||||
|
||||
holder.binding.pageRecyclerView.apply {
|
||||
layoutManager = LinearLayoutManager(holder.binding.root.context, LinearLayoutManager.VERTICAL, false)
|
||||
adapter = StudIPEventAdapter(events, currentDay = position, onClickListener) // TODO check if empty
|
||||
adapter = StudIPEventItemAdapter(events, currentDay = position, onClickListener) // TODO check if empty
|
||||
scheduleLayoutAnimation()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ class StwParser {
|
||||
|
||||
private fun parseFromPage(url: String): List<CanteenOffer> {
|
||||
val items = mutableListOf<CanteenOffer>()
|
||||
var date = ""
|
||||
var date: String
|
||||
var day = 0
|
||||
|
||||
val doc = Jsoup.connect(url).get()
|
||||
@@ -59,6 +59,7 @@ class StwParser {
|
||||
val newItem = CanteenOffer(
|
||||
id = id,
|
||||
date = date,
|
||||
dateId = day,
|
||||
category = categoryTitle,
|
||||
title = tableRows[1].getFilteredText(),
|
||||
price = tableRows.getTextOrEmpty(2),
|
||||
@@ -75,8 +76,6 @@ class StwParser {
|
||||
)
|
||||
items.add(newItem)
|
||||
id += 1
|
||||
|
||||
Log.d("eek!", newItem.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,7 +87,7 @@ class StwParser {
|
||||
getElementsByAttributeValue("src", preference).isNotEmpty()
|
||||
|
||||
private fun Element.getFilteredText(): String {
|
||||
var text = html()
|
||||
var text = html().replace("&", "&")
|
||||
|
||||
while (text.contains("<sup>")) {
|
||||
text = text.substring(0 until text.indexOf("<sup>")) + text.substring(text.indexOf("</sup>")+6 until text.length)
|
||||
|
||||
@@ -7,7 +7,7 @@ import androidx.room.RoomDatabase
|
||||
import com.denizk0461.studip.model.CanteenOffer
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
|
||||
@Database(entities = [StudIPEvent::class, CanteenOffer::class], version = 6)
|
||||
@Database(entities = [StudIPEvent::class, CanteenOffer::class], version = 7)
|
||||
abstract class EventDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun dao(): EventDAO
|
||||
|
||||
@@ -6,8 +6,10 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
|
||||
import com.denizk0461.studip.databinding.FragmentCanteenBinding
|
||||
import com.denizk0461.studip.viewmodel.CanteenViewModel
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
|
||||
class CanteenFragment : Fragment() {
|
||||
|
||||
@@ -17,6 +19,9 @@ class CanteenFragment : Fragment() {
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
private var dates = listOf<String>()
|
||||
|
||||
private lateinit var viewPagerAdapter: CanteenOfferPageAdapter
|
||||
private val viewModel: CanteenViewModel by viewModels()
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
@@ -27,7 +32,18 @@ class CanteenFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0)
|
||||
binding.viewPager.adapter = viewPagerAdapter
|
||||
|
||||
createTabLayoutMediator()
|
||||
|
||||
viewModel.allOffers.observe(viewLifecycleOwner) { offers ->
|
||||
|
||||
dates = offers.map { it.date }.distinct()
|
||||
createTabLayoutMediator()
|
||||
|
||||
viewPagerAdapter.setNewItems(offers, dates.size)
|
||||
|
||||
// TODO this must be changed. if an exception is raised, then this will not fire
|
||||
binding.swipeRefreshLayout.isRefreshing = false
|
||||
}
|
||||
@@ -39,6 +55,14 @@ class CanteenFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun createTabLayoutMediator() {
|
||||
if (dates.isNotEmpty()) {
|
||||
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
|
||||
tab.text = dates[position]
|
||||
}.attach()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
|
||||
@@ -7,7 +7,7 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.denizk0461.studip.R
|
||||
import com.denizk0461.studip.adapter.StudIPEventAdapter
|
||||
import com.denizk0461.studip.adapter.StudIPEventItemAdapter
|
||||
import com.denizk0461.studip.adapter.StudIPEventPageAdapter
|
||||
import com.denizk0461.studip.databinding.FragmentEventBinding
|
||||
import com.denizk0461.studip.model.StudIPEvent
|
||||
@@ -26,7 +26,6 @@ class EventFragment : Fragment() {
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
private lateinit var viewPagerAdapter: StudIPEventPageAdapter
|
||||
private var dayOfWeek: Int = 0
|
||||
private val dayStrings = listOf(
|
||||
R.string.monday,
|
||||
@@ -36,6 +35,7 @@ class EventFragment : Fragment() {
|
||||
R.string.friday,
|
||||
)
|
||||
|
||||
private lateinit var viewPagerAdapter: StudIPEventPageAdapter
|
||||
private val viewModel: EventViewModel by viewModels()
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
@@ -54,7 +54,7 @@ class EventFragment : Fragment() {
|
||||
else -> 0
|
||||
}
|
||||
|
||||
viewPagerAdapter = StudIPEventPageAdapter(listOf(), object : StudIPEventAdapter.OnClickListener {
|
||||
viewPagerAdapter = StudIPEventPageAdapter(listOf(), object : StudIPEventItemAdapter.OnClickListener {
|
||||
override fun onClick(event: StudIPEvent) {
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import androidx.room.PrimaryKey
|
||||
data class CanteenOffer(
|
||||
@PrimaryKey val id: Int,
|
||||
val date: String,
|
||||
val dateId: Int, // gives every item on the same day the same ID for easier filtering
|
||||
val category: String,
|
||||
val title: String,
|
||||
val price: String, // price for students
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingHorizontal="8dp"
|
||||
android:paddingVertical="4dp">
|
||||
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/Widget.Material3.CardView.Outlined"
|
||||
android:backgroundTint="?attr/colorCardBackground"
|
||||
app:strokeColor="?attr/colorTextHintLighter">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="16dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_category"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="18sp"
|
||||
android:fontFamily="@font/lato_bolditalic"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_marginBottom="8dp"/>
|
||||
|
||||
<androidx.appcompat.widget.LinearLayoutCompat
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintTop_toBottomOf="@+id/text_category"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/temp_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
@@ -2,7 +2,6 @@
|
||||
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:paddingHorizontal="8dp"
|
||||
android:paddingVertical="4dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
Reference in New Issue
Block a user