Skip to content

Commit

Permalink
Fixed NSAttributeString race condition
Browse files Browse the repository at this point in the history
+ Made some changes to the LoadingCircle element to make it look better
  • Loading branch information
mattrighetti committed Jul 21, 2021
1 parent b6764eb commit 9586b60
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 39 deletions.
12 changes: 8 additions & 4 deletions HNReader.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
C9B83DD926A863C000036AC6 /* Job.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DD826A863C000036AC6 /* Job.swift */; };
C9B83DDB26A863D000036AC6 /* Story.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DDA26A863D000036AC6 /* Story.swift */; };
C9B83DDF26A8808900036AC6 /* CommentCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DDE26A8808900036AC6 /* CommentCell.swift */; };
C9B83DE126A8820F00036AC6 /* HTMLText.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DE026A8820F00036AC6 /* HTMLText.swift */; };
C9B83DE926A8A23C00036AC6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9B83DE826A8A23C00036AC6 /* Assets.xcassets */; };
C9B83DEB26A8A76800036AC6 /* +NSTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DEA26A8A76800036AC6 /* +NSTextField.swift */; };
C9B83DED26A8ACD000036AC6 /* LoadingCircle.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DEC26A8ACD000036AC6 /* LoadingCircle.swift */; };
C9B83DEF26A8AF4A00036AC6 /* +String.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DEE26A8AF4A00036AC6 /* +String.swift */; };
C9D0937726741BBE002CC786 /* HNReaderApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D0937626741BBE002CC786 /* HNReaderApp.swift */; };
C9D0937926741BBE002CC786 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D0937826741BBE002CC786 /* HomeView.swift */; };
C9D0937E26741BBF002CC786 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9D0937D26741BBF002CC786 /* Preview Assets.xcassets */; };
Expand Down Expand Up @@ -72,9 +73,10 @@
C9B83DD826A863C000036AC6 /* Job.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Job.swift; sourceTree = "<group>"; };
C9B83DDA26A863D000036AC6 /* Story.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Story.swift; sourceTree = "<group>"; };
C9B83DDE26A8808900036AC6 /* CommentCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentCell.swift; sourceTree = "<group>"; };
C9B83DE026A8820F00036AC6 /* HTMLText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTMLText.swift; sourceTree = "<group>"; };
C9B83DE826A8A23C00036AC6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C9B83DEA26A8A76800036AC6 /* +NSTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "+NSTextField.swift"; sourceTree = "<group>"; };
C9B83DEC26A8ACD000036AC6 /* LoadingCircle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingCircle.swift; sourceTree = "<group>"; };
C9B83DEE26A8AF4A00036AC6 /* +String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "+String.swift"; sourceTree = "<group>"; };
C9D0937326741BBE002CC786 /* HNReader.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HNReader.app; sourceTree = BUILT_PRODUCTS_DIR; };
C9D0937626741BBE002CC786 /* HNReaderApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HNReaderApp.swift; sourceTree = "<group>"; };
C9D0937826741BBE002CC786 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -126,6 +128,7 @@
children = (
3307159309D438EFAA1259C7 /* +Date.swift */,
C9B83DEA26A8A76800036AC6 /* +NSTextField.swift */,
C9B83DEE26A8AF4A00036AC6 /* +String.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -160,7 +163,7 @@
children = (
C93F99B5267554F00046F870 /* StoryCell.swift */,
C9B83DDE26A8808900036AC6 /* CommentCell.swift */,
C9B83DE026A8820F00036AC6 /* HTMLText.swift */,
C9B83DEC26A8ACD000036AC6 /* LoadingCircle.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -420,18 +423,19 @@
C9D0938326741BBF002CC786 /* HNReader.xcdatamodeld in Sources */,
C9E9BD032674D095001B4E19 /* User.swift in Sources */,
C9E9BCFF2674CB6C001B4E19 /* HackerNewsClient.swift in Sources */,
C9B83DEF26A8AF4A00036AC6 /* +String.swift in Sources */,
C9E9BD012674D007001B4E19 /* HackerNews.swift in Sources */,
C9D0937726741BBE002CC786 /* HNReaderApp.swift in Sources */,
330713D3016ED410AFD53FDF /* ItemListViewModel.swift in Sources */,
C9B83DDB26A863D000036AC6 /* Story.swift in Sources */,
330711A9216E762026AF98A0 /* +Date.swift in Sources */,
C9B58794267C153C005E0A50 /* DetailStoryView.swift in Sources */,
C9B83DD426A8631300036AC6 /* Comment.swift in Sources */,
C9B83DE126A8820F00036AC6 /* HTMLText.swift in Sources */,
33071F1C64D4742E1F947FAA /* ItemDownloader.swift in Sources */,
3307147AB95F03650FC40B97 /* ItemCache.swift in Sources */,
C9B83DD926A863C000036AC6 /* Job.swift in Sources */,
C9B83DDF26A8808900036AC6 /* CommentCell.swift in Sources */,
C9B83DED26A8ACD000036AC6 /* LoadingCircle.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
27 changes: 27 additions & 0 deletions HNReader/Utils/+String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// +String.swift
// HNReader
//
// Created by Mattia Righetti on 21/07/21.
//

import Foundation

extension String {
init?(htmlEncodedString: String) {
guard let data = htmlEncodedString.data(using: .utf8) else {
return nil
}

let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]

guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else {
return nil
}

self.init(attributedString.string)
}
}
2 changes: 1 addition & 1 deletion HNReader/View/Components/CommentCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct CommentCell: View {
.foregroundColor(.yellow)

HStack {
HTMLText(text: comment.text ?? "Empty comment")
Text(comment.text!)
Spacer()
}
}
Expand Down
33 changes: 0 additions & 33 deletions HNReader/View/Components/HTMLText.swift

This file was deleted.

41 changes: 41 additions & 0 deletions HNReader/View/Components/LoadingCircle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// LoadingCircle.swift
// HNReader
//
// Created by Mattia Righetti on 21/07/21.
//

import SwiftUI

struct LoadingCircle: View {
@State private var isLoading = false

var body: some View {
ZStack {
Circle()
.stroke(Color.gray.opacity(0.5), lineWidth: 10)
.frame(width: 50, height: 50)

Circle()
.trim(from: 0, to: 0.2)
.stroke(Color.green.opacity(0.7), style: .init(lineWidth: 7, lineCap: .round))
.frame(width: 50, height: 50)
.rotationEffect(Angle(degrees: isLoading ? 360 : 0))
.animation(
Animation
.linear(duration: 1)
.repeatForever(autoreverses: false)
)
.onAppear() {
self.isLoading = true
}
}
.frame(width: 70, height: 70)
}
}

struct LoadingCircle_Previews: PreviewProvider {
static var previews: some View {
LoadingCircle()
}
}
7 changes: 6 additions & 1 deletion HNReader/View/DetailStoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct DetailStoryView: View {
@ViewBuilder
func CommentsSection() -> some View {
if fetching {
Text("Fetching")
LoadingCircle()
} else {
if let comments = comments {
VStack(alignment: .leading) {
Expand Down Expand Up @@ -92,6 +92,11 @@ struct DetailStoryView: View {

DispatchQueue.main.async {
self.comments = flattenComments.compactMap { $0 } as? [Comment]
if let comments = self.comments {
for comment in comments {
comment.text = String(htmlEncodedString: comment.text ?? "Empty comment")
}
}
self.fetching = false
}
}
Expand Down

0 comments on commit 9586b60

Please sign in to comment.