Make sure there is always a 'today' indicator in meetings list

This commit is contained in:
Sylvain Berfini 2024-01-23 14:20:05 +01:00
parent a198fab204
commit fcbe629e48
8 changed files with 159 additions and 65 deletions

View file

@ -14,15 +14,21 @@ import androidx.recyclerview.widget.RecyclerView
import org.linphone.R import org.linphone.R
import org.linphone.databinding.MeetingListCellBinding import org.linphone.databinding.MeetingListCellBinding
import org.linphone.databinding.MeetingsListDecorationBinding import org.linphone.databinding.MeetingsListDecorationBinding
import org.linphone.ui.main.meetings.model.MeetingListItemModel
import org.linphone.ui.main.meetings.model.MeetingModel import org.linphone.ui.main.meetings.model.MeetingModel
import org.linphone.utils.Event import org.linphone.utils.Event
import org.linphone.utils.HeaderAdapter import org.linphone.utils.HeaderAdapter
class MeetingsListAdapter : class MeetingsListAdapter :
ListAdapter<MeetingModel, RecyclerView.ViewHolder>( ListAdapter<MeetingListItemModel, RecyclerView.ViewHolder>(
MeetingDiffCallback() MeetingDiffCallback()
), ),
HeaderAdapter { HeaderAdapter {
companion object {
const val MEETING = 1
const val TODAY_INDICATOR = 2
}
var selectedAdapterPosition = -1 var selectedAdapterPosition = -1
val meetingClickedEvent: MutableLiveData<Event<MeetingModel>> by lazy { val meetingClickedEvent: MutableLiveData<Event<MeetingModel>> by lazy {
@ -49,13 +55,39 @@ class MeetingsListAdapter :
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
TODAY_INDICATOR -> createTodayIndicatorViewHolder(parent)
else -> createMeetingViewHolder(parent)
}
}
override fun getItemViewType(position: Int): Int {
val data = getItem(position)
if (data.isToday) {
return TODAY_INDICATOR
}
return MEETING
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is MeetingViewHolder) {
holder.bind(getItem(position).model as MeetingModel)
}
}
fun resetSelection() {
notifyItemChanged(selectedAdapterPosition)
selectedAdapterPosition = -1
}
private fun createMeetingViewHolder(parent: ViewGroup): MeetingViewHolder {
val binding: MeetingListCellBinding = DataBindingUtil.inflate( val binding: MeetingListCellBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context), LayoutInflater.from(parent.context),
R.layout.meeting_list_cell, R.layout.meeting_list_cell,
parent, parent,
false false
) )
val viewHolder = ViewHolder(binding) val viewHolder = MeetingViewHolder(binding)
binding.apply { binding.apply {
lifecycleOwner = parent.findViewTreeLifecycleOwner() lifecycleOwner = parent.findViewTreeLifecycleOwner()
@ -73,16 +105,17 @@ class MeetingsListAdapter :
return viewHolder return viewHolder
} }
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { private fun createTodayIndicatorViewHolder(parent: ViewGroup): TodayIndicatorViewHolder {
(holder as ViewHolder).bind(getItem(position)) return TodayIndicatorViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.meeting_list_today_indicator,
parent,
false
)
)
} }
fun resetSelection() { inner class MeetingViewHolder(
notifyItemChanged(selectedAdapterPosition)
selectedAdapterPosition = -1
}
inner class ViewHolder(
val binding: MeetingListCellBinding val binding: MeetingListCellBinding
) : RecyclerView.ViewHolder(binding.root) { ) : RecyclerView.ViewHolder(binding.root) {
@UiThread @UiThread
@ -97,13 +130,26 @@ class MeetingsListAdapter :
} }
} }
private class MeetingDiffCallback : DiffUtil.ItemCallback<MeetingModel>() { inner class TodayIndicatorViewHolder(
override fun areItemsTheSame(oldItem: MeetingModel, newItem: MeetingModel): Boolean { val view: View
return oldItem.id == newItem.id ) : RecyclerView.ViewHolder(view)
private class MeetingDiffCallback : DiffUtil.ItemCallback<MeetingListItemModel>() {
override fun areItemsTheSame(oldItem: MeetingListItemModel, newItem: MeetingListItemModel): Boolean {
if (oldItem.model is MeetingModel && newItem.model is MeetingModel) {
return oldItem.model.id == newItem.model.id
}
return false
} }
override fun areContentsTheSame(oldItem: MeetingModel, newItem: MeetingModel): Boolean { override fun areContentsTheSame(
return oldItem.subject.value == newItem.subject.value && oldItem.time == newItem.time oldItem: MeetingListItemModel,
newItem: MeetingListItemModel
): Boolean {
if (oldItem.model is MeetingModel && newItem.model is MeetingModel) {
return oldItem.model.subject.value == newItem.model.subject.value && oldItem.model.time == newItem.model.time
}
return false
} }
} }
} }

