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.databinding.MeetingListCellBinding
import org.linphone.databinding.MeetingsListDecorationBinding
import org.linphone.ui.main.meetings.model.MeetingListItemModel
import org.linphone.ui.main.meetings.model.MeetingModel
import org.linphone.utils.Event
import org.linphone.utils.HeaderAdapter
class MeetingsListAdapter :
ListAdapter<MeetingModel, RecyclerView.ViewHolder>(
ListAdapter<MeetingListItemModel, RecyclerView.ViewHolder>(
MeetingDiffCallback()
),
HeaderAdapter {
companion object {
const val MEETING = 1
const val TODAY_INDICATOR = 2
}
var selectedAdapterPosition = -1
val meetingClickedEvent: MutableLiveData<Event<MeetingModel>> by lazy {
@ -49,13 +55,39 @@ class MeetingsListAdapter :
}
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(
LayoutInflater.from(parent.context),
R.layout.meeting_list_cell,
parent,
false
)
val viewHolder = ViewHolder(binding)
val viewHolder = MeetingViewHolder(binding)
binding.apply {
lifecycleOwner = parent.findViewTreeLifecycleOwner()
@ -73,16 +105,17 @@ class MeetingsListAdapter :
return viewHolder
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as ViewHolder).bind(getItem(position))
private fun createTodayIndicatorViewHolder(parent: ViewGroup): TodayIndicatorViewHolder {
return TodayIndicatorViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.meeting_list_today_indicator,
parent,
false
)
)
}
fun resetSelection() {
notifyItemChanged(selectedAdapterPosition)
selectedAdapterPosition = -1
}
inner class ViewHolder(
inner class MeetingViewHolder(
val binding: MeetingListCellBinding
) : RecyclerView.ViewHolder(binding.root) {
@UiThread
@ -97,13 +130,26 @@ class MeetingsListAdapter :
}
}
private class MeetingDiffCallback : DiffUtil.ItemCallback<MeetingModel>() {
override fun areItemsTheSame(oldItem: MeetingModel, newItem: MeetingModel): Boolean {
return oldItem.id == newItem.id
inner class TodayIndicatorViewHolder(
val view: View
) : 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 {
return oldItem.subject.value == newItem.subject.value && oldItem.time == newItem.time
override fun areContentsTheSame(
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.meetings.adapter.MeetingsListAdapter
import org.linphone.ui.main.meetings.viewmodel.MeetingsListViewModel
import org.linphone.utils.AppUtils
import org.linphone.utils.RecyclerViewHeaderDecoration
@UiThread
@ -219,14 +218,11 @@ class MeetingsListFragment : AbstractTopBarFragment() {
}
private fun scrollToToday() {
Log.i("$TAG Scrolling to today's meeting (if any)")
val todayMeeting = listViewModel.meetings.value.orEmpty().find {
it.displayTodayIndicator.value == true
it.isToday
}
val index = listViewModel.meetings.value.orEmpty().indexOf(todayMeeting)
(binding.meetingsList.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
index,
AppUtils.getDimension(R.dimen.meeting_list_decoration_height).toInt()
)
Log.i("$TAG Scrolling to 'today' at position [$index]")
(binding.meetingsList.layoutManager as LinearLayoutManager).scrollToPosition(index)
}
}

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 isAfterToday = TimestampUtils.isAfterToday(timestamp)
private val startTime = TimestampUtils.timeToString(timestamp)
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 displayTodayIndicator = MutableLiveData<Boolean>()
init {
subject.postValue(conferenceInfo.subject)

View file

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

View file

@ -110,13 +110,21 @@ class TimestampUtils {
fun month(timestamp: Long, timestampInSecs: Boolean = true): String {
val calendar = Calendar.getInstance()
calendar.timeInMillis = if (timestampInSecs) timestamp * 1000 else timestamp
return calendar.getDisplayName(
val month = calendar.getDisplayName(
Calendar.MONTH,
TextStyle.SHORT.ordinal,
Locale.getDefault()
)
?.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
@ -197,9 +205,9 @@ class TimestampUtils {
cal1: Calendar,
cal2: Calendar
): Boolean {
return cal1[Calendar.ERA] == cal2[Calendar.ERA] &&
cal1[Calendar.YEAR] == cal2[Calendar.YEAR] &&
cal1[Calendar.DAY_OF_YEAR] == cal2[Calendar.DAY_OF_YEAR]
return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
}
@AnyThread
@ -207,8 +215,8 @@ class TimestampUtils {
cal1: Calendar,
cal2: Calendar
): Boolean {
return cal1[Calendar.ERA] == cal2[Calendar.ERA] &&
cal1[Calendar.YEAR] == cal2[Calendar.YEAR]
return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
}
}
}

View file

@ -22,26 +22,6 @@
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"
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
style="@style/default_text_style"
android:id="@+id/header_day"
@ -55,13 +35,14 @@
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@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
android:id="@+id/today_background"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="5dp"
android:contentDescription="@null"
android:src="@drawable/shape_circle_primary_background"
android:visibility="@{model.isToday &amp;&amp; model.firstMeetingOfTheDay ? View.VISIBLE : View.INVISIBLE}"
app:layout_constraintStart_toStartOf="parent"
@ -96,7 +77,7 @@
android:elevation="5dp"
app:layout_constraintStart_toEndOf="@id/header_day"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/today_separator"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<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>