SyncServerII / Neebla

Private and Self-Owned Social Media
MIT License
1 stars 2 forks source link

Predicate in sharing extension #3

Open crspybits opened 3 years ago

crspybits commented 3 years ago

This is documentation for my future use.

If I just put this one:

    (
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.heic"
                ).@count == 1
            ).@count == 1
    )

then my extension is only "active" when a single image is selected. And by "active" I mean, is available in the UI as a choice. If I select a movie in Photos, it is not active. If I select a screen grab (PNG, I assume), it is not ative. BUT: If I select a PNG and an image, it is active.


It looks like .@count == $extensionItem.attachments.@count may be the solution. When I use:

    (
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.heic"
                ).@count == $extensionItem.attachments.@count
            ).@count == 1
    )

If I select a PNG and an other (jpeg) image, my extension is not active. Though, if I select two live images, it is active.


When I do this, it seems improved:

            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.heic"
                ).@count == 1
            ).@count == 1

            AND

            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.heic"
                ).@count == $extensionItem.attachments.@count
            ).@count == 1

a) If I select a single image, it is active. b) Selecting two images, it is not active. c) Selecting two live images, it is not active. d) Selecting an image and a movie it is not active.


Now to try the parallel thing for url's (not going to worry about live images as that doesn't seem to work from sharing extensions):

        SUBQUERY (
            extensionItems,
            $extensionItem,
            SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
            ).@count == $extensionItem.attachments.@count
        ).@count <= 2

This does allow images to be selected in Photos!


Trying this:

        SUBQUERY (
            extensionItems,
            $extensionItem,
            SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
            ).@count <= 2
        ).@count <= 2

Same thing: This does allow images to be selected in Photos! Probably because of the <= 2 constraint. 0 is <= 2.


        SUBQUERY (
            extensionItems,
            $extensionItem,
            SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
            ).(@count &lt;= 2 AND @count &gt;= 1)
        ).(@count &lt;= 2 AND @count &gt;= 1)

That's not allowing a URL to be selected in Safari.


        SUBQUERY (
            extensionItems,
            $extensionItem,
            SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
            ).@count &lt;= 2
        ).@count &lt;= 2

        AND

         SUBQUERY (
            extensionItems,
            $extensionItem,
            SUBQUERY (
                $extensionItem.attachments,
                $attachment,
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
            ).@count &gt;=1
        ).@count &gt;=1  

That seems to do the trick!


Next to try to combine the two results:

        (
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
                ).@count &lt;= 2
            ).@count &lt;= 2
            AND
             SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
                ).@count &gt;=1
            ).@count &gt;=1
        )
        OR
        (
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.heic"
                ).@count == 1
            ).@count == 1
            AND
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.heic"
                ).@count == $extensionItem.attachments.@count
            ).@count == 1
        )

This seems to work OK, but has a problem with some images labeled HDR in the simulator.


Try to remove HEIC since I'm not dealing with them as still images yet:

        (
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
                ).@count &lt;= 2
            ).@count &lt;= 2
            AND
             SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
                ).@count &gt;=1
            ).@count &gt;=1
        )
        OR
        (
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg"
                ).@count == 1
            ).@count == 1
            AND
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg"
                ).@count == $extensionItem.attachments.@count
            ).@count == 1
        )

The same issue occurs with the same image. But it seems to work well enough so that's where I'm going to leave it for now.

crspybits commented 3 years ago

Related references:

https://medium.com/@cmoulinet/ios-share-extension-custom-rules-to-limit-type-or-numbers-of-medias-selected-91ab596d505

https://github.com/irccloud/ios/blob/master/ShareExtension/Info.plist

https://irace.me/tumblr-ios-extension

https://stackoverflow.com/questions/34411753/how-to-only-share-an-image-or-video-with-a-share-extension

https://stackoverflow.com/questions/26218627/ios-appextension-how-can-i-combine-nsextensionactivationrule-and-nspredicate

https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html