Skip to content

Commit

Permalink
Merge branch 'feature/comments' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrighetti committed Jul 21, 2021
2 parents 27dce82 + 76b088f commit b6764eb
Show file tree
Hide file tree
Showing 19 changed files with 479 additions and 106 deletions.
85 changes: 66 additions & 19 deletions HNReader.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 52;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -15,12 +15,19 @@
330718D415D21296AA14E7CA /* HackerNewsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33071291447141A6D31E671B /* HackerNewsTests.swift */; };
330719203034BDB177F28C41 /* +DateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33071B0E5439D8D207CB68F4 /* +DateTests.swift */; };
33071F1C64D4742E1F947FAA /* ItemDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33071D0E5913DB91DDDBDADB /* ItemDownloader.swift */; };
C93F99B6267554F00046F870 /* ItemCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B5267554F00046F870 /* ItemCell.swift */; };
C93F99B8267557FC0046F870 /* ItemList.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B7267557FC0046F870 /* ItemList.swift */; };
C93F99BA267580CE0046F870 /* HTMLText.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B9267580CE0046F870 /* HTMLText.swift */; };
C93F99B6267554F00046F870 /* StoryCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B5267554F00046F870 /* StoryCell.swift */; };
C93F99B8267557FC0046F870 /* StoryList.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93F99B7267557FC0046F870 /* StoryList.swift */; };
C9B58794267C153C005E0A50 /* DetailStoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B58793267C153C005E0A50 /* DetailStoryView.swift */; };
C9B83DD426A8631300036AC6 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9B83DD326A8631300036AC6 /* Comment.swift */; };
C9B83DD726A8637D00036AC6 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = C9B83DD626A8637D00036AC6 /* Alamofire */; };
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 */; };
C9D0937726741BBE002CC786 /* HNReaderApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D0937626741BBE002CC786 /* HNReaderApp.swift */; };
C9D0937926741BBE002CC786 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D0937826741BBE002CC786 /* HomeView.swift */; };
C9D0937B26741BBF002CC786 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9D0937A26741BBF002CC786 /* Assets.xcassets */; };
C9D0937E26741BBF002CC786 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C9D0937D26741BBF002CC786 /* Preview Assets.xcassets */; };
C9D0938026741BBF002CC786 /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9D0937F26741BBF002CC786 /* Persistence.swift */; };
C9D0938326741BBF002CC786 /* HNReader.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = C9D0938126741BBF002CC786 /* HNReader.xcdatamodeld */; };
Expand Down Expand Up @@ -58,13 +65,19 @@
33071D0E5913DB91DDDBDADB /* ItemDownloader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ItemDownloader.swift; sourceTree = "<group>"; };
33071E538EC434DF1A245518 /* HackerNewsClientTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HackerNewsClientTests.swift; sourceTree = "<group>"; };
33071EEBE46634E658582AE3 /* ItemTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ItemTests.swift; sourceTree = "<group>"; };
C93F99B5267554F00046F870 /* ItemCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemCell.swift; sourceTree = "<group>"; };
C93F99B7267557FC0046F870 /* ItemList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemList.swift; sourceTree = "<group>"; };
C93F99B9267580CE0046F870 /* HTMLText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTMLText.swift; sourceTree = "<group>"; };
C93F99B5267554F00046F870 /* StoryCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoryCell.swift; sourceTree = "<group>"; };
C93F99B7267557FC0046F870 /* StoryList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoryList.swift; sourceTree = "<group>"; };
C9B58793267C153C005E0A50 /* DetailStoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailStoryView.swift; sourceTree = "<group>"; };
C9B83DD326A8631300036AC6 /* Comment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Comment.swift; sourceTree = "<group>"; };
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>"; };
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>"; };
C9D0937A26741BBF002CC786 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C9D0937D26741BBF002CC786 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
C9D0937F26741BBF002CC786 /* Persistence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
C9D0938226741BBF002CC786 /* HNReader.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = HNReader.xcdatamodel; sourceTree = "<group>"; };
Expand All @@ -87,6 +100,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C9B83DD726A8637D00036AC6 /* Alamofire in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -111,6 +125,7 @@
isa = PBXGroup;
children = (
3307159309D438EFAA1259C7 /* +Date.swift */,
C9B83DEA26A8A76800036AC6 /* +NSTextField.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -143,7 +158,9 @@
C9926691267588B80035A88F /* Components */ = {
isa = PBXGroup;
children = (
C93F99B9267580CE0046F870 /* HTMLText.swift */,
C93F99B5267554F00046F870 /* StoryCell.swift */,
C9B83DDE26A8808900036AC6 /* CommentCell.swift */,
C9B83DE026A8820F00036AC6 /* HTMLText.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -171,12 +188,12 @@
C9D0937526741BBE002CC786 /* HNReader */ = {
isa = PBXGroup;
children = (
C9B83DE826A8A23C00036AC6 /* Assets.xcassets */,
C9D093AA26741BFD002CC786 /* HNClient */,
C9D093A926741BF6002CC786 /* ViewModel */,
C9D093A826741BF0002CC786 /* Model */,
C9D093A726741BE1002CC786 /* View */,
C9D0937626741BBE002CC786 /* HNReaderApp.swift */,
C9D0937A26741BBF002CC786 /* Assets.xcassets */,
C9D0937F26741BBF002CC786 /* Persistence.swift */,
C9D0938426741BBF002CC786 /* Info.plist */,
C9D0938526741BBF002CC786 /* HNReader.entitlements */,
Expand Down Expand Up @@ -219,9 +236,9 @@
isa = PBXGroup;
children = (
C9D0937826741BBE002CC786 /* HomeView.swift */,
C93F99B5267554F00046F870 /* ItemCell.swift */,
C93F99B7267557FC0046F870 /* ItemList.swift */,
C93F99B7267557FC0046F870 /* StoryList.swift */,
C9926691267588B80035A88F /* Components */,
C9B58793267C153C005E0A50 /* DetailStoryView.swift */,
);
path = View;
sourceTree = "<group>";
Expand All @@ -231,6 +248,9 @@
children = (
C9D093AB26741C25002CC786 /* Item.swift */,
C9E9BD022674D095001B4E19 /* User.swift */,
C9B83DD326A8631300036AC6 /* Comment.swift */,
C9B83DD826A863C000036AC6 /* Job.swift */,
C9B83DDA26A863D000036AC6 /* Story.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -272,6 +292,7 @@
);
name = HNReader;
packageProductDependencies = (
C9B83DD626A8637D00036AC6 /* Alamofire */,
);
productName = HNReader;
productReference = C9D0937326741BBE002CC786 /* HNReader.app */;
Expand Down Expand Up @@ -345,6 +366,7 @@
);
mainGroup = C9D0936A26741BBE002CC786;
packageReferences = (
C9B83DD526A8637D00036AC6 /* XCRemoteSwiftPackageReference "Alamofire" */,
);
productRefGroup = C9D0937426741BBE002CC786 /* Products */;
projectDirPath = "";
Expand All @@ -362,8 +384,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C9B83DE926A8A23C00036AC6 /* Assets.xcassets in Resources */,
C9D0937E26741BBF002CC786 /* Preview Assets.xcassets in Resources */,
C9D0937B26741BBF002CC786 /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -388,22 +410,28 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C9B83DEB26A8A76800036AC6 /* +NSTextField.swift in Sources */,
C9D093AC26741C25002CC786 /* Item.swift in Sources */,
C9D0938026741BBF002CC786 /* Persistence.swift in Sources */,
C9D0937926741BBE002CC786 /* HomeView.swift in Sources */,
C93F99B6267554F00046F870 /* ItemCell.swift in Sources */,
C93F99B8267557FC0046F870 /* ItemList.swift in Sources */,
C93F99B6267554F00046F870 /* StoryCell.swift in Sources */,
C93F99B8267557FC0046F870 /* StoryList.swift in Sources */,
C9E9BCFD2674C80E001B4E19 /* AppState.swift in Sources */,
C9D0938326741BBF002CC786 /* HNReader.xcdatamodeld in Sources */,
C9E9BD032674D095001B4E19 /* User.swift in Sources */,
C93F99BA267580CE0046F870 /* HTMLText.swift in Sources */,
C9E9BCFF2674CB6C001B4E19 /* HackerNewsClient.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 */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -563,7 +591,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = HNReader/HNReader.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
Expand All @@ -589,7 +617,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = HNReader/HNReader.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
Expand Down Expand Up @@ -734,6 +762,25 @@
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
C9B83DD526A8637D00036AC6 /* XCRemoteSwiftPackageReference "Alamofire" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/Alamofire/Alamofire.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 5.4.3;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
C9B83DD626A8637D00036AC6 /* Alamofire */ = {
isa = XCSwiftPackageProductDependency;
package = C9B83DD526A8637D00036AC6 /* XCRemoteSwiftPackageReference "Alamofire" */;
productName = Alamofire;
};
/* End XCSwiftPackageProductDependency section */

/* Begin XCVersionGroup section */
C9D0938126741BBF002CC786 /* HNReader.xcdatamodeld */ = {
isa = XCVersionGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "Alamofire",
"repositoryURL": "https://github.com/Alamofire/Alamofire.git",
"state": {
"branch": null,
"revision": "f96b619bcb2383b43d898402283924b80e2c4bae",
"version": "5.4.3"
}
}
]
},
"version": 1
}
20 changes: 0 additions & 20 deletions HNReader/Assets.xcassets/AccentColor.colorset/Contents.json

