Skip to content

Commit

Permalink
fix parsing. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Livinglist authored May 11, 2023
1 parent 8e15dcd commit 9c3e520
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class StoriesRepository {

public func fetchAllStories(from storyType: StoryType, onStoryFetched: @escaping (Story) -> Void) async -> Void {
let storyIds = await fetchStoryIds(from: storyType)

for id in storyIds {
let story = await self.fetchStory(id)
if let story = story {
Expand All @@ -28,26 +27,21 @@ public class StoriesRepository {
return storyIds ?? [Int]()
}

public func fetchStories(ids: [Int], filtered: Bool = true, onStoryFetched: @escaping (Story) -> Void) async -> Void {
public func fetchStories(ids: [Int], onStoryFetched: @escaping (Story) -> Void) async -> Void {
for id in ids {
let story = await fetchStory(id)
if var story = story {
if filtered {
let filteredText = story.text.htmlStripped
story = story.copyWith(text: filteredText)
}
if let story = story {
onStoryFetched(story)
}
}
}

public func fetchStory(_ id: Int) async -> Story?{
let response = await AF.request("\(self.baseUrl)item/\(id).json").serializingString().response

if let data = response.data,
var story = try? JSONDecoder().decode(Story.self, from: data) {
let filteredText = story.text.htmlStripped
story = story.copyWith(text: filteredText)
let formattedText = story.text.htmlStripped
story = story.copyWith(text: formattedText)
return story
} else {
return nil
Expand All @@ -56,25 +50,21 @@ public class StoriesRepository {

// MARK: - Comment related.

public func fetchComments(ids: [Int], filtered: Bool = true, onCommentFetched: @escaping (Comment) -> Void) async -> Void {
public func fetchComments(ids: [Int], onCommentFetched: @escaping (Comment) -> Void) async -> Void {
for id in ids {
let comment = await fetchComment(id)

if var comment = comment {
if filtered {
let filteredText = comment.text.htmlStripped
comment = comment.copyWith(text: filteredText)
}
if let comment = comment {
onCommentFetched(comment)
}
}
}

public func fetchComment(_ id: Int) async -> Comment?{
public func fetchComment(_ id: Int) async -> Comment? {
let response = await AF.request("\(self.baseUrl)item/\(id).json").serializingString().response

if let data = response.data {
let comment = try? JSONDecoder().decode(Comment.self, from: data)
if let data = response.data,
var comment = try? JSONDecoder().decode(Comment.self, from: data) {
let formattedText = comment.text.htmlStripped
comment = comment.copyWith(text: formattedText)
return comment
} else {
return nil
Expand All @@ -86,41 +76,30 @@ public class StoriesRepository {
public func fetchItems(ids: [Int], filtered: Bool = true, onItemFetched: @escaping (any Item) -> Void) async -> Void {
for id in ids {
let item = await fetchItem(id)

guard let item = item else { continue }

if var story = item as? Story {
if filtered {
let filteredText = story.text.htmlStripped
story = story.copyWith(text: filteredText)
}
if let story = item as? Story {
onItemFetched(story)
} else if var cmt = item as? Comment {
if filtered {
let filteredText = cmt.text.htmlStripped
cmt = cmt.copyWith(text: filteredText)
}
} else if let cmt = item as? Comment {
onItemFetched(cmt)
}
}
}

public func fetchItem(_ id: Int) async -> (any Item)? {
let response = await AF.request("\(self.baseUrl)item/\(id).json").serializingString().response

if let data = response.data,
let result = try? response.result.get(),
let map = result.toJSON() as? [String: AnyObject],
let type = map["type"] as? String {
switch type {
case "story":
let story = try? JSONDecoder().decode(Story.self, from: data)
let filteredText = story?.text.htmlStripped
return story?.copyWith(text: filteredText)
let formattedText = story?.text.htmlStripped
return story?.copyWith(text: formattedText)
case "comment":
let comment = try? JSONDecoder().decode(Comment.self, from: data)
let filteredText = comment?.text.htmlStripped
return comment?.copyWith(text: filteredText)
let formattedText = comment?.text.htmlStripped
return comment?.copyWith(text: formattedText)
default:
return nil
}
Expand All @@ -133,12 +112,10 @@ public class StoriesRepository {

public func fetchUser(_ id: String) async -> User? {
let response = await AF.request("\(self.baseUrl)/user/\(id).json").serializingString().response

if let data = response.data,
let user = try? JSONDecoder().decode(User.self, from: data) {
let filteredText = user.about.orEmpty.htmlStripped

return user.copyWith(about: filteredText)
let formattedText = user.about.orEmpty.htmlStripped
return user.copyWith(about: formattedText)
} else {
return nil
}
Expand Down
17 changes: 0 additions & 17 deletions Shared/Extensions/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ extension String {
!isEmpty
}

var htmlStripped: String {
let res = try? NSAttributedString(data: self.data(using: .unicode)!,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil).string
return res.orEmpty
}

var withExtraLineBreak: String {
if isEmpty { return self }
let range = startIndex..<index(endIndex, offsetBy: -1)
var str = String(replacingOccurrences(of: "\n", with: "\n\n", range: range))
while str.last?.isWhitespace == true {
str = String(str.dropLast())
}
return str
}

var markdowned: AttributedString {
// Regex matching URLs.
let regex = try! NSRegularExpression(pattern: #"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)"#)
Expand Down
4 changes: 2 additions & 2 deletions ZCombinator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = "ZCombinator (iOS).entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 7;
CURRENT_PROJECT_VERSION = 8;
DEVELOPMENT_TEAM = QMWX3X2NF7;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -1033,7 +1033,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = "ZCombinator (iOS).entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 7;
CURRENT_PROJECT_VERSION = 8;
DEVELOPMENT_TEAM = QMWX3X2NF7;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down

0 comments on commit 9c3e520

Please sign in to comment.