-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentView.swift
136 lines (119 loc) · 3.59 KB
/
ContentView.swift
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// ContentView.swift
// base16384
//
// Created by oboard on 2024/2/12.
//
import SwiftUI
extension View {
/// Layers the given views behind this ``TextEditor``.
func clearTextEditorBackground() -> some View {
// if #available(iOS 16.0, *) {
// return self.scrollContentBackground(.hidden)
// } else {
UITextView.appearance().backgroundColor = .clear
return self
// }
}
}
struct ContentView: View {
init() {
submit()
}
// @Environment(\.modelContext) private var modelContext
// @Query private var items: [Item]
@State private var textInput: String = "Example"
@State private var textOutput: String = "彞吖菁穥㴀"
@State private var selectedTab = 0
#if os(tvOS)
var content: some View {
VStack {
TextField("", text: $textInput)
.onChange(of: textInput) { newValue in
submit()
}
TextField("", text: $textOutput)
}.padding().background(.thickMaterial).cornerRadius(10)
}
#else
var content: some View {
VStack {
TextEditor(text: $textInput)
.onChange(of: textInput) {
newValue in
submit()
}
.padding()
.clearTextEditorBackground()
.background(.thickMaterial)
.cornerRadius(10)
// .border(.ultraThickMaterial)
// .buttonBorderShape(.roundedRectangle)
// .scrollContentBackground(.hidden)
TextEditor(text: $textOutput)
.padding()
.clearTextEditorBackground()
// .scrollContentBackground(.hidden)
.background(.ultraThickMaterial)
.cornerRadius(10)
}.padding()
}
#endif
var body: some View {
#if os(iOS)
NavigationView {
TabView(selection: $selectedTab) {
content
.tabItem {
Image(systemName: "lock")
Text("Encode")
}.tag(0)
content
.tabItem {
Image(systemName: "key")
Text("Decode")
}.tag(1)
}
.onChange(of: selectedTab) {
newTab in
swap(&textInput, &textOutput)
submit()
}.navigationTitle("base16384")
}
#else
TabView(selection: $selectedTab) {
content
.tabItem {
Image(systemName: "lock")
Text("Encode")
}.tag(0)
content
.tabItem {
Image(systemName: "key")
Text("Decode")
}.tag(1)
}
.onChange(of: selectedTab) { newTab in
swap(&textInput, &textOutput)
submit()
}
.frame(minWidth: 400, minHeight: 600)
.padding()
#endif
}
private func submit() {
if (selectedTab == 0){
textOutput = toSource(input: encode(input: toUint8Array(source: textInput)))
} else {
textOutput = toSource8(input: decode(input: toUint16Array(source: textInput)))
}
}
private func clearText() {
withAnimation {
textInput = ""
}
}
}
#Preview {
ContentView()
}