From 264df440568be0b394aa40c9602f2530415a6385 Mon Sep 17 00:00:00 2001 From: "Ma. de Lourdes Chaparro Candiani" Date: Sat, 23 Nov 2019 14:06:01 -0600 Subject: [PATCH 1/2] [WIP] Implement detail view --- Podfile.lock | 2 +- fakestagram.xcodeproj/project.pbxproj | 8 ++ fakestagram/Base.lproj/Main.storyboard | 35 +++++-- fakestagram/Services/CommentsService.swift | 17 ++++ .../PostDetailController.swift | 95 +++++++++++++++++++ .../ViewControllers/PostsViewController.swift | 6 ++ .../Views/ShowPostViewController.swift | 1 + 7 files changed, 155 insertions(+), 9 deletions(-) create mode 100644 fakestagram/Services/CommentsService.swift create mode 100644 fakestagram/ViewControllers/PostDetailController.swift diff --git a/Podfile.lock b/Podfile.lock index 12505e8..b5bd883 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -17,4 +17,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 02c21c317ba9be4fdecdfff242f3fa20e9b269bd -COCOAPODS: 1.8.3 +COCOAPODS: 1.8.4 diff --git a/fakestagram.xcodeproj/project.pbxproj b/fakestagram.xcodeproj/project.pbxproj index fd88dc7..b29a77b 100644 --- a/fakestagram.xcodeproj/project.pbxproj +++ b/fakestagram.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 05D8E963168944918FFCC7FF /* Pods_fakestagram.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FFAA8B9133CEE464C903FDF5 /* Pods_fakestagram.framework */; }; + 98A0E6D32389B32B005349A8 /* PostDetailController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98A0E6D22389B32B005349A8 /* PostDetailController.swift */; }; + 98A0E6D52389BC54005349A8 /* CommentsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98A0E6D42389BC54005349A8 /* CommentsService.swift */; }; D3D164DC7444072103DE027D /* Pods_fakestagramTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E669E31C92D68155914FC99D /* Pods_fakestagramTests.framework */; }; E023A5542380859200B13B7D /* NotificationKeys.swift in Sources */ = {isa = PBXBuildFile; fileRef = E023A5532380859200B13B7D /* NotificationKeys.swift */; }; E038FCED233FC78600E28571 /* PostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E038FCEC233FC78600E28571 /* PostViewController.swift */; }; @@ -65,6 +67,8 @@ /* Begin PBXFileReference section */ 5E0AE2BFBF9C8D81E583E1BE /* Pods-fakestagramTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagramTests.release.xcconfig"; path = "Target Support Files/Pods-fakestagramTests/Pods-fakestagramTests.release.xcconfig"; sourceTree = ""; }; 66342726A6BCEEEF6289B1AA /* Pods-fakestagramTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagramTests.debug.xcconfig"; path = "Target Support Files/Pods-fakestagramTests/Pods-fakestagramTests.debug.xcconfig"; sourceTree = ""; }; + 98A0E6D22389B32B005349A8 /* PostDetailController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostDetailController.swift; sourceTree = ""; }; + 98A0E6D42389BC54005349A8 /* CommentsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentsService.swift; sourceTree = ""; }; CCC6C6EA9D17CA169951A745 /* Pods-fakestagram.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagram.release.xcconfig"; path = "Target Support Files/Pods-fakestagram/Pods-fakestagram.release.xcconfig"; sourceTree = ""; }; DB68F7FEC717545506064663 /* Pods-fakestagram.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagram.debug.xcconfig"; path = "Target Support Files/Pods-fakestagram/Pods-fakestagram.debug.xcconfig"; sourceTree = ""; }; E023A5532380859200B13B7D /* NotificationKeys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationKeys.swift; sourceTree = ""; }; @@ -154,6 +158,7 @@ children = ( E045844823762E10006574FA /* CreatePostService.swift */, E0458456237725B9006574FA /* IndexService.swift */, + 98A0E6D42389BC54005349A8 /* CommentsService.swift */, ); path = Services; sourceTree = ""; @@ -241,6 +246,7 @@ E0A9ED50235A782F00A0527E /* PostsViewController.swift */, E038FCEC233FC78600E28571 /* PostViewController.swift */, E0E25462235B675100C55767 /* CameraViewController.swift */, + 98A0E6D22389B32B005349A8 /* PostDetailController.swift */, ); path = ViewControllers; sourceTree = ""; @@ -514,12 +520,14 @@ E0E8B96E2348203B00DA9D1A /* RequestBuilder.swift in Sources */, E0E8B9682348036D00DA9D1A /* Client.swift in Sources */, E0A9ED51235A782F00A0527E /* PostsViewController.swift in Sources */, + 98A0E6D52389BC54005349A8 /* CommentsService.swift in Sources */, E045844923762E10006574FA /* CreatePostService.swift in Sources */, E0C93EA62352454E00FD330C /* Author.swift in Sources */, E0A9ED54235A7FF300A0527E /* PostCollectionViewCell.swift in Sources */, E0E8B96C23481E1700DA9D1A /* HttpResponse.swift in Sources */, E045844F23765B0F006574FA /* ImageStore.swift in Sources */, E045844D23765677006574FA /* DataContainer.swift in Sources */, + 98A0E6D32389B32B005349A8 /* PostDetailController.swift in Sources */, E0E8B9652348018200DA9D1A /* Credentials.swift in Sources */, E0E8B971234916D100DA9D1A /* RestClient.swift in Sources */, E084FADC233A8734009AC50D /* SceneDelegate.swift in Sources */, diff --git a/fakestagram/Base.lproj/Main.storyboard b/fakestagram/Base.lproj/Main.storyboard index 469596b..cffe55e 100644 --- a/fakestagram/Base.lproj/Main.storyboard +++ b/fakestagram/Base.lproj/Main.storyboard @@ -2,13 +2,13 @@ - + - + @@ -17,7 +17,7 @@ - + @@ -46,14 +46,14 @@ - + - + @@ -99,7 +99,7 @@ - + @@ -115,7 +115,7 @@ - + @@ -128,7 +128,7 @@ - + @@ -137,6 +137,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/fakestagram/Services/CommentsService.swift b/fakestagram/Services/CommentsService.swift new file mode 100644 index 0000000..7055fa2 --- /dev/null +++ b/fakestagram/Services/CommentsService.swift @@ -0,0 +1,17 @@ +// +// GetCommentsService.swift +// fakestagram +// +// Created by Ma. de Lourdes Chaparro Candiani on 11/23/19. +// Copyright © 2019 3zcurdia. All rights reserved. +// + +import Foundation + +class CommentsService { + + func getComments(postId: Int, success: @escaping ([Comment]?) -> Void) { + let client = RestClient<[Comment]>(client: Client.fakestagram, basePath: "/api/v1/posts/\(postId)/comments") + client.show(success: success) + } +} diff --git a/fakestagram/ViewControllers/PostDetailController.swift b/fakestagram/ViewControllers/PostDetailController.swift new file mode 100644 index 0000000..4257a73 --- /dev/null +++ b/fakestagram/ViewControllers/PostDetailController.swift @@ -0,0 +1,95 @@ +// +// PostDetailController.swift +// fakestagram +// +// Created by Ma. de Lourdes Chaparro Candiani on 11/23/19. +// Copyright © 2019 3zcurdia. All rights reserved. +// + +import UIKit + +class PostDetailController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { + + var postData: Post! + let commentsService = CommentsService() + + var comments: [Comment]? { + didSet { + self.commentsTable.reloadData() + } + } + + let postImage: UIImageView = { + let imgView = UIImageView() + imgView.translatesAutoresizingMaskIntoConstraints = false + return imgView + }() + + let postTitle: UILabel = { + let titleLabel = UILabel() + titleLabel.translatesAutoresizingMaskIntoConstraints = false + titleLabel.textColor = .white + titleLabel.font = UIFont(name: "Marker Felt", size: 25) + return titleLabel + }() + + let likesCounter: UILabel = { + let likesLabel = UILabel() + likesLabel.translatesAutoresizingMaskIntoConstraints = false + likesLabel.textColor = .white + likesLabel.font = UIFont(name: "Marker Felt", size: 25) + return likesLabel + }() + + let commentsTable: UITableView = { + let comments = UITableView() + comments.translatesAutoresizingMaskIntoConstraints = false + return comments + }() + + override func viewDidLoad() { + super.viewDidLoad() + + view.addSubview(postImage) + view.addSubview(postTitle) + view.addSubview(likesCounter) + view.addSubview(commentsTable) + + postImage.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true + postImage.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true + postImage.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true + postImage.heightAnchor.constraint(equalToConstant: 340).isActive = true + + postTitle.topAnchor.constraint(equalTo: postImage.bottomAnchor, constant: 15).isActive = true + postTitle.leftAnchor.constraint(equalTo: postImage.leftAnchor).isActive = true + postTitle.heightAnchor.constraint(equalToConstant: 40).isActive = true + postTitle.widthAnchor.constraint(equalToConstant: 340).isActive = true + + likesCounter.topAnchor.constraint(equalTo: postTitle.bottomAnchor).isActive = true + likesCounter.leftAnchor.constraint(equalTo: postTitle.leftAnchor).isActive = true + likesCounter.heightAnchor.constraint(equalToConstant: 40).isActive = true + likesCounter.widthAnchor.constraint(equalToConstant: 250).isActive = true + + commentsTable.topAnchor.constraint(equalTo: likesCounter.bottomAnchor, constant: 20).isActive = true + commentsTable.leftAnchor.constraint(equalTo: likesCounter.leftAnchor).isActive = true + + postTitle.text = postData.title + likesCounter.text = postData.likesCountText() + postData.load { [unowned self] img in + self.postImage.image = img + } + commentsService.getComments(postId: postData.id!) { [unowned self] data in + self.comments = data + } + } + + + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return comments?.count ?? 0 + } + + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + <#code#> + } + +} diff --git a/fakestagram/ViewControllers/PostsViewController.swift b/fakestagram/ViewControllers/PostsViewController.swift index a49ab76..e70078d 100644 --- a/fakestagram/ViewControllers/PostsViewController.swift +++ b/fakestagram/ViewControllers/PostsViewController.swift @@ -71,4 +71,10 @@ class PostsViewController: UIViewController, UICollectionViewDelegate, UICollect func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: self.view.frame.width, height: 650) } + + func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { + let postDetail = PostDetailController() + postDetail.postData = posts![indexPath.item] + navigationController?.pushViewController(postDetail, animated: true) + } } diff --git a/fakestagram/Views/ShowPostViewController.swift b/fakestagram/Views/ShowPostViewController.swift index c0afe40..e2bba50 100644 --- a/fakestagram/Views/ShowPostViewController.swift +++ b/fakestagram/Views/ShowPostViewController.swift @@ -34,6 +34,7 @@ class ShowPostViewController: UIViewController { func onTapShowComments(_ sender: Any) { print("to show comment") } + /* // MARK: - Navigation From be8d8af0723fcc0ad25278af87434e9decbd4e2e Mon Sep 17 00:00:00 2001 From: "Ma. de Lourdes Chaparro Candiani" Date: Wed, 5 Feb 2020 13:54:25 -0600 Subject: [PATCH 2/2] Finished programmatic detail view --- fakestagram.xcodeproj/project.pbxproj | 4 ++ .../PostDetailController.swift | 52 ++++++++++++------- fakestagram/Views/CommentTableViewCell.swift | 21 ++++++++ 3 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 fakestagram/Views/CommentTableViewCell.swift diff --git a/fakestagram.xcodeproj/project.pbxproj b/fakestagram.xcodeproj/project.pbxproj index b29a77b..bf778cc 100644 --- a/fakestagram.xcodeproj/project.pbxproj +++ b/fakestagram.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 05D8E963168944918FFCC7FF /* Pods_fakestagram.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FFAA8B9133CEE464C903FDF5 /* Pods_fakestagram.framework */; }; 98A0E6D32389B32B005349A8 /* PostDetailController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98A0E6D22389B32B005349A8 /* PostDetailController.swift */; }; 98A0E6D52389BC54005349A8 /* CommentsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98A0E6D42389BC54005349A8 /* CommentsService.swift */; }; + 98F533E123EB3AF500C4994F /* CommentTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 98F533E023EB3AF500C4994F /* CommentTableViewCell.swift */; }; D3D164DC7444072103DE027D /* Pods_fakestagramTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E669E31C92D68155914FC99D /* Pods_fakestagramTests.framework */; }; E023A5542380859200B13B7D /* NotificationKeys.swift in Sources */ = {isa = PBXBuildFile; fileRef = E023A5532380859200B13B7D /* NotificationKeys.swift */; }; E038FCED233FC78600E28571 /* PostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E038FCEC233FC78600E28571 /* PostViewController.swift */; }; @@ -69,6 +70,7 @@ 66342726A6BCEEEF6289B1AA /* Pods-fakestagramTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagramTests.debug.xcconfig"; path = "Target Support Files/Pods-fakestagramTests/Pods-fakestagramTests.debug.xcconfig"; sourceTree = ""; }; 98A0E6D22389B32B005349A8 /* PostDetailController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostDetailController.swift; sourceTree = ""; }; 98A0E6D42389BC54005349A8 /* CommentsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentsService.swift; sourceTree = ""; }; + 98F533E023EB3AF500C4994F /* CommentTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentTableViewCell.swift; sourceTree = ""; }; CCC6C6EA9D17CA169951A745 /* Pods-fakestagram.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagram.release.xcconfig"; path = "Target Support Files/Pods-fakestagram/Pods-fakestagram.release.xcconfig"; sourceTree = ""; }; DB68F7FEC717545506064663 /* Pods-fakestagram.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-fakestagram.debug.xcconfig"; path = "Target Support Files/Pods-fakestagram/Pods-fakestagram.debug.xcconfig"; sourceTree = ""; }; E023A5532380859200B13B7D /* NotificationKeys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationKeys.swift; sourceTree = ""; }; @@ -236,6 +238,7 @@ E0C93E9F23523B7C00FD330C /* ShowPostViewController.swift */, E0C93EA023523B7C00FD330C /* ShowPostViewController.xib */, E0C93EA32352452900FD330C /* AuthorView.swift */, + 98F533E023EB3AF500C4994F /* CommentTableViewCell.swift */, ); path = Views; sourceTree = ""; @@ -521,6 +524,7 @@ E0E8B9682348036D00DA9D1A /* Client.swift in Sources */, E0A9ED51235A782F00A0527E /* PostsViewController.swift in Sources */, 98A0E6D52389BC54005349A8 /* CommentsService.swift in Sources */, + 98F533E123EB3AF500C4994F /* CommentTableViewCell.swift in Sources */, E045844923762E10006574FA /* CreatePostService.swift in Sources */, E0C93EA62352454E00FD330C /* Author.swift in Sources */, E0A9ED54235A7FF300A0527E /* PostCollectionViewCell.swift in Sources */, diff --git a/fakestagram/ViewControllers/PostDetailController.swift b/fakestagram/ViewControllers/PostDetailController.swift index 4257a73..d4f1238 100644 --- a/fakestagram/ViewControllers/PostDetailController.swift +++ b/fakestagram/ViewControllers/PostDetailController.swift @@ -8,10 +8,11 @@ import UIKit -class PostDetailController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { - +class PostDetailController: UIViewController, UITableViewDelegate, UITableViewDataSource { + var postData: Post! let commentsService = CommentsService() + let cellIdentifier = "commentCell" var comments: [Comment]? { didSet { @@ -22,6 +23,7 @@ class PostDetailController: UIViewController, UICollectionViewDelegate, UICollec let postImage: UIImageView = { let imgView = UIImageView() imgView.translatesAutoresizingMaskIntoConstraints = false + imgView.contentMode = .scaleAspectFit return imgView }() @@ -29,7 +31,7 @@ class PostDetailController: UIViewController, UICollectionViewDelegate, UICollec let titleLabel = UILabel() titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.textColor = .white - titleLabel.font = UIFont(name: "Marker Felt", size: 25) + titleLabel.font = UIFont(name: "System Semibold", size: 25) return titleLabel }() @@ -37,7 +39,7 @@ class PostDetailController: UIViewController, UICollectionViewDelegate, UICollec let likesLabel = UILabel() likesLabel.translatesAutoresizingMaskIntoConstraints = false likesLabel.textColor = .white - likesLabel.font = UIFont(name: "Marker Felt", size: 25) + likesLabel.font = UIFont(name: "System", size: 25) return likesLabel }() @@ -58,38 +60,48 @@ class PostDetailController: UIViewController, UICollectionViewDelegate, UICollec postImage.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true postImage.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true postImage.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true - postImage.heightAnchor.constraint(equalToConstant: 340).isActive = true + postImage.heightAnchor.constraint(equalToConstant: 400).isActive = true + + postTitle.topAnchor.constraint(equalTo: postImage.bottomAnchor, constant: 10).isActive = true + postTitle.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true + postTitle.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true + postTitle.heightAnchor.constraint(equalToConstant: 30).isActive = true - postTitle.topAnchor.constraint(equalTo: postImage.bottomAnchor, constant: 15).isActive = true - postTitle.leftAnchor.constraint(equalTo: postImage.leftAnchor).isActive = true - postTitle.heightAnchor.constraint(equalToConstant: 40).isActive = true - postTitle.widthAnchor.constraint(equalToConstant: 340).isActive = true likesCounter.topAnchor.constraint(equalTo: postTitle.bottomAnchor).isActive = true likesCounter.leftAnchor.constraint(equalTo: postTitle.leftAnchor).isActive = true - likesCounter.heightAnchor.constraint(equalToConstant: 40).isActive = true + likesCounter.heightAnchor.constraint(equalToConstant: 30).isActive = true likesCounter.widthAnchor.constraint(equalToConstant: 250).isActive = true - commentsTable.topAnchor.constraint(equalTo: likesCounter.bottomAnchor, constant: 20).isActive = true - commentsTable.leftAnchor.constraint(equalTo: likesCounter.leftAnchor).isActive = true + commentsTable.topAnchor.constraint(equalTo: likesCounter.bottomAnchor, constant: 10).isActive = true + commentsTable.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true + commentsTable.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true + commentsTable.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true + commentsTable.register(CommentTableViewCell.self, forCellReuseIdentifier: cellIdentifier) + commentsTable.dataSource = self + commentsTable.delegate = self postTitle.text = postData.title likesCounter.text = postData.likesCountText() - postData.load { [unowned self] img in + postData.load { img in self.postImage.image = img } - commentsService.getComments(postId: postData.id!) { [unowned self] data in - self.comments = data - } + + commentsService.getComments(postId: postData.id!) { data in + self.comments = data + } } - - func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return comments?.count ?? 0 } - func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - <#code#> + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = commentsTable.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) + guard let comments = self.comments else {return cell} + cell.textLabel?.text = comments[indexPath.row].content + cell.detailTextLabel?.text = comments[indexPath.row].author!.name + return cell } } diff --git a/fakestagram/Views/CommentTableViewCell.swift b/fakestagram/Views/CommentTableViewCell.swift new file mode 100644 index 0000000..3243bbb --- /dev/null +++ b/fakestagram/Views/CommentTableViewCell.swift @@ -0,0 +1,21 @@ +// +// CommentTableViewCell.swift +// fakestagram +// +// Created by Ma. de Lourdes Chaparro Candiani on 05/02/20. +// Copyright © 2020 3zcurdia. All rights reserved. +// + +import UIKit + +class CommentTableViewCell: UITableViewCell { + + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { + super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +}