From fd88496dcbc784ce3c0c4481c56b8c9f770e2dee Mon Sep 17 00:00:00 2001 From: Taufi Date: Tue, 22 Oct 2024 18:52:53 +0200 Subject: [PATCH 1/2] fix image download bug (#2131) --- IceCubesApp.xcodeproj/project.pbxproj | 2 ++ .../App/IceCubesApp-release.entitlements | 2 ++ IceCubesApp/App/IceCubesApp.entitlements | 2 ++ .../MediaUI/Sources/MediaUI/MediaUIView.swift | 17 ++++++++++++++--- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/IceCubesApp.xcodeproj/project.pbxproj b/IceCubesApp.xcodeproj/project.pbxproj index d92541c997..2764c57b44 100644 --- a/IceCubesApp.xcodeproj/project.pbxproj +++ b/IceCubesApp.xcodeproj/project.pbxproj @@ -1545,6 +1545,7 @@ INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; INFOPLIST_KEY_NSCameraUsageDescription = "Upload photos & videos to attach to your Mastodon posts."; INFOPLIST_KEY_NSHumanReadableCopyright = "© 2024 Thomas Ricouard"; + INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = ""; INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Upload photos & videos to Mastodon"; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; @@ -1611,6 +1612,7 @@ INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; INFOPLIST_KEY_NSCameraUsageDescription = "Upload photos & videos to attach to your Mastodon posts."; INFOPLIST_KEY_NSHumanReadableCopyright = "© 2024 Thomas Ricouard"; + INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = ""; INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Upload photos & videos to Mastodon"; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; diff --git a/IceCubesApp/App/IceCubesApp-release.entitlements b/IceCubesApp/App/IceCubesApp-release.entitlements index 4ed8052590..b0793e6002 100644 --- a/IceCubesApp/App/IceCubesApp-release.entitlements +++ b/IceCubesApp/App/IceCubesApp-release.entitlements @@ -28,6 +28,8 @@ com.apple.security.network.client + com.apple.security.personal-information.photos-library + keychain-access-groups $(AppIdentifierPrefix)$(BUNDLE_ID_PREFIX).IceCubesApp diff --git a/IceCubesApp/App/IceCubesApp.entitlements b/IceCubesApp/App/IceCubesApp.entitlements index 4ed8052590..b0793e6002 100644 --- a/IceCubesApp/App/IceCubesApp.entitlements +++ b/IceCubesApp/App/IceCubesApp.entitlements @@ -28,6 +28,8 @@ com.apple.security.network.client + com.apple.security.personal-information.photos-library + keychain-access-groups $(AppIdentifierPrefix)$(BUNDLE_ID_PREFIX).IceCubesApp diff --git a/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift b/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift index a8c096d8b2..54e75ba849 100644 --- a/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift +++ b/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift @@ -3,6 +3,7 @@ import Models import Nuke import QuickLook import SwiftUI +import Photos public struct MediaUIView: View, @unchecked Sendable { private let data: [DisplayData] @@ -144,6 +145,8 @@ private struct SavePhotoToolbarItem: ToolbarContent, @unchecked Sendable { state = .unsaved } } + } else { + state = .unsaved } } } label: { @@ -180,11 +183,19 @@ private struct SavePhotoToolbarItem: ToolbarContent, @unchecked Sendable { } return nil } - + private func saveImage(url: URL) async -> Bool { + var status = PHPhotoLibrary.authorizationStatus(for: .addOnly) + if let image = try? await uiimageFor(url: url) { - UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) - return true + if status != .authorized { + await PHPhotoLibrary.requestAuthorization(for: .addOnly) + status = PHPhotoLibrary.authorizationStatus(for: .addOnly) + } + if status == .authorized { + UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) + return true + } } return false } From 49899ccb2cbe54e584ec2fdfef95bb7dae103192 Mon Sep 17 00:00:00 2001 From: Taufi Date: Thu, 14 Nov 2024 18:12:13 +0100 Subject: [PATCH 2/2] Improved PR following reviewer recommendations --- IceCubesApp.xcodeproj/project.pbxproj | 4 ++-- .../MediaUI/Sources/MediaUI/MediaUIView.swift | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/IceCubesApp.xcodeproj/project.pbxproj b/IceCubesApp.xcodeproj/project.pbxproj index 2764c57b44..8d44e97538 100644 --- a/IceCubesApp.xcodeproj/project.pbxproj +++ b/IceCubesApp.xcodeproj/project.pbxproj @@ -1545,7 +1545,7 @@ INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; INFOPLIST_KEY_NSCameraUsageDescription = "Upload photos & videos to attach to your Mastodon posts."; INFOPLIST_KEY_NSHumanReadableCopyright = "© 2024 Thomas Ricouard"; - INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = ""; + INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = "Ice Cubes would like to save the selected photo in your photo library."; INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Upload photos & videos to Mastodon"; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; @@ -1612,7 +1612,7 @@ INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; INFOPLIST_KEY_NSCameraUsageDescription = "Upload photos & videos to attach to your Mastodon posts."; INFOPLIST_KEY_NSHumanReadableCopyright = "© 2024 Thomas Ricouard"; - INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = ""; + INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = "Ice Cubes would like to save the selected photo in your photo library."; INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Upload photos & videos to Mastodon"; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; diff --git a/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift b/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift index 54e75ba849..0023d4a80c 100644 --- a/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift +++ b/Packages/MediaUI/Sources/MediaUI/MediaUIView.swift @@ -185,17 +185,17 @@ private struct SavePhotoToolbarItem: ToolbarContent, @unchecked Sendable { } private func saveImage(url: URL) async -> Bool { + guard let image = try? await uiimageFor(url: url) else { return false } + var status = PHPhotoLibrary.authorizationStatus(for: .addOnly) - if let image = try? await uiimageFor(url: url) { - if status != .authorized { - await PHPhotoLibrary.requestAuthorization(for: .addOnly) - status = PHPhotoLibrary.authorizationStatus(for: .addOnly) - } - if status == .authorized { - UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) - return true - } + if status != .authorized { + await PHPhotoLibrary.requestAuthorization(for: .addOnly) + status = PHPhotoLibrary.authorizationStatus(for: .addOnly) + } + if status == .authorized { + UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) + return true } return false }