Daggerpov / Quote-Droplet-iOS

iOS app that serves quotes through its feed, search, widgets, and notifications (Available on iOS App Store).
https://apps.apple.com/app/quote-droplet/id6455084603
1 stars 0 forks source link

Refactoring: Modularize String Highlight Helper #110

Open Daggerpov opened 1 day ago

Daggerpov commented 1 day ago

This refactoring will be easier after #109 is done.

I need to move this out of SearchView.swift, perhaps by turning it into a function:

private var attributedString: AttributedString {
        var attributedString = AttributedString(quote.text)
        let searchTextLowercased = (searchText ?? "").lowercased()
        let textLowercased = quote.text.lowercased()
        var searchStartIndex = textLowercased.startIndex

        // Loop to find and highlight all occurrences
        while let range = textLowercased.range(of: searchTextLowercased, range: searchStartIndex..<textLowercased.endIndex) {
            // Convert String.Index to AttributedString.Index
            if let attributedRange = Range(NSRange(range, in: textLowercased), in: attributedString) {
                attributedString[attributedRange].backgroundColor = (colorPalettes[safe: sharedVars.colorPaletteIndex]?[2] ?? .yellow).opacity(0.3)
            }
            // Move searchStartIndex to the end of the found range to continue searching
            searchStartIndex = range.upperBound
        }

        return attributedString
    }