diff --git a/app/Devnull/RowInputs.php b/app/Devnull/RowInputs.php index 7e70e67..e87342d 100644 --- a/app/Devnull/RowInputs.php +++ b/app/Devnull/RowInputs.php @@ -24,15 +24,18 @@ final class RowInputs */ public static function for(Commit $commit, array $scope): array { - $viewer = $scope['viewer'] ?? app(Account::class)->user(); + // The screen's own answer, null included: null is a real answer (signed out, pastéis mode off), so it's + // only read from the account when the screen gave none. Reading it again cost a query a row on a phone. + $viewer = array_key_exists('viewer', $scope) ? $scope['viewer'] : app(Account::class)->user(); $ownership = $scope['ownership'] ?? app(Account::class)->ownership(); return [ 'viewer' => $viewer?->id, 'organizations' => $ownership->organizationIds, - 'pasteis' => $scope['pasteis'] ?? app(Account::class)->pasteis(), - 'stashed' => $scope['stashed'] ?? [], - 'unfolded' => $scope['unfolded'] ?? [], + 'pasteis' => array_key_exists('pasteis', $scope) ? $scope['pasteis'] : app(Account::class)->pasteis(), + // Only this commit's own drafts and unfolding: a draft started anywhere used to build every row again. + 'stashed' => [in_array('quote-'.$commit->id, $scope['stashed'] ?? [], true), in_array('reply-'.$commit->id, $scope['stashed'] ?? [], true)], + 'unfolded' => in_array($commit->id, $scope['unfolded'] ?? [], true), 'expandable' => isset($scope['threads']), 'opened' => array_key_exists($commit->id, $scope['threads'] ?? []) && ! in_array($commit->id, $scope['closingThreads'] ?? [], true), 'faded' => in_array($commit->id, $scope['fadedRows'] ?? [], true), @@ -41,6 +44,8 @@ public static function for(Commit $commit, array $scope): array 'tombstoneReplies' => array_key_exists($commit->id, $scope['tombstoneReplies'] ?? []), 'shown' => [self::changing($commit), $commit->quotedCommit === null ? null : self::changing($commit->quotedCommit)], 'poll' => [$scope['pollChoices'][$commit->id] ?? null, in_array($commit->id, $scope['voting'] ?? [], true)], + // A poll's Vote button wears your lane colour. + 'colour' => $scope['myColour'] ?? null, ]; } diff --git a/app/NativeComponents/CommitDetail.php b/app/NativeComponents/CommitDetail.php index 7e6d818..5f28577 100644 --- a/app/NativeComponents/CommitDetail.php +++ b/app/NativeComponents/CommitDetail.php @@ -219,7 +219,9 @@ class CommitDetail extends NativeComponent public int $scrollKey = 0; /** - * The replies from the last frame, so a render only builds the replies that changed, as the Log does its rows. + * The page's rows from the last frame, so a render only builds the ones that changed, as the Log does its rows: + * the commit, what it replies to and the rest of its series, its replies, and the related commits. Typing a reply + * draws a frame a few times a second, and each used to build every commit above the reply bar again. */ private ?RowCache $replyRows = null; diff --git a/app/NativeComponents/Log.php b/app/NativeComponents/Log.php index 5d3dc68..276688e 100644 --- a/app/NativeComponents/Log.php +++ b/app/NativeComponents/Log.php @@ -244,6 +244,14 @@ class Log extends NativeComponent */ private array $rowCaches = []; + /** + * The logs beside yours as they were last drawn, by log: what was kept + * for them, hashed, and what they showed (see previewState()). + * + * @var array}> + */ + private array $previews = []; + /** * Which rows of each log are drawn. * @@ -431,6 +439,10 @@ private function openAfresh(): void * A log you're not on: the first few commits kept for it, as it will * open, or skeleton rows with nothing kept, or while it's two swipes away. * + * Every frame draws it, so its commits are only made again when what's + * kept for them changes. Made afresh, they cost the frame as much again + * to lay out and to check against the rows kept for them. + * * @return array */ private function previewState(string $log): array @@ -438,19 +450,25 @@ private function previewState(string $log): array $beside = abs(array_search($log, self::Logs, true) - array_search($this->log, self::Logs, true)) === 1; $json = $beside && ! $this->skeletonFrame ? $this->cached('log/'.$log) : null; $kept = is_array($json['data'] ?? null) ? $json['data'] : []; - $page = $json === null ? null : Page::fromArray(['data' => array_slice($kept, 0, self::PreviewCommits)] + $json, Commit::fromArray(...)); + $shown = $json === null ? null : [array_slice($kept, 0, self::PreviewCommits), count($kept) > self::PreviewCommits, array_diff_key($json, ['data' => true])]; + $key = hash('xxh3', serialize($shown)); + + if (($this->previews[$log][0] ?? null) !== $key) { + $page = $json === null ? null : Page::fromArray(['data' => array_slice($kept, 0, self::PreviewCommits)] + $json, Commit::fromArray(...)); + + $this->previews[$log] = [$key, [ + 'commits' => $page->items ?? [], + // Rows past the preview are left out, with a skeleton row for them, as for the next page. + 'nextCursor' => count($kept) > self::PreviewCommits ? 'preview' : $page?->nextCursor, + 'threads' => [], + 'latestCommits' => [], + 'suggestions' => array_values(array_map(Commit::fromArray(...), array_filter(is_array($json['suggestions'] ?? null) ? $json['suggestions'] : [], is_array(...)))), + 'loaded' => $page !== null, + 'error' => null, + ]]; + } - return [ - 'commits' => $page->items ?? [], - // Rows past the preview are left out, with a skeleton row for them, as for the next page. - 'nextCursor' => count($kept) > self::PreviewCommits ? 'preview' : $page?->nextCursor, - 'threads' => [], - 'latestCommits' => [], - 'suggestions' => array_values(array_map(Commit::fromArray(...), array_filter(is_array($json['suggestions'] ?? null) ? $json['suggestions'] : [], is_array(...)))), - 'loaded' => $page !== null, - 'lastVisitAt' => $this->lastVisitSeenAt($log), - 'error' => null, - ]; + return [...$this->previews[$log][1], 'lastVisitAt' => $this->lastVisitSeenAt($log)]; } public function isRanked(): bool diff --git a/resources/views/native/commit-detail.blade.php b/resources/views/native/commit-detail.blade.php index 0f71787..9f73ee6 100644 --- a/resources/views/native/commit-detail.blade.php +++ b/resources/views/native/commit-detail.blade.php @@ -41,7 +41,7 @@ @foreach ($rows as $row) @php($isFocused = $row->commit->id === $commit?->id) @php($hasThread = $isFocused && $repliesShown && $replies !== []) - @include('native.partials.commit-row', [ + @php($threadRow = [ 'commit' => $row->commit, 'previousLane' => $row->previousLane, 'continuesRun' => $row->continuesRun, @@ -53,6 +53,9 @@ 'withTail' => ! $loop->last, 'hairline' => ! ($loop->last && $relatedBelow), ]) + {{-- Kept like the replies, so typing a reply only builds the reply bar. The commit a page is about also shows its replies' toggle and count, and its time in your timezone. --}} + @php($rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])) + @php($replyRows->include('thread-'.$row->commit->id, [...\App\Devnull\RowInputs::for($row->commit, $rowScope), 'focused' => $isFocused ? [$repliesShown, $replyCount, app(\App\Devnull\Timezone::class)->yours()] : null], 'native.partials.commit-row', $threadRow, $rowScope)) @if ($isFocused && $repliesShown && $replies === [] && ! $repliesLoaded && $error === null) diff --git a/resources/views/native/partials/commit-actions.blade.php b/resources/views/native/partials/commit-actions.blade.php index b8371dc..5118560 100644 --- a/resources/views/native/partials/commit-actions.blade.php +++ b/resources/views/native/partials/commit-actions.blade.php @@ -22,7 +22,9 @@ A commit with a quote or a reply you started and didn't send marks that button, as the web does with stash@{0} beside its label. That doesn't fit the row, so here it's a dot after the label, inside the button. - The screen passes the names of the drafts it has kept ($stashed, see App\Devnull\Stash). + The screen passes the names of the drafts it has kept ($stashed, see App\Devnull\Stash). It passes you ($viewer) + and your pastéis ($pasteis) too, and null is an answer: a row only asks the account when the screen gave neither, + since asking is a query on a phone, and with pastéis mode off every row asked again. Each action is at least 44pt wide and 36pt tall to tap, as Apple asks. The row has no negative vertical margins: Android draws a negative margin as an offset and keeps the space, so the row sat 6pt lower there than on iOS, and @@ -52,7 +54,7 @@ @php($repliesCount ??= $commit->repliesCount) @php($iconSize = $compact ? 14 : 16) @php($labelSize = $compact ? 'text-xs' : 'text-[13px]') -@php($viewer ??= app(\App\Devnull\Account::class)->user()) +@php($viewer = array_key_exists('viewer', get_defined_vars()) ? $viewer : app(\App\Devnull\Account::class)->user()) @php($ownership ??= app(\App\Devnull\Account::class)->ownership()) @php($stashed ??= []) @php($hasQuoteStash = in_array('quote-'.$commit->id, $stashed, true)) @@ -63,7 +65,7 @@ @php($endDot ??= null) @php($starPop = \App\Devnull\Pop::transform($popping['star-'.$commit->id] ?? null, 'star')) @php($pastelPop = \App\Devnull\Pop::transform($popping['pastel-'.$commit->id] ?? null, 'pastel')) -@php($pasteis ??= app(\App\Devnull\Account::class)->pasteis()) +@php($pasteis = array_key_exists('pasteis', get_defined_vars()) ? $pasteis : app(\App\Devnull\Account::class)->pasteis()) @php($showsPastel = $pasteis !== null && ! $commit->author->isOrganization) @php($canGivePastel = $showsPastel && $viewer !== null && ! $isOwn && $commit->isApproved()) @php($pastelLabel = \App\Devnull\Display::count($commit->pasteisCount, 'pastel', 'pastéis')) diff --git a/resources/views/native/partials/feed.blade.php b/resources/views/native/partials/feed.blade.php index 74d9e1d..ac57b37 100644 --- a/resources/views/native/partials/feed.blade.php +++ b/resources/views/native/partials/feed.blade.php @@ -5,8 +5,8 @@ with "Show more" under the last, and a reply's own replies opened under it one step in (see App\Devnull\ThreadRows). The commit's rail runs on past them when its author's run goes on below. - Given a $rowCache (see App\Devnull\RowCache), commits and replies that haven't changed since the last frame are - drawn from it rather than built again. Given a $rowWindow (see App\Devnull\RowWindow), rows far from the screen + Given a $rowCache (see App\Devnull\RowCache), commits, replies and markers that haven't changed since the last frame + are drawn from it rather than built again. Given a $rowWindow (see App\Devnull\RowWindow), rows far from the screen go as placeholders with the keys their partials give them, so the phone keeps each one's place and height. --}} @php @@ -16,11 +16,18 @@ @endphp @foreach ($rows as $feedIndex => $row) @if ($row->isDayMarker()) - @if ($rowWindow?->place($feedIndex) ?? true) + @php + $markerDraws = $rowWindow?->place($feedIndex) ?? true; + @endphp + @if (! $markerDraws) + @php + $rowWindow->placeholder('day-'.$row->label); + @endphp + @elseif ($rowCache === null) @include('native.partials.day-marker', ['label' => $row->label, 'lane' => $row->previousLane]) @else @php - $rowWindow->placeholder('day-'.$row->label); + $rowCache->include('day-'.$row->label, [], 'native.partials.day-marker', ['label' => $row->label, 'lane' => $row->previousLane], []); @endphp @endif @elseif ($row->isWhoToFollow()) @@ -32,11 +39,18 @@ @endphp @endif @elseif ($row->isLastVisitMarker()) - @if ($rowWindow?->place($feedIndex) ?? true) + @php + $markerDraws = $rowWindow?->place($feedIndex) ?? true; + @endphp + @if (! $markerDraws) + @php + $rowWindow->placeholder('last-visit'); + @endphp + @elseif ($rowCache === null) @include('native.partials.last-visit-marker', ['lane' => $row->previousLane]) @else @php - $rowWindow->placeholder('last-visit'); + $rowCache->include('last-visit', [], 'native.partials.last-visit-marker', ['lane' => $row->previousLane], []); @endphp @endif @else diff --git a/resources/views/native/partials/log-page.blade.php b/resources/views/native/partials/log-page.blade.php index ebee83b..13f32df 100644 --- a/resources/views/native/partials/log-page.blade.php +++ b/resources/views/native/partials/log-page.blade.php @@ -26,7 +26,9 @@ class="w-full flex-1 bg-theme-surface" > @if (! $page['loaded'] && $page['error'] === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']], []); + @endphp @endforeach @endif @@ -45,9 +47,13 @@ class="w-full flex-1 bg-theme-surface" @if ($page['lastCommit']) @if ($page['nextCursor'] !== null) - @include('native.partials.rail-loading', ['pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-more', [], 'native.partials.rail-loading', ['pulse' => $page['showing']], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $page['lastCommit']->lane(), 'message' => "That's the root commit. There's nothing before it."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $page['lastCommit']->lane(), 'message' => "That's the root commit. There's nothing before it."], []); + @endphp @endif @endif diff --git a/resources/views/native/partials/organization-page.blade.php b/resources/views/native/partials/organization-page.blade.php index c97e6be..a478e0d 100644 --- a/resources/views/native/partials/organization-page.blade.php +++ b/resources/views/native/partials/organization-page.blade.php @@ -21,7 +21,9 @@ class="w-full flex-1 bg-theme-surface" ]) @elseif (! $page['loaded']) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']], []); + @endphp @endforeach @elseif ($page['isEmpty']) @if ($page['tab'] === 'mentions') @@ -36,11 +38,17 @@ class="w-full flex-1 bg-theme-surface" @if ($page['loaded'] && ! $page['isEmpty']) @if ($page['nextCursor'] !== null) - @include('native.partials.rail-loading', ['pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-more', [], 'native.partials.rail-loading', ['pulse' => $page['showing']], []); + @endphp @elseif ($page['tab'] === 'mentions') - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's the first mention of {$user->handle()}."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's the first mention of {$user->handle()}."], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's {$user->handle()}'s root commit."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => "That's {$user->handle()}'s root commit."], []); + @endphp @endif @endif @endif diff --git a/resources/views/native/partials/profile-page.blade.php b/resources/views/native/partials/profile-page.blade.php index 9b1c5f6..80d0cad 100644 --- a/resources/views/native/partials/profile-page.blade.php +++ b/resources/views/native/partials/profile-page.blade.php @@ -4,6 +4,8 @@ The pager draws the header and the tabs at the top of this list, so they scroll with it. As on the Log, each tab has its own list, a new $page['listKey'] puts it back at the top, and only the tab you're on asks for its next page. + The pinned commit, the skeleton rows and the row at the end are kept in the tab's row cache with its commits, so + a frame only builds what changed. --}} @if (! $page['loaded'] && $page['error'] === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading commits', 'pulse' => $page['showing']], []); + @endphp @endforeach @endif @@ -39,18 +43,27 @@ class="w-full flex-1 bg-theme-surface" Pinned - @include('native.partials.commit-row', ['commit' => $page['pinnedCommit'], 'previousLane' => null, 'continuesRun' => false, 'rowKey' => 'pinned-'.$page['pinnedCommit']->id]) + @php + $rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]); + $page['rowCache']->include('pinned-'.$page['pinnedCommit']->id, \App\Devnull\RowInputs::for($page['pinnedCommit'], $rowScope), 'native.partials.commit-row', ['commit' => $page['pinnedCommit'], 'previousLane' => null, 'continuesRun' => false, 'rowKey' => 'pinned-'.$page['pinnedCommit']->id], $rowScope); + @endphp @endif @include('native.partials.feed', ['rows' => $page['rows'], 'threads' => $page['threads'], 'rowCache' => $page['rowCache']]) @if (! $page['isEmpty'] || $page['pinnedCommit']) @if ($page['nextCursor'] !== null) - @include('native.partials.rail-loading', ['pulse' => $page['showing']]) + @php + $page['rowCache']->include('loading-more', [], 'native.partials.rail-loading', ['pulse' => $page['showing']], []); + @endphp @elseif ($page['tab'] === 'replies') - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first reply.' : "That's {$user->handle()}'s first reply."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first reply.' : "That's {$user->handle()}'s first reply."], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first commit. Everyone starts somewhere.' : "That's {$user->handle()}'s root commit."]) + @php + $page['rowCache']->include('root-commit', [], 'native.partials.rail-end', ['lane' => $user->lane(), 'message' => $isMe ? 'That was your first commit. Everyone starts somewhere.' : "That's {$user->handle()}'s root commit."], []); + @endphp @endif @endif diff --git a/resources/views/native/partials/related-commits.blade.php b/resources/views/native/partials/related-commits.blade.php index 452eaed..9c88601 100644 --- a/resources/views/native/partials/related-commits.blade.php +++ b/resources/views/native/partials/related-commits.blade.php @@ -2,7 +2,8 @@ Other threads, in relevance order, on a fresh rail below this thread as on the web. They start closed under a header with their count, which opens them in place and closes them again, its chevron turning as they do (see CommitDetail::toggleRelated()). Each commit fades and slides into place a little after the one above it, and fades - out together as they close. The header is the whole row, so it's an easy target at the foot of the page. + out together as they close. The header is the whole row, so it's an easy target at the foot of the page. Their rows + are kept with the page's other rows (see CommitDetail::$replyRows), so a frame only builds the ones that changed. --}} @php($relatedOpen = $relatedShown && ! $relatedClosing) @php($relatedLabel = \App\Devnull\Display::count(count($relatedRows), 'related commit', 'related commits')) @@ -18,7 +19,8 @@ @if ($relatedShown) @foreach ($relatedRows as $relatedRow) - @include('native.partials.commit-row', [ + @php($rowScope = array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1])) + @php($replyRows->include('related-'.$relatedRow->commit->id, \App\Devnull\RowInputs::for($relatedRow->commit, $rowScope), 'native.partials.commit-row', [ 'commit' => $relatedRow->commit, 'previousLane' => $relatedRow->previousLane, 'continuesRun' => $relatedRow->continuesRun, @@ -28,7 +30,7 @@ 'seriesExpanded' => false, 'threadBelow' => false, 'withTail' => ! $loop->last, - ]) + ], $rowScope)) @endforeach @endif diff --git a/resources/views/native/search.blade.php b/resources/views/native/search.blade.php index 29f914c..bcd42a7 100644 --- a/resources/views/native/search.blade.php +++ b/resources/views/native/search.blade.php @@ -50,7 +50,9 @@ @if ($commitsFoundFor === null && $error === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Searching commits']) + @php + $rowCache->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Searching commits'], []); + @endphp @endforeach @elseif ($commitsFoundFor !== null && $commits === []) @include('native.partials.empty', [ diff --git a/resources/views/native/starred.blade.php b/resources/views/native/starred.blade.php index 61117ac..34fcede 100644 --- a/resources/views/native/starred.blade.php +++ b/resources/views/native/starred.blade.php @@ -23,7 +23,9 @@ @if (! $loaded && $error === null) @foreach (range(1, 3) as $skeleton) - @include('native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading starred commits']) + @php + $rowCache->include('loading-'.$skeleton, [], 'native.partials.rail-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading starred commits'], []); + @endphp @endforeach @endif @@ -40,9 +42,13 @@ @if ($lastCommit) @if ($nextCursor !== null) - @include('native.partials.rail-loading') + @php + $rowCache->include('loading-more', [], 'native.partials.rail-loading', [], []); + @endphp @else - @include('native.partials.rail-end', ['lane' => $lastCommit->lane(), 'message' => "You've reached the end of your starred commits."]) + @php + $rowCache->include('root-commit', [], 'native.partials.rail-end', ['lane' => $lastCommit->lane(), 'message' => "You've reached the end of your starred commits."], []); + @endphp @endif @endif diff --git a/tests/Feature/Screens/LogRowCacheTest.php b/tests/Feature/Screens/LogRowCacheTest.php index 8dc278d..aeb27ba 100644 --- a/tests/Feature/Screens/LogRowCacheTest.php +++ b/tests/Feature/Screens/LogRowCacheTest.php @@ -3,9 +3,13 @@ use App\Devnull\Account; use App\Devnull\Data\Pasteis; use App\Devnull\Data\StashedDraft; +use App\Devnull\Data\User; use App\Devnull\ImageCache; use App\Devnull\Stash; use App\NativeComponents\Log; +use Illuminate\Cache\Events\CacheHit; +use Illuminate\Cache\Events\CacheMissed; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\View; use Native\Mobile\Testing\Native; @@ -24,6 +28,11 @@ 'GET /api/v1/log/following' => [pageJson([ commitJson(['id' => 'c3', 'body' => $long, 'preview_segments' => [['type' => 'text', 'text' => substr($long, 0, 270).'…']]]), commitJson(['id' => 'c2', 'replies_count' => 1, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada', 'avatar_url' => 'https://avatars.githubusercontent.com/u/2?v=4'])]), + commitJson(['id' => 'p1', 'body' => 'Tabs or spaces?', 'author' => $grace, 'poll' => [ + 'id' => 'poll1', 'closes_at' => now()->addDay()->toIso8601ZuluString(), 'is_closed' => false, 'has_voted' => false, + 'options' => [['id' => 'o1', 'label' => 'Tabs'], ['id' => 'o2', 'label' => 'Spaces']], + ]]), + commitJson(['id' => 'g1', 'body' => 'From the organization', 'author' => userJson(['type' => 'organization', 'id' => 'o1', 'handle' => 'acme', 'name' => 'Acme'])]), commitJson(['id' => 'c1', 'body' => 'The oldest commit', 'author' => $grace]), ])], 'GET /api/v1/commits/c2/replies' => [pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c2', 'body' => 'A reply in place'])])], @@ -69,6 +78,16 @@ function expectKeptRowsToMatchFreshOnes(TestableComponent $screen, array $before app(Account::class)->rememberPasteis(new Pasteis); $screen->call('isRanked'); }, + // An organization's commit is yours once you're in it, whoever posted it. + 'joining an organization' => function () use ($screen) { + app(Account::class)->rememberOrganizations([User::fromArray(userJson(['type' => 'organization', 'id' => 'o1', 'handle' => 'acme', 'name' => 'Acme']))]); + $screen->call('isRanked'); + }, + // A poll's Vote button wears your lane colour. + 'your colour' => function () use ($screen) { + app(Account::class)->remember(User::fromArray(userJson(['lane' => 5]))); + $screen->call('isRanked'); + }, ]; foreach ($changes as $change) { @@ -108,4 +127,50 @@ function expectKeptRowsToMatchFreshOnes(TestableComponent $screen, array $before $screen->tap('star-c1'); expect($built)->toBe(['c1']); + + // A draft marks the button of the commit it's for, and only that row. + $built = []; + app(Stash::class)->put(app(Account::class)->user()?->id, 'reply-c1', new StashedDraft('Half a reply')); + $screen->call('isRanked'); + expect($built)->toBe(['c1']); +}); + +it('asks the account for you and your pastéis once a frame, not once a row', function () { + $screen = Native::visit('/log')->tap('replies-c2'); + $reads = []; + Event::listen([CacheHit::class, CacheMissed::class], function (CacheHit|CacheMissed $event) use (&$reads) { + $reads[$event->key] = ($reads[$event->key] ?? 0) + 1; + }); + + // Pastéis mode is off: null is the answer, not a reason to ask again. + $screen->call('isRanked'); + expect($reads['devnull.pasteis'] ?? 0)->toBe(1); + + // Every row built afresh asks no more often. + $reads = []; + Closure::bind(fn () => $this->rowCaches = [], $screen->instance(), Log::class)(); + $screen->call('isRanked'); + expect($reads['devnull.pasteis'] ?? 0)->toBe(1); +}); + +it('keeps the skeleton, loading and day rows, and draws them as building them again would', function () { + $built = []; + View::composer(['native.partials.rail-loading', 'native.partials.day-marker'], function ($view) use (&$built) { + $built[] = $view->name(); + }); + + // Latest is beside Following and has nothing kept, so it shows skeleton rows; Following ends in a loading row. + keep('log/following', pageJson([commitJson(['id' => 'c9', 'created_at' => now()->subDays(2)->toIso8601ZuluString()])], 'next')); + $screen = Native::visit('/log'); + $built = []; + + $screen->call('isRanked'); + expect($built)->toBe([]); + + // Swiping to Latest stops the loading row pulsing on Following, and starts the skeletons pulsing on Latest. + foreach (['latest', 'following'] as $log) { + $before = $screen->tree(); + tapLogTab($screen, $log); + expectKeptRowsToMatchFreshOnes($screen, $before); + } }); diff --git a/tests/Feature/Screens/LogsTest.php b/tests/Feature/Screens/LogsTest.php index 615b43c..b072768 100644 --- a/tests/Feature/Screens/LogsTest.php +++ b/tests/Feature/Screens/LogsTest.php @@ -2,6 +2,7 @@ use App\Devnull\Client; use App\Devnull\FirstFrame; +use App\Devnull\ResponseCache; use App\Devnull\Timeline; use App\NativeComponents\CommitDetail; use App\NativeComponents\Log; @@ -277,6 +278,26 @@ function ada(): array } }); +it('keeps the commits shown for the log beside yours until what was kept for it changes', function () { + holdRefreshes(); + keep('log/latest', pageJson([commitJson(['id' => 'l1', 'body' => 'Latest 1', 'author' => ada()])])); + + $screen = Native::visit('/log'); + $previews = fn (): array => Closure::bind(fn (): array => $this->previews, $screen->instance(), Log::class)(); + $shown = $previews()['latest'][1]['commits'][0]; + + // Drawn again with nothing changed, the commit is the one made last frame, so its row is the one kept for it. + $screen->call('isRanked'); + expect($previews()['latest'][1]['commits'][0])->toBe($shown); + + // A star given on another screen changes what was kept for Latest, and its preview shows it. + app(ResponseCache::class)->starChanged('l1', true); + $screen->call('isRanked'); + + expect($previews()['latest'][1]['commits'][0])->not->toBe($shown) + ->and(nodeByRef($screen, 'star-l1')['props']['a11y_label'])->toStartWith('Unstar commit'); +}); + it('leaves the log two swipes away as skeleton rows until you are beside it', function () { holdRefreshes(); keep('log/trending', pageJson(array_map(fn (int $n): array => commitJson(['id' => "t{$n}", 'body' => "Trending {$n}"]), range(1, 5)))); diff --git a/tests/Feature/Screens/RowCacheScreensTest.php b/tests/Feature/Screens/RowCacheScreensTest.php index 902cbd0..2befa18 100644 --- a/tests/Feature/Screens/RowCacheScreensTest.php +++ b/tests/Feature/Screens/RowCacheScreensTest.php @@ -1,5 +1,6 @@ tap('replies-c2')->assertSee('Second reply'); - // The commits above the replies read what the screen shows of the thread, so they're built every time. The last - // reply of a page changes when the next page comes, since it no longer carries "more". - expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe(['c2']) - ->and(rowsBuiltBy(fn () => $screen->call('loadMoreReplies')->assertSee('Third reply')))->toBe(['c2', 'r2', 'r3']); + // The last reply of a page changes when the next page comes, since it no longer carries "more". + expect(rowsBuiltBy(fn () => $screen->call('navTitle')))->toBe([]) + ->and(rowsBuiltBy(fn () => $screen->call('loadMoreReplies')->assertSee('Third reply')))->toBe(['r2', 'r3']); expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); }); + +it("keeps a commit page's commits between renders, and builds the commit again when what it shows of its thread changes", function () { + fakeApi([ + 'GET /api/v1/commits/c2' => [commitPageJson(commitJson(['id' => 'c2', 'replies_count' => 1, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada'])]), [commitJson(['id' => 'c1', 'body' => 'What it replies to'])])], + 'GET /api/v1/commits/c2/replies' => [pageJson([replyJson(['id' => 'r1', 'parent_id' => 'c2', 'body' => 'First reply'])])], + 'GET /api/v1/commits/c2/related' => [['data' => [commitJson(['id' => 'x1', 'body' => 'Another thread'])]]], + 'POST /api/v1/commits/c2/star' => [['data' => commitJson(['id' => 'c2', 'replies_count' => 1, 'stars_count' => 1, 'starred' => true, 'author' => userJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada'])])]], + ]); + $screen = finishRefreshes(Native::visit('/log/commits/c2'))->tap('related-commits-toggle'); + + // Typing a reply only builds the commit it replies to once, when the draft is first kept, which its row marks. + expect(rowsBuiltBy(fn () => $screen->input('reply-input', 'N')))->toBe(['c2']) + ->and(rowsBuiltBy(fn () => $screen->input('reply-input', 'Nice')))->toBe([]); + + $changes = [ + 'its replies opened' => fn () => $screen->tap('replies-c2')->assertSee('First reply'), + 'a star' => fn () => $screen->tap('star-c2'), + 'its replies closed' => fn () => $screen->tap('replies-c2'), + 'the time' => function () use ($screen) { + $this->travel(2)->hours(); + $screen->call('navTitle')->assertSee('2h'); + }, + 'your timezone' => function () use ($screen) { + app(Account::class)->rememberTimezone('Asia/Tokyo'); + $screen->call('navTitle'); + }, + ]; + + foreach ($changes as $change) { + $change(); + expectKeptRowsMatchFreshOnes($screen, CommitDetail::class, 'replyRows', null); + } +});