/
ContentView.swift
118 lines (94 loc) · 2.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
//
// ContentView.swift
// Project1-2SwiftUI
//
// Created by Tanin on 26/08/2019.
// Copyright © 2019 landtanin. All rights reserved.
//
import SwiftUI
import Combine
class DataSource: ObservableObject {
var willChange = PassthroughSubject<Void, Never>()
var images = [String]()
init() {
let fm = FileManager.default
guard let path = Bundle.main.resourcePath else { return }
guard let items = try? fm.contentsOfDirectory(atPath: path) else { return }
for item in items {
if item.hasPrefix("nssl") {
images.append(item)
}
}
willChange.send(())
}
}
/*
extension String: Identifiable {
// not the best
/*
public var id: UnsafeMutableRawPointer {
return Unmanaged.passUnretained(self as AnyObject).toOpaque()
}
*/
// not bad
/*
public var id: String {
self
}
*/
// not reusable
/*
public var id: UUID {
UUID()
}
*/
}
*/
struct DetailView: View {
@State private var hidesNavBar = false
var selectedImage: String
var body: some View {
let img = UIImage(named: selectedImage)!
return Image(uiImage: img)
.resizable()
.aspectRatio(contentMode: .fit)
.navigationBarHidden(hidesNavBar)
.navigationBarTitle(Text(selectedImage), displayMode: .inline)
.onTapGesture {
self.hidesNavBar.toggle()
}
}
}
struct ContentView: View {
// ObjectBinding should be used here to prevent reloading of DataSource every time this struct changes
// But ObjectBidning is deprecated
var dataSource = DataSource()
var body: some View {
NavigationView {
// Using ForEach
/*
List {
ForEach(dataSource.images, id: \.self) { image in
NavigationLink(
destination: DetailView(selectedImage: image)
) {
Text(image)
}
}
}.navigationBarTitle(Text("Storm Viewer"))
*/
List(dataSource.images, id: \.self) { image in
NavigationLink(
destination: DetailView(selectedImage: image)
) {
Text(image)
}
}.navigationBarTitle(Text("Storm Viewer"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}