Post

Replies

Boosts

Views

Activity

Building a json app that plays videos (troubleshooting and better ways)
I've been watching various tutorials and have managed to come up with the following code: import SwiftUI import AVKit struct ContentView: View {     @State private var wolData = [Main]()          var body: some View {                  NavigationView{List(wolData, id: \.id) { item in             NavigationLink(destination: MainDetail(json: item)) {                                  HStack() {                     Text(item.name)                         .font(.headline)                     Text(item.date)                         .font(.footnote)                     if #available(iOS 15.0, *) {                         AsyncImage(url: URL(string: item.thumbnail))                         { image in                             image                                 .resizable()                                 .scaledToFill()                         } placeholder: {                             Color.purple.opacity(0.1)                         }                         .frame(width: 20, height: 20)                     } else {                         // Fallback on earlier versions                     }                 }                              }                      }.onAppear(perform: loadData)}              }           } extension ContentView {     func loadData() {                  guard let url = URL(string: "https://wolvideos.firebaseapp.com/Simple.json") else {             return         }                  let request = URLRequest(url: url)         URLSession.shared.dataTask(with: request) { data, response, error in                          if let data = data {                 if let response_obj = try? JSONDecoder().decode([Main].self, from: data) {                                          DispatchQueue.main.async {                         self.wolData = response_obj                     }                 }             }                      }.resume()     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } import Foundation struct Main: Decodable {     var id: Int     var name: String     var interactive: String     var thumbnail: String     var date: String     var videolink: String     var sharelink: String } import SwiftUI import AVKit struct MainDetail: View {     var json: Main    private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)        var body: some View {      VideoPlayer(player: player)             .onAppear() {                 // Start the player going, otherwise controls don't appear                 player.play()             }             .onDisappear() {                 // Stop the player when the view disappears                 player.pause()             }    } } Now that I've shared that, the intent is to when the user clicks an option load the corresponding video (from the json not the current test file) via HLS in a default player (like the one in safari)... should I be going about this in a different way? I'm starting to think committing to SwiftUI is a mistake.
0
0
918
Jun ’22
passing json video link to avplayer
I'm attempting to simply pass the video link inside the json to the navigation menu... my brain is fried as I'm lost in why I can't do this view the navigation link protocol. import SwiftUI import AVKit struct MainDetail: View {          var json: Main         private let player = AVPlayer(url: URL(string: item.interactive))        var body: some View {      VideoPlayer(player: player)             .onAppear() {                 // Start the player going, otherwise controls don't appear                 player.play()             }             .onDisappear() {                 // Stop the player when the view disappears                 player.pause()             }    } } this returns a "Cannot find 'item' in scope" despite being declared back on the main content view. the intended action is loading the "interactive" link (m3u8 file) in a player view when clicked on. I apologize for not understanding what's going on here.
0
0
715
Jun ’22
Is it possible to convert this to using just play view for links in 16?
here is the code: struct LiveView: View {      @State var allowsInlineMediaPlayback: Bool = false      var body: some View {            VStack {                  VideoView(videoID:"vimeo.com/event/000000/embed")                  }                }            } struct VideoView: UIViewRepresentable {      let videoID: String      func makeUIView(context: Context) -> WKWebView {       let configuration = WKWebViewConfiguration()       configuration.allowsInlineMediaPlayback = true       let WKWebView = WKWebView(frame: .zero, configuration: configuration)       return WKWebView        }      func updateUIView(_ uiView: WKWebView, context: Context) {     guard let youtubeURL = URL(string: "https://\(videoID)") else     {return}     uiView.load(URLRequest (url: youtubeURL))   } } Is their a better way to accomplish feeding a live event link into a playerview instead of needing a WKWebView?
0
0
898
Jul ’22
NLtagger not filtering words such as "And, to, a, in"
what am I not understanding here. in short the view loads text from the jsons descriptions and then should filter out the words. and return and display a list of most used words, debugging shows words being identified by the code but does not filter them out private func loadWordCounts() { DispatchQueue.global(qos: .background).async { let fileManager = FileManager.default guard let documentsDirectory = try? fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) else { return } let descriptions = loadDescriptions(fileManager: fileManager, documentsDirectory: documentsDirectory) var counts = countWords(in: descriptions) let tagsToRemove: Set<NLTag> = [ .verb, .pronoun, .determiner, .particle, .preposition, .conjunction, .interjection, .classifier ] for (word, _) in counts { let tagger = NLTagger(tagSchemes: [.lexicalClass]) tagger.string = word let (tag, _) = tagger.tag(at: word.startIndex, unit: .word, scheme: .lexicalClass) if let unwrappedTag = tag, tagsToRemove.contains(unwrappedTag) { counts[word] = 0 } } DispatchQueue.main.async { self.wordCounts = counts } } }
0
0
580
Oct ’24
Share button will not move to the right.
I'm running on fumes but logically this view should be showing the share button on the bottom right, and the Title/Date/PlayButton in the middle? What am I doing wrong? It currently just shows the share button underneath import SwiftUI import AVKit struct ContentView: View {     @State private var items = [Item]()     @State private var currentPage = 0     var body: some View {         TabView(selection: $currentPage) {             ForEach(items, id: \.id) { item in                 VideoPlayerView(url: item.interactive)                     .aspectRatio(contentMode: .fill)                     .tag(item.id)                     .onAppear {                         let controller = AVPlayerViewController()                         controller.player = AVPlayer(url: URL(string: item.interactive)!)                         controller.showsPlaybackControls = false                         controller.player?.play()                     }                     .overlay(                         VStack() {                             Spacer()                             Text(item.name)                                 .font(.title)                             Text(item.date)                                 .font(.caption)                             Button(action: {                                 let player = AVPlayer(url: URL(string: item.videolink)!)                                 let playerController = AVPlayerViewController()                                 playerController.player = player                                 playerController.showsPlaybackControls = true                                 UIApplication.shared.windows.first?.rootViewController?.present(playerController, animated: true, completion: {                                     player.play()                                 })                             }) {                                 HStack(spacing: 5) {                                     Text("PLAY")                                     Image(systemName: "play.circle")                                 }                                 .padding(.horizontal, 10)                                 .padding(.vertical, 5)                                 .background(Color.white)                                 .foregroundColor(.black)                                 .cornerRadius(20)                             }                             Button(action: {                                 let activityVC = UIActivityViewController(activityItems: [URL(string: item.sharelink)!], applicationActivities: nil)                                 UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)                             }) {                                 Image(systemName: "square.and.arrow.up.circle")                                     .foregroundColor(.black)                             }                             .padding(.bottom, 50)                         }                     )             }         }         .tabViewStyle(.page)         .onAppear(perform: loadData)         .gesture(DragGesture()             .onEnded { value in                 let offset = value.translation.width / UIScreen.main.bounds.width                 let newIndex = (CGFloat(currentPage) - offset).rounded()                 currentPage = min(max(Int(newIndex), 0), items.count - 1)             }         )     }     func loadData() {         guard let url = URL(string: "jsonfile") else { return }         URLSession.shared.dataTask(with: url) { data, response, error in             guard let data = data else { return }             do {                 let decoder = JSONDecoder()                 let items = try decoder.decode([Item].self, from: data)                 DispatchQueue.main.async {                     self.items = items                 }             } catch {                 print(error.localizedDescription)             }         }.resume()     } } struct VideoPlayerView: UIViewRepresentable {     let url: String          func makeUIView(context: Context) -> AVPlayerView {         let view = AVPlayerView()         view.playerLayer.player = AVPlayer(url: URL(string: url)!)         view.playerLayer.videoGravity = .resizeAspectFill         view.playerLayer.backgroundColor = UIColor.black.cgColor         view.playerLayer.player?.play()         return view     }          func updateUIView(_ uiView: AVPlayerView, context: Context) {         // Nothing to update     } } class AVPlayerView: UIView {     override class var layerClass: AnyClass {         return AVPlayerLayer.self     }          var playerLayer: AVPlayerLayer {         return layer as! AVPlayerLayer     }          override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {         let hitView = super.hitTest(point, with: event)         return hitView == self ? nil : hitView     } } struct Item: Codable, Identifiable {     let id: Int     let name: String     let interactive: String     let thumbnail: String     let date: String     let videolink: String     let sharelink: String }
1
0
805
Mar ’23
Preventing objects from overlapping in geometry reader
I'm attempting to create a word map that shows the words without overlapping without bloating the code load. how would you do it and why? I'm also open to handling this in other ways beside a geo reader. let description = trigger.description.lowercased() let wordsWithoutPunctuation = description.components(separatedBy: CharacterSet.punctuationCharacters).joined() let words = wordsWithoutPunctuation.components(separatedBy: " ") let determiners = ["the", "a", "an", "this", "that", "these", "those", "my", "your", "and", "her", "its", "our", "to"] let filteredWords = words.filter { !determiners.contains($0) } let wordCounts = Dictionary(grouping: filteredWords, by: { $0 }).mapValues { $0.count } let sortedWords = wordCounts.sorted { $1.value < $0.value } let topWords = Array(sortedWords.prefix(5)) let maxCount = topWords.first?.value ?? 1 GeometryReader { geometry in ZStack { ForEach(topWords, id: \.key) { word in let wordSize = "\(word.key) (\(word.value))".size(withAttributes: [.font: UIFont.systemFont(ofSize: min(CGFloat(word.value) / CGFloat(maxCount) * 30, 30))]) Text("\(word.key) (\(word.value))") .foregroundColor(.BrandGreen) .font(.system(size: min(CGFloat(word.value) / CGFloat(maxCount) * 30, 30))) // Adjust the multiplier and maximum size as needed .position( x: CGFloat.random(in: wordSize.width / 2..<geometry.size.width - wordSize.width / 2), y: CGFloat.random(in: wordSize.height / 2..<geometry.size.height - wordSize.height / 2) ) } } } } .frame(width: 180, height: 180) .padding(5) .background(Color.harp) .cornerRadius(30) .aspectRatio(1, contentMode: .fit) .clipShape(RoundedRectangle(cornerRadius: 30)) .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 0)
1
0
457
Jan ’24
HKCategoryValueSleepAnalysis Sending incorrect sleep data.
The edited code still has the problem of not lining up with the health app private func fetchSleepData(for date: Date) { let sleepType = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)! let endOfPeriod = date let startOfPeriod = Calendar.current.date(byAdding: .day, value: -1, to: endOfPeriod)! let predicate = HKQuery.predicateForSamples(withStart: startOfPeriod, end: endOfPeriod, options: [.strictStartDate, .strictEndDate]) let query = HKSampleQuery(sampleType: sleepType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { query, samples, error in guard let samples = samples as? [HKCategorySample], !samples.isEmpty else { DispatchQueue.main.async { self.inBedTime = 0 self.coreTime = 0 self.deepTime = 0 self.remTime = 0 self.isSleepDataAvailable = false } print("No sleep data available for date: \(date)") return } print("Fetched \(samples.count) sleep samples for date: \(date)") var inBedTime = 0.0 var asleepTime = 0.0 var deepTime = 0.0 var remTime = 0.0 for sample in samples { print("Sample value: \(sample.value)") let duration = sample.endDate.timeIntervalSince(sample.startDate) / 60 // convert to minutes switch sample.value { case HKCategoryValueSleepAnalysis.inBed.rawValue: inBedTime += duration case HKCategoryValueSleepAnalysis.asleepCore.rawValue: coreTime += duration case HKCategoryValueSleepAnalysis.asleepDeep.rawValue: deepTime += duration case HKCategoryValueSleepAnalysis.asleepREM.rawValue: remTime += duration default: break } } DispatchQueue.main.async { self.inBedTime = inBedTime self.coreTime = coreTime self.deepTime = deepTime self.remTime = remTime self.isSleepDataAvailable = true } } healthStore?.execute(query) }
1
0
946
Jan ’24
Charts showing yesterday as todays data
I'm at my Witts end trying to figure out why charts is incorrectly labeling the days! struct SunlightSupportBox: View { @ObservedObject var viewModel = SunlightViewModel() @EnvironmentObject var themeSettings: ThemeSettings var sortedSunlightData: [SunlightData] { viewModel.sunlightData.sorted(by: { $0.date < $1.date }) } var body: some View { VStack { if !sortedSunlightData.isEmpty { Chart { ForEach(sortedSunlightData) { data in BarMark( x: .value("Day", formattedDate(date: data.date)), y: .value("Triggers/Reflections", Double((data.triggersCount * 10 + data.reflectionsCount * 10))) // Each trigger/reflection represents 5 minutes ) .foregroundStyle(Color.green.opacity(0.5)) BarMark( x: .value("Day", formattedDate(date: data.date)), yStart: .value("Sunlight Start", 0), yEnd: .value("Minutes of Sunlight", data.duration * 60) // Convert hours to minutes ) .foregroundStyle(Color.orange.opacity(0.5)) } } .frame(maxWidth: .infinity, maxHeight: .infinity) .padding(10) .clipShape(RoundedRectangle(cornerRadius: 25)) .padding() .background(themeSettings.currentColor) .cornerRadius(25) } else { Text("No sunlight data") .foregroundColor(.black) .background(Color.white) .cornerRadius(10) .padding() } } .frame(width: 350, height: 200) .background(themeSettings.currentColor) .cornerRadius(30) } private func formattedDate(date: Date) -> String { let formatter = DateFormatter() formatter.dateFormat = "E" return formatter.string(from: date) } } This view correctly shows todays day with the correct data struct SleepSupportBox: View { @ObservedObject var viewModel = SleepViewModel() @EnvironmentObject var themeSettings: ThemeSettings var body: some View { VStack { if !viewModel.sleepData.isEmpty { Chart(viewModel.sleepData) { data in BarMark( x: .value("Day", formattedDate(date: data.date)), y: .value("Triggers/Reflections", Double(data.triggersCount + data.reflectionsCount)) ) .foregroundStyle(Color.green.opacity(0.5)) BarMark( x: .value("Day", formattedDate(date: data.date)), y: .value("Hours of Sleep", data.hours) ) .foregroundStyle(Color.asblue) } .frame(maxWidth: .infinity, maxHeight: .infinity) .padding(10) .clipShape(RoundedRectangle(cornerRadius: 25)) .padding() .background(themeSettings.currentColor) .cornerRadius(25) } else { Text("No sleep data") .foregroundColor(.black) .background(Color.white) .cornerRadius(10) .padding() } } .frame(width: 350, height: 200) .background(themeSettings.currentColor) .cornerRadius(30) } private func formattedDate(date: Date) -> String { let formatter = DateFormatter() formatter.dateFormat = "E" return formatter.string(from: date) } }
1
0
625
Jun ’24
Loading json remotely into SwiftUI ListView
I'm using this documentation as my base https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation I'm failing to understand how I would simply adapt the app to load the json remotely import Foundation var landmarks: [Landmark] = load("landmarkData.json") func load<T: Decodable>(_ filename: String) -> T {     let data: Data     guard let file = Bundle.main.url(forResource: filename, withExtension: nil)         else {             fatalError("Couldn't find \(filename) in main bundle.")     }     do {         data = try Data(contentsOf: file)     } catch {         fatalError("Couldn't load \(filename) from main snippet from Model data.swift the only example I've found is included below, however it's using an entirely different setup and does not use a detail view. import SwiftUI import Combine struct ContentView: View { @ObservedObject var fetcher = MovieFetcher() var body: some View {     VStack {         List(fetcher.movies) { movie in             VStack (alignment: .leading) {                 Text(movie.name)                 Image(movie.thumbnail)                 Text(movie.released)                     .font(.system(size: 11))                     .foregroundColor(Color.gray)             }         }     } } } public class MovieFetcher: ObservableObject { @Published var movies = [Movie]() init(){     load() } func load() {     let url = URL(string: "https://wolvideos.web.app/videos.js")!     URLSession.shared.dataTask(with: url) {(data,response,error) in         do {             if let d = data {                 let decodedLists = try JSONDecoder().decode([Movie].self, from: d)                 DispatchQueue.main.async {                     self.movies = decodedLists                 }             }else {                 print("No Data")             }         } catch {             print ("Error")         }              }.resume()       } } struct Movie: Codable, Identifiable { public var id: Int public var name: String public var thumbnail: String public var released: String enum CodingKeys: String, CodingKey {        case id = "id"        case name = "name"        case thumbnail = "thumbnail"        case released = "description"     } } struct ContentView_Previews: PreviewProvider { static var previews: some View {     ContentView() } } If possible can someone explain how and the best way to load a json file and then let the app format it into detail views?
2
0
2.1k
Jul ’21
Can we get a time estimated for "disable making device ready for development"?
it's infuriating that it sometimes only takes 2 minuets and then takes hours on other devices. knowing how long it's going to take would actually give us information to make adjustments. I'm. asking this one to see how many other developers would also like this feature, and to see if anyone knows of an add-on or such.
Replies
0
Boosts
0
Views
678
Activity
Jul ’21
Where is documentation, demos or implementation of this?
This video session is essentially a consumer facing video, there isn't even a single line of code shown. VideoPlayer(player: player) doesn't give the shown "new features" by default, an example / implementation should be expected of a WWDC session.
Replies
0
Boosts
0
Views
807
Activity
Jun ’22
Building a json app that plays videos (troubleshooting and better ways)
I've been watching various tutorials and have managed to come up with the following code: import SwiftUI import AVKit struct ContentView: View {     @State private var wolData = [Main]()          var body: some View {                  NavigationView{List(wolData, id: \.id) { item in             NavigationLink(destination: MainDetail(json: item)) {                                  HStack() {                     Text(item.name)                         .font(.headline)                     Text(item.date)                         .font(.footnote)                     if #available(iOS 15.0, *) {                         AsyncImage(url: URL(string: item.thumbnail))                         { image in                             image                                 .resizable()                                 .scaledToFill()                         } placeholder: {                             Color.purple.opacity(0.1)                         }                         .frame(width: 20, height: 20)                     } else {                         // Fallback on earlier versions                     }                 }                              }                      }.onAppear(perform: loadData)}              }           } extension ContentView {     func loadData() {                  guard let url = URL(string: "https://wolvideos.firebaseapp.com/Simple.json") else {             return         }                  let request = URLRequest(url: url)         URLSession.shared.dataTask(with: request) { data, response, error in                          if let data = data {                 if let response_obj = try? JSONDecoder().decode([Main].self, from: data) {                                          DispatchQueue.main.async {                         self.wolData = response_obj                     }                 }             }                      }.resume()     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } import Foundation struct Main: Decodable {     var id: Int     var name: String     var interactive: String     var thumbnail: String     var date: String     var videolink: String     var sharelink: String } import SwiftUI import AVKit struct MainDetail: View {     var json: Main    private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)        var body: some View {      VideoPlayer(player: player)             .onAppear() {                 // Start the player going, otherwise controls don't appear                 player.play()             }             .onDisappear() {                 // Stop the player when the view disappears                 player.pause()             }    } } Now that I've shared that, the intent is to when the user clicks an option load the corresponding video (from the json not the current test file) via HLS in a default player (like the one in safari)... should I be going about this in a different way? I'm starting to think committing to SwiftUI is a mistake.
Replies
0
Boosts
0
Views
918
Activity
Jun ’22
passing json video link to avplayer
I'm attempting to simply pass the video link inside the json to the navigation menu... my brain is fried as I'm lost in why I can't do this view the navigation link protocol. import SwiftUI import AVKit struct MainDetail: View {          var json: Main         private let player = AVPlayer(url: URL(string: item.interactive))        var body: some View {      VideoPlayer(player: player)             .onAppear() {                 // Start the player going, otherwise controls don't appear                 player.play()             }             .onDisappear() {                 // Stop the player when the view disappears                 player.pause()             }    } } this returns a "Cannot find 'item' in scope" despite being declared back on the main content view. the intended action is loading the "interactive" link (m3u8 file) in a player view when clicked on. I apologize for not understanding what's going on here.
Replies
0
Boosts
0
Views
715
Activity
Jun ’22
is there any way allow video player to rotate in a portrait locked app (SwiftUI)?
I can only find UIKit examples that don't work when used... I would prefer to never use appdelgate again.
Replies
0
Boosts
0
Views
1.1k
Activity
Jul ’22
Is it possible to convert this to using just play view for links in 16?
here is the code: struct LiveView: View {      @State var allowsInlineMediaPlayback: Bool = false      var body: some View {            VStack {                  VideoView(videoID:"vimeo.com/event/000000/embed")                  }                }            } struct VideoView: UIViewRepresentable {      let videoID: String      func makeUIView(context: Context) -> WKWebView {       let configuration = WKWebViewConfiguration()       configuration.allowsInlineMediaPlayback = true       let WKWebView = WKWebView(frame: .zero, configuration: configuration)       return WKWebView        }      func updateUIView(_ uiView: WKWebView, context: Context) {     guard let youtubeURL = URL(string: "https://\(videoID)") else     {return}     uiView.load(URLRequest (url: youtubeURL))   } } Is their a better way to accomplish feeding a live event link into a playerview instead of needing a WKWebView?
Replies
0
Boosts
0
Views
898
Activity
Jul ’22
NLtagger not filtering words such as "And, to, a, in"
what am I not understanding here. in short the view loads text from the jsons descriptions and then should filter out the words. and return and display a list of most used words, debugging shows words being identified by the code but does not filter them out private func loadWordCounts() { DispatchQueue.global(qos: .background).async { let fileManager = FileManager.default guard let documentsDirectory = try? fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) else { return } let descriptions = loadDescriptions(fileManager: fileManager, documentsDirectory: documentsDirectory) var counts = countWords(in: descriptions) let tagsToRemove: Set<NLTag> = [ .verb, .pronoun, .determiner, .particle, .preposition, .conjunction, .interjection, .classifier ] for (word, _) in counts { let tagger = NLTagger(tagSchemes: [.lexicalClass]) tagger.string = word let (tag, _) = tagger.tag(at: word.startIndex, unit: .word, scheme: .lexicalClass) if let unwrappedTag = tag, tagsToRemove.contains(unwrappedTag) { counts[word] = 0 } } DispatchQueue.main.async { self.wordCounts = counts } } }
Replies
0
Boosts
0
Views
580
Activity
Oct ’24
Share button will not move to the right.
I'm running on fumes but logically this view should be showing the share button on the bottom right, and the Title/Date/PlayButton in the middle? What am I doing wrong? It currently just shows the share button underneath import SwiftUI import AVKit struct ContentView: View {     @State private var items = [Item]()     @State private var currentPage = 0     var body: some View {         TabView(selection: $currentPage) {             ForEach(items, id: \.id) { item in                 VideoPlayerView(url: item.interactive)                     .aspectRatio(contentMode: .fill)                     .tag(item.id)                     .onAppear {                         let controller = AVPlayerViewController()                         controller.player = AVPlayer(url: URL(string: item.interactive)!)                         controller.showsPlaybackControls = false                         controller.player?.play()                     }                     .overlay(                         VStack() {                             Spacer()                             Text(item.name)                                 .font(.title)                             Text(item.date)                                 .font(.caption)                             Button(action: {                                 let player = AVPlayer(url: URL(string: item.videolink)!)                                 let playerController = AVPlayerViewController()                                 playerController.player = player                                 playerController.showsPlaybackControls = true                                 UIApplication.shared.windows.first?.rootViewController?.present(playerController, animated: true, completion: {                                     player.play()                                 })                             }) {                                 HStack(spacing: 5) {                                     Text("PLAY")                                     Image(systemName: "play.circle")                                 }                                 .padding(.horizontal, 10)                                 .padding(.vertical, 5)                                 .background(Color.white)                                 .foregroundColor(.black)                                 .cornerRadius(20)                             }                             Button(action: {                                 let activityVC = UIActivityViewController(activityItems: [URL(string: item.sharelink)!], applicationActivities: nil)                                 UIApplication.shared.windows.first?.rootViewController?.present(activityVC, animated: true, completion: nil)                             }) {                                 Image(systemName: "square.and.arrow.up.circle")                                     .foregroundColor(.black)                             }                             .padding(.bottom, 50)                         }                     )             }         }         .tabViewStyle(.page)         .onAppear(perform: loadData)         .gesture(DragGesture()             .onEnded { value in                 let offset = value.translation.width / UIScreen.main.bounds.width                 let newIndex = (CGFloat(currentPage) - offset).rounded()                 currentPage = min(max(Int(newIndex), 0), items.count - 1)             }         )     }     func loadData() {         guard let url = URL(string: "jsonfile") else { return }         URLSession.shared.dataTask(with: url) { data, response, error in             guard let data = data else { return }             do {                 let decoder = JSONDecoder()                 let items = try decoder.decode([Item].self, from: data)                 DispatchQueue.main.async {                     self.items = items                 }             } catch {                 print(error.localizedDescription)             }         }.resume()     } } struct VideoPlayerView: UIViewRepresentable {     let url: String          func makeUIView(context: Context) -> AVPlayerView {         let view = AVPlayerView()         view.playerLayer.player = AVPlayer(url: URL(string: url)!)         view.playerLayer.videoGravity = .resizeAspectFill         view.playerLayer.backgroundColor = UIColor.black.cgColor         view.playerLayer.player?.play()         return view     }          func updateUIView(_ uiView: AVPlayerView, context: Context) {         // Nothing to update     } } class AVPlayerView: UIView {     override class var layerClass: AnyClass {         return AVPlayerLayer.self     }          var playerLayer: AVPlayerLayer {         return layer as! AVPlayerLayer     }          override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {         let hitView = super.hitTest(point, with: event)         return hitView == self ? nil : hitView     } } struct Item: Codable, Identifiable {     let id: Int     let name: String     let interactive: String     let thumbnail: String     let date: String     let videolink: String     let sharelink: String }
Replies
1
Boosts
0
Views
805
Activity
Mar ’23
Preventing objects from overlapping in geometry reader
I'm attempting to create a word map that shows the words without overlapping without bloating the code load. how would you do it and why? I'm also open to handling this in other ways beside a geo reader. let description = trigger.description.lowercased() let wordsWithoutPunctuation = description.components(separatedBy: CharacterSet.punctuationCharacters).joined() let words = wordsWithoutPunctuation.components(separatedBy: " ") let determiners = ["the", "a", "an", "this", "that", "these", "those", "my", "your", "and", "her", "its", "our", "to"] let filteredWords = words.filter { !determiners.contains($0) } let wordCounts = Dictionary(grouping: filteredWords, by: { $0 }).mapValues { $0.count } let sortedWords = wordCounts.sorted { $1.value < $0.value } let topWords = Array(sortedWords.prefix(5)) let maxCount = topWords.first?.value ?? 1 GeometryReader { geometry in ZStack { ForEach(topWords, id: \.key) { word in let wordSize = "\(word.key) (\(word.value))".size(withAttributes: [.font: UIFont.systemFont(ofSize: min(CGFloat(word.value) / CGFloat(maxCount) * 30, 30))]) Text("\(word.key) (\(word.value))") .foregroundColor(.BrandGreen) .font(.system(size: min(CGFloat(word.value) / CGFloat(maxCount) * 30, 30))) // Adjust the multiplier and maximum size as needed .position( x: CGFloat.random(in: wordSize.width / 2..<geometry.size.width - wordSize.width / 2), y: CGFloat.random(in: wordSize.height / 2..<geometry.size.height - wordSize.height / 2) ) } } } } .frame(width: 180, height: 180) .padding(5) .background(Color.harp) .cornerRadius(30) .aspectRatio(1, contentMode: .fit) .clipShape(RoundedRectangle(cornerRadius: 30)) .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 0)
Replies
1
Boosts
0
Views
457
Activity
Jan ’24
SwiftUI calling custom color into ListViewRow
I cant get either a custom of basic color to not timeout Xcode .listRowBackground( Capsule().fill(Color(white: 1, opacity(0.8))) .padding(.vertical, 2) .padding(.horizontal, 10) )
Replies
1
Boosts
0
Views
529
Activity
Jan ’24
HKCategoryValueSleepAnalysis Sending incorrect sleep data.
The edited code still has the problem of not lining up with the health app private func fetchSleepData(for date: Date) { let sleepType = HKObjectType.categoryType(forIdentifier: .sleepAnalysis)! let endOfPeriod = date let startOfPeriod = Calendar.current.date(byAdding: .day, value: -1, to: endOfPeriod)! let predicate = HKQuery.predicateForSamples(withStart: startOfPeriod, end: endOfPeriod, options: [.strictStartDate, .strictEndDate]) let query = HKSampleQuery(sampleType: sleepType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { query, samples, error in guard let samples = samples as? [HKCategorySample], !samples.isEmpty else { DispatchQueue.main.async { self.inBedTime = 0 self.coreTime = 0 self.deepTime = 0 self.remTime = 0 self.isSleepDataAvailable = false } print("No sleep data available for date: \(date)") return } print("Fetched \(samples.count) sleep samples for date: \(date)") var inBedTime = 0.0 var asleepTime = 0.0 var deepTime = 0.0 var remTime = 0.0 for sample in samples { print("Sample value: \(sample.value)") let duration = sample.endDate.timeIntervalSince(sample.startDate) / 60 // convert to minutes switch sample.value { case HKCategoryValueSleepAnalysis.inBed.rawValue: inBedTime += duration case HKCategoryValueSleepAnalysis.asleepCore.rawValue: coreTime += duration case HKCategoryValueSleepAnalysis.asleepDeep.rawValue: deepTime += duration case HKCategoryValueSleepAnalysis.asleepREM.rawValue: remTime += duration default: break } } DispatchQueue.main.async { self.inBedTime = inBedTime self.coreTime = coreTime self.deepTime = deepTime self.remTime = remTime self.isSleepDataAvailable = true } } healthStore?.execute(query) }
Replies
1
Boosts
0
Views
946
Activity
Jan ’24
anything I create in Figma shows up as filled black.
the file does not show a black fill but the app shows a black circle. since it's to long https://gist.github.com/YutaTheTraveler/0b8c11faf997f2118d19bbd009d490ba.js
Topic: Design SubTopic: General Tags:
Replies
1
Boosts
0
Views
959
Activity
May ’24
Charts showing yesterday as todays data
I'm at my Witts end trying to figure out why charts is incorrectly labeling the days! struct SunlightSupportBox: View { @ObservedObject var viewModel = SunlightViewModel() @EnvironmentObject var themeSettings: ThemeSettings var sortedSunlightData: [SunlightData] { viewModel.sunlightData.sorted(by: { $0.date < $1.date }) } var body: some View { VStack { if !sortedSunlightData.isEmpty { Chart { ForEach(sortedSunlightData) { data in BarMark( x: .value("Day", formattedDate(date: data.date)), y: .value("Triggers/Reflections", Double((data.triggersCount * 10 + data.reflectionsCount * 10))) // Each trigger/reflection represents 5 minutes ) .foregroundStyle(Color.green.opacity(0.5)) BarMark( x: .value("Day", formattedDate(date: data.date)), yStart: .value("Sunlight Start", 0), yEnd: .value("Minutes of Sunlight", data.duration * 60) // Convert hours to minutes ) .foregroundStyle(Color.orange.opacity(0.5)) } } .frame(maxWidth: .infinity, maxHeight: .infinity) .padding(10) .clipShape(RoundedRectangle(cornerRadius: 25)) .padding() .background(themeSettings.currentColor) .cornerRadius(25) } else { Text("No sunlight data") .foregroundColor(.black) .background(Color.white) .cornerRadius(10) .padding() } } .frame(width: 350, height: 200) .background(themeSettings.currentColor) .cornerRadius(30) } private func formattedDate(date: Date) -> String { let formatter = DateFormatter() formatter.dateFormat = "E" return formatter.string(from: date) } } This view correctly shows todays day with the correct data struct SleepSupportBox: View { @ObservedObject var viewModel = SleepViewModel() @EnvironmentObject var themeSettings: ThemeSettings var body: some View { VStack { if !viewModel.sleepData.isEmpty { Chart(viewModel.sleepData) { data in BarMark( x: .value("Day", formattedDate(date: data.date)), y: .value("Triggers/Reflections", Double(data.triggersCount + data.reflectionsCount)) ) .foregroundStyle(Color.green.opacity(0.5)) BarMark( x: .value("Day", formattedDate(date: data.date)), y: .value("Hours of Sleep", data.hours) ) .foregroundStyle(Color.asblue) } .frame(maxWidth: .infinity, maxHeight: .infinity) .padding(10) .clipShape(RoundedRectangle(cornerRadius: 25)) .padding() .background(themeSettings.currentColor) .cornerRadius(25) } else { Text("No sleep data") .foregroundColor(.black) .background(Color.white) .cornerRadius(10) .padding() } } .frame(width: 350, height: 200) .background(themeSettings.currentColor) .cornerRadius(30) } private func formattedDate(date: Date) -> String { let formatter = DateFormatter() formatter.dateFormat = "E" return formatter.string(from: date) } }
Replies
1
Boosts
0
Views
625
Activity
Jun ’24
VisionOS 2 virtual Mac & current version of MacOS
Has anyone who’s installed the beta happened to test if you need to have the MacOS beta installed to have virtual desktop? Also I know the hype is about the widescreen but was there clips or notes I missed suggesting that we‘ll get a vertical display this time round As well.
Replies
1
Boosts
0
Views
554
Activity
Jun ’24
Loading json remotely into SwiftUI ListView
I'm using this documentation as my base https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation I'm failing to understand how I would simply adapt the app to load the json remotely import Foundation var landmarks: [Landmark] = load("landmarkData.json") func load<T: Decodable>(_ filename: String) -> T {     let data: Data     guard let file = Bundle.main.url(forResource: filename, withExtension: nil)         else {             fatalError("Couldn't find \(filename) in main bundle.")     }     do {         data = try Data(contentsOf: file)     } catch {         fatalError("Couldn't load \(filename) from main snippet from Model data.swift the only example I've found is included below, however it's using an entirely different setup and does not use a detail view. import SwiftUI import Combine struct ContentView: View { @ObservedObject var fetcher = MovieFetcher() var body: some View {     VStack {         List(fetcher.movies) { movie in             VStack (alignment: .leading) {                 Text(movie.name)                 Image(movie.thumbnail)                 Text(movie.released)                     .font(.system(size: 11))                     .foregroundColor(Color.gray)             }         }     } } } public class MovieFetcher: ObservableObject { @Published var movies = [Movie]() init(){     load() } func load() {     let url = URL(string: "https://wolvideos.web.app/videos.js")!     URLSession.shared.dataTask(with: url) {(data,response,error) in         do {             if let d = data {                 let decodedLists = try JSONDecoder().decode([Movie].self, from: d)                 DispatchQueue.main.async {                     self.movies = decodedLists                 }             }else {                 print("No Data")             }         } catch {             print ("Error")         }              }.resume()       } } struct Movie: Codable, Identifiable { public var id: Int public var name: String public var thumbnail: String public var released: String enum CodingKeys: String, CodingKey {        case id = "id"        case name = "name"        case thumbnail = "thumbnail"        case released = "description"     } } struct ContentView_Previews: PreviewProvider { static var previews: some View {     ContentView() } } If possible can someone explain how and the best way to load a json file and then let the app format it into detail views?
Replies
2
Boosts
0
Views
2.1k
Activity
Jul ’21