From 61974dae4de24cd80d1ab2b15254822b8a1e1779 Mon Sep 17 00:00:00 2001 From: Sean Hagstrom Date: Tue, 16 Apr 2024 13:26:52 +0200 Subject: [PATCH] Implement share button inside profile settings (#19559) * tweak: implement the share button inside the profile settings page * chore: define event and effect handler for opening share sheet * tidy: refactor share buttons to use open-share event * tidy: refactor open-share effect to use react-native-share bindings * tidy: remove unused code for old bindings to react share sheet * tidy: move effects.share/open definition to navigation effects * tweak: ensure navigation effects are loaded * tidy: use open-share dispatch in ui * tidy: use open-share dispatch instead of calling open function directly * tidy: refactor react-native.share/open to only receive options and return promise * tidy: refactor open-share effect to receive map of content and handlers * tidy: replace strings with keywords * tweak: always handle failure case when opening share-sheet * tweak: rename :content to :options * fix: conditionally call on-error * tweak: add extra information when logging error from attempting to share-sheet --- src/react_native/core.cljs | 4 --- src/react_native/share.cljs | 6 +--- src/status_im/common/lightbox/effects.cljs | 11 ++++--- src/status_im/contexts/chat/home/view.cljs | 3 +- .../contexts/communities/sharing/events.cljs | 33 ++++++++++--------- .../profile/contact/actions/view.cljs | 3 +- .../contexts/profile/contact/share/view.cljs | 6 ++-- .../contexts/profile/settings/view.cljs | 6 ++-- .../contexts/shell/share/profile/view.cljs | 3 +- .../contexts/shell/share/wallet/view.cljs | 25 +++++++------- .../wallet/account/share_address/view.cljs | 25 +++++++------- src/status_im/contexts/wallet/effects.cljs | 7 +--- src/status_im/contexts/wallet/events.cljs | 20 +++++------ src/status_im/events.cljs | 1 + src/status_im/navigation/effects.cljs | 14 ++++++++ src/status_im/navigation/events.cljs | 6 ++++ 16 files changed, 91 insertions(+), 82 deletions(-) diff --git a/src/react_native/core.cljs b/src/react_native/core.cljs index 5d434a3ee7..1c66caa3ab 100644 --- a/src/react_native/core.cljs +++ b/src/react_native/core.cljs @@ -226,7 +226,3 @@ (defn open-url [link] (.openURL ^js linking link)) (def set-status-bar-style react-native/StatusBar.setBarStyle) - -(defn sharing - [content] - (.share (.-Share ^js react-native) (clj->js content))) diff --git a/src/react_native/share.cljs b/src/react_native/share.cljs index 2afb5c430c..5ae770dc26 100644 --- a/src/react_native/share.cljs +++ b/src/react_native/share.cljs @@ -4,9 +4,5 @@ (defn open ([options] - (open options #() #())) - ([options on-success on-error] (-> ^js react-native-share - (.open (clj->js options)) - (.then on-success) - (.catch on-error)))) + (.open (clj->js options))))) diff --git a/src/status_im/common/lightbox/effects.cljs b/src/status_im/common/lightbox/effects.cljs index f00910d0da..5917020043 100644 --- a/src/status_im/common/lightbox/effects.cljs +++ b/src/status_im/common/lightbox/effects.cljs @@ -3,7 +3,6 @@ [react-native.cameraroll :as cameraroll] [react-native.fs :as fs] [react-native.platform :as platform] - [react-native.share :as share] [utils.re-frame :as rf])) (def config @@ -15,10 +14,12 @@ (blob/fetch uri config (fn [downloaded-url] - (share/open {:url (str (when platform/android? "file://") downloaded-url) - :isNewTask true} - #(fs/unlink downloaded-url) - #(fs/unlink downloaded-url)))))) + (rf/dispatch [:open-share + {:options {:url (str (when platform/android? "file://") + downloaded-url) + :isNewTask true} + :on-success #(fs/unlink downloaded-url) + :on-error #(fs/unlink downloaded-url)}]))))) (rf/reg-fx :effects.lightbox/save-image-to-gallery (fn [[uri on-success]] diff --git a/src/status_im/contexts/chat/home/view.cljs b/src/status_im/contexts/chat/home/view.cljs index 5e44d1b56c..5dedf968b8 100644 --- a/src/status_im/contexts/chat/home/view.cljs +++ b/src/status_im/contexts/chat/home/view.cljs @@ -5,7 +5,6 @@ [re-frame.core :as re-frame] [react-native.core :as rn] [react-native.reanimated :as reanimated] - [react-native.share :as share] [status-im.common.contact-list-item.view :as contact-list-item] [status-im.common.contact-list.view :as contact-list] [status-im.common.home.actions.view :as actions] @@ -122,7 +121,7 @@ [:show-bottom-sheet {:content chat.actions.view/new-chat}]) :accessibility-label :new-chat-button} :card-props - {:on-press #(share/open {:url profile-link}) + {:on-press #(rf/dispatch [:open-share {:options {:url profile-link}}]) :banner (resources/get-image :invite-friends) :title (i18n/label :t/invite-friends-to-status) :description (i18n/label :t/share-invite-link)}}) diff --git a/src/status_im/contexts/communities/sharing/events.cljs b/src/status_im/contexts/communities/sharing/events.cljs index c865e8578a..c506fd539c 100644 --- a/src/status_im/contexts/communities/sharing/events.cljs +++ b/src/status_im/contexts/communities/sharing/events.cljs @@ -38,23 +38,24 @@ :url %}])] {:fx [[:dispatch [:communities/get-community-channel-share-data chat-id on-success]]]}))) -(rf/reg-event-fx :communities/share-community-url-with-data - (fn [_ [community-id]] - (let [title (i18n/label :t/community-on-status) +(rf/reg-event-fx :communities/share-community-channel-url-with-data + (fn [_ [chat-id]] + (let [title (i18n/label :t/channel-on-status) on-success (fn [url] - (share/open - (if platform/ios? - {:activityItemSources [{:placeholderItem {:type "text" - :content title} - :item {:default {:type "url" - :content url}} - :linkMetadata {:title title}}]} - {:title title - :subject title - :message url - :url url - :isNewTask true})))] - {:fx [[:dispatch [:communities/get-community-share-data community-id on-success]]]}))) + (rf/dispatch [:open-share + {:options (if platform/ios? + {:activityItemSources + [{:placeholderItem {:type :text + :content title} + :item {:default {:type :url + :content url}} + :linkMetadata {:title title}}]} + {:title title + :subject title + :message url + :url url + :isNewTask true})}]))] + {:fx [[:dispatch [:communities/get-community-channel-share-data chat-id on-success]]]}))) (rf/reg-event-fx :communities/get-community-channel-share-data (fn [_ [chat-id on-success]] diff --git a/src/status_im/contexts/profile/contact/actions/view.cljs b/src/status_im/contexts/profile/contact/actions/view.cljs index 991c679296..989f5c86ad 100644 --- a/src/status_im/contexts/profile/contact/actions/view.cljs +++ b/src/status_im/contexts/profile/contact/actions/view.cljs @@ -48,7 +48,8 @@ (fn [] (rf/dispatch [:universal-links/generate-profile-url {:public-key public-key - :on-success #(rn/sharing {:message %})}])) + :on-success #(rf/dispatch [:open-share + {:options {:message %}}])}])) [public-key]) on-remove-contact (rn/use-callback (fn [] diff --git a/src/status_im/contexts/profile/contact/share/view.cljs b/src/status_im/contexts/profile/contact/share/view.cljs index 2a84a55977..03e4d4ffa0 100644 --- a/src/status_im/contexts/profile/contact/share/view.cljs +++ b/src/status_im/contexts/profile/contact/share/view.cljs @@ -1,6 +1,5 @@ (ns status-im.contexts.profile.contact.share.view - (:require [legacy.status-im.ui.components.list-selection :as list-selection] - [quo.core :as quo] + (:require [quo.core :as quo] [react-native.core :as rn] [react-native.safe-area :as safe-area] [status-im.common.qr-codes.view :as qr-codes] @@ -19,7 +18,8 @@ abbreviated-url (rn/use-memo (fn [] (address/get-abbreviated-profile-url universal-profile-url)) [universal-profile-url]) - on-share-press (rn/use-callback #(list-selection/open-share {:message universal-profile-url}) + on-share-press (rn/use-callback #(rf/dispatch [:open-share + {:options {:message universal-profile-url}}]) [universal-profile-url]) on-copy-press (rn/use-callback (fn [] (rf/dispatch [:share/copy-text-and-show-toast diff --git a/src/status_im/contexts/profile/settings/view.cljs b/src/status_im/contexts/profile/settings/view.cljs index 599eaaca3b..a20957ed39 100644 --- a/src/status_im/contexts/profile/settings/view.cljs +++ b/src/status_im/contexts/profile/settings/view.cljs @@ -5,7 +5,6 @@ [react-native.core :as rn] [react-native.reanimated :as reanimated] [react-native.safe-area :as safe-area] - [status-im.common.not-implemented :as not-implemented] [status-im.contexts.profile.settings.header.view :as settings.header] [status-im.contexts.profile.settings.list-items :as settings.items] [status-im.contexts.profile.settings.style :as style] @@ -62,7 +61,10 @@ :on-press #(rf/dispatch [:navigate-back]) :right-side [{:icon-name :i/qr-code :on-press #(debounce/throttle-and-dispatch [:open-modal :share-shell] 1000)} - {:icon-name :i/share :on-press not-implemented/alert}]}]] + {:icon-name :i/share + :on-press #(rf/dispatch [:open-share + {:options {:message (:universal-profile-url + profile)}}])}]}]] [rn/flat-list {:key :list :header [settings.header/view {:scroll-y scroll-y}] diff --git a/src/status_im/contexts/shell/share/profile/view.cljs b/src/status_im/contexts/shell/share/profile/view.cljs index 95d86fb7a8..aefff9e7be 100644 --- a/src/status_im/contexts/shell/share/profile/view.cljs +++ b/src/status_im/contexts/shell/share/profile/view.cljs @@ -1,7 +1,6 @@ (ns status-im.contexts.shell.share.profile.view (:require [clojure.string :as string] - [legacy.status-im.ui.components.list-selection :as list-selection] [quo.core :as quo] [quo.foundations.colors :as colors] [react-native.core :as rn] @@ -30,7 +29,7 @@ :width (- window-width (* style/screen-padding 2)) :qr-data universal-profile-url :qr-data-label-shown abbreviated-url - :on-share-press #(list-selection/open-share {:message universal-profile-url}) + :on-share-press #(rf/dispatch [:open-share {:options {:message universal-profile-url}}]) :on-text-press #(rf/dispatch [:share/copy-text-and-show-toast {:text-to-copy universal-profile-url :post-copy-message (i18n/label :t/link-to-profile-copied)}]) diff --git a/src/status_im/contexts/shell/share/wallet/view.cljs b/src/status_im/contexts/shell/share/wallet/view.cljs index a0aa7a7ee5..dd9e0fa669 100644 --- a/src/status_im/contexts/shell/share/wallet/view.cljs +++ b/src/status_im/contexts/shell/share/wallet/view.cljs @@ -4,7 +4,6 @@ [quo.core :as quo] [react-native.core :as rn] [react-native.platform :as platform] - [react-native.share :as share] [reagent.core :as reagent] [status-im.constants :as constants] [status-im.contexts.shell.share.style :as style] @@ -20,18 +19,18 @@ (defn- share-action [address share-title] - (share/open - (if platform/ios? - {:activityItemSources [{:placeholderItem {:type "text" - :content address} - :item {:default {:type "text" - :content - address}} - :linkMetadata {:title share-title}}]} - {:title share-title - :subject share-title - :message address - :isNewTask true}))) + (rf/dispatch [:open-share + {:options (if platform/ios? + {:activityItemSources [{:placeholderItem {:type :text + :content address} + :item {:default {:type :text + :content + address}} + :linkMetadata {:title share-title}}]} + {:title share-title + :subject share-title + :message address + :isNewTask true})}])) (defn- open-preferences [selected-networks account] diff --git a/src/status_im/contexts/wallet/account/share_address/view.cljs b/src/status_im/contexts/wallet/account/share_address/view.cljs index eddbfeede0..f2374c91e4 100644 --- a/src/status_im/contexts/wallet/account/share_address/view.cljs +++ b/src/status_im/contexts/wallet/account/share_address/view.cljs @@ -4,7 +4,6 @@ [react-native.core :as rn] [react-native.platform :as platform] [react-native.safe-area :as safe-area] - [react-native.share :as share] [reagent.core :as reagent] [status-im.constants :as constants] [status-im.contexts.wallet.account.share-address.style :as style] @@ -18,18 +17,18 @@ (defn- share-action [address share-title] - (share/open - (if platform/ios? - {:activityItemSources [{:placeholderItem {:type "text" - :content address} - :item {:default {:type "text" - :content - address}} - :linkMetadata {:title share-title}}]} - {:title share-title - :subject share-title - :message address - :isNewTask true}))) + (rf/dispatch [:open-share + {:options (if platform/ios? + {:activityItemSources [{:placeholderItem {:type :text + :content address} + :item {:default {:type :text + :content + address}} + :linkMetadata {:title share-title}}]} + {:title share-title + :subject share-title + :message address + :isNewTask true})}])) (defn- open-preferences [selected-networks] diff --git a/src/status_im/contexts/wallet/effects.cljs b/src/status_im/contexts/wallet/effects.cljs index 7c4037b17c..af5ab290f1 100644 --- a/src/status_im/contexts/wallet/effects.cljs +++ b/src/status_im/contexts/wallet/effects.cljs @@ -2,12 +2,7 @@ (:require [clojure.string :as string] [native-module.core :as native-module] - [re-frame.core :as rf] - [react-native.share :as share])) - -(rf/reg-fx :effects.share/open - (fn [content] - (share/open content))) + [re-frame.core :as rf])) (rf/reg-fx :effects.wallet/create-account-from-mnemonic diff --git a/src/status_im/contexts/wallet/events.cljs b/src/status_im/contexts/wallet/events.cljs index c8693dd3d4..1865ce38ac 100644 --- a/src/status_im/contexts/wallet/events.cljs +++ b/src/status_im/contexts/wallet/events.cljs @@ -402,16 +402,16 @@ (rf/reg-event-fx :wallet/share-account (fn [_ [{:keys [content title]}]] {:fx [[:effects.share/open - (if platform/ios? - {:activityItemSources - [{:placeholderItem {:type "text" - :content content} - :item {:default {:type "text" - :content content}} - :linkMetadata {:title title}}]} - {:title title - :subject title - :message content})]]})) + {:options (if platform/ios? + {:activityItemSources + [{:placeholderItem {:type :text + :content content} + :item {:default {:type :text + :content content}} + :linkMetadata {:title title}}]} + {:title title + :subject title + :message content})}]]})) (rf/reg-event-fx :wallet/blockchain-status-changed diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index c0da090607..23641cf36b 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -39,6 +39,7 @@ status-im.contexts.wallet.send.events status-im.contexts.wallet.signals [status-im.db :as db] + status-im.navigation.effects status-im.navigation.events [utils.re-frame :as rf])) diff --git a/src/status_im/navigation/effects.cljs b/src/status_im/navigation/effects.cljs index b422aed818..369c6a4394 100644 --- a/src/status_im/navigation/effects.cljs +++ b/src/status_im/navigation/effects.cljs @@ -5,6 +5,7 @@ [react-native.core :as rn] [react-native.navigation :as navigation] [react-native.platform :as platform] + [react-native.share :as share] [status-im.contexts.shell.jump-to.constants :as shell.constants] [status-im.contexts.shell.jump-to.utils :as jump-to.utils] [status-im.navigation.options :as options] @@ -183,6 +184,19 @@ (rf/reg-fx :open-modal-fx open-modal) +;;;; Share + +(rf/reg-fx :effects.share/open + (fn [{:keys [options on-success on-error]}] + (cond-> (share/open options) + (fn? on-success) (.then on-success) + :always (.catch (fn [error] + (log/error "Failed to share content" + {:error error + :effect :effects.share/open}) + (when (fn? on-error) + (on-error error))))))) + ;;;; Overlay (defn show-overlay diff --git a/src/status_im/navigation/events.cljs b/src/status_im/navigation/events.cljs index 96a6b6bed5..ad5b6d4e66 100644 --- a/src/status_im/navigation/events.cljs +++ b/src/status_im/navigation/events.cljs @@ -144,3 +144,9 @@ {:events [:reload-status-nav-color]} [{:keys [db]} view-id] {:reload-status-nav-color-fx (or view-id (:view-id db))}) + +(defn open-share + [_ [config]] + {:fx [[:effects.share/open config]]}) + +(rf/reg-event-fx :open-share open-share)