This file was deleted.

21 changes: 21 additions & 0 deletions HNReader/Assets.xcassets/NoComments.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "NoComments.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions HNReader/Assets.xcassets/Select.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "Select.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions HNReader/HNClient/HackerNewsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import Combine
import Alamofire

/// HackerNews Client that exposes methods to get users, items and stories from https://news.ycombinator.com
class HackerNewsClient {
Expand Down Expand Up @@ -65,4 +66,34 @@ class HackerNewsClient {
.flatMap(getStories)
.eraseToAnyPublisher()
}

/// Recursively fetches comments and all its childrens
public func getComments(commentsIds: [Int], completionHandler: @escaping (Bool, [Comment]) -> Void) {
var returnComments : [Comment] = []

let commentsGroup = DispatchGroup()
for commentId in commentsIds {
commentsGroup.enter()
let itemUrl = HackerNews.API.Item.id(commentId).urlString
AF.request(itemUrl).responseJSON { response in
if let commentJSON = try! response.result.get() as? NSDictionary {
let commentObject = Comment.init(json: commentJSON)
commentObject.getComments(completionHandler: { (success) in
returnComments.append(commentObject)
commentsGroup.leave()
})
} else {
commentsGroup.leave()
}
}
}

commentsGroup.notify(queue: .main) {
returnComments.sort {a, b in
commentsIds.firstIndex(of: a.id!)! < commentsIds.firstIndex(of: b.id!)!
}

completionHandler(true, returnComments)
}
}
}
14 changes: 2 additions & 12 deletions HNReader/HNClient/ItemDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,11 @@
import Foundation

