Skip to content

Commit

Permalink
iOS version specific action sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
anas-p committed Dec 4, 2021
1 parent 4d3fbc1 commit fd974dc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
16 changes: 14 additions & 2 deletions ImagePicker-SwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
58A1406227046E45002EEE11 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 58A1406127046E45002EEE11 /* Assets.xcassets */; };
58A1406527046E45002EEE11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 58A1406427046E45002EEE11 /* Preview Assets.xcassets */; };
58A1406C27046FDE002EEE11 /* ImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58A1406B27046FDE002EEE11 /* ImagePicker.swift */; };
58A37A86275B2A11002977D4 /* ActionSheet+Modifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58A37A85275B2A11002977D4 /* ActionSheet+Modifier.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -21,6 +22,7 @@
58A1406127046E45002EEE11 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
58A1406427046E45002EEE11 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
58A1406B27046FDE002EEE11 /* ImagePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePicker.swift; sourceTree = "<group>"; };
58A37A85275B2A11002977D4 /* ActionSheet+Modifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ActionSheet+Modifier.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -53,6 +55,7 @@
58A1405C27046E44002EEE11 /* ImagePicker-SwiftUI */ = {
isa = PBXGroup;
children = (
58A37A84275B29F7002977D4 /* View Modifiers */,
58A1405D27046E44002EEE11 /* ImagePicker_SwiftUIApp.swift */,
58A1405F27046E44002EEE11 /* ContentView.swift */,
58A1406B27046FDE002EEE11 /* ImagePicker.swift */,
Expand All @@ -70,6 +73,14 @@
path = "Preview Content";
sourceTree = "<group>";
};
58A37A84275B29F7002977D4 /* View Modifiers */ = {
isa = PBXGroup;
children = (
58A37A85275B2A11002977D4 /* ActionSheet+Modifier.swift */,
);
path = "View Modifiers";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -142,6 +153,7 @@
files = (
58A1406C27046FDE002EEE11 /* ImagePicker.swift in Sources */,
58A1406027046E44002EEE11 /* ContentView.swift in Sources */,
58A37A86275B2A11002977D4 /* ActionSheet+Modifier.swift in Sources */,
58A1405E27046E44002EEE11 /* ImagePicker_SwiftUIApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -283,7 +295,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -315,7 +327,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
25 changes: 12 additions & 13 deletions ImagePicker-SwiftUI/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import SwiftUI

/// This view shows a Image view and a Button,
/// Tap on both views will display an action sheet to choose image picker
///
struct ContentView: View {

@State private var showImagePickerOptions: Bool = false
Expand All @@ -25,7 +28,7 @@ struct ContentView: View {
.padding(.top, 60)


Button("Add Photo", action: {
Button(Localization.addPhotoTitle, action: {
showImagePickerOptions = true
})
.font(.system(size: 17))
Expand All @@ -34,18 +37,8 @@ struct ContentView: View {
.foregroundColor(Color.white)
.cornerRadius(10)
.padding(.top, 40)
.confirmationDialog("Choose", isPresented: $showImagePickerOptions, titleVisibility: .visible) {
Button("Photo Library") {
sourceType = .photoLibrary
showImagePicker = true
}

Button("Camera") {
sourceType = .camera
showImagePicker = true
}
}

.ActionSheet(showImagePickerOptions: $showImagePickerOptions, showImagePicker: $showImagePicker, sourceType: $sourceType)

Spacer()
}
.sheet(isPresented: $showImagePicker) {
Expand All @@ -59,3 +52,9 @@ struct ContentView_Previews: PreviewProvider {
ContentView()
}
}

// MARK: - Localization

private enum Localization {
static let addPhotoTitle = NSLocalizedString("Add Photo", comment: "Button title for Add Photo")
}
5 changes: 2 additions & 3 deletions ImagePicker-SwiftUI/ImagePicker.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import SwiftUI

/// Image Picker Representable
///
struct ImagePicker: UIViewControllerRepresentable {

typealias imagePickerController = UIImagePickerController
Expand Down Expand Up @@ -47,7 +49,4 @@ struct ImagePicker: UIViewControllerRepresentable {
isShown = false
}
}



}
74 changes: 74 additions & 0 deletions ImagePicker-SwiftUI/View Modifiers/ActionSheet+Modifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

import SwiftUI

/// iOS version specific action sheet
///
/// Action Sheet modifier to support iOS 14, iOS 15 and later
///
struct ActionSheetModifier: ViewModifier {

@Binding var showImagePickerOptions: Bool
@Binding var showImagePicker: Bool
@Binding var sourceType: UIImagePickerController.SourceType

func body(content: Content) -> some View {
if #available(iOS 15.0, *) {
content
.confirmationDialog(Localization.chooseTitle, isPresented: $showImagePickerOptions, titleVisibility: .visible) {
Button(Localization.photoLibraryTitle) {
sourceType = .photoLibrary
showImagePicker = true
}

Button(Localization.cameraTitle) {
sourceType = .camera
showImagePicker = true
}
}
}
else {
content
.actionSheet(isPresented: $showImagePickerOptions) {
ActionSheet(title: Text(Localization.chooseTitle), buttons: [
.default(Text(Localization.photoLibraryTitle)) {
sourceType = .photoLibrary
showImagePicker = true
},

.default(Text(Localization.cameraTitle)) {
sourceType = .camera
showImagePicker = true

},
.cancel()
])
}
}
}
}

// MARK: View extension

extension View {

/// iOS version specific action sheet
///
/// Action Sheet modifier to support iOS 14, iOS 15 and later
///
func ActionSheet(showImagePickerOptions: Binding<Bool>, showImagePicker: Binding<Bool>, sourceType: Binding<UIImagePickerController.SourceType>) -> some View {
// Conditional check has to be done inside the Group function builder,
// otherwise the iOS 15 modifier will be loaded into memory and the app will crash.
Group {
self.modifier(ActionSheetModifier(showImagePickerOptions: showImagePickerOptions, showImagePicker: showImagePicker, sourceType: sourceType))
}
}
}

// MARK: Localization

private enum Localization {
static let chooseTitle = NSLocalizedString("Choose", comment: "Action sheet title")
static let photoLibraryTitle = NSLocalizedString("Photo Library", comment: "Action sheet text for `Photo Library` option")
static let cameraTitle = NSLocalizedString("Camera", comment: " Action sheet text `Camera` option")
}

0 comments on commit fd974dc

Please sign in to comment.