Skip to content

Commit

Permalink
Merge pull request #409 from vbh/group-recents
Browse files Browse the repository at this point in the history
Group recent calls
  • Loading branch information
roeiedri authored Apr 13, 2022
2 parents 4d343ea + 73af5fd commit 680bd07
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ class RecentsAdapter @Inject constructor(
private val preferences: PreferencesInteractor
) : ListAdapter<RecentAccount>(animations) {

private var _showHistory: Boolean = false

var showHistory: Boolean
get() = _showHistory
set(value) {
_showHistory = value
}

override fun onBindListItem(listItem: ListItem, item: RecentAccount) {
listItem.apply {
isCompact = preferences.isCompact
captionText = if (item.date != null) context.getHoursString(item.date) else null
val date = if (item.date != null) context.getHoursString(item.date) else ""
captionText = if (item.callCount > 1) "(${item.callCount}) $date" else date
phones.lookupAccount(item.number) {
titleText = it?.name ?: item.number
it?.let {
Expand All @@ -42,5 +51,6 @@ class RecentsAdapter @Inject constructor(
}
}

override fun convertDataToListData(data: List<RecentAccount>) = ListData.fromRecents(data)
override fun convertDataToListData(data: List<RecentAccount>) =
ListData.fromRecents(data, !showHistory)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,45 @@ data class ListData<DataType>(
return ListData(contacts, headersToCounts)
}

fun fromRecents(recents: List<RecentAccount>) = ListData(
recents,
recents.groupingBy { getRelativeDateString(it.date) }.eachCount()
)
fun fromRecents(recents: List<RecentAccount>, isGrouped: Boolean) =
if (isGrouped && recents.size > 1) getGroupedRecents(recents)
else ListData(
recents,
recents.groupingBy { getRelativeDateString(it.date) }.eachCount()
)

private fun getGroupedRecents(recents: List<RecentAccount>): ListData<RecentAccount> {
var prevItem: RecentAccount = recents[0]
var prevDate = getRelativeDateString(prevItem.date)
var count = 1
val groupedRecents: MutableList<RecentAccount> = ArrayList()
recents.drop(1).forEach {
val date = getRelativeDateString(it.date)
if (prevItem.number == it.number && prevDate == date) {
count++
} else {
groupedRecents.add(
RecentAccount(
prevItem.number, prevItem.type, prevItem.id,
prevItem.duration, prevItem.date, prevItem.cachedName, count
)
)
count = 1
prevItem = it
}
prevDate = date
}
groupedRecents.add(
RecentAccount(
prevItem.number, prevItem.type, prevItem.id,
prevItem.duration, prevItem.date, prevItem.cachedName, count
)
)
return ListData(
groupedRecents,
groupedRecents.groupingBy { getRelativeDateString(it.date) }.eachCount()
)
}

fun fromPhones(phones: List<PhoneAccount>): ListData<PhoneAccount> {
val phones = phones.toList().distinctBy { it.normalizedNumber }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data class RecentAccount(
val duration: Long = 0,
val date: Date? = null,
val cachedName: String? = null,
val callCount: Int = 1,
) : Serializable {

@kotlin.annotation.Retention(AnnotationRetention.SOURCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ package com.chooloo.www.chooloolib.ui.recents

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import com.chooloo.www.chooloolib.adapter.RecentsAdapter
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class RecentsHistoryFragment : RecentsFragment() {
override val viewState: RecentsHistoryViewState by activityViewModels()

@Inject
override lateinit var adapter: RecentsAdapter

override fun onSetup() {
super.onSetup()

adapter.showHistory = true
}

companion object {
fun newInstance(filter: String? = null) = RecentsHistoryFragment().apply {
arguments = Bundle().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ open class ListItem : LinearLayout {

caption.id.also {
connect(it, TOP, title.id, BOTTOM)
connect(it, START, title.id, START)
connect(it, START, captionImage.id, END)
connect(it, BOTTOM, PARENT_ID, BOTTOM)
}

captionImage.id.also {
connect(it, TOP, caption.id, TOP)
connect(it, START, caption.id, END)
connect(it, START, title.id, START)
connect(it, BOTTOM, caption.id, BOTTOM)
}

Expand Down

0 comments on commit 680bd07

Please sign in to comment.