-
Notifications
You must be signed in to change notification settings - Fork 4
Add horizontal calendar row to schedule screen #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
15
commits into
master
Choose a base branch
from
copilot/add-horizontal-calendar-row
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
042449c
Initial plan
Copilot 767b787
Add horizontal calendar view to schedule screen - initial implementation
Copilot 3a5b360
Fix binding initialization for horizontal calendar view
Copilot c94f460
Address code review feedback - remove unused constant, improve clarity
Copilot ce5d4de
Final code review fixes - improve error handling and consistency
Copilot 87bdcab
Merge branch 'master' into copilot/add-horizontal-calendar-row
mmathieum aa8bb7e
Update app-android/src/main/java/org/mtransit/android/ui/schedule/Sch…
mmathieum 2c67531
Refactor horizontal calendar to use RecyclerView with adapter and vie…
Copilot 09eca59
Add smooth scrolling to center selected day in calendar
Copilot c0d1f57
Fix code review issues: improve performance and null safety
Copilot 14ddc39
Update calendar design: toolbar background, month text, bold today, p…
Copilot fc6b1e9
Optimize performance: cache calendar calculations in bind method
Copilot 257a809
wip
mmathieum ad54765
Add FF to hide new horizontal calendar
mmathieum bd5d755
Merge branch 'master' into copilot/add-horizontal-calendar-row
mmathieum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
app-android/src/main/java/org/mtransit/android/ui/schedule/HorizontalCalendarAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| package org.mtransit.android.ui.schedule | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.graphics.Typeface | ||
| import android.view.LayoutInflater | ||
| import android.view.ViewGroup | ||
| import androidx.annotation.ColorInt | ||
| import androidx.core.view.isVisible | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import org.mtransit.android.commons.MTLog | ||
| import org.mtransit.android.data.UISchedule | ||
| import org.mtransit.android.databinding.LayoutScheduleCalendarDayItemBinding | ||
| import org.mtransit.android.util.UITimeUtils | ||
| import org.mtransit.commons.beginningOfDay | ||
| import org.mtransit.commons.isSameDay | ||
| import org.mtransit.commons.toCalendar | ||
| import java.text.SimpleDateFormat | ||
| import java.util.Calendar | ||
| import java.util.Locale | ||
| import java.util.TimeZone | ||
|
|
||
| class HorizontalCalendarAdapter : RecyclerView.Adapter<HorizontalCalendarAdapter.DayViewHolder>(), | ||
| MTLog.Loggable { | ||
|
|
||
| companion object { | ||
| private val LOG_TAG = HorizontalCalendarAdapter::class.java.simpleName | ||
| } | ||
|
|
||
| override fun getLogTag(): String = LOG_TAG | ||
|
|
||
| @ColorInt | ||
| var colorInt: Int? = null | ||
| @SuppressLint("NotifyDataSetChanged") | ||
| set(value) { | ||
| if (field == value) return | ||
| field = value | ||
| notifyDataSetChanged() | ||
| } | ||
|
|
||
| private val dayNameFormat = SimpleDateFormat("EEE", Locale.getDefault()) | ||
| private val monthNameFormat = SimpleDateFormat("MMM", Locale.getDefault()) | ||
| private val days = mutableListOf<Long>() // List of day timestamps in milliseconds | ||
| private var selectedDayInMs: Long? = null | ||
| private var localTimeZone: TimeZone = TimeZone.getDefault() | ||
| private var onDaySelected: ((Long) -> Unit)? = null | ||
|
|
||
| var startInMs: Long? = null | ||
| @SuppressLint("NotifyDataSetChanged") | ||
| set(value) { | ||
| if (field == value) return | ||
| field = value | ||
| updateDays() | ||
| } | ||
|
|
||
| var endInMs: Long? = null | ||
| @SuppressLint("NotifyDataSetChanged") | ||
| set(value) { | ||
| if (field == value) return | ||
| field = value | ||
| updateDays() | ||
| } | ||
|
|
||
| fun setTimeZone(timeZone: TimeZone) { | ||
| localTimeZone = timeZone | ||
| dayNameFormat.timeZone = timeZone | ||
| monthNameFormat.timeZone = timeZone | ||
| } | ||
|
|
||
| fun setOnDaySelectedListener(listener: (Long) -> Unit) { | ||
| onDaySelected = listener | ||
| } | ||
|
|
||
| @SuppressLint("NotifyDataSetChanged") | ||
| private fun updateDays() { | ||
| val startInMs = this.startInMs ?: return | ||
| val endInMs = this.endInMs ?: return | ||
|
|
||
| days.clear() | ||
|
|
||
| val calendar = startInMs.toCalendar(localTimeZone).beginningOfDay | ||
| val endCalendar = endInMs.toCalendar(localTimeZone).beginningOfDay | ||
|
|
||
| while (calendar.timeInMillis <= endCalendar.timeInMillis) { | ||
| days.add(calendar.timeInMillis) | ||
| calendar.add(Calendar.DAY_OF_YEAR, 1) | ||
| } | ||
|
|
||
| notifyDataSetChanged() | ||
| } | ||
|
|
||
| @SuppressLint("NotifyDataSetChanged") | ||
| fun selectDay(dayInMs: Long, notifyAdapter: Boolean = true) { | ||
| val previousSelectedIndex = getSelectedPosition() | ||
| selectedDayInMs = dayInMs | ||
| val newSelectedIndex = getSelectedPosition() | ||
| if (previousSelectedIndex == newSelectedIndex) return | ||
| if (notifyAdapter) { | ||
| notifyDataSetChanged() // need to redraw all days to remove selected | ||
| } | ||
| } | ||
|
|
||
| fun getPositionForDay(dayInMs: Long): Int { | ||
| val targetCalendar = dayInMs.toCalendar(localTimeZone).beginningOfDay | ||
| return days.indexOfFirst { dayMs -> | ||
| val dayCalendar = dayMs.toCalendar(localTimeZone).beginningOfDay | ||
| dayCalendar.isSameDay(targetCalendar) | ||
| } | ||
| } | ||
|
|
||
| fun getSelectedPosition(): Int { | ||
| return selectedDayInMs?.let { getPositionForDay(it) } ?: -1 | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DayViewHolder { | ||
| val binding = LayoutScheduleCalendarDayItemBinding.inflate( | ||
| LayoutInflater.from(parent.context), | ||
| parent, | ||
| false | ||
| ) | ||
| return DayViewHolder(binding) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: DayViewHolder, position: Int) { | ||
| val dayInMs = days[position] | ||
| holder.bind(dayInMs) | ||
| } | ||
|
|
||
| override fun getItemCount(): Int = days.size | ||
|
|
||
| inner class DayViewHolder( | ||
| private val binding: LayoutScheduleCalendarDayItemBinding | ||
| ) : RecyclerView.ViewHolder(binding.root) { | ||
|
|
||
| @get:ColorInt | ||
| private val pastTextColorInt: Int by lazy { UISchedule.getDefaultPastTextColor(binding.root.context) } | ||
|
|
||
| @get:ColorInt | ||
| private val nowTextColorInt: Int by lazy { UISchedule.getDefaultNowTextColor(binding.root.context) } | ||
|
|
||
| @get:ColorInt | ||
| private val futureTextColorInt: Int by lazy { UISchedule.getDefaultFutureTextColor(binding.root.context) } | ||
|
|
||
| private val calendar = Calendar.getInstance(localTimeZone) | ||
|
|
||
| fun bind(dayInMs: Long) = binding.apply { | ||
| calendar.timeInMillis = dayInMs | ||
|
|
||
| val todayBeginning = UITimeUtils.currentTimeMillis().toCalendar(localTimeZone).beginningOfDay | ||
| val dayBeginning = dayInMs.toCalendar(localTimeZone).beginningOfDay | ||
|
|
||
| val isSelected = selectedDayInMs?.let { selectedMs -> | ||
| val selectedBeginning = selectedMs.toCalendar(localTimeZone).beginningOfDay | ||
| dayBeginning.isSameDay(selectedBeginning) | ||
| } == true | ||
|
|
||
| val isToday = dayBeginning.isSameDay(todayBeginning) | ||
| val isPast = dayInMs < todayBeginning.timeInMillis | ||
|
|
||
| // Set text values | ||
| dayName.text = dayNameFormat.format(calendar.time) | ||
| dayNumber.text = calendar.get(Calendar.DAY_OF_MONTH).toString() | ||
| monthName.text = monthNameFormat.format(calendar.time) | ||
|
|
||
| val typeface = if (isToday) Typeface.DEFAULT_BOLD else Typeface.DEFAULT | ||
| // Make today bold | ||
| dayName.typeface = typeface | ||
| dayNumber.typeface = typeface | ||
| monthName.typeface = typeface | ||
|
|
||
| // Update selection state | ||
| selectionIndicator.apply { | ||
| colorInt?.let { setBackgroundColor(it) } | ||
| isVisible = isSelected | ||
| } | ||
| // Set alpha for past days (75% = 0.75f) | ||
| val color = when { | ||
| isPast -> pastTextColorInt | ||
| isToday -> nowTextColorInt | ||
| else -> futureTextColorInt | ||
| } | ||
| dayName.setTextColor(color) | ||
| dayNumber.setTextColor(color) | ||
| monthName.setTextColor(color) | ||
|
|
||
| root.setOnClickListener { | ||
| selectDay(dayInMs) | ||
| onDaySelected?.invoke(dayInMs) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.