summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/HelperViews/CopyShare.swift
blob: 078244af126d1b71201e796b3188db4a67a79727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import UniformTypeIdentifiers
import SwiftUI
import SymLog

struct CopyButton: View {
    private let symLog = SymLogV(0)
    @Environment(\.isEnabled) private var isEnabled: Bool
    let textToCopy: String
    let vertical: Bool

    func copyAction() -> Void {
        symLog.log(textToCopy)
        UIPasteboard.general.setValue(textToCopy,
                                      forPasteboardType: UTType.plainText.identifier)
    }

    var body: some View {
        Button(action: copyAction) {
            if vertical {
                VStack {
                    Image(systemName: "doc.on.doc")
                        .accessibility(hidden: true)
                    Text("Copy", comment: "5 letters max, else abbreviate")
                }
            } else {
                HStack {
                    Image(systemName: "doc.on.doc")
                        .accessibility(hidden: true)
                    Text("Copy", comment: "may be a bit longer")
                }
            }
        }
        .accessibilityFont(.body)
        .disabled(!isEnabled)
    }
}
// MARK: -
@MainActor
struct ShareButton: View {
    private let symLog = SymLogV(0)
    @Environment(\.isEnabled) private var isEnabled: Bool

    let textToShare: String

    func shareAction() -> Void {
        symLog.log(textToShare)
        ShareSheet.shareSheet(url: textToShare)
    }

    var body: some View {
        Button(action: shareAction) {
            HStack {
                Image(systemName: "square.and.arrow.up")
                    .accessibility(hidden: true)
                Text("Share")
            }
        }
        .accessibilityFont(.body)
        .disabled(!isEnabled)
    }
}
// MARK: -
struct CopyShare: View {
    @Environment(\.isEnabled) private var isEnabled: Bool

    let textToCopy: String

    var body: some View {
        HStack {
            CopyButton(textToCopy: textToCopy, vertical: false)
                .buttonStyle(TalerButtonStyle(type: .bordered))
            ShareButton(textToShare: textToCopy)
                .buttonStyle(TalerButtonStyle(type: .bordered))
        } // two buttons
    }
}
// MARK: -
struct CopyShare_Previews: PreviewProvider {
    static var previews: some View {
        CopyShare(textToCopy: "Hallö")
    }
}