Skip to content
Draft
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
16 changes: 15 additions & 1 deletion app/NativeComponents/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use App\NativeComponents\Concerns\PlaysVoiceNotes;
use App\NativeComponents\Concerns\RecordsVoiceNotes;
use App\NativeComponents\Concerns\RefreshesInBackground;
use App\NativeComponents\Concerns\ShowsImageViewer;
use App\NativeComponents\Concerns\SuggestsMentions;
use App\NativeComponents\Concerns\SyncsOutbox;
use App\NativeComponents\Concerns\TalksToDevnull;
Expand Down Expand Up @@ -77,7 +78,8 @@
* and letting go there sets the composer replying to it; it springs back
* either way. A double tap toggles a heart. A long press opens its menu (see
* MessageMenu), whose React shows the web's quick reactions in a bubble
* above it, with More for the phone's own emoji picker.
* above it, with More for the phone's own emoji picker. A tap on one of its
* images opens them full-screen (see ShowsImageViewer).
*
* An org's pinned messages show as the web shows them: the latest in a bar
* over the top of the thread, which opens them all in a sheet, and a pin
Expand Down Expand Up @@ -109,6 +111,7 @@ class Conversation extends NativeComponent
use PlaysVoiceNotes;
use RecordsVoiceNotes;
use RefreshesInBackground;
use ShowsImageViewer;
use SuggestsMentions;
use SyncsOutbox;
use TalksToDevnull;
Expand Down Expand Up @@ -1109,6 +1112,17 @@ public function openLink(int $id): void
}
}

/**
* Opens a message's images full-screen (see ShowsImageViewer), from the one
* tapped, paging through that message's images only, as a commit's do. A
* message the thread isn't showing opens nothing, nor does one still being
* pushed, whose images are only on the phone.
*/
public function openMessageImage(int $id, int $index = 0): void
{
$this->showImagesInViewer($this->findMessage($id)?->images ?? [], $index);
}

/**
* Opens a link in a message's body, by its place among the body's links: a
* commit or another page on dev/null the app has a screen for here in the
Expand Down
3 changes: 3 additions & 0 deletions resources/views/native/conversation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@

{{-- More reactions, from the bubble or anywhere else: the emoji keyboard on iOS, Jetpack's emoji picker on Android. --}}
<native:emoji-picker ref="emoji-picker" :visible="$emojiPickerFor !== null" @change="emojiPicked" @dismiss="emojiPickerClosed" />

{{-- A message's images full-screen, over everything else. --}}
@include('native.partials.image-viewer')
</native:column>
10 changes: 8 additions & 2 deletions resources/views/native/partials/message-images.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{{--
A message's images. A long press on them opens the message's menu, as on its bubble ($photosMenu).
A message's images. A long press on them opens the message's menu, as on its bubble ($photosMenu). A tap on one
opens the message's images full-screen from that one (see Conversation::openMessageImage), once devnull has the
message: a message still being pushed has its images only on the phone.
--}}
@if ($message->images !== [])
<native:pressable ref="photos-{{ $key }}" class="gap-1" :context-menu="$photosMenu">
@foreach ($message->images as $index => $image)
<native:image ref="image-{{ $key }}-{{ $index }}" :src="$image->url" :width="240" :height="(int) round(240 / $image->ratio())" :fit="2" class="rounded-xl bg-theme-wash" a11y-label="{{ $image->label($index, count($message->images)) }}" />
@if ($message->isPending())
<native:image ref="image-{{ $key }}-{{ $index }}" :src="$image->url" :width="240" :height="(int) round(240 / $image->ratio())" :fit="2" class="rounded-xl bg-theme-wash" a11y-label="{{ $image->label($index, count($message->images)) }}" />
@else
<native:image ref="image-{{ $key }}-{{ $index }}" @press="openMessageImage({{ $message->id }}, {{ $index }})" :src="$image->url" :width="240" :height="(int) round(240 / $image->ratio())" :fit="2" class="rounded-xl bg-theme-wash" a11y-label="{{ $image->label($index, count($message->images)) }}" />
@endif
@endforeach
</native:pressable>
@endif
95 changes: 95 additions & 0 deletions tests/Feature/Screens/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

use App\Devnull\Account;
use App\Devnull\Data\Remotes;
use App\Devnull\ImageCache;
use App\Devnull\ImageViewer;
use App\NativeComponents\Conversation;
use Devnull\Photos\Events\Saved;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Native\Mobile\Events\Alert\ButtonPressed;
use Native\Mobile\Testing\Native;
Expand Down Expand Up @@ -1389,3 +1393,94 @@ function orgWithMembers(array $people): array
->tap('mention-ada')
->assertSet('draft', 'hi');
});

/**
* A message's photos as devnull sends them: $count of them, numbered from $first, the first described.
*
* @return list<array<string, mixed>>
*/
function remotePhotos(int $count, int $first = 1): array
{
return array_map(fn (int $n): array => [
'id' => $n,
'width' => 400,
'height' => 300,
'description' => $n === $first ? 'The new logo' : null,
'url' => 'https://img.test/'.$n,
], range($first, $first + $count - 1));
}

