Skip to content

Commit

Permalink
images: add scan for qr code to image context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ericholguin committed Oct 3, 2023
1 parent 476f525 commit 282d0c7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
2 changes: 1 addition & 1 deletion damus/Components/ImageCarousel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct ImageCarousel: View {
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
.fullScreenCover(isPresented: $open_sheet) {
ImageView(video_controller: state.video, urls: urls, disable_animation: state.settings.disable_animation)
ImageView(video_controller: state.video, urls: urls, settings: state.settings)
}
.frame(height: height)
.onChange(of: selectedIndex) { value in
Expand Down
9 changes: 4 additions & 5 deletions damus/Views/Images/ImageContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import Kingfisher
struct ImageContainerView: View {
let video_controller: VideoController
let url: MediaUrl
let settings: UserSettingsStore

@State private var image: UIImage?
@State private var showShareSheet = false

let disable_animation: Bool

private struct ImageHandler: ImageModifier {
@Binding var handler: UIImage?

Expand All @@ -29,13 +28,13 @@ struct ImageContainerView: View {

func Img(url: URL) -> some View {
KFAnimatedImage(url)
.imageContext(.note, disable_animation: disable_animation)
.imageContext(.note, disable_animation: settings.disable_animation)
.configure { view in
view.framePreloadCount = 3
}
.imageModifier(ImageHandler(handler: $image))
.clipped()
.modifier(ImageContextMenuModifier(url: url, image: image, showShareSheet: $showShareSheet))
.modifier(ImageContextMenuModifier(url: url, image: image, settings: settings, showShareSheet: $showShareSheet))
.sheet(isPresented: $showShareSheet) {
ShareSheet(activityItems: [url])
}
Expand All @@ -57,6 +56,6 @@ let test_image_url = URL(string: "https://jb55.com/red-me.jpg")!

struct ImageContainerView_Previews: PreviewProvider {
static var previews: some View {
ImageContainerView(video_controller: test_damus_state.video, url: .image(test_image_url), disable_animation: false)
ImageContainerView(video_controller: test_damus_state.video, url: .image(test_image_url), settings: test_damus_state.settings)
}
}
48 changes: 48 additions & 0 deletions damus/Views/Images/ImageContextMenuModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ import UIKit
struct ImageContextMenuModifier: ViewModifier {
let url: URL?
let image: UIImage?
let settings: UserSettingsStore

@State var qrCodeLink: String = ""
@State var open_link_confirm: Bool = false
@State var no_link_found: Bool = false

@Binding var showShareSheet: Bool

@Environment(\.openURL) var openURL

func body(content: Content) -> some View {
return content.contextMenu {
Button {
Expand All @@ -32,12 +40,52 @@ struct ImageContextMenuModifier: ViewModifier {
} label: {
Label(NSLocalizedString("Save Image", comment: "Context menu option to save an image."), image: "download")
}
Button {
qrCodeLink = ""
guard let detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh]) else {
return
}
guard let ciImage = CIImage(image:someImage) else {
return
}
let features = detector.features(in: ciImage)
if let qrfeatures = features as? [CIQRCodeFeature] {
for feature in qrfeatures {
if let msgStr = feature.messageString {
qrCodeLink += msgStr
}
}
}

if qrCodeLink == "" {
no_link_found.toggle()
} else {
if qrCodeLink.contains("lnurl") {
open_with_wallet(wallet: settings.default_wallet.model, invoice: qrCodeLink)
} else if let _ = URL(string: qrCodeLink) {
open_link_confirm.toggle()
}
}
} label: {
Label(NSLocalizedString("Scan for QR Code", comment: "Context menu option to scan image for a QR Code."), image: "qr-code.fill")
}
}
Button {
showShareSheet = true
} label: {
Label(NSLocalizedString("Share", comment: "Button to share an image."), image: "upload")
}
}
.alert(NSLocalizedString("Found \(qrCodeLink).\nOpen link?", comment: "Alert message asking if the user wants to open the link."), isPresented: $open_link_confirm) {
Button(NSLocalizedString("Open", comment: "Button to proceed with opening link."), role: .none) {
if let url = URL(string: qrCodeLink) {
openURL(url)
}
}
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
}
.alert(NSLocalizedString("Unable to find a QR Code", comment: "Alert message letting user know a link was not found."), isPresented: $no_link_found) {
Button(NSLocalizedString("Dismiss", comment: "Button to dismiss alert"), role: .cancel) {}
}
}
}
6 changes: 3 additions & 3 deletions damus/Views/Images/ImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct ImageView: View {
@State private var selectedIndex = 0
@State var showMenu = true

let disable_animation: Bool
let settings: UserSettingsStore

var tabViewIndicator: some View {
HStack(spacing: 10) {
Expand All @@ -42,7 +42,7 @@ struct ImageView: View {
TabView(selection: $selectedIndex) {
ForEach(urls.indices, id: \.self) { index in
ZoomableScrollView {
ImageContainerView(video_controller: video_controller, url: urls[index], disable_animation: disable_animation)
ImageContainerView(video_controller: video_controller, url: urls[index], settings: settings)
.aspectRatio(contentMode: .fit)
.padding(.top, Theme.safeAreaInsets?.top)
.padding(.bottom, Theme.safeAreaInsets?.bottom)
Expand Down Expand Up @@ -85,6 +85,6 @@ struct ImageView: View {
struct ImageView_Previews: PreviewProvider {
static var previews: some View {
let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!)
ImageView(video_controller: test_damus_state.video, urls: [url], disable_animation: false)
ImageView(video_controller: test_damus_state.video, urls: [url], settings: test_damus_state.settings)
}
}
13 changes: 6 additions & 7 deletions damus/Views/Images/ProfilePicImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import Kingfisher

struct ProfileImageContainerView: View {
let url: URL?
let settings: UserSettingsStore

@State private var image: UIImage?
@State private var showShareSheet = false

let disable_animation: Bool

private struct ImageHandler: ImageModifier {
@Binding var handler: UIImage?

Expand All @@ -27,13 +26,13 @@ struct ProfileImageContainerView: View {
var body: some View {

KFAnimatedImage(url)
.imageContext(.pfp, disable_animation: disable_animation)
.imageContext(.pfp, disable_animation: settings.disable_animation)
.configure { view in
view.framePreloadCount = 3
}
.imageModifier(ImageHandler(handler: $image))
.clipShape(Circle())
.modifier(ImageContextMenuModifier(url: url, image: image, showShareSheet: $showShareSheet))
.modifier(ImageContextMenuModifier(url: url, image: image, settings: settings, showShareSheet: $showShareSheet))
.sheet(isPresented: $showShareSheet) {
ShareSheet(activityItems: [url])
}
Expand Down Expand Up @@ -64,7 +63,7 @@ struct NavDismissBarView: View {
struct ProfilePicImageView: View {
let pubkey: Pubkey
let profiles: Profiles
let disable_animation: Bool
let settings: UserSettingsStore

@Environment(\.presentationMode) var presentationMode

Expand All @@ -74,7 +73,7 @@ struct ProfilePicImageView: View {
.ignoresSafeArea()

ZoomableScrollView {
ProfileImageContainerView(url: get_profile_url(picture: nil, pubkey: pubkey, profiles: profiles), disable_animation: disable_animation)
ProfileImageContainerView(url: get_profile_url(picture: nil, pubkey: pubkey, profiles: profiles), settings: settings)
.aspectRatio(contentMode: .fit)
.padding(.top, Theme.safeAreaInsets?.top)
.padding(.bottom, Theme.safeAreaInsets?.bottom)
Expand All @@ -94,7 +93,7 @@ struct ProfileZoomView_Previews: PreviewProvider {
ProfilePicImageView(
pubkey: test_pubkey,
profiles: make_preview_profiles(test_pubkey),
disable_animation: false
settings: test_damus_state.settings
)
}
}
2 changes: 1 addition & 1 deletion damus/Views/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ struct ProfileView: View {
is_zoomed.toggle()
}
.fullScreenCover(isPresented: $is_zoomed) {
ProfilePicImageView(pubkey: profile.pubkey, profiles: damus_state.profiles, disable_animation: damus_state.settings.disable_animation)
ProfilePicImageView(pubkey: profile.pubkey, profiles: damus_state.profiles, settings: damus_state.settings)
}

Spacer()
Expand Down

0 comments on commit 282d0c7

Please sign in to comment.