Post

Replies

Boosts

Views

Activity

I'm missing something basic here but I am unable to to get the SwiftUI function to save a new file and delete the old one
it should be creating a new file and saving it, and then deleting the original file. iv'e attempted to have it load the UUID and by the title to avoid the need to even have the correct filename.... import SwiftUI struct file1DetailView: View { var file1: file1 @Binding var showFile1: Bool var saveFile1: ((file1) -> Void) @Environment(\.presentationMode) var presentationMode @State private var addFile1View: AddFile1View? var dateFormatter: DateFormatter { let formatter = DateFormatter() formatter.dateStyle = .long return formatter } var timeFormatter: DateFormatter = { let formatter = DateFormatter() formatter.timeStyle = .short return formatter }() var body: some View { ZStack { ScrollView { VStack { VStack{ HStack{ Text(file1.title) .font(.largeTitle.weight(.semibold)) .foregroundColor(.BrandGreen) .disabled(true) Spacer() } Spacer() .frame(height: 5) HStack{ Text(dateFormatter.string(from: file1.date)) .foregroundColor(.BrandGreen) .font(.subheadline.weight(.light)) Text(timeFormatter.string(from: file1.date)) .foregroundColor(.BrandGreen) .font(.subheadline.weight(.light)) Spacer() } Spacer() .frame(height: 15) Text(file1.description) .foregroundColor(.BrandGreen) .disabled(true) Spacer() .frame(height: 10) } .padding() .background(Color.white) .cornerRadius(30) Spacer() } Spacer() } .background( Image("calm-gradient") .resizable() .edgesIgnoringSafeArea(.all) ) VStack { Spacer() Button("Start file1") { let newFile1 = file1(title: file1.title, description: file1.description, date: file1.date, reflectionDate: Date(), preventible: false, Option1: "", Option2: "") addFile1View = AddFile1View(id: file1.id, file1: newFile1, saveFile1: saveFile1) showFile1 = true } .frame(maxWidth: .infinity) .padding(.vertical, 15) .foregroundColor(.white) .background(Color("BrandGreen")) } } .fullScreenCover(item: $addFile1View) { view in NavigationView { view .onDisappear { showFile1 = false presentationMode.wrappedValue.dismiss() } } } } } struct AddFile1View: View, Identifiable { var id: UUID @Environment(\.presentationMode) var presentationMode @State private var preventible = false @State private var Option1 = "" @State private var Option2 = "" @State private var reflectionDate = Date() var file1: file1 var saveFile1: ((file1) -> Void)? // AddFile1View initializer init(id: UUID, file1: file1, saveFile1: ((file1) -> Void)?) { self.id = id self.file1 = file1 self.saveFile1 = saveFile1 } var body: some View { VStack { Toggle("Preventible", isOn: $preventible) TextField("Option1", text: $Option1) TextField("Option2", text: $Option2) DatePicker("file1 Date", selection: $reflectionDate) Button("Save") { let newFile1 = file1(title: file1.title, description: file1.description, date: file1.date, reflectionDate: reflectionDate, preventible: preventible, Option1: Option1, Option2: Option2) saveFile1?(newFile1) presentationMode.wrappedValue.dismiss() } } .navigationTitle("Add file1") } } func saveFile1(file1: file1) { let encoder = JSONEncoder() let fm = FileManager.default let documentsDirectory = fm.urls(for: .documentDirectory, in: .userDomainMask).first! let file1sURL = documentsDirectory.appendingPathComponent("file1s.json") // Read the existing file1s from the file system var file1s: [file1] = [] if fm.fileExists(atPath: file1sURL.path) { do { let data = try Data(contentsOf: file1sURL) file1s = try JSONDecoder().decode([file1].self, from: data) } catch { print("Failed to read file1s from file system: \(error)") } } // Add the new file1 to the list file1s.append(file1) // Write the updated list back to the file system do { let data = try encoder.encode(file1s) try data.write(to: file1sURL) print("file1s saved to device: \(file1s)") print("file1s file URL: \(file1sURL)") } catch { print("Failed to save file1s: \(error)") } // Delete the old file1 file let oldFile1URL = documentsDirectory.appendingPathComponent("\(file1.id).json") if fm.fileExists(atPath: oldFile1URL.path) { do { try fm.removeItem(at: oldFile1URL) print("Deleted old file1 file: \(oldFile1URL.lastPathComponent)") } catch { print("Failed to delete old file1 file: \(error)") } } } struct file1DetailView_Previews: PreviewProvider { static var previews: some View { NavigationView { file1DetailView( file1: file1( title: "Example file1", description: "This is an example file1 description.", date: Date(), file1Date: Date(), preventible: true, Option1: "Example Option1", Option2: "Example Option2" ), showFile1: .constant(false), saveFile1: saveFile1 ) } } }
2
0
696
Sep ’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
443
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
934
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
609
Jun ’24
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
566
Oct ’24