FaviconFinder icon indicating copy to clipboard operation
FaviconFinder copied to clipboard

FavIcon is inconsistent in showing in List

Open arbyruns opened this issue 3 years ago • 1 comments

To be honest this maybe how I have implemented this. I've implemented this for SwiftUI based on the mac demo.

In short my repro looks like this and when used in a list the icon does not always appear, but I'm using FavIconView by itself not in a list or a foreach everything works fine.

struct MyView: View {
    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                FavIconView(urlStr: $url)
            }
        }
    }
}
List {
    ForEach(cloudKitManager.jobs, id: \.self){ flagged in
        MyView(url: url)
        }
    }

FavIconView.swift

struct FavIconView: View {
    @ObservedObject var imageLoader = ImageLoader()
    @Binding var urlStr: String
    @State var foundFavIcon = false

    var body: some View {
        VStack {
            if foundFavIcon {
                Image(uiImage: self.imageLoader.image ?? UIImage())
                    .resizable()
                    .frame(width: 25, height: 25, alignment: .center)
                    .aspectRatio(contentMode: .fit)
            }
        }
        .onAppear {
            guard let url = URL(string: self.urlStr) else {
                print("not a vaild URL")
                foundFavIcon = false
                return
            }
            foundFavIcon = true
            self.imageLoader.load(url: url)
        }
    }
}

struct FavIconView_Previews: PreviewProvider {
    static var previews: some View {
        FavIconView(urlStr: .constant("https://microsoft.com/"))
    }
}

final class ImageLoader: ObservableObject {
    @Published private(set) var image: UIImage? = nil

    private var url: URL? = nil

    func load(url: URL) {
        FaviconFinder(url: url, preferredType: .html, preferences: [
            FaviconDownloadType.html: FaviconType.appleTouchIcon.rawValue,
            FaviconDownloadType.ico: "favicon.ico"
        ]).downloadFavicon { result in
            switch result {
            case .success(let favicon):
                print("URL of Favicon: \(favicon.url)")
                self.image = favicon.image

            case .failure(let error):
                print(error)
            }
        }
    }
} 

arbyruns avatar Mar 17 '22 01:03 arbyruns

@arbyruns Apologies for getting back to you so late. I'm struggling to reproduce this issue with the code you provided. Does this still happen on the latest version of SwiftUI?

will-lumley avatar Jul 10 '22 12:07 will-lumley