Post

Replies

Boosts

Views

Activity

Poor ScrollView performance
Hello, I have a scroll view that when it appears I get a constant stream of warnings from the console: CGBitmapContextInfoCreate: CGColorSpace which uses extended range requires floating point or CIF10 bitmap context This happens whether or not I'm actively scrolling, so maybe it's not a scroll view issue at all? The reason I initially thought it was a scrollView issue was because when I scrolled the scrolling was no longer smooth, it was pretty choppy. Also, I only get these warnings when I run my code on a real device - I do not get these on any simulators. Could it be the mesh gradient causing an issue? I'm not sure what's causing the issue so I apologize in advance for what may be irrelevant code. struct ModernStoryPicker: View { @Environment(CategoryPickerViewModel.self) var categoryPickerViewModel @EnvironmentObject var navigationPath: GrowNavigationState @State private var hasPreselectedStory: Bool = false @State private var storyGenerationType: StoryGenerationType = .arabicCompanion var userInstructions: String { if categoryPickerViewModel.userInputCategory.isEmpty { "Select a category" } else if categoryPickerViewModel.userInputSubCategory.isEmpty { "Select a subcategory" } else { "Select a story" } } var body: some View { ZStack { MeshGradient(width: 3, height: 3, points: [ [0.0, 0.0], [0.5, 0.0], [1.0, 0.0], [0.0, 0.5], [0.9, 0.3], [1.0, 0.5], [0.0, 1.0], [0.5, 1.0], [1.0, 1.0] ], colors: [ .orange, .indigo, .orange, .blue, .blue, .cyan, .green, .indigo, .pink ]) .ignoresSafeArea() VStack { Picker("", selection: $storyGenerationType) { ForEach(StoryGenerationType.allCases) { type in Text(type.rawValue).tag(type) } }.pickerStyle(.segmented) Text(userInstructions) .textScale(.secondary) automatedOrUserStories() Spacer() }.padding() } .onAppear {...} } @ViewBuilder func automatedOrUserStories() -> some View { switch storyGenerationType { case .userGenerated: VStack { Spacer() Text("Coming soon!") } case .arabicCompanion: VStack { // TODO: Unnecessary passing of data, only the 2nd view really needs all these props CategoryPickerView( categories: categoryPickerViewModel.mainCategories(), isSubCategory: false, selectionColor: .blue ) CategoryPickerView( categories: categoryPickerViewModel.subCategories(), isSubCategory: true, selectionColor: .purple ) ScrollView(.horizontal) { HStack { if categoryPickerViewModel.booksForCategories.isEmpty { Text("More books coming soon!") } ForEach(categoryPickerViewModel.booksForCategories) { bookCover in Button(action: { // navigates to BookDetailView.swift navigationPath.path.append(bookCover) }) { ModernStoryCardView( loadedImage: categoryPickerViewModel.imageForBook(bookCover), bookCover: bookCover ) .scrollTransition(axis: .horizontal) { content, phase in content .scaleEffect(phase.isIdentity ? 1 : 0.5) .opacity(phase.isIdentity ? 1 : 0.8) } }.buttonStyle(.plain) } }.scrollTargetLayout() } .contentMargins(80, for: .scrollContent) .scrollTargetBehavior(.viewAligned) }.padding() } } } struct CategoryPickerView: View { @Environment(CategoryPickerViewModel.self) var viewModel let categories: [String] let isSubCategory: Bool let selectionColor: Color var body: some View { ScrollView(.horizontal) { HStack { ForEach(categories, id: \.self) { category in Button(action: { withAnimation { selectCategory(category) } }) { TextRoundedRectangleView(text: category, color: effectiveColor(for: category)) .tag(category) }.buttonStyle(.plain) } } }.scrollIndicators(.hidden) } private func selectCategory(_ string: String) { if !isSubCategory { viewModel.userInputCategory = string } else { viewModel.userInputSubCategory = string } } private func effectiveColor(for category: String) -> Color { let chosenCategory = isSubCategory ? viewModel.userInputSubCategory : viewModel.userInputCategory if category == chosenCategory { return selectionColor } return .white } }
4
0
395
Jan ’25
@Attribute(.unique) working as intended, kind of
I have a model that has a unique property, e.g.: @Model final class UserWord @Attribute(.unique) let word: String let partOfSpeech: PartOfSpeech let metaData: ... At the end of the init I have this: init(...) { if partOfSpeech == .verb { metaData = fetchMeta() } } This works fine when a word is newly created and saved. But let's say there's a unique conflict and a user tries to save a new entry with the same word. Apparently this init still fires and fetchMeta edits the existing entry which gives me the error: CoreData: error: Mutating a managed object 0xb890d8167c911ade <x-coredata://4C75194F-D923-477F-BB22-ACBDECCD7530/UserWord/p2> (0x600002170af0) after it has been removed from its context. I think the solution here is to do some manual checking of the modelContext before saving. Would love to hear other's thoughts.
0
0
353
Jun ’24
how to include secrets file
Hi. My app hits an API. I have the API key stored in a config.plist file. Of course, I don't want to include this file in version control. So I omitted it. I have a workflow that when I push to my main branch, it pushes the newest version to internal testers on test flight. The problem is, the deployed code cannot find the config file, which makes sense. My approach to this problem has been to use resource tags Targets > Resource Tags And I declared that this app should download this resource. I can't figure out how to categorize it as other than download on demand . I would like it to be downloaded immediately with the app. Am I even on the right track here?
1
0
833
Jun ’24
Showing text in a ForEach
I would like to show paragraphs of text in a ForEach. This was my latest attempt: struct ContentView: View { let bodyText = "This is a sample body text that will wrap naturally within the bounds of the container." var body: some View { WordWrapView(words: identifyWords(words: bodyText)) .padding() } func identifyWords(words: String) -> [IdentifiedWord] { let wordsArray = words.split(separator: " ").map { String($0) } var idCounter = 1 return wordsArray.reversed().map { word in let identifiedWord = IdentifiedWord(id: String(idCounter), word: word) idCounter += 1 return identifiedWord } } } struct IdentifiedWord: Identifiable { let id: String let word: String } struct WordWrapView: View { let words: [IdentifiedWord] var body: some View { GeometryReader { geometry in self.createWrappedWords(in: geometry.size) } } func createWrappedWords(in size: CGSize) -> some View { var width: CGFloat = 0 var height: CGFloat = 0 return ZStack(alignment: .topLeading) { ForEach(words) { word in self.wordView(for: word.word) .alignmentGuide(.leading, computeValue: { d in if (abs(width - d.width) > size.width) { width = 0 height -= d.height } let result = width if word == self.words.last! { width = 0 // last item } else { width -= d.width } return result }) .alignmentGuide(.top, computeValue: { _ in let result = height if word == self.words.last! { height = 0 // last item } return result }) } } } func wordView(for text: String) -> some View { Text(text) .padding([.horizontal], 4) .padding([.vertical], 2) .background(Color.gray.opacity(0.2)) .cornerRadius(4) } } The problem is that some words are overlapping other words. Am I on the right track here? I reversed the array because I'm showing arabic words, and the ForEach is placing the words in reverse order...
1
0
433
Jun ’24
Xcode stuck in VCS diff mode
Version 14.0 beta 2 (14A5229c) Whenever I type Xcode mirrors the line I just typed showing me that this version has been updated. This is really unexpected behavior as I am not trying to review my changes or do anything git related. I got into this weird state after making a commit and pushing to my repo, making a pull request and then git pull on my local main. Xcode then mistakenly thought that all files were behind by a commit(s) as previously changed files had the downward arrow next to them. This is a bit annoying than I would like because this is affecting other things. I cannot view anything in my assets.xcassets, which also weirdly seems to be affected by this VCS bug(?). I tried turning off VCS altogether which fixes the Xcode mirroring problem, but I still can't view anything in assets. I also tried refreshing the file status which ultimately returns me back to the same state. Any suggestions?
1
0
1.1k
Jul ’22