it("opens a message's image full-screen from the one tapped, and pages through that message's images only", function (string $platform) {
serveRemoteThread(threadJson([
remoteMessageJson(['id' => 1, 'body' => '', 'images' => remotePhotos(3)]),
remoteMessageJson(['id' => 2, 'body' => 'and the dark one', 'images' => remotePhotos(2, first: 4)]),
], ['read_through_id' => 2]));

$screen = finishRefreshes(Native::visit('/remotes/conversations/ada', platform: $platform));

// Hidden until a tap, always in the tree.
expect(nodeByRef($screen, 'image-viewer')['props']['visible'])->toBeFalse()
->and(nodeByRef($screen, 'image-1-1')['on_press'] ?? null)->not->toBeNull();

$screen->tap('image-1-1')->assertSet('viewerOpen', true)->assertSet('viewerIndex', 1);

$shown = findNodes(nodeByRef($screen, 'image-viewer'), fn (array $node): bool => ($node['type'] ?? null) === 'image');

expect(nodeByRef($screen, 'image-viewer')['props']['visible'])->toBeTrue()
->and(array_map(fn (array $node): string => $node['props']['src'], $shown))->toBe(['https://img.test/1', 'https://img.test/2', 'https://img.test/3'])
->and(nodeByRef($screen, 'viewed-image-0')['props']['alt'])->toBe('The new logo, image 1 of 3')
->and(nodeByRef($screen, 'viewer-dots')['props']['a11y_label'])->toBe('Image 2 of 3')
->and(nodeByRef($screen, 'viewer-save')['props']['a11y_label'])->toBe('Save to Photos');

$screen->changeTab('viewerPaged', 2)->assertSet('viewerIndex', 2);
$screen->dismissSheet('image-viewer')->assertSet('viewerOpen', false)->assertSet('viewerImages', []);

// A message with text keeps its images tappable too, and opens on its own.
$screen->tap('image-2-1')->assertSet('viewerIndex', 1);

expect(array_map(fn ($image): string => $image->url, $screen->instance()->viewerImages))->toBe(['https://img.test/4', 'https://img.test/5'])
->and(nodeByRef($screen, 'viewer-dots')['props']['a11y_label'])->toBe('Image 2 of 2');

$screen->assertNativeNotCalled('Browser.OpenInApp');
})->with(['ios', 'android']);

it("keeps a message's long-press menu on its images while a tap opens them", function () {
serveRemoteThread(threadJson([remoteMessageJson(['id' => 1, 'body' => '', 'images' => remotePhotos(2)])], ['read_through_id' => 1]));

$screen = finishRefreshes(Native::visit('/remotes/conversations/ada', platform: 'ios'));

expect(array_column(menuItems($screen, 'photos-1'), 1))->toBe(['Reply', 'React']);

$screen->tap('image-1-0')->assertSet('viewerOpen', true);
});

it("drags a message's image away and saves it to the phone's photos", function () {
serveRemoteThread(threadJson([remoteMessageJson(['id' => 1, 'body' => '', 'images' => remotePhotos(2)])], ['read_through_id' => 1]));
$this->bridge->respondTo('Photos.Save', ['success' => true]);

$kept = ImageCache::path('https://img.test/2');
File::ensureDirectoryExists(dirname($kept));
File::put($kept, 'jpeg bytes');

try {
$screen = finishRefreshes(Native::visit('/remotes/conversations/ada', platform: 'ios'))->tap('image-1-1');

$screen->tap('viewer-save')
->assertNativeCalled('Photos.Save', fn (array $params): bool => $params['path'] === $kept)
->emitNative(Saved::class)
->assertNativeCalled('ToastKit.Show', fn (array $params): bool => $params['message'] === 'Saved to Photos');

$screen->dragEnd('viewer-dismiss', 0, ImageViewer::DismissDistance)->assertSet('viewerOpen', false);
} finally {
File::delete($kept);
}
});

it("opens nothing for a message the thread isn't showing, or a place with no image", function () {
serveRemoteThread(threadJson([remoteMessageJson(['id' => 1, 'body' => '', 'images' => remotePhotos(1)])], ['read_through_id' => 1]));

$screen = finishRefreshes(Native::visit('/remotes/conversations/ada', platform: 'ios'));

$screen->call('openMessageImage', 42, 0)->assertSet('viewerOpen', false);
$screen->call('openMessageImage', 1, 3)->assertSet('viewerOpen', false);
});
6 changes: 4 additions & 2 deletions tests/Feature/Screens/RemotesComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ function threadWithAda(string $platform = 'ios'): TestableComponent
$screen->tap('push')->assertSee('the new logo')->assertSee('staged')->assertSet('photos', []);
$clientId = $screen->instance()->pending[0]->clientId;

// Drawn from the phone while they go.
// Drawn from the phone while they go, and not opened full-screen until devnull has them.
expect(nodeByRef($screen, 'image-'.$clientId.'-0')['props']['src'])->toBe($kept[0])
->and(nodeByRef($screen, 'image-'.$clientId.'-1')['props']['src'])->toBe($kept[1]);
->and(nodeByRef($screen, 'image-'.$clientId.'-1')['props']['src'])->toBe($kept[1])
->and(nodeByRef($screen, 'image-'.$clientId.'-0'))->not->toHaveKey('on_press');

finishRefreshes($screen)->assertSee('pushed')->assertSet('pending', []);

Expand All @@ -121,6 +122,7 @@ function threadWithAda(string $platform = 'ios'): TestableComponent
['name' => 'images[]', 'type' => 'image/png', 'contents' => $contents[1]],
]])
->and(nodeByRef($screen, 'image-99-0')['props']['src'])->toBe('http://devnull.test/message-images/1?signature=abc')
->and(nodeByRef($screen, 'image-99-0')['on_press'] ?? null)->not->toBeNull()
->and(array_filter($kept, file_exists(...)))->toBe([]);
});

Expand Down
Loading