Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept limited photo library authorization #22

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions Queryable/Queryable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
94E917DF2A5A47A300324937 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 94E917D72A5A47A300324937 /* Localizable.strings */; };
94E917E02A5A47A300324937 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 94E917DA2A5A47A300324937 /* Localizable.strings */; };
94ECA38F2A5D52060066F64B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 94ECA38E2A5D52050066F64B /* Assets.xcassets */; };

DE4D625A2AECD3CF007AB439 /* FeedbackItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE4D62592AECD3CF007AB439 /* FeedbackItemView.swift */; };

/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -105,14 +107,17 @@
94E917D82A5A47A300324937 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = Localizable.strings; sourceTree = "<group>"; };
94E917DB2A5A47A300324937 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = Localizable.strings; sourceTree = "<group>"; };
94ECA38E2A5D52050066F64B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };

DE4D62592AECD3CF007AB439 /* FeedbackItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbackItemView.swift; sourceTree = "<group>"; };

/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
94E917442A5A45C000324937 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D78D1FE82AF0790F004B45E4 /* PhotosUI.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -143,6 +148,7 @@
94E917D62A5A47A300324937 /* it.lproj */,
94E917492A5A45C000324937 /* Queryable */,
94E917482A5A45C000324937 /* Products */,
D78D1FE62AF0790F004B45E4 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand All @@ -159,6 +165,7 @@
94E917492A5A45C000324937 /* Queryable */ = {
isa = PBXGroup;
children = (
D78D1FE92AF0894B004B45E4 /* Info.plist */,
4D3B1D632A5F95300004C06F /* CoreMLModels */,
94E9178C2A5A475B00324937 /* CLIP */,
94E917922A5A475B00324937 /* Model */,
Expand Down Expand Up @@ -230,6 +237,7 @@
94E917A42A5A475B00324937 /* ViewForMac */,
94E917A92A5A475B00324937 /* SearchBarView.swift */,
94E917AA2A5A475B00324937 /* SearchResultsView.swift */,
D78D1FE42AF073F8004B45E4 /* LimitedLibraryPicker.swift */,
94E917AB2A5A475B00324937 /* ConfigPageViews */,
94E917AE2A5A475B00324937 /* ContentView.swift */,
);
Expand Down Expand Up @@ -316,6 +324,14 @@
path = es.lproj;
sourceTree = "<group>";
};
D78D1FE62AF0790F004B45E4 /* Frameworks */ = {
isa = PBXGroup;
children = (
D78D1FE72AF0790F004B45E4 /* PhotosUI.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -485,6 +501,7 @@
94E917C32A5A475B00324937 /* PhotoCollectionViewForMac.swift in Sources */,
94E917CB2A5A475B00324937 /* ContentView.swift in Sources */,
94E917C12A5A475B00324937 /* PhotoItemView.swift in Sources */,
D78D1FE52AF073F8004B45E4 /* LimitedLibraryPicker.swift in Sources */,
94E917C62A5A475B00324937 /* SearchResultViewForMac.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -687,6 +704,7 @@
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Queryable/Info.plist;
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Allow access to all your photos for offline search.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down Expand Up @@ -718,6 +736,7 @@
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Queryable/Info.plist;
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Allow access to all your photos for offline search.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down
8 changes: 8 additions & 0 deletions Queryable/Queryable/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PHPhotoLibraryPreventAutomaticLimitedAccessAlert</key>
<true/>
</dict>
</plist>
9 changes: 4 additions & 5 deletions Queryable/Queryable/PhotoHelper/PhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import Photos
import os.log

class PhotoLibrary {

static func checkAuthorization() async -> Bool {
switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
static func checkAuthorization(status: PHAuthorizationStatus = PHPhotoLibrary.authorizationStatus(for: .readWrite)) async -> Bool {
switch status {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops almost forgot to swap this out

I think I don't like using the default argument in this way, but waiting for feedback re: logs & style before changing anything

case .authorized:
logger.debug("Photo library access authorized.")
return true
case .notDetermined:
logger.debug("Photo library access not determined.")
return await PHPhotoLibrary.requestAuthorization(for: .readWrite) == .authorized
return await checkAuthorization(status: PHPhotoLibrary.requestAuthorization(for: .readWrite))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think this fixes the issue! I was only considering limited authorization when checking if already authorized. I also needed to accept limited authorization at the time of request.

Let me know if you want me to restructure this code somehow. I think the recursive nature is a bit weird, but I assume the OS won't return notDetermined repeatedly. (Maybe they'll add a cancel button in the future that changes that though?)

It's worth noting there's a minor logging behavior change here. Previously, you wouldn't log the new status after requesting authorization. Now, we do.

If you want me to refactor this, let me know how! If logging new status after request isn't your preference, I can just do something simple like [.authorized, .limited].contains(PHPhotoLibrary.requestAuthorization(for: .readWrite)).

case .denied:
logger.debug("Photo library access denied.")
return false
case .limited:
logger.debug("Photo library access limited.")
return false
return true
case .restricted:
logger.debug("Photo library access restricted.")
return false
Expand Down
46 changes: 46 additions & 0 deletions Queryable/Queryable/View/LimitedLibraryPicker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// LimitedLibraryPicker.swift
// Queryable
//
// Created by Jaden Geller on 10/30/23.
//

import SwiftUI
import PhotosUI

struct LimitedLibraryPicker: UIViewControllerRepresentable {
@Binding var isPresented: Bool

func makeUIViewController(context: Context) -> UIViewController {
.init()
}

class Coordinator {
var isPresented = false
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator tracks whether or not the picker is actually presented. Binding tracks whether or not the view wants it to be presented.

This allows us to: (a) prevent attempting to present the picker again when it is already presented, and (b) update the binding value back to true if the picker is still presented (since we cannot programmatically dismiss).

Technically, neither of these code paths are exercised by the app, but it seemed worth implementing this properly in case the calling code changes.

}
func makeCoordinator() -> Coordinator {
.init()
}

func updateUIViewController(_ controller: UIViewController, context: Context) {
if isPresented, !context.coordinator.isPresented {
Task {
context.coordinator.isPresented = true
await PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: controller)
context.coordinator.isPresented = false
await MainActor.run { isPresented = false }
}
}
else if !isPresented, context.coordinator.isPresented {
Task {
await MainActor.run { isPresented = true }
}
}
}
}

extension View {
func limitedLibraryPicker(isPresented: Binding<Bool>) -> some View {
overlay(LimitedLibraryPicker(isPresented: isPresented))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't actually matter if this is an overlay, background, etc.

It just matters that the UIViewControllerRepresentable is part of the view hierarchy so it can get a reference to a UIViewController to present the limited library picker.

}
}
17 changes: 14 additions & 3 deletions Queryable/Queryable/View/SearchResultsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct SearchResultsView: View {
case .HAS_RESULT:
// Has result
VStack {
if photoSearcher.totalUnIndexedPhotosNum > 0 {
if photoSearcher.totalUnIndexedPhotosNum > 0 || PHPhotoLibrary.authorizationStatus(for: .readWrite) == .limited {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always show the button if the library has a limited authorization status, since it's always true that there may be more photos the user could index.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable.

UpdateIndexView(goToIndexView: $goToIndexView, photoSearcher: photoSearcher)
}

Expand Down Expand Up @@ -248,6 +248,7 @@ struct TipsView: View {


struct UpdateIndexView: View {
@State var showLimitedLibraryPicker = false
@Binding var goToIndexView: Bool
@ObservedObject var photoSearcher: PhotoSearcher

Expand All @@ -256,13 +257,23 @@ struct UpdateIndexView: View {
.accessibilityAddTraits(.isLink)
.accessibilityHint(Text("Click to build index for new photos to make them searchable"))
.foregroundColor(Color.weakgreen)
.limitedLibraryPicker(isPresented: $showLimitedLibraryPicker)
.onTapGesture {
goToIndexView = true
if PHPhotoLibrary.authorizationStatus(for: .readWrite) == .limited {
showLimitedLibraryPicker = true
} else {
goToIndexView = true
}
}
.onChange(of: showLimitedLibraryPicker) { showLimitedLibraryPicker in
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a confusing state machine, but I don't know of a better way to represent this here.

If the authorization is limited, instead of immediately going to index view, show the picker. Then, when the picker is dismissed, refetch the photos and go to the index view.

if !showLimitedLibraryPicker {
Task { await photoSearcher.fetchPhotos() }
goToIndexView = true
}
}
}
}


struct SearchResultsView_Previews: PreviewProvider {
static var previews: some View {
SearchResultsView(goToIndexView: .constant(false), photoSearcher: PhotoSearcher())
Expand Down