Skip to content
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

Group recent calls #409

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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