View file

@ -38,7 +38,6 @@ import org.linphone.databinding.MeetingsListFragmentBinding
import org.linphone.ui.main.fragment.AbstractTopBarFragment import org.linphone.ui.main.fragment.AbstractTopBarFragment
import org.linphone.ui.main.meetings.adapter.MeetingsListAdapter import org.linphone.ui.main.meetings.adapter.MeetingsListAdapter
import org.linphone.ui.main.meetings.viewmodel.MeetingsListViewModel import org.linphone.ui.main.meetings.viewmodel.MeetingsListViewModel
import org.linphone.utils.AppUtils
import org.linphone.utils.RecyclerViewHeaderDecoration import org.linphone.utils.RecyclerViewHeaderDecoration
@UiThread @UiThread
@ -219,14 +218,11 @@ class MeetingsListFragment : AbstractTopBarFragment() {
} }
private fun scrollToToday() { private fun scrollToToday() {
Log.i("$TAG Scrolling to today's meeting (if any)")
val todayMeeting = listViewModel.meetings.value.orEmpty().find { val todayMeeting = listViewModel.meetings.value.orEmpty().find {
it.displayTodayIndicator.value == true it.isToday
} }
val index = listViewModel.meetings.value.orEmpty().indexOf(todayMeeting) val index = listViewModel.meetings.value.orEmpty().indexOf(todayMeeting)
(binding.meetingsList.layoutManager as LinearLayoutManager).scrollToPositionWithOffset( Log.i("$TAG Scrolling to 'today' at position [$index]")
index, (binding.meetingsList.layoutManager as LinearLayoutManager).scrollToPosition(index)
AppUtils.getDimension(R.dimen.meeting_list_decoration_height).toInt()
)
} }
} }

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2010-2023 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.ui.main.meetings.model
import androidx.annotation.WorkerThread
import org.linphone.utils.TimestampUtils
class MeetingListItemModel @WorkerThread constructor(meetingModel: MeetingModel?) {
val isToday = meetingModel == null
val month = meetingModel?.month ?: TimestampUtils.month(System.currentTimeMillis(), false)
val model = meetingModel ?: TodayModel()
class TodayModel
}

View file

