Skip to content

Commit

Permalink
made image share share image directly instead of url
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews committed Nov 19, 2023
1 parent 4e5a315 commit 6dd34ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
24 changes: 21 additions & 3 deletions Mlem/Models/User-Interactable/Menu Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
//

import Foundation
import SwiftUI

enum MenuFunction: Identifiable {
var id: String {
switch self {
case let .standard(standardMenuFunction):
return standardMenuFunction.id
case let .share(shareMenuFunction):
case let .shareUrl(shareMenuFunction):
return shareMenuFunction.id
case let .shareImage(shareImageFunction):
return shareImageFunction.id
}
}

case standard(StandardMenuFunction)
case share(ShareMenuFunction)
case shareUrl(ShareMenuFunction)
case shareImage(ShareImageFunction)
}

// some convenience initializers because MenuFunction.standard(StandardMenuFunction...) is ugly
Expand All @@ -40,7 +44,11 @@ extension MenuFunction {
}

static func shareMenuFunction(url: URL) -> MenuFunction {
MenuFunction.share(ShareMenuFunction(url: url))
MenuFunction.shareUrl(ShareMenuFunction(url: url))
}

static func shareImageFunction(image: Image) -> MenuFunction {
MenuFunction.shareImage(ShareImageFunction(image: image))
}
}

Expand All @@ -51,6 +59,16 @@ struct ShareMenuFunction: Identifiable {
let url: URL
}

struct ShareImageFunction: Identifiable {
let id: String
let image: Image

init(image: Image) {
self.image = image
self.id = UUID().uuidString
}
}

/// MenuFunction to perform a generic menu action
struct StandardMenuFunction: Identifiable {
var id: String { text }
Expand Down
6 changes: 3 additions & 3 deletions Mlem/Temp Image Viewer/ZoomableImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct ZoomableImageView: View {
.scaledToFit()
.scaleEffect(zoom)
.contextMenu {
ForEach(genMenuFunctions()) { item in
ForEach(genMenuFunctions(image: image)) { item in
MenuButton(menuFunction: item, confirmDestructive: nil)
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ struct ZoomableImageView: View {
}
}

func genMenuFunctions() -> [MenuFunction] {
func genMenuFunctions(image: Image) -> [MenuFunction] {
var ret: [MenuFunction] = .init()

ret.append(MenuFunction.standardMenuFunction(
Expand All @@ -98,7 +98,7 @@ struct ZoomableImageView: View {
}
})

ret.append(MenuFunction.share(ShareMenuFunction(url: url)))
ret.append(MenuFunction.shareImageFunction(image: image))

return ret
}
Expand Down
4 changes: 3 additions & 1 deletion Mlem/Views/Shared/Components/Components/Menu Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ struct MenuButton: View {

var body: some View {
switch menuFunction {
case let .share(shareMenuFunction):
case let .shareUrl(shareMenuFunction):
ShareLink(item: shareMenuFunction.url)
case let .shareImage(shareImageFunction):
ShareLink(item: shareImageFunction.image, preview: .init("photo", image: shareImageFunction.image))
case let .standard(standardMenuFunction):
let role: ButtonRole? = standardMenuFunction.destructiveActionPrompt != nil ? .destructive : nil
Button(role: role) {
Expand Down

0 comments on commit 6dd34ac

Please sign in to comment.