Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Added extra days to month-view (#1600)
Browse files Browse the repository at this point in the history
* Added extra days to month-view

* Undo the formatting changes in CalendarFragment

* Resolved the problems

* Added method comment to dayToIndex

* Update

* Added method comment

* Fixed formatting issues

---------

Co-authored-by: ge78fug <[email protected]>
Co-authored-by: Frank Elsinga <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2023
1 parent 91421e9 commit fe04880
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import de.tum.`in`.tumcampusapp.utils.Utils
import de.tum.`in`.tumcampusapp.utils.sync.SyncManager
import org.joda.time.DateTime
import org.joda.time.LocalDate
import java.time.YearMonth
import java.util.*

/**
Expand Down Expand Up @@ -62,22 +63,62 @@ class CalendarController(private val context: Context) : ProvidesCard, ProvidesN
fun getFromDbNotCancelledBetweenDates(begin: DateTime, end: DateTime) =
applyEventColors(calendarDao.getAllNotCancelledBetweenDates(begin, end))

fun getEventsForMonth(date: LocalDate): Map<String, List<CalendarItem>> {
/**
* Returns all events for one page in the month-view.
* One page in the month-view contains 42 days.
*
* @param date The selected date
* @return A map with the index/day of the page as key and the list of events for the respective day as value
*/
fun getEventsForMonth(date: LocalDate): Map<Int, List<CalendarItem>> {
val startOfMonth = date.withDayOfMonth(1)
val endOfMonth = date.withDayOfMonth(date.dayOfMonth().maximumValue)
val events = getFromDbBetweenDates(startOfMonth.toDateTimeAtCurrentTime(), endOfMonth.toDateTimeAtCurrentTime())
val eventMap = mutableMapOf<String, MutableList<CalendarItem>>()
val yearMonth = YearMonth.of(date.year, date.monthOfYear)

// First day of the page, minus offset to the current month
val start = startOfMonth.minusDays(startOfMonth.dayOfWeek)

// Last day of the page, days of page - length of the current month - offset to the current month
val end = endOfMonth.plusDays(42 - yearMonth.lengthOfMonth() - startOfMonth.dayOfWeek)
val events = getFromDbBetweenDates(start.toDateTimeAtCurrentTime(), end.toDateTimeAtCurrentTime())
val eventMap = mutableMapOf<Int, MutableList<CalendarItem>>()
for (event in events) {
val day = event.dtstart.toLocalDate().dayOfMonth.toString()
if (!eventMap.containsKey(day)) {
eventMap[day] = mutableListOf(event)
val day = event.dtstart.toLocalDate()
val index = dayToIndex(day, date)
if (!eventMap.containsKey(index)) {
eventMap[index] = mutableListOf(event)
} else {
eventMap[day]?.add(event)
eventMap[index]?.add(event)
}
}
return eventMap
}

/**
* Returns the index on the page of the event`s day.
* Index goes from 0 to 41.
*
* @param eventDate The date of the event
* @param date The selected date
* @return The index of the day where the event is on the page
*/
private fun dayToIndex(eventDate: LocalDate, date: LocalDate): Int {
val yearMonth = YearMonth.of(date.year, date.monthOfYear)
val daysInMonth = yearMonth.lengthOfMonth()
val daysInPreviousMonth = yearMonth.minusMonths(1).lengthOfMonth()
val firstDayOfMonth = date.withDayOfMonth(1)
val lastDayOfMonth = date.withDayOfMonth(date.dayOfMonth().maximumValue)

if (eventDate < firstDayOfMonth) {
return eventDate.dayOfMonth - 1 - daysInPreviousMonth + firstDayOfMonth.dayOfWeek - 1
}
if (eventDate > lastDayOfMonth) {
return eventDate.dayOfMonth - 1 + firstDayOfMonth.dayOfWeek - 1 + daysInMonth
}

return eventDate.dayOfMonth - 1 + firstDayOfMonth.dayOfWeek - 1
}

private fun applyEventColors(calendarItems: List<CalendarItem>): List<CalendarItem> {
calendarItems.forEach {
it.color = eventColorProvider.getColor(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.joda.time.LocalDate
import org.joda.time.format.DateTimeFormat
import java.time.YearMonth
import java.util.*
import kotlin.math.abs

class CalendarFragment :
FragmentForAccessingTumOnline<EventsResponse>(
Expand Down Expand Up @@ -579,10 +580,10 @@ class CalendarFragment :
val eventMap = calendarController.getEventsForMonth(selectedDate)

if (!::monthViewAdapter.isInitialized) {
monthViewAdapter = MonthViewAdapter(daysInMonth, eventMap)
monthViewAdapter = MonthViewAdapter(daysInMonth, eventMap, selectedDate)
monthRecyclerView.adapter = monthViewAdapter
} else {
monthViewAdapter.updateData(daysInMonth, eventMap)
monthViewAdapter.updateData(daysInMonth, eventMap, selectedDate)
}

val layoutManager: RecyclerView.LayoutManager = GridLayoutManager(requireContext(), 7)
Expand All @@ -593,11 +594,14 @@ class CalendarFragment :
val daysInMonthArray: ArrayList<String> = ArrayList()
val yearMonth = YearMonth.of(date.year, date.monthOfYear)
val daysInMonth = yearMonth.lengthOfMonth()
val daysInPreviousMonth = yearMonth.minusMonths(1).lengthOfMonth()
val firstOfMonth = date.withDayOfMonth(1)
var dayOfWeek = firstOfMonth.dayOfWeek().get() - 1 // Monday is the first day of the week in Europe
var dayOfWeek = firstOfMonth.dayOfWeek - 1 // Monday is the first day of the week in Europe
for (i in 1..42) {
if (i <= dayOfWeek || i > daysInMonth + dayOfWeek) {
daysInMonthArray.add("")
if (i <= dayOfWeek) {
daysInMonthArray.add((daysInPreviousMonth - dayOfWeek + i).toString())
} else if (i > daysInMonth + dayOfWeek) {
daysInMonthArray.add((abs(i - daysInMonth - dayOfWeek)).toString())
} else {
daysInMonthArray.add((i - dayOfWeek).toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,58 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.tum.`in`.tumcampusapp.R
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.CalendarItem
import org.joda.time.LocalDate
import java.time.YearMonth

class MonthViewAdapter(private var daysOfMonth: ArrayList<String>, private var eventMap: Map<String, List<CalendarItem>>) :
class MonthViewAdapter(
private var daysOfMonth: ArrayList<String>,
private var eventMap: Map<Int, List<CalendarItem>>,
private var selectedDate: LocalDate
) :
RecyclerView.Adapter<MonthViewHolder>() {

private lateinit var monthViewEventAdapter: MonthViewEventAdapter
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MonthViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.calendar_month_view_cell, parent, false)
val layoutParams = view.layoutParams
layoutParams.height = (parent.height * 1 / 6)
monthViewEventAdapter = MonthViewEventAdapter(emptyList(), selectedDate)
return MonthViewHolder(view)
}

override fun onBindViewHolder(holder: MonthViewHolder, position: Int) {
val events = eventMap[daysOfMonth[position]]
val events = eventMap[position]

holder.dayOfMonth.text = daysOfMonth[position]
holder.eventsContainer.adapter = events?.let { MonthViewEventAdapter(it) }
holder.eventsContainer.layoutManager = LinearLayoutManager(holder.itemView.context)

if (events.isNullOrEmpty()) {
holder.eventsContainer.adapter = null
} else {
holder.eventsContainer.adapter = monthViewEventAdapter
holder.eventsContainer.layoutManager = LinearLayoutManager(holder.itemView.context)
monthViewEventAdapter.updateData(events, selectedDate)
}

if (
position < selectedDate.withDayOfMonth(1).dayOfWeek - 1 ||
position > selectedDate.withDayOfMonth(1).dayOfWeek - 1 +
YearMonth.of(selectedDate.year, selectedDate.monthOfYear).lengthOfMonth() - 1
) {
holder.dayOfMonth.alpha = 0.2F
} else {
holder.dayOfMonth.alpha = 1F
}
}

override fun getItemCount(): Int {
return daysOfMonth.size
}

fun updateData(daysOfMonth: ArrayList<String>, eventMap: Map<String, List<CalendarItem>>) {
fun updateData(daysOfMonth: ArrayList<String>, eventMap: Map<Int, List<CalendarItem>>, selectedDate: LocalDate) {
this.daysOfMonth = daysOfMonth
this.eventMap = eventMap
this.selectedDate = selectedDate
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import de.tum.`in`.tumcampusapp.R
import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.CalendarItem
import org.jetbrains.anko.withAlpha
import org.joda.time.LocalDate

class MonthViewEventAdapter(private val events: List<CalendarItem>) : RecyclerView.Adapter<MonthViewEventViewHolder>() {
class MonthViewEventAdapter(private var events: List<CalendarItem>, private var selectedDate: LocalDate) :
RecyclerView.Adapter<MonthViewEventViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MonthViewEventViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.calendar_month_event_view, parent, false)
Expand All @@ -18,10 +21,21 @@ class MonthViewEventAdapter(private val events: List<CalendarItem>) : RecyclerVi
val event = events[position]
holder.title.text = event.title
val background = holder.title.background as GradientDrawable

background.setColor(event.color!!)

if (event.eventStart.monthOfYear != selectedDate.monthOfYear) {
background.setColor(event.color!!.withAlpha(100))
}
}

override fun getItemCount(): Int {
return events.size
}

fun updateData(events: List<CalendarItem>, selectedDate: LocalDate) {
this.events = events
this.selectedDate = selectedDate
notifyDataSetChanged()
}
}

0 comments on commit fe04880

Please sign in to comment.