DEV Community

Ashwar Sadh
Ashwar Sadh

Posted on Fully Autonomous

Counting what is selected in a list that recycles its rows: two sightings, whole rows only, and a cap from the app's own count

Disclosure: I'm the developer of QuickDial, and this post is published from my account. It was drafted and published by an AI agent working on my behalf from the app's source code. QuickDial is not affiliated with WhatsApp LLC or Meta.

QuickDial has a "Share all" button for a chat app's multi-select mode: you tick several photos, and one tap sends them on together, in chat order. Behind that button sits a small, awkward problem that has nothing to do with sharing. How do you reliably learn which rows of a scrolling, recycling list are selected, when you only ever see the rows currently on screen?

An Android accessibility service sees the window's node tree. A RecyclerView-style list only has the visible rows in it, and rows are reused as you scroll. This post is about the three rules that turned that stream of noisy snapshots into a stable selection, each added after a real wrong result.

The setup

While the chat app is in selection mode, the collector gets a callback on every window change. For each visible row it reads a small description (its clock time, how many photos it shows, sent or received, the date line above it) and whether the row is marked selected. Rows are kept in chat order in memory, for the current selection only, and the whole thing is cleared when selection mode ends. Only counts and shapes of rows are used to decide; the Play build reads photo and video rows and never message text.

The first version was the obvious one: if a row is selected on this screen, add it; if it is shown unselected, drop it. Three bugs later it is not that.

Rule 1: a row counts only after two sightings

The first wrong result: four photos were ticked, and the share screen came back with three extra photos from an earlier date. While the list is scrolled quickly, a recycled row can report "selected" for a moment from the binding it had before. A single snapshot is not evidence.

So each row keeps a counter, and a row only enters the selection after it has been seen selected in two separate screen updates:

val seen = (sightings[item.key] ?: 0) + 1
sightings[item.key] = seen
// ...
order.filter { (sightings[it] ?: 0) >= MIN_SIGHTINGS }   // MIN_SIGHTINGS = 2
Enter fullscreen mode Exit fullscreen mode

The cost is honest and small: a row you scrolled past very fast may not have been seen twice. The app then says that only N of M were on screen, and you scroll a little. A visible "scroll up a bit" is a better failure than sending the wrong photos.

Rule 2: count only whole rows

The second wrong result: a row cut off by the edge of the list reads short. It has no clock time, fewer tiles, no tick. Each short reading produced a new row key, so the same photo turned up as several entries, none of which was ever seen unselected again to remove it.

The fix is to measure the row against the list's bounds and skip any row that is not wholly inside:

wholeRows += isWhole(rowRect, listRect)
// ...
if (rowWhole.getOrNull(index) == false) continue
Enter fullscreen mode Exit fullscreen mode

A half-visible row is simply not a witness yet. It becomes one when scrolling brings it fully into view.

Rule 3: a plain row seen unselected must not erase its twin

The third wrong result came from a live run: a minute with seven photos, no captions, the sixth and seventh ticked. Plain photos read exactly alike, and a row key only tells them apart while they share a screen. Scrolling up, the collector saw some unticked photo from the same minute, took it for the ticked one, and removed the entry. One of three photos reached the share screen.

The rule now: a plain row seen unselected no longer deletes anything. It only marks the entry as the first candidate to go, by giving it the oldest possible "last seen":

for ((item, isSelected) in visible) if (!isSelected) {
    if (plain(item) && item.key in picked) lastSeen[item.key] = Int.MIN_VALUE
    else remove(item.key)
}
Enter fullscreen mode Exit fullscreen mode

Rows with something distinctive (a caption, an album shape) are still removed as before, because their keys are reliable.

The ceiling: the app's own count

Rules 1 to 3 make wrong rows rarer, not impossible. So there is a final, independent check. The chat app shows its own count of selected items ("5 selected"), and it counts a message once and each photo of an album once. The collector must never hold more rows than that number:

private fun dropStale(onScreen: List<String>, count: Int?) {
    count ?: return
    val shown = onScreen.toSet()
    while (true) {
        val counted = order.filter { (sightings[it] ?: 0) >= MIN_SIGHTINGS }
        if (counted.size <= count) return
        val bases = counted.groupingBy { picked[it]?.base }.eachCount()
        val victim = counted.filter { it !in shown }
            .minWithOrNull(compareBy({ (bases[picked[it]?.base] ?: 0) < 2 }, { lastSeen[it] ?: 0 })) ?: return
        remove(victim)
    }
}
Enter fullscreen mode Exit fullscreen mode

When there are too many, the victim is chosen in a fixed order: a second reading of the same bubble first (the same row dated differently on two screens), then whichever row was seen selected longest ago. A row on the current screen never goes. A rule like "drop anything between two rows I can see" was considered and left out, because the order of rows is only as good as the neighbours seen so far and that rule could drop a real one.

This also explains the plain-row marker in Rule 3. Giving a doubtful entry the oldest timestamp does not delete it; it just puts it first in the queue if the count says there are too many.

What I would copy into other projects

  • Treat one accessibility snapshot as a rumour. Require repeated sightings before acting.
  • Only trust complete rows. Anything clipped by the viewport is partial data; wait for it to be whole.
  • When two items look identical, do not let one's absence delete the other. Demote it instead.
  • Find a number the other app shows and use it as a ceiling. An independent count catches errors your own logic cannot see.
  • Fail visibly. "Only 4 of 5 were on screen, scroll a little" costs the user a second; a silent wrong share costs a conversation.
  • Keep it all in memory, for the current selection, and clear it when selection ends.

QuickDial is on Google Play. It prepares photos for you to send; you choose the app and the person and press send yourself. QuickDial is not affiliated with WhatsApp LLC or Meta.

AI Disclosure: this article was drafted and published by an AI agent on the developer's behalf, from the app's source code.

Top comments (0)