protocol ItemDownloader {
var cacheKey: Int { get }
func downloadItem(completion: @escaping (Item?) -> Void)
func downloadItem(itemId: Int, completion: @escaping (Item?) -> Void)
}

class DefaultItemDownloader: ItemDownloader {
let itemId: Int
var cacheKey: Int {
itemId
}

init(itemId: Int) {
self.itemId = itemId
}

func downloadItem(completion: @escaping (Item?) -> ()) {
func downloadItem(itemId: Int, completion: @escaping (Item?) -> ()) {
guard let url = URL(string: HackerNews.API.Item.id(itemId).urlString) else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
Expand Down
13 changes: 6 additions & 7 deletions HNReader/HNReaderApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ struct HNReaderApp: App {

private var displayModeBind: Binding<ColorScheme> {
Binding<ColorScheme>(
get: { appState.getColorScheme() },
set: {
appState.setColorScheme($0)
displayMode = $0
}
get: { appState.getColorScheme() },
set: {
appState.setColorScheme($0)
displayMode = $0
}
)
}
@State var displayMode: ColorScheme?
Expand All @@ -44,8 +44,7 @@ struct HNReaderApp: App {
.frame(maxWidth: 100)
}
}
.frame(minHeight: 100)
.frame(minWidth: 300)
.frame(minWidth: 300, minHeight: 100)
.preferredColorScheme(displayMode)
}
}
Expand Down
Loading

0 comments on commit b6764eb

Please sign in to comment.