Skip to content

Commit 850d515

Browse files
committed
Show/Hide History Button + Remote Data (JSON URL / Firebase RTDB)
1 parent 17681b6 commit 850d515

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

Sources/SwiftNEW/SwiftNEW.swift

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public struct SwiftNEW: View {
1212

1313
@State var items: [Vmodel] = []
1414
@State var loading = true
15+
@State var historySheet: Bool = false
1516

1617
@Binding var show: Bool
1718
@Binding var align: HorizontalAlignment
@@ -20,17 +21,19 @@ public struct SwiftNEW: View {
2021
@Binding var labelColor: Color
2122
@Binding var label: String
2223
@Binding var labelImage: String
23-
24-
@State var showHistory: Bool = false
24+
@Binding var history: Bool
25+
@Binding var data: String
2526

26-
public init( show: Binding<Bool>, align: Binding<HorizontalAlignment>, color: Binding<Color>, size: Binding<String>, labelColor: Binding<Color>, label: Binding<String>, labelImage: Binding<String>) {
27+
public init( show: Binding<Bool>, align: Binding<HorizontalAlignment>, color: Binding<Color>, size: Binding<String>, labelColor: Binding<Color>, label: Binding<String>, labelImage: Binding<String>, history: Binding<Bool>, data: Binding<String>) {
2728
_show = show
2829
_align = align
2930
_color = color
3031
_size = size
3132
_labelColor = labelColor
3233
_label = label
3334
_labelImage = labelImage
35+
_history = history
36+
_data = data
3437
compareVersion()
3538
}
3639

@@ -50,14 +53,14 @@ public struct SwiftNEW: View {
5053
}
5154
}
5255
.sheet(isPresented: $show) {
53-
sheetContent
54-
.sheet(isPresented: $showHistory) {
56+
sheetCurrent
57+
.sheet(isPresented: $historySheet) {
5558
sheetHistory
5659
}
5760
}
5861
}
5962

60-
public var sheetContent: some View {
63+
public var sheetCurrent: some View {
6164
VStack(alignment: align) {
6265
Spacer()
6366
Text("What's New?").bold().font(.largeTitle)
@@ -93,12 +96,14 @@ public struct SwiftNEW: View {
9396
}.frame(width: 300, height: 450)
9497
}
9598
Spacer()
96-
Button(action: { showHistory = true }) {
97-
HStack {
98-
Text("Show History")
99-
Image(systemName: "arrow.up.bin")
100-
}.font(.body)
101-
}.foregroundColor(color)
99+
if history {
100+
Button(action: { historySheet = true }) {
101+
HStack {
102+
Text("Show History")
103+
Image(systemName: "arrow.up.bin")
104+
}.font(.body)
105+
}.foregroundColor(color)
106+
}
102107
Button(action: { show = false }) {
103108
HStack{
104109
Text("Continue").bold()
@@ -156,7 +161,7 @@ public struct SwiftNEW: View {
156161
}
157162
}.frame(width: 300, height: 450)
158163
Spacer()
159-
Button(action: { showHistory = false }) {
164+
Button(action: { historySheet = false }) {
160165
HStack{
161166
Text("Return").bold()
162167
Image(systemName: "arrow.down.circle.fill")
@@ -188,15 +193,31 @@ public struct SwiftNEW: View {
188193
}
189194
}
190195
public func loadData() {
191-
// MARK :- Local Data
192-
if let url = Bundle.main.url(forResource: "data", withExtension: "json") {
193-
do {
194-
let data = try Data(contentsOf: url)
195-
let decoder = JSONDecoder()
196-
items = try decoder.decode([Vmodel].self, from: data)
197-
loading = false
198-
} catch {
199-
print("error: \(error)")
196+
if data.contains("http") {
197+
// MARK: Remote Data
198+
let url = URL(string: data)
199+
URLSession.shared.dataTask(with: url!) { data, response, error in
200+
if let data = data {
201+
do {
202+
let decoder = JSONDecoder()
203+
items = try decoder.decode([Vmodel].self, from: data)
204+
self.loading = false
205+
} catch {
206+
print(error)
207+
}
208+
}
209+
}.resume()
210+
} else {
211+
// MARK: Local Data
212+
if let url = Bundle.main.url(forResource: data, withExtension: "json") {
213+
do {
214+
let data = try Data(contentsOf: url)
215+
let decoder = JSONDecoder()
216+
items = try decoder.decode([Vmodel].self, from: data)
217+
loading = false
218+
} catch {
219+
print("error: \(error)")
220+
}
200221
}
201222
}
202223
}
@@ -213,9 +234,3 @@ public struct Model: Codable, Hashable {
213234
var subtitle: String
214235
var body: String
215236
}
216-
217-
//struct Previews: PreviewProvider {
218-
// static var previews: some View {
219-
//
220-
// }
221-
//}

0 commit comments

Comments
 (0)