@ -45,6 +45,8 @@ class MeetingModel @WorkerThread constructor(private val conferenceInfo: Confere
val isToday = TimestampUtils.isToday(timestamp) val isToday = TimestampUtils.isToday(timestamp)
val isAfterToday = TimestampUtils.isAfterToday(timestamp)
private val startTime = TimestampUtils.timeToString(timestamp) private val startTime = TimestampUtils.timeToString(timestamp)
private val endTime = TimestampUtils.timeToString(timestamp + (conferenceInfo.duration * 60)) private val endTime = TimestampUtils.timeToString(timestamp + (conferenceInfo.duration * 60))
@ -57,8 +59,6 @@ class MeetingModel @WorkerThread constructor(private val conferenceInfo: Confere
val firstMeetingOfTheDay = MutableLiveData<Boolean>() val firstMeetingOfTheDay = MutableLiveData<Boolean>()
val displayTodayIndicator = MutableLiveData<Boolean>()
init { init {
subject.postValue(conferenceInfo.subject) subject.postValue(conferenceInfo.subject)

View file

@ -27,16 +27,16 @@ import org.linphone.core.ConferenceInfo
import org.linphone.core.Core import org.linphone.core.Core
import org.linphone.core.CoreListenerStub import org.linphone.core.CoreListenerStub
import org.linphone.core.tools.Log import org.linphone.core.tools.Log
import org.linphone.ui.main.meetings.model.MeetingListItemModel
import org.linphone.ui.main.meetings.model.MeetingModel import org.linphone.ui.main.meetings.model.MeetingModel
import org.linphone.ui.main.viewmodel.AbstractTopBarViewModel import org.linphone.ui.main.viewmodel.AbstractTopBarViewModel
import org.linphone.utils.TimestampUtils
class MeetingsListViewModel @UiThread constructor() : AbstractTopBarViewModel() { class MeetingsListViewModel @UiThread constructor() : AbstractTopBarViewModel() {
companion object { companion object {
private const val TAG = "[Meetings List ViewModel]" private const val TAG = "[Meetings List ViewModel]"
} }
val meetings = MutableLiveData<ArrayList<MeetingModel>>() val meetings = MutableLiveData<ArrayList<MeetingListItemModel>>()
val fetchInProgress = MutableLiveData<Boolean>() val fetchInProgress = MutableLiveData<Boolean>()
@ -74,7 +74,7 @@ class MeetingsListViewModel @UiThread constructor() : AbstractTopBarViewModel()
@WorkerThread @WorkerThread
private fun computeMeetingsList(filter: String) { private fun computeMeetingsList(filter: String) {
val list = arrayListOf<MeetingModel>() val list = arrayListOf<MeetingListItemModel>()
var source = coreContext.core.defaultAccount?.conferenceInformationList var source = coreContext.core.defaultAccount?.conferenceInformationList
if (source == null) { if (source == null) {
@ -85,7 +85,7 @@ class MeetingsListViewModel @UiThread constructor() : AbstractTopBarViewModel()
} }
var previousModel: MeetingModel? = null var previousModel: MeetingModel? = null
var firstMeetingOfTodayFound = false var meetingForTodayFound = false
for (info: ConferenceInfo in source) { for (info: ConferenceInfo in source) {
val add = if (filter.isNotEmpty()) { val add = if (filter.isNotEmpty()) {
val organizerCheck = info.organizer?.asStringUriOnly()?.contains( val organizerCheck = info.organizer?.asStringUriOnly()?.contains(
@ -101,6 +101,7 @@ class MeetingsListViewModel @UiThread constructor() : AbstractTopBarViewModel()
} else { } else {
true true
} }
if (add) { if (add) {
val model = MeetingModel(info) val model = MeetingModel(info)
val firstMeetingOfTheDay = if (previousModel != null) { val firstMeetingOfTheDay = if (previousModel != null) {
@ -110,22 +111,26 @@ class MeetingsListViewModel @UiThread constructor() : AbstractTopBarViewModel()
} }
model.firstMeetingOfTheDay.postValue(firstMeetingOfTheDay) model.firstMeetingOfTheDay.postValue(firstMeetingOfTheDay)
// Insert "Today" fake model before the first one of today
if (firstMeetingOfTheDay && model.isToday) { if (firstMeetingOfTheDay && model.isToday) {
firstMeetingOfTodayFound = true list.add(MeetingListItemModel(null))
model.displayTodayIndicator.postValue(true) meetingForTodayFound = true
} }
list.add(model) // If no meeting was found for today, insert "Today" fake model before the next meeting to come
if (!meetingForTodayFound && model.isAfterToday) {
list.add(MeetingListItemModel(null))
meetingForTodayFound = true
}
list.add(MeetingListItemModel(model))
previousModel = model previousModel = model
} }
} }
if (!firstMeetingOfTodayFound) { // If no meeting was found after today, insert "Today" fake model at the end
val firstMeetingAfterToday = list.find { if (!meetingForTodayFound) {
TimestampUtils.isAfterToday(it.timestamp) list.add(MeetingListItemModel(null))
}
Log.i("$TAG $firstMeetingAfterToday")
firstMeetingAfterToday?.displayTodayIndicator?.postValue(true)
} }
meetings.postValue(list) meetings.postValue(list)

View file

@ -110,13 +110,21 @@ class TimestampUtils {
fun month(timestamp: Long, timestampInSecs: Boolean = true): String { fun month(timestamp: Long, timestampInSecs: Boolean = true): String {
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
calendar.timeInMillis = if (timestampInSecs) timestamp * 1000 else timestamp calendar.timeInMillis = if (timestampInSecs) timestamp * 1000 else timestamp
return calendar.getDisplayName( val month = calendar.getDisplayName(
Calendar.MONTH, Calendar.MONTH,
TextStyle.SHORT.ordinal, TextStyle.SHORT.ordinal,
Locale.getDefault() Locale.getDefault()
) )
?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } ?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
?: "?" ?: "?"
val now = Calendar.getInstance()
if (isSameYear(now, calendar)) {
return month
}
val year = calendar.get(Calendar.YEAR)
return "$month $year"
} }
@AnyThread @AnyThread
@ -197,9 +205,9 @@ class TimestampUtils {
cal1: Calendar, cal1: Calendar,
cal2: Calendar cal2: Calendar
): Boolean { ): Boolean {
return cal1[Calendar.ERA] == cal2[Calendar.ERA] && return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1[Calendar.YEAR] == cal2[Calendar.YEAR] && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1[Calendar.DAY_OF_YEAR] == cal2[Calendar.DAY_OF_YEAR] cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
} }
@AnyThread @AnyThread
@ -207,8 +215,8 @@ class TimestampUtils {
cal1: Calendar, cal1: Calendar,
cal2: Calendar cal2: Calendar
): Boolean { ): Boolean {
return cal1[Calendar.ERA] == cal2[Calendar.ERA] && return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1[Calendar.YEAR] == cal2[Calendar.YEAR] cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
} }
} }
} }

View file

@ -22,26 +22,6 @@
android:paddingStart="16dp" android:paddingStart="16dp"
android:paddingEnd="16dp"> android:paddingEnd="16dp">
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style_300"
android:id="@+id/today_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@string/meetings_list_today_indicator"
android:textSize="12sp"
android:textColor="?attr/color_main1_500"
android:visibility="@{model.displayTodayIndicator ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/today_separator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="?attr/color_main1_500"
android:visibility="@{model.displayTodayIndicator ? View.VISIBLE : View.GONE, default=gone}"
app:layout_constraintTop_toBottomOf="@id/today_indicator" />
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style" style="@style/default_text_style"
android:id="@+id/header_day" android:id="@+id/header_day"
@ -55,13 +35,14 @@
android:textSize="14sp" android:textSize="14sp"
app:layout_constraintStart_toStartOf="@id/header_day_number" app:layout_constraintStart_toStartOf="@id/header_day_number"
app:layout_constraintEnd_toEndOf="@id/header_day_number" app:layout_constraintEnd_toEndOf="@id/header_day_number"
app:layout_constraintTop_toBottomOf="@id/today_separator"/> app:layout_constraintTop_toTopOf="parent"/>
<ImageView <ImageView
android:id="@+id/today_background" android:id="@+id/today_background"
android:layout_width="32dp" android:layout_width="32dp"
android:layout_height="32dp" android:layout_height="32dp"
android:layout_marginStart="5dp" android:layout_marginStart="5dp"
android:contentDescription="@null"
android:src="@drawable/shape_circle_primary_background" android:src="@drawable/shape_circle_primary_background"
android:visibility="@{model.isToday &amp;&amp; model.firstMeetingOfTheDay ? View.VISIBLE : View.INVISIBLE}" android:visibility="@{model.isToday &amp;&amp; model.firstMeetingOfTheDay ? View.VISIBLE : View.INVISIBLE}"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
@ -96,7 +77,7 @@
android:elevation="5dp" android:elevation="5dp"
app:layout_constraintStart_toEndOf="@id/header_day" app:layout_constraintStart_toEndOf="@id/header_day"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/today_separator" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"> app:layout_constraintBottom_toBottomOf="parent">
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<androidx.appcompat.widget.AppCompatTextView
style="@style/default_text_style_300"
android:id="@+id/today_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@string/meetings_list_today_indicator"
android:textSize="12sp"
android:textColor="?attr/color_main1_500" />
<View
android:id="@+id/today_separator"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="?attr/color_main1_500" />
</LinearLayout>