Repository navigation
Expand file tree
/
Copy pathContentView_v2
More file actions
948 lines (791 loc) · 38.1 KB
/
Copy pathContentView_v2
File metadata and controls
948 lines (791 loc) · 38.1 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
import SwiftUI
import Combine
import UIKit
import SDWebImageSwiftUI
import CoreLocation
import WebKit
import PythonKit
struct SatelliteImage: Identifiable {
let id: UUID = UUID()
let url: String
let title: String
let description: String
}
class ImageRepository: ObservableObject {
@Published var satelliteImages: [String: [SatelliteImage]] = [:] // Dictionary for different sections
init() {
fetchImages()
}
func fetchImages() {
Task.init {
do {
try await self.fetchImagesAsync()
} catch {
print(error)
}
}
}
func asyncRemoveImage(forKey key: String) async {
await withCheckedContinuation { continuation in
SDImageCache.shared.removeImage(forKey: key) {
continuation.resume(returning: ())
}
}
}
func fetchImagesAsync() async throws {
// URLs of the images
let urls1 = [
"https://mausam.imd.gov.in/Satellite/rswmo_mp.jpg",
"https://mausam.imd.gov.in/Satellite/rs_olr.jpg",
"https://mausam.imd.gov.in/Satellite/rswmo_vis.jpg",
"https://nwp.imd.gov.in/wrf/WRF3km-03-RAIN_24.png",
"https://mausam.imd.gov.in/Satellite/3Dasiasec_wv.jpg",
"https://img.nsmc.org.cn/CLOUDIMAGE/FY2H/GLL/FY2H_ETV_SEC_GLB.jpg"
]
let urls2 = [
"https://mausam.imd.gov.in/Radar/sri_delhi.gif",
"https://mausam.imd.gov.in/Radar/pac_delhi.gif",
"https://mausam.imd.gov.in/Radar/caz_delhi.gif",
"https://mausam.imd.gov.in/Radar/ppi_delhi.gif"
]
let urls3 = [
"https://mausam.imd.gov.in/Radar/caz_vrv.gif",
"https://mausam.imd.gov.in/Radar/sri_vrv.gif",
"https://mausam.imd.gov.in/Radar/pac_vrv.gif"
]
let urls4 = [
"https://mausam.imd.gov.in/Satellite/rs_olr.jpg",
"https://mausam.imd.gov.in/Satellite/rs_imr.jpg",
"https://mausam.imd.gov.in/Satellite/rs_hem.jpg"
]
let urls5 = [
"http://satellite.imd.gov.in/imgr/swsec_vis.jpg",
"http://satellite.imd.gov.in/imgr/swsec_ir1.jpg",
"http://satellite.imd.gov.in/imgr/swsec_mp.jpg"
]
// Remove images from the cache asynchronously
for url in urls1 + urls2 {
await asyncRemoveImage(forKey: url)
}
let previousImages = [
SatelliteImage(url: urls1[0], title: "Day Microphysics", description: "Day Microphysics RGB combines the data of 3 channels or channel differences into one image, helpful for cloud detection."),
SatelliteImage(url: urls1[1], title: "Outgoing Longwave Radiation", description: "Outgoing Longwave Radiation (OLR) maps depict the top-of-atmosphere energy emitted by the Earth system in the 4-50 µm band."),
SatelliteImage(url: urls1[2], title: "Visible Spectrum", description: "This specific image shows precipitation forecast for the next 3 hours."),
SatelliteImage(url: urls1[3], title: "Infrared Channel 10.8µm", description: "These images display cloud-top temperatures or equivalent blackbody brightness temperature in Kelvin."),
SatelliteImage(url: urls1[4], title: "Water Vapour", description: "Mesoscale models are weather prediction models that can predict highly detailed weather phenomena. This specific image shows precipitation forecast for the next 3 hours."),
SatelliteImage(url: urls1[5], title: "Water Vapour", description: "Weather prediction models that can predict highly detailed weather phenomena. This specific image shows precipitation forecast for the next 20 Minutes.")
]
let nwsecImages = [
SatelliteImage(url: urls2[0], title: "Storm Relative Intensity", description: "The Storm Relative Intensity Map shows the intensity of precipitation relative to a moving storm."),
SatelliteImage(url: urls2[1], title: "Precipitation Accumulation", description: "Precipitation Accumulation maps display the cumulative rainfall over a certain period."),
SatelliteImage(url: urls2[2], title: "Constant Altitude Plan Position Indicator", description: "This radar image captures the precipitation at a fixed altitude. It helps to analyze the spatial distribution of precipitation at that altitude."),
SatelliteImage(url: urls2[3], title: "Plan Position Indicator", description: "Plan Position Indicator is the most common type of radar display. The radar antenna rotates 360 degrees around a fixed point, mapping the surrounding area.")
]
let nesacImages = [
SatelliteImage(url: urls3[0], title: "Velocity Radial Velocity", description: "Deals with the velocity at which precipitation or other detected particles are moving towards or away from the radar. This is an essential tool for determining wind patterns, including rotation in thunderstorms that could indicate a tornado."),
SatelliteImage(url: urls3[1], title: "Plan Position Indicato", description: " It's the most common type of radar display and represents a 2D horizontal view of radar returns. The center of the PPI image typically represents the location of the radar, and it shows the radial return data in a 360-degree sweep around the radar."),
SatelliteImage(url: urls3[2], title: "PPZ ", description: "Refers to a vertical cross-section or another form of vertical display, allowing meteorologists to see the structure of storms from the side.While PPI shows horizontal views, this does vertical views.")
]
let sesecImages = [
SatelliteImage(url: urls4[0], title: "Global OLR", description: " OLR refers to the energy emitted by the Earth back into space. This energy is mainly in the form of infrared radiation, which is a measure of the Earth's heat."),
SatelliteImage(url: urls4[1], title: "Multispectral Rainfall", description: "Multispectral imagery involves capturing image data at specific frequencies across the electromagnetic spectrum, including both visible light and infrared. In the context of rainfall, multispectral sensors can differentiate between rain, clouds, land, and other features."),
SatelliteImage(url: urls4[2], title: "Hydro Estimator Precipitation", description: "The Hydro Estimator is a satellite-based method for estimating rainfall over large regions. It provides meteorologists and hydrologists with real-time data about rainfall, which can be used for flood forecasts, agricultural insights, and more.")
]
let swsecImages = [
SatelliteImage(url: urls5[0], title: "SWSEC Visual Image", description: "This is a visual image captured by the SWSEC satellite."),
SatelliteImage(url: urls5[1], title: "SWSEC Infrared Image", description: "This is an infrared image captured by the SWSEC satellite."),
SatelliteImage(url: urls5[2], title: "SWSEC Microphysics", description: "This image displays some kind of microphysical data from the SWSEC satellite.")
]
DispatchQueue.main.async {
self.satelliteImages = [
"1. Indian Sub-Continent Images": previousImages,
"2. Delhi Radar Data": nwsecImages,
"3. Mumbai Radar Data": nesacImages,
"4. Global View": sesecImages
]
}
}
}
class QueryManager {
let API_TOKEN = "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" // Replace this with your actual token
let API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-xxl"
func query(input: String, completion: @escaping (String?) -> Void) {
guard let url = URL(string: API_URL) else {
completion(nil)
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("Bearer \(API_TOKEN)", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let payload: [String: Any] = ["inputs": input]
request.httpBody = try? JSONSerialization.data(withJSONObject: payload, options: [])
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let results = json["results"] as? [[String: Any]],
let generatedText = results.first?["generated_text"] as? String else {
completion(nil)
return
}
let responseString = String(data: data, encoding: .utf8)
print("Response: \(responseString ?? "No response")")
completion(generatedText)
}
task.resume()
}
}
struct SunARView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
// Update if needed when SwiftUI view updates, though for this example we won't need to do anything here.
}
}
struct ChatView: View {
@State private var messages: [String] = []
@State private var userInput: String = ""
private let queryManager = QueryManager()
var body: some View {
VStack {
List(messages, id: \.self) { message in
Text(message)
}
HStack {
TextField("Enter message...", text: $userInput)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button("Send") {
processUserInput(userInput)
userInput = ""
}
}
.padding()
}
.navigationTitle("Chat")
}
func processUserInput(_ userInput: String) {
queryManager.query(input: userInput) { response in
DispatchQueue.main.async {
if let response = response {
messages.append("User: \(userInput)")
messages.append("AI: \(response)")
} else {
messages.append("I'm sorry, I didn't understand that.")
}
}
}
}
}
struct SunPathView: View {
@ObservedObject var locationManager: LocationManager
var sunDetails: [(time: Date, altitude: Double, azimuth: Double)] {
guard let latitude = locationManager.latitude, let longitude = locationManager.longitude else { return [] }
// Adjusted the location access here
let hours = Array(0...23)
let today = Calendar.current.startOfDay(for: Date())
let times = hours.map { Calendar.current.date(byAdding: .hour, value: $0, to: today)! }
return times.map {
let sunDataForTime = SunCalculation.sun(date: $0, latitude: latitude, longitude: longitude)
return (time: $0, altitude: sunDataForTime.altitude, azimuth: sunDataForTime.azimuth)
}
}
var body: some View {
VStack {
// Table of sun positions
List {
ForEach(sunDetails, id: \.time) { detail in
HStack {
Text("\(detail.time, formatter: DateFormatter.shortTime)")
Spacer()
Text("Altitude: \(detail.altitude, specifier: "%.2f")°")
Spacer()
Text("Azimuth: \(detail.azimuth, specifier: "%.2f")°")
}
}
}
.padding()
// Visualization of the sun path
// ... (Your ZStack and drawing code goes here)
}
.onAppear {
self.locationManager.requestLocation()
}
}
}
extension DateFormatter {
static let shortTime: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter
}()
}
struct WebView: UIViewRepresentable {
@Binding var results: String
var locationManager: LocationManager
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.navigationDelegate = context.coordinator
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let lat = locationManager.latitude
let lon = locationManager.longitude
let js = """
var times = SunCalc.getTimes(new Date(), \(String(describing: lat)), \(String(describing: lon)));
var sunrise = times.sunrise;
var sunset = times.sunset;
sunrise + ' - ' + sunset;
"""
if let url = Bundle.main.url(forResource: "index", withExtension: "html") {
uiView.load(URLRequest(url: url))
uiView.evaluateJavaScript(js) { (result, error) in
if let resultString = result as? String {
results = resultString
}
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate {
var parent: WebView
init(_ parent: WebView) {
self.parent = parent
}
}
}
struct ContentView: View {
@StateObject private var weatherManager = WeatherManager() // Assuming WeatherManager is initializable without parameters
@StateObject private var imageRepository = ImageRepository()
@StateObject private var locationManager = LocationManager()
@State private var cancellables = Set<AnyCancellable>()
@State private var selectedSatelliteImage: SatelliteImage?
@State private var showSatelliteDetail = false
var gifUrls: [String] = [
"https://mausam.imd.gov.in/Satellite/Converted/IR1.gif",
"https://mausam.imd.gov.in/Satellite/Converted/VIS.gif",
"https://mausam.imd.gov.in/Satellite/Converted/WV.gif"
]
let colors: [Color] = [ .blue, .yellow, .pink, .red, .gray ]
@State private var showWeatherView: Bool = false
@State private var userLocation: CLLocationCoordinate2D?
var body: some View {
TabView {
NavigationView {
VStack {
ChatView()
}
.tabItem {
Image(systemName: "network")
Text("Chat")
}
}
// Create a tab item for Satellite View
NavigationView {
ScrollView {
VStack {
ForEach(imageRepository.satelliteImages.keys.sorted(), id: \.self) { key in
VStack(alignment: .leading) {
Text(key)
.font(.title2)
.fontWeight(.bold)
.padding(.leading, 15)
.padding(.top, 5)
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 10) {
ForEach(imageRepository.satelliteImages[key]!, id: \.url) { image in
Button(action: {
self.selectedSatelliteImage = image
self.showSatelliteDetail = true
}) {
SatelliteRow(image: image, colors: colors)
}
}
}
.frame(height: 450)
}
}
}
}
}
.sheet(isPresented: $showSatelliteDetail) {
if let selectedImage = selectedSatelliteImage {
SatelliteDetailSheet(image: selectedImage)
}
}
}
.tabItem {
Image(systemName: "network")
Text("Satellite View")
}
NavigationView {
AnimatedImagesView()
}
.tabItem {
Image(systemName: "wind.circle")
Text("Air Quality")
}
NavigationView {
SunARView() // This is the SwiftUI wrapper around the ARKit view controller
.edgesIgnoringSafeArea(.all) // Ensure it uses the full available screen space
.navigationBarTitle("Sun Position AR", displayMode: .inline)
}
.tabItem {
Image(systemName: "location.north.fill") // You can choose a more appropriate icon if desired
Text("Sun AR")
}
NavigationView {
SunPathView(locationManager: locationManager)
.navigationBarTitle("Sun Path", displayMode: .inline)
.onAppear { // Moved .onAppear here
// Check if the permission hasn't been determined yet
if self.locationManager.manager.authorizationStatus == .notDetermined {
self.locationManager.requestLocation()
}
}
}
.tabItem {
Image(systemName: "sun.max.fill")
Text("Sun Path")
}
NavigationView {
ScrollView {
VStack {
VStack(alignment: .leading) {
Text("Indian Sub-Continent")
.font(.title2)
.fontWeight(.bold)
.padding(.leading, 15)
.padding(.all, 5)
VStack(alignment: .leading, spacing: 10) {
ForEach(gifUrls, id: \.self) { gifUrl in
NavigationLink(destination: GifDetailSheet(gifUrl: gifUrl)) {
GifRow(gifUrl: gifUrl, colors: colors)
}
}
}
.padding(.bottom, 20)
}
}
}
.padding(.all)
.navigationTitle("Animations")
}
.tabItem {
Image(systemName: "waveform.path.ecg")
Text("Animations")
}
.padding([.leading, .bottom, .trailing, .horizontal], 0.8)
.navigationViewStyle(.stack)
}
}
struct WebView: UIViewRepresentable {
let url: URL
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.load(URLRequest(url: url))
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// No update code needed.
}
}
struct GifRow: View {
@State private var showShareSheet = false
@State private var showDetailSheet = false
var gifUrl: String
@State private var color: Color
let colors: [Color]
let timer = Timer.publish(every: 1.9, on: .main, in: .common).autoconnect()
init(gifUrl: String, colors: [Color]) {
self.gifUrl = gifUrl
self._color = State(initialValue: colors.randomElement()!)
self.colors = colors
}
var body: some View {
VStack {
Button(action: {
self.showDetailSheet.toggle()
}) {
AnimatedImage(url: URL(string: gifUrl))
.resizable()
.indicator(.activity)
.frame(width: 350, height: 350)
.background(Color.black.opacity(0.9))
.cornerRadius(10)
.padding([.top, .bottom, .horizontal])
.background(color.opacity(0.6))
.cornerRadius(30)
.padding([.top, .horizontal, .bottom])
.onReceive(timer) { _ in
withAnimation {
var newColor: Color
repeat {
newColor = colors.randomElement()!
} while newColor == color
self.color = newColor
}
}
}
.sheet(isPresented: $showDetailSheet) {
GifDetailSheet(gifUrl: gifUrl)
}
}
.background(color.opacity(0.6))
.cornerRadius(30)
.padding([.top, .horizontal, .bottom])
}
}
// For GifDetailSheet:
struct GifDetailSheet: View {
var gifUrl: String
@State private var showShareSheet = false
@State private var cumulativeScale: CGFloat = 1.0
@State private var cumulativeDrag: CGSize = CGSize.zero
@GestureState private var magnificationState = CGFloat(1.0)
@GestureState private var translationState = CGSize.zero
var body: some View {
VStack(spacing: 20) {
Text("GIF Detail")
.font(.title2)
.fontWeight(.bold)
.padding(.leading, 15)
.padding(.all, 5)
AnimatedImage(url: URL(string: gifUrl))
.resizable()
.indicator(.activity)
.frame(width: 350, height: 350)
.background(Color.black.opacity(0.9))
.padding([.top, .bottom, .horizontal])
.cornerRadius(30)
.padding([.top, .horizontal, .bottom])
.scaleEffect(cumulativeScale * magnificationState)
.offset(x: cumulativeDrag.width + translationState.width, y: cumulativeDrag.height + translationState.height)
.gesture(
MagnificationGesture()
.updating($magnificationState) { currentState, gestureState, _ in
gestureState = currentState
}
.onEnded { value in
self.cumulativeScale *= value
}
.simultaneously(with:
DragGesture()
.updating($translationState) { value, state, _ in
state = value.translation
}
.onEnded { value in
self.cumulativeDrag = CGSize(width: self.cumulativeDrag.width + value.translation.width, height: self.cumulativeDrag.height + value.translation.height)
}
)
)
.clipped()
// You can add GIF description here
Button(action: {
self.showShareSheet = true
}) {
HStack {
Image(systemName: "paperplane")
}
.padding()
.foregroundColor(.white)
.cornerRadius(8)
}
.sheet(isPresented: $showShareSheet) {
ShareSheet(items: [gifUrl])
}
}
.padding()
.background(Color.black.opacity(0.7))
}
}
class SO2GasImagesLoader: ObservableObject {
@Published var images: [UIImage] = []
var cancellables = Set<AnyCancellable>()
// Removed loadImages() call from init
init() { }
// Function to start loading images
func startLoading() {
loadImages()
}
private func loadImages() {
let imageUrls = (2...72).map { index -> URL in
return URL(string: "https://ews.tropmet.res.in/mumbai/data/mumbai/\(index).png?time=15:04:47")!
}
imageUrls.publisher
.flatMap { url in
URLSession.shared.dataTaskPublisher(for: url)
.map { UIImage(data: $0.data) }
.replaceError(with: nil)
}
.compactMap { $0 }
.collect()
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in }, receiveValue: { self.images = $0 })
.store(in: &cancellables)
}
}
struct AnimatedImagesView: View {
@StateObject var imageLoader = SO2GasImagesLoader()
@State private var currentImageIndex: Double = 0
@State private var isPlaying: Bool = true
let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()
let timeIntervals = Array(stride(from: 0, through: 72, by: 6))
var body: some View {
VStack {
Text("Mumbai Air Quality and Wind Direction")
.font(.title2)
.fontWeight(.bold)
.padding(.leading, 15)
.padding(.all, 5)
.multilineTextAlignment(.center)
Spacer()
Image(uiImage: imageLoader.images.isEmpty ? UIImage() : imageLoader.images[Int(currentImageIndex)])
.resizable()
.padding(.all)
.aspectRatio(contentMode: .fit)
.onReceive(timer) { _ in
if isPlaying && !imageLoader.images.isEmpty {
currentImageIndex = (currentImageIndex + 1).truncatingRemainder(dividingBy: Double(imageLoader.images.count))
}
}
.onAppear {
imageLoader.startLoading()
}
Spacer()
HStack {
ForEach(timeIntervals, id: \.self) { interval in
Text("\(interval)H")
.font(.footnote)
.fontWeight(.semibold)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
}
}
Slider(value: $currentImageIndex.animation(.linear),
in: 00...71,
step: 1)
Button(action: {
isPlaying.toggle()
}) {
Image(systemName: isPlaying ? "pause.circle" : "play.circle")
.resizable()
.frame(width: 35, height: 35)
.foregroundColor(.white)
}
.padding(.all)
}
}
}
struct SatelliteRow: View {
@State private var showShareSheet = false
@State private var showDetailSheet = false
@State private var shareImage: UIImage?
var image: SatelliteImage
@State private var color: Color
let colors: [Color]
let timer = Timer.publish(every: 1.9, on: .main, in: .common).autoconnect()
init(image: SatelliteImage, colors: [Color]) {
self.image = image
self.colors = colors
_color = State(initialValue: colors.randomElement()!)
}
func fetchImage(from url: String) {
if let imageUrl = URL(string: url) {
DispatchQueue.global().async {
if let data = try? Data(contentsOf: imageUrl) {
DispatchQueue.main.async {
self.shareImage = UIImage(data: data)
}
}
}
}
}
var body: some View {
VStack {
Button(action: {
self.showDetailSheet.toggle()
}) {
WebImage(url: URL(string: image.url))
.resizable()
.indicator(.activity)
.scaledToFit()
.background(Color.black.opacity(0.9))
.cornerRadius(10)
.padding([.top, .bottom, .horizontal])
.background(color.opacity(0.6))
.cornerRadius(30)
.padding([.top, .horizontal, .bottom])
.onReceive(timer) { _ in
withAnimation {
var newColor: Color
repeat {
newColor = colors.randomElement()!
} while newColor == color
self.color = newColor
}
}
}
.sheet(isPresented: $showDetailSheet) {
SatelliteDetailSheet(image: image)
}
HStack {
Text(image.title)
.font(.title2)
.fontWeight(.semibold)
.foregroundColor(.black)
.multilineTextAlignment(.center)
.padding([.all])
}
}
.background(color.opacity(0.6))
.cornerRadius(30)
.padding([.top, .horizontal, .bottom])
.onReceive(timer) { _ in
withAnimation {
var newColor: Color
repeat {
newColor = colors.randomElement()!
} while newColor == color
self.color = newColor
}
}
}
}
struct SatelliteDetailSheet: View {
var image: SatelliteImage
@State private var showShareSheet = false
@State private var cumulativeScale: CGFloat = 1.0
@State private var cumulativeDrag: CGSize = CGSize.zero
@GestureState private var magnificationState = CGFloat(1.0)
@GestureState private var translationState = CGSize.zero
var body: some View {
VStack(spacing: 20) {
Text(image.title)
.font(.title2)
.fontWeight(.bold)
.padding(.leading, 15)
.padding(.all, 5)
WebImage(url: URL(string: image.url))
.resizable()
.indicator(.activity)
.scaledToFit()
.background(Color.black.opacity(0.9))
.cornerRadius(10)
.padding()
.scaleEffect(cumulativeScale * magnificationState)
.offset(x: cumulativeDrag.width + translationState.width, y: cumulativeDrag.height + translationState.height)
.gesture(
MagnificationGesture()
.updating($magnificationState) { currentState, gestureState, _ in
gestureState = currentState
}
.onEnded { value in
self.cumulativeScale *= value
}
.simultaneously(with:
DragGesture()
.updating($translationState) { value, state, _ in
state = value.translation
}
.onEnded { value in
self.cumulativeDrag = CGSize(width: self.cumulativeDrag.width + value.translation.width, height: self.cumulativeDrag.height + value.translation.height)
}
)
)
.clipped()
Text(image.description)
.padding()
Button(action: {
self.showShareSheet = true
}) {
HStack {
Image(systemName: "paperplane")
}
.padding()
.foregroundColor(.white)
.cornerRadius(8)
}
.sheet(isPresented: $showShareSheet) {
ShareSheet(items: [image.url, image.description])
}
}
.padding()
.background(Color.black.opacity(0.7))
}
}
struct DetailView: View {
var imageUrl: String
var imageDescription: String // Description of the image
@State private var cumulativeScale: CGFloat = 1.0
@State private var cumulativeDrag: CGSize = CGSize.zero
@GestureState private var magnificationState = CGFloat(1.0) // scale state using GestureState
@GestureState private var translationState = CGSize.zero // offset state using GestureState
@State private var showInfo = false // State variable to control the info view visibility
var body: some View {
ZStack {
Color.black.ignoresSafeArea()
WebImage(url: URL(string: imageUrl))
.resizable()
.indicator(.activity) // Show activity indicator while loading
.scaledToFit()
.background(Color.black.opacity(0.7))
.cornerRadius(10)
.padding()
.scaleEffect(cumulativeScale * magnificationState) // Apply the scale effect
.offset(x: cumulativeDrag.width + translationState.width, y: cumulativeDrag.height + translationState.height) // Apply the offset for panning
.gesture(
// Add the MagnificationGesture for pinch-to-zoom
MagnificationGesture()
.updating($magnificationState) { currentState, gestureState, _ in
gestureState = currentState
}
.onEnded { value in
self.cumulativeScale *= value
}
.simultaneously(with: // Add the DragGesture for panning
DragGesture()
.updating($translationState) { value, state, _ in
state = value.translation
}
.onEnded { value in
self.cumulativeDrag = CGSize(width: self.cumulativeDrag.width + value.translation.width, height: self.cumulativeDrag.height + value.translation.height)
}
)
)
.overlay( // Add the overlay
Button(action: { showInfo.toggle() }) {
Image(systemName: "info.square.fill")
.font(.largeTitle)
.foregroundColor(.black)
.padding()
},
alignment: .topTrailing
)
.alert(isPresented: $showInfo) {
Alert(title: Text("Image Information"), message: Text(imageDescription))
}
}
}
}
struct ShareSheet: UIViewControllerRepresentable {
var items: [Any]
var excludedActivityTypes: [UIActivity.ActivityType]? = nil
func makeUIViewController(context: Context) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
controller.excludedActivityTypes = excludedActivityTypes
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
// Nothing to update here
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}