From 41c1a4b3f3b75781259eeef8bfc9fe89ce6e9293 Mon Sep 17 00:00:00 2001 From: Victor Aceves Date: Sat, 28 Aug 2021 22:41:34 -0500 Subject: [PATCH 1/3] Add comments UI components --- Secretly.xcodeproj/project.pbxproj | 36 ++++++++++ Secretly/Base.lproj/Main.storyboard | 64 +++++++++++++++++ Secretly/Models/Author.swift | 14 ++++ Secretly/Models/Comment.swift | 31 +++++++++ .../CommentsViewController.swift | 69 +++++++++++++++++++ ...oller+PostCollectionViewCellDelegate.swift | 17 +++++ ...ntroller+UICollectionViewDataSource .swift | 2 +- Secretly/Views/LeftViewCell.swift | 29 ++++++++ Secretly/Views/LeftViewCell.xib | 63 +++++++++++++++++ Secretly/Views/PostCollectionViewCell.swift | 9 +++ Secretly/Views/PostCollectionViewCell.xib | 36 +++++----- Secretly/Views/RightViewCell.swift | 29 ++++++++ Secretly/Views/RightViewCell.xib | 63 +++++++++++++++++ 13 files changed, 444 insertions(+), 18 deletions(-) create mode 100644 Secretly/Models/Author.swift create mode 100644 Secretly/Models/Comment.swift create mode 100644 Secretly/ViewControllers/CommentsViewController.swift create mode 100644 Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift create mode 100644 Secretly/Views/LeftViewCell.swift create mode 100644 Secretly/Views/LeftViewCell.xib create mode 100644 Secretly/Views/RightViewCell.swift create mode 100644 Secretly/Views/RightViewCell.xib diff --git a/Secretly.xcodeproj/project.pbxproj b/Secretly.xcodeproj/project.pbxproj index 8dd4829..1723a83 100644 --- a/Secretly.xcodeproj/project.pbxproj +++ b/Secretly.xcodeproj/project.pbxproj @@ -56,6 +56,15 @@ 30C77CB6266AF48300A888DC /* CurrentUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30C77CB5266AF48300A888DC /* CurrentUser.swift */; }; 30C77CB8266BD44300A888DC /* CreatePostViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30C77CB7266BD44300A888DC /* CreatePostViewController.swift */; }; 30FD0E722659645A006E309A /* Faker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30FD0E712659645A006E309A /* Faker.swift */; }; + 651015F826DB17460047C3EB /* CommentsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651015F726DB17460047C3EB /* CommentsViewController.swift */; }; + 651015FA26DB17D40047C3EB /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651015F926DB17D40047C3EB /* Comment.swift */; }; + 651015FC26DB18090047C3EB /* Author.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651015FB26DB18090047C3EB /* Author.swift */; }; + 651015FE26DB19620047C3EB /* LeftViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651015FD26DB19620047C3EB /* LeftViewCell.swift */; }; + 6510160026DB19AE0047C3EB /* RightViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651015FF26DB19AE0047C3EB /* RightViewCell.swift */; }; + 6510160226DB1B3C0047C3EB /* RightViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6510160126DB1B3C0047C3EB /* RightViewCell.xib */; }; + 6510160426DB1B460047C3EB /* LeftViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6510160326DB1B460047C3EB /* LeftViewCell.xib */; }; + 6510160626DB20030047C3EB /* UIView+Rounded.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6510160526DB20030047C3EB /* UIView+Rounded.swift */; }; + 6510160826DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6510160726DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift */; }; E021984723FA35E00025C28E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021984623FA35E00025C28E /* AppDelegate.swift */; }; E021984923FA35E00025C28E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021984823FA35E00025C28E /* SceneDelegate.swift */; }; E021984B23FA35E00025C28E /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021984A23FA35E00025C28E /* WelcomeViewController.swift */; }; @@ -125,6 +134,15 @@ 30C77CB5266AF48300A888DC /* CurrentUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUser.swift; sourceTree = ""; }; 30C77CB7266BD44300A888DC /* CreatePostViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreatePostViewController.swift; sourceTree = ""; }; 30FD0E712659645A006E309A /* Faker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Faker.swift; sourceTree = ""; }; + 651015F726DB17460047C3EB /* CommentsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentsViewController.swift; sourceTree = ""; }; + 651015F926DB17D40047C3EB /* Comment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Comment.swift; sourceTree = ""; }; + 651015FB26DB18090047C3EB /* Author.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Author.swift; sourceTree = ""; }; + 651015FD26DB19620047C3EB /* LeftViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeftViewCell.swift; sourceTree = ""; }; + 651015FF26DB19AE0047C3EB /* RightViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RightViewCell.swift; sourceTree = ""; }; + 6510160126DB1B3C0047C3EB /* RightViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = RightViewCell.xib; sourceTree = ""; }; + 6510160326DB1B460047C3EB /* LeftViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LeftViewCell.xib; sourceTree = ""; }; + 6510160526DB20030047C3EB /* UIView+Rounded.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "UIView+Rounded.swift"; path = "../../../../../UIView+Rounded.swift"; sourceTree = ""; }; + 6510160726DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FeedCollectionViewController+PostCollectionViewCellDelegate.swift"; sourceTree = ""; }; E021984323FA35E00025C28E /* Secretly.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Secretly.app; sourceTree = BUILT_PRODUCTS_DIR; }; E021984623FA35E00025C28E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; E021984823FA35E00025C28E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -166,6 +184,8 @@ 307A30592661B73A0020DF8B /* ViewControllers */ = { isa = PBXGroup; children = ( + 651015F726DB17460047C3EB /* CommentsViewController.swift */, + 6510160726DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift */, E021984A23FA35E00025C28E /* WelcomeViewController.swift */, 30C77CB7266BD44300A888DC /* CreatePostViewController.swift */, 307A305A2661B7A20020DF8B /* FeedCollectionViewController.swift */, @@ -197,10 +217,14 @@ 307A306326629B590020DF8B /* Views */ = { isa = PBXGroup; children = ( + 6510160326DB1B460047C3EB /* LeftViewCell.xib */, + 6510160126DB1B3C0047C3EB /* RightViewCell.xib */, 307A305C2661CD510020DF8B /* PostCollectionViewCell.swift */, 307A305D2661CD510020DF8B /* PostCollectionViewCell.xib */, 307A306426629B990020DF8B /* AuthorView.swift */, 302BB61B267D7CC800FD74F5 /* PreviewPostVIew.swift */, + 651015FD26DB19620047C3EB /* LeftViewCell.swift */, + 651015FF26DB19AE0047C3EB /* RightViewCell.swift */, ); path = Views; sourceTree = ""; @@ -235,6 +259,7 @@ 304E06CB2674442800A99128 /* UIImage+encodeBase64.swift */, 30B9B93A268CA9E6007B1942 /* UIImage+PixelBuffer.swift */, 304E06CE267468DA00A99128 /* UIColor+Pastel.swift */, + 6510160526DB20030047C3EB /* UIView+Rounded.swift */, ); path = Extensions; sourceTree = ""; @@ -248,6 +273,8 @@ 307A30572661AD540020DF8B /* User.swift */, 30C77CB3266AF47300A888DC /* Credentials.swift */, 30C77CB5266AF48300A888DC /* CurrentUser.swift */, + 651015F926DB17D40047C3EB /* Comment.swift */, + 651015FB26DB18090047C3EB /* Author.swift */, ); path = Models; sourceTree = ""; @@ -406,7 +433,9 @@ E021985323FA35E20025C28E /* LaunchScreen.storyboard in Resources */, 302BB620267E316900FD74F5 /* PostInputViewController.xib in Resources */, E021985023FA35E20025C28E /* Assets.xcassets in Resources */, + 6510160226DB1B3C0047C3EB /* RightViewCell.xib in Resources */, E021984E23FA35E00025C28E /* Main.storyboard in Resources */, + 6510160426DB1B460047C3EB /* LeftViewCell.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -431,6 +460,7 @@ 302B5845267E658E007133E6 /* HttpResponse.swift in Sources */, 302B584A267E658E007133E6 /* RestClient.swift in Sources */, 302B5848267E658E007133E6 /* HttpClient.swift in Sources */, + 6510160626DB20030047C3EB /* UIView+Rounded.swift in Sources */, 30B9B93D268CB063007B1942 /* NudityChecker.swift in Sources */, 30BC8B9D2662ACBC00F7E6A5 /* ImageLoader.swift in Sources */, 302B5859267E720B007133E6 /* ResponseError.swift in Sources */, @@ -439,13 +469,17 @@ 304E06C626741A5100A99128 /* UIColor+theme.swift in Sources */, 304E06C826742BDA00A99128 /* CreatePostService.swift in Sources */, E021984723FA35E00025C28E /* AppDelegate.swift in Sources */, + 651015FE26DB19620047C3EB /* LeftViewCell.swift in Sources */, 30337955267536980066D94A /* PostInputViewController+CLLocationManagerDelegate.swift in Sources */, + 6510160826DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift in Sources */, 307A30562661AD480020DF8B /* Image.swift in Sources */, 307A30582661AD540020DF8B /* User.swift in Sources */, 302B5849267E658E007133E6 /* RequestError.swift in Sources */, 302B5846267E658E007133E6 /* AmacaConfig.swift in Sources */, 302BB61C267D7CC800FD74F5 /* PreviewPostVIew.swift in Sources */, 3033795D267537B40066D94A /* FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift in Sources */, + 651015FC26DB18090047C3EB /* Author.swift in Sources */, + 651015FA26DB17D40047C3EB /* Comment.swift in Sources */, 30BC8BA82662CEBA00F7E6A5 /* Checksum.swift in Sources */, 30FD0E722659645A006E309A /* Faker.swift in Sources */, 30B9B939268CA989007B1942 /* Nudity.mlmodel in Sources */, @@ -456,6 +490,7 @@ 307A305B2661B7A20020DF8B /* FeedCollectionViewController.swift in Sources */, 304E06CC2674442800A99128 /* UIImage+encodeBase64.swift in Sources */, 30BC8BA62662C02300F7E6A5 /* CacheImage.swift in Sources */, + 6510160026DB19AE0047C3EB /* RightViewCell.swift in Sources */, 302B5847267E658E007133E6 /* StatusCode.swift in Sources */, 302B584B267E658E007133E6 /* RequestBuilder.swift in Sources */, 307A306526629B990020DF8B /* AuthorView.swift in Sources */, @@ -472,6 +507,7 @@ 30B9B93B268CA9E6007B1942 /* UIImage+PixelBuffer.swift in Sources */, 303379592675371C0066D94A /* FeedCollectionViewController+UICollectionViewDataSource .swift in Sources */, 304E06CA26742CC500A99128 /* FeedService.swift in Sources */, + 651015F826DB17460047C3EB /* CommentsViewController.swift in Sources */, 302BB61F267E316900FD74F5 /* PostInputViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Secretly/Base.lproj/Main.storyboard b/Secretly/Base.lproj/Main.storyboard index 4ed5b13..9682926 100644 --- a/Secretly/Base.lproj/Main.storyboard +++ b/Secretly/Base.lproj/Main.storyboard @@ -167,6 +167,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -182,6 +243,9 @@ + + + diff --git a/Secretly/Models/Author.swift b/Secretly/Models/Author.swift new file mode 100644 index 0000000..86d88aa --- /dev/null +++ b/Secretly/Models/Author.swift @@ -0,0 +1,14 @@ +// +// Author.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct Author: Codable { + var name: String + let avatarUrl: String +} diff --git a/Secretly/Models/Comment.swift b/Secretly/Models/Comment.swift new file mode 100644 index 0000000..3cb1365 --- /dev/null +++ b/Secretly/Models/Comment.swift @@ -0,0 +1,31 @@ +// +// Comment.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct Comment: Restable { + var id: Int? + let content: String + let createdAt: Date? + let updatedAt: Date? + let author: Author? + + init(content: String) { + self.id = nil + self.content = content + self.createdAt = nil + self.updatedAt = nil + self.author = nil + } + + func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(content, forKey: .content) + } + +} diff --git a/Secretly/ViewControllers/CommentsViewController.swift b/Secretly/ViewControllers/CommentsViewController.swift new file mode 100644 index 0000000..da17894 --- /dev/null +++ b/Secretly/ViewControllers/CommentsViewController.swift @@ -0,0 +1,69 @@ +// +// CommentsViewController.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +class CommentsViewController: UIViewController { + + @IBOutlet weak var tableView: UITableView! + var comments = [Comment]() + + override func viewDidLoad() { + super.viewDidLoad() + // set title + title = "Post Comments" + setupTable() + fetchData() + } + + func setupTable() { + // config tableView + tableView.rowHeight = UITableView.automaticDimension + tableView.dataSource = self + tableView.backgroundColor = UIColor(hex: "#E4DDD6") + tableView.tableFooterView = UIView() + // cell setup + tableView.register(UINib(nibName: "RightViewCell", bundle: nil), forCellReuseIdentifier: "RightViewCell") + tableView.register(UINib(nibName: "LeftViewCell", bundle: nil), forCellReuseIdentifier: "LeftViewCell") + + } + + func fetchData() { + comments = getAll() + tableView.reloadData() + } + + + func getAll() -> [Comment] { + let date: NSDate = NSDate() + var all = [Comment(content: "Esto es un mensaje"),Comment(content: "Esto es un mensaje 1"),] + return all + } + +} + +extension CommentsViewController: UITableViewDataSource { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return comments.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let comment = comments[indexPath.row] + + if comment.author != nil { + let cell = tableView.dequeueReusableCell(withIdentifier: "LeftViewCell") as! LeftViewCell + cell.configureCell(comment: comment) + return cell + } else { + let cell = tableView.dequeueReusableCell(withIdentifier: "RightViewCell") as! RightViewCell + cell.configureCell(comment: comment) + return cell + } + } +} + diff --git a/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift b/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift new file mode 100644 index 0000000..13a9518 --- /dev/null +++ b/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift @@ -0,0 +1,17 @@ +// +// FeedCollectionViewController+PostCollectionViewCellDelegate.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +extension FeedCollectionViewController: PostCollectionViewCellDelegate { + func didButtonPressed() { + let commentsViewController = self.storyboard?.instantiateViewController(withIdentifier: "CommentsViewController") as! CommentsViewController + self.navigationController?.pushViewController(commentsViewController, animated: true) + } + +} diff --git a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift index 15100dd..64a8ff2 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift +++ b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift @@ -19,7 +19,7 @@ extension FeedCollectionViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PostCollectionViewCell.reuseIdentifier, for: indexPath) as! PostCollectionViewCell - + cell.delegate = self cell.post = self.posts?[indexPath.row] return cell } diff --git a/Secretly/Views/LeftViewCell.swift b/Secretly/Views/LeftViewCell.swift new file mode 100644 index 0000000..25ce5af --- /dev/null +++ b/Secretly/Views/LeftViewCell.swift @@ -0,0 +1,29 @@ +// +// LeftViewCell.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +class LeftViewCell: UITableViewCell { + + @IBOutlet weak var commentContainerView: UIView! + @IBOutlet weak var commentLabel: UILabel! + + override func awakeFromNib() { + super.awakeFromNib() + commentContainerView.rounded(radius: 12) + commentContainerView.backgroundColor = .white + + contentView.backgroundColor = .clear + backgroundColor = .clear + } + + func configureCell(comment: Comment) { + commentLabel.text = comment.content + } + +} diff --git a/Secretly/Views/LeftViewCell.xib b/Secretly/Views/LeftViewCell.xib new file mode 100644 index 0000000..93d62d7 --- /dev/null +++ b/Secretly/Views/LeftViewCell.xib @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Secretly/Views/PostCollectionViewCell.swift b/Secretly/Views/PostCollectionViewCell.swift index ee08d53..2066cc4 100644 --- a/Secretly/Views/PostCollectionViewCell.swift +++ b/Secretly/Views/PostCollectionViewCell.swift @@ -10,6 +10,7 @@ import UIKit class PostCollectionViewCell: UICollectionViewCell { static let reuseIdentifier = "feedPostCell" + var delegate: PostCollectionViewCellDelegate? var post: Post? { didSet { updateView() @@ -38,4 +39,12 @@ class PostCollectionViewCell: UICollectionViewCell { } self.authorView.author = post.user } + + @IBAction func showComments(_ sender: UIButton) { + delegate?.didButtonPressed() + } +} + +protocol PostCollectionViewCellDelegate { + func didButtonPressed() } diff --git a/Secretly/Views/PostCollectionViewCell.xib b/Secretly/Views/PostCollectionViewCell.xib index 7b3a4e2..37f336a 100644 --- a/Secretly/Views/PostCollectionViewCell.xib +++ b/Secretly/Views/PostCollectionViewCell.xib @@ -11,10 +11,10 @@ - + - + @@ -25,13 +25,13 @@ - + @@ -39,7 +39,7 @@ - - - - - - - - @@ -62,28 +54,38 @@ + + + + - - - + @@ -91,7 +93,7 @@ - + diff --git a/Secretly/Views/RightViewCell.swift b/Secretly/Views/RightViewCell.swift new file mode 100644 index 0000000..9f4297b --- /dev/null +++ b/Secretly/Views/RightViewCell.swift @@ -0,0 +1,29 @@ +// +// RightViewCell.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +class RightViewCell: UITableViewCell { + + + @IBOutlet weak var commentContainerView: UIView! + @IBOutlet weak var commentLabel: UILabel! + + override func awakeFromNib() { + super.awakeFromNib() + commentContainerView.rounded(radius: 12) + commentContainerView.backgroundColor = UIColor(hex: "#E1F7CB") + + contentView.backgroundColor = .clear + backgroundColor = .clear + } + + func configureCell(comment: Comment) { + commentLabel.text = comment.content + } +} diff --git a/Secretly/Views/RightViewCell.xib b/Secretly/Views/RightViewCell.xib new file mode 100644 index 0000000..b865927 --- /dev/null +++ b/Secretly/Views/RightViewCell.xib @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 4f6b593db0abcedc9bd41a0617cf22919b67fcf6 Mon Sep 17 00:00:00 2001 From: Victor Aceves Date: Sun, 29 Aug 2021 01:04:21 -0500 Subject: [PATCH 2/3] Add comment from Post Comment screen --- Secretly.xcodeproj/project.pbxproj | 4 + Secretly/Base.lproj/Main.storyboard | 4 + Secretly/Models/Comment.swift | 2 +- Secretly/Services/CommentsService.swift | 74 ++++++++++++++++++ .../CommentsViewController.swift | 76 ++++++++++++++++--- ...oller+PostCollectionViewCellDelegate.swift | 12 ++- .../FeedCollectionViewController.swift | 8 ++ Secretly/Views/PostCollectionViewCell.swift | 4 +- 8 files changed, 167 insertions(+), 17 deletions(-) create mode 100644 Secretly/Services/CommentsService.swift diff --git a/Secretly.xcodeproj/project.pbxproj b/Secretly.xcodeproj/project.pbxproj index 1723a83..89d2da0 100644 --- a/Secretly.xcodeproj/project.pbxproj +++ b/Secretly.xcodeproj/project.pbxproj @@ -65,6 +65,7 @@ 6510160426DB1B460047C3EB /* LeftViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6510160326DB1B460047C3EB /* LeftViewCell.xib */; }; 6510160626DB20030047C3EB /* UIView+Rounded.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6510160526DB20030047C3EB /* UIView+Rounded.swift */; }; 6510160826DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6510160726DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift */; }; + 6510160A26DB399B0047C3EB /* CommentsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6510160926DB399B0047C3EB /* CommentsService.swift */; }; E021984723FA35E00025C28E /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021984623FA35E00025C28E /* AppDelegate.swift */; }; E021984923FA35E00025C28E /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021984823FA35E00025C28E /* SceneDelegate.swift */; }; E021984B23FA35E00025C28E /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021984A23FA35E00025C28E /* WelcomeViewController.swift */; }; @@ -143,6 +144,7 @@ 6510160326DB1B460047C3EB /* LeftViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LeftViewCell.xib; sourceTree = ""; }; 6510160526DB20030047C3EB /* UIView+Rounded.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "UIView+Rounded.swift"; path = "../../../../../UIView+Rounded.swift"; sourceTree = ""; }; 6510160726DB2EF60047C3EB /* FeedCollectionViewController+PostCollectionViewCellDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FeedCollectionViewController+PostCollectionViewCellDelegate.swift"; sourceTree = ""; }; + 6510160926DB399B0047C3EB /* CommentsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentsService.swift; sourceTree = ""; }; E021984323FA35E00025C28E /* Secretly.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Secretly.app; sourceTree = BUILT_PRODUCTS_DIR; }; E021984623FA35E00025C28E /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; E021984823FA35E00025C28E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -246,6 +248,7 @@ 30C77CAF266AD69700A888DC /* CurrentUserService.swift */, 304E06C726742BDA00A99128 /* CreatePostService.swift */, 304E06C926742CC500A99128 /* FeedService.swift */, + 6510160926DB399B0047C3EB /* CommentsService.swift */, ); path = Services; sourceTree = ""; @@ -486,6 +489,7 @@ 30337957267536E30066D94A /* FeedCollectionViewController+UICollectionViewDelegate.swift in Sources */, 307A305E2661CD510020DF8B /* PostCollectionViewCell.swift in Sources */, 302BB626267E447900FD74F5 /* PostInputViewController+UITextFieldDelegate.swift in Sources */, + 6510160A26DB399B0047C3EB /* CommentsService.swift in Sources */, 30BC8BA02662B8A700F7E6A5 /* StorageType.swift in Sources */, 307A305B2661B7A20020DF8B /* FeedCollectionViewController.swift in Sources */, 304E06CC2674442800A99128 /* UIImage+encodeBase64.swift in Sources */, diff --git a/Secretly/Base.lproj/Main.storyboard b/Secretly/Base.lproj/Main.storyboard index 9682926..999643c 100644 --- a/Secretly/Base.lproj/Main.storyboard +++ b/Secretly/Base.lproj/Main.storyboard @@ -190,6 +190,9 @@ + + + @@ -221,6 +224,7 @@ + diff --git a/Secretly/Models/Comment.swift b/Secretly/Models/Comment.swift index 3cb1365..d2053c2 100644 --- a/Secretly/Models/Comment.swift +++ b/Secretly/Models/Comment.swift @@ -13,7 +13,7 @@ struct Comment: Restable { let content: String let createdAt: Date? let updatedAt: Date? - let author: Author? + var author: Author? init(content: String) { self.id = nil diff --git a/Secretly/Services/CommentsService.swift b/Secretly/Services/CommentsService.swift new file mode 100644 index 0000000..b7c64a5 --- /dev/null +++ b/Secretly/Services/CommentsService.swift @@ -0,0 +1,74 @@ +// +// CommentsService.swift +// Secretly +// +// Created by Victor Aceves on 28/08/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct CommentService { + private var endpoint: RestClient + private var post: Post? + + init(post: Post) { + endpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(post.id ?? 1)/comments") + self.post = post + } + + func create(_ model: Comment, complete: @escaping (Result) -> Void ) { + try? endpoint.create(model: model) { result in + DispatchQueue.main.async { complete(result) } + } + } + + func delete(_ model: Comment, complete: @escaping (Result) -> Void ) { + let deleteEndpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(post?.id ?? 1)/comments/\(model.id ?? -1)") + print(deleteEndpoint.path) + deleteEndpoint.delete(model: model, complete: { result in + DispatchQueue.main.async { + complete(result) + } + }) + } + + func update(_ model: Comment, complete: @escaping (Result) -> Void ) { + let updateEndpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(post?.id ?? 1)/comments/\(model.id ?? -1)") + print(updateEndpoint.path) + + try? updateEndpoint.update(model: model){ result in + DispatchQueue.main.async { + complete(result) + } + } + } + + + func load(completion: @escaping ([Comment]) -> Void) { + endpoint.list { result in + guard let posts = try? result.get() else { return } + DispatchQueue.main.async { completion(posts) } + } + } + + static func count(post:Post,completion: @escaping ([Comment]) -> Void){ + let endPoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(post.id ?? 1)/comments") + endPoint.list { result in + guard let posts = try? result.get() else { return } + DispatchQueue.main.async { + completion(posts) + } + } + } + + static func countByID(id:Int,completion: @escaping ([Comment]) -> Void){ + let endPoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(id)/comments") + endPoint.list { result in + guard let posts = try? result.get() else { return } + DispatchQueue.main.async { + completion(posts) + } + } + } +} diff --git a/Secretly/ViewControllers/CommentsViewController.swift b/Secretly/ViewControllers/CommentsViewController.swift index da17894..4d9b274 100644 --- a/Secretly/ViewControllers/CommentsViewController.swift +++ b/Secretly/ViewControllers/CommentsViewController.swift @@ -9,12 +9,21 @@ import UIKit class CommentsViewController: UIViewController { - + var commentService:CommentService? = nil + var post : Post? + var callBack: ((_ reloadPosts: Bool)-> Void)? + var comments:[Comment] = [] { + didSet { + self.tableView.reloadData() + } + } + @IBOutlet weak var tableView: UITableView! - var comments = [Comment]() + @IBOutlet weak var commentTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() + commentService = CommentService(post: post!) // set title title = "Post Comments" setupTable() @@ -34,17 +43,64 @@ class CommentsViewController: UIViewController { } func fetchData() { - comments = getAll() - tableView.reloadData() + commentService?.load { [unowned self] comments in + self.comments = comments + } } + @IBAction func sendComment(_ sender: UIButton) { + do{ + try createComment() + } catch let err { + self.errorAlert(err) + } + } - func getAll() -> [Comment] { - let date: NSDate = NSDate() - var all = [Comment(content: "Esto es un mensaje"),Comment(content: "Esto es un mensaje 1"),] - return all + private func createComment() throws { + let text:String? = commentTextField.text + guard let safeText = text else { return } + let comment = try self.buildComment(comment: safeText) + guard let safeComment = comment else { return } + + commentService?.create(safeComment) { [unowned self] result in + switch result { + case .success(let newComment): + commentTextField.text = "" + self.showAlert(title: "Comentario publicado", message: newComment?.content ?? "") + self.fetchData() + callBack?(true) + case .failure(let err): + self.errorAlert(err) + } + } } + private func buildComment(comment: String) throws -> Comment? { + if (comment.isBlank || comment.count<4 ) { + let alert = UIAlertController(title: "Comentario invalido", message: "El comentario no puede estar vacío y debe tener al menos 4 caracteres", preferredStyle: .alert) + let okAction = UIAlertAction(title: "OK", style: .default) + alert.addAction(okAction) + self.present(alert, animated: true, completion: nil) + return nil + } else { + return Comment(content: comment) + } + } + + func showAlert(title: String, message: String){ + let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert) + let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) + alertVC.addAction(okAction) + present(alertVC, animated: true, completion: nil) + } + + func errorAlert(_ error: Error) { + let err = error as? Titleable + let alert = UIAlertController(title: (err?.title ?? "Ocurrio un error en el servidor"), message: error.localizedDescription, preferredStyle: .alert) + let okAction = UIAlertAction(title: "OK", style: .default) + alert.addAction(okAction) + self.present(alert, animated: true, completion: nil) + } } extension CommentsViewController: UITableViewDataSource { @@ -54,8 +110,8 @@ extension CommentsViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let comment = comments[indexPath.row] - - if comment.author != nil { + let isCurrentUser = CurrentUser.load()?.username == comment.author?.name + if !isCurrentUser { let cell = tableView.dequeueReusableCell(withIdentifier: "LeftViewCell") as! LeftViewCell cell.configureCell(comment: comment) return cell diff --git a/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift b/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift index 13a9518..af9d1d9 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift +++ b/Secretly/ViewControllers/FeedCollectionViewController+PostCollectionViewCellDelegate.swift @@ -9,9 +9,13 @@ import Foundation extension FeedCollectionViewController: PostCollectionViewCellDelegate { - func didButtonPressed() { + func didButtonPressed(post: Post) { let commentsViewController = self.storyboard?.instantiateViewController(withIdentifier: "CommentsViewController") as! CommentsViewController - self.navigationController?.pushViewController(commentsViewController, animated: true) - } - + commentsViewController.post = post + //callBack block will execute when user back from SecondViewController. + commentsViewController.callBack = { (reload: Bool) in + self.reload = reload + } + // self.navigationController?.pushViewController(commentsViewController, animated: true) + } } diff --git a/Secretly/ViewControllers/FeedCollectionViewController.swift b/Secretly/ViewControllers/FeedCollectionViewController.swift index 9180d42..329a051 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController.swift +++ b/Secretly/ViewControllers/FeedCollectionViewController.swift @@ -10,6 +10,7 @@ import UIKit import CoreLocation class FeedCollectionViewController: UIViewController { + var reload: Bool = false let feedService = FeedService() var posts: [Post]? { didSet { @@ -51,6 +52,13 @@ class FeedCollectionViewController: UIViewController { postInputView.clear() present(postInputView, animated: true) } + + override func viewWillAppear(_ animated: Bool) { + if reload { + loadPosts() + reload = false + } + } } extension FeedCollectionViewController: PostInputViewDelegate { diff --git a/Secretly/Views/PostCollectionViewCell.swift b/Secretly/Views/PostCollectionViewCell.swift index 2066cc4..68c040b 100644 --- a/Secretly/Views/PostCollectionViewCell.swift +++ b/Secretly/Views/PostCollectionViewCell.swift @@ -41,10 +41,10 @@ class PostCollectionViewCell: UICollectionViewCell { } @IBAction func showComments(_ sender: UIButton) { - delegate?.didButtonPressed() + delegate?.didButtonPressed(post: post!) } } protocol PostCollectionViewCellDelegate { - func didButtonPressed() + func didButtonPressed(post: Post) } From f520617deb1e8501d8ad1b404252ac55178de1cf Mon Sep 17 00:00:00 2001 From: Victor Aceves Date: Wed, 22 Sep 2021 02:00:25 -0500 Subject: [PATCH 3/3] Add missing extension --- Secretly/Extensions/UIView+Rounded.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Secretly/Extensions/UIView+Rounded.swift diff --git a/Secretly/Extensions/UIView+Rounded.swift b/Secretly/Extensions/UIView+Rounded.swift new file mode 100644 index 0000000..be69464 --- /dev/null +++ b/Secretly/Extensions/UIView+Rounded.swift @@ -0,0 +1,17 @@ +// +// UIView+Rounded.swift +// Secretly +// +// Created by Victor Aceves on 22/09/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +extension UIView{ + func rounded(radius: CGFloat) { + self.layer.cornerRadius = radius + self.clipsToBounds = true + } +} +