diff --git a/Secretly.xcodeproj/project.pbxproj b/Secretly.xcodeproj/project.pbxproj index 8dd4829..31d5923 100644 --- a/Secretly.xcodeproj/project.pbxproj +++ b/Secretly.xcodeproj/project.pbxproj @@ -56,6 +56,14 @@ 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 */; }; + 3B7AC01C26867F920055B340 /* ComentService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC01B26867F920055B340 /* ComentService.swift */; }; + 3B7AC020268952B40055B340 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC01F268952B40055B340 /* Comment.swift */; }; + 3B7AC037268C35D90055B340 /* NewCommentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC036268C35D90055B340 /* NewCommentViewController.swift */; }; + 3B7AC03D268D89FD0055B340 /* FeedCommentService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC03C268D89FD0055B340 /* FeedCommentService.swift */; }; + 3B7AC048268F6D0B0055B340 /* CommentCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC046268F6D0B0055B340 /* CommentCollectionViewCell.swift */; }; + 3B7AC049268F6D0B0055B340 /* CommentCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3B7AC047268F6D0B0055B340 /* CommentCollectionViewCell.xib */; }; + 3B7AC059269A8B090055B340 /* Author.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC058269A8B080055B340 /* Author.swift */; }; + 3B7AC05D269A94B50055B340 /* AuthorCommentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B7AC05C269A94B50055B340 /* AuthorCommentView.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 +133,14 @@ 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 = ""; }; + 3B7AC01B26867F920055B340 /* ComentService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComentService.swift; sourceTree = ""; }; + 3B7AC01F268952B40055B340 /* Comment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Comment.swift; sourceTree = ""; }; + 3B7AC036268C35D90055B340 /* NewCommentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewCommentViewController.swift; sourceTree = ""; }; + 3B7AC03C268D89FD0055B340 /* FeedCommentService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedCommentService.swift; sourceTree = ""; }; + 3B7AC046268F6D0B0055B340 /* CommentCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentCollectionViewCell.swift; sourceTree = ""; }; + 3B7AC047268F6D0B0055B340 /* CommentCollectionViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CommentCollectionViewCell.xib; sourceTree = ""; }; + 3B7AC058269A8B080055B340 /* Author.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Author.swift; sourceTree = ""; }; + 3B7AC05C269A94B50055B340 /* AuthorCommentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthorCommentView.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 = ""; }; @@ -174,11 +190,13 @@ 3033795A267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.swift */, 3033795C267537B40066D94A /* FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift */, 302BB61D267E316900FD74F5 /* PostInputViewController.swift */, - 302BB61E267E316900FD74F5 /* PostInputViewController.xib */, + 307A305C2661CD510020DF8B /* PostCollectionViewCell.swift */, 30337954267536980066D94A /* PostInputViewController+CLLocationManagerDelegate.swift */, 302BB621267E38E800FD74F5 /* PostInputViewController+UIImagePickerControllerDelegate.swift */, 302BB623267E3A8700FD74F5 /* PostInputViewController+UIColorPickerViewControllerDelegate.swift */, 302BB625267E447900FD74F5 /* PostInputViewController+UITextFieldDelegate.swift */, + 3B7AC036268C35D90055B340 /* NewCommentViewController.swift */, + 3B7AC046268F6D0B0055B340 /* CommentCollectionViewCell.swift */, ); path = ViewControllers; sourceTree = ""; @@ -197,9 +215,11 @@ 307A306326629B590020DF8B /* Views */ = { isa = PBXGroup; children = ( - 307A305C2661CD510020DF8B /* PostCollectionViewCell.swift */, + 302BB61E267E316900FD74F5 /* PostInputViewController.xib */, + 3B7AC047268F6D0B0055B340 /* CommentCollectionViewCell.xib */, 307A305D2661CD510020DF8B /* PostCollectionViewCell.xib */, 307A306426629B990020DF8B /* AuthorView.swift */, + 3B7AC05C269A94B50055B340 /* AuthorCommentView.swift */, 302BB61B267D7CC800FD74F5 /* PreviewPostVIew.swift */, ); path = Views; @@ -222,6 +242,8 @@ 30C77CAF266AD69700A888DC /* CurrentUserService.swift */, 304E06C726742BDA00A99128 /* CreatePostService.swift */, 304E06C926742CC500A99128 /* FeedService.swift */, + 3B7AC01B26867F920055B340 /* ComentService.swift */, + 3B7AC03C268D89FD0055B340 /* FeedCommentService.swift */, ); path = Services; sourceTree = ""; @@ -248,6 +270,8 @@ 307A30572661AD540020DF8B /* User.swift */, 30C77CB3266AF47300A888DC /* Credentials.swift */, 30C77CB5266AF48300A888DC /* CurrentUser.swift */, + 3B7AC01F268952B40055B340 /* Comment.swift */, + 3B7AC058269A8B080055B340 /* Author.swift */, ); path = Models; sourceTree = ""; @@ -401,6 +425,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 3B7AC049268F6D0B0055B340 /* CommentCollectionViewCell.xib in Resources */, 307A305F2661CD510020DF8B /* PostCollectionViewCell.xib in Resources */, 302BB610267C658600FD74F5 /* Amaca.plist in Resources */, E021985323FA35E20025C28E /* LaunchScreen.storyboard in Resources */, @@ -424,6 +449,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 3B7AC048268F6D0B0055B340 /* CommentCollectionViewCell.swift in Sources */, 30C77CB8266BD44300A888DC /* CreatePostViewController.swift in Sources */, E021984B23FA35E00025C28E /* WelcomeViewController.swift in Sources */, 3072FBDF2680FA5A00B35C8C /* ImageProcessor.swift in Sources */, @@ -439,20 +465,24 @@ 304E06C626741A5100A99128 /* UIColor+theme.swift in Sources */, 304E06C826742BDA00A99128 /* CreatePostService.swift in Sources */, E021984723FA35E00025C28E /* AppDelegate.swift in Sources */, + 3B7AC05D269A94B50055B340 /* AuthorCommentView.swift in Sources */, 30337955267536980066D94A /* PostInputViewController+CLLocationManagerDelegate.swift in Sources */, 307A30562661AD480020DF8B /* Image.swift in Sources */, 307A30582661AD540020DF8B /* User.swift in Sources */, 302B5849267E658E007133E6 /* RequestError.swift in Sources */, 302B5846267E658E007133E6 /* AmacaConfig.swift in Sources */, + 3B7AC03D268D89FD0055B340 /* FeedCommentService.swift in Sources */, 302BB61C267D7CC800FD74F5 /* PreviewPostVIew.swift in Sources */, 3033795D267537B40066D94A /* FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift in Sources */, 30BC8BA82662CEBA00F7E6A5 /* Checksum.swift in Sources */, + 3B7AC01C26867F920055B340 /* ComentService.swift in Sources */, 30FD0E722659645A006E309A /* Faker.swift in Sources */, 30B9B939268CA989007B1942 /* Nudity.mlmodel in Sources */, 30337957267536E30066D94A /* FeedCollectionViewController+UICollectionViewDelegate.swift in Sources */, 307A305E2661CD510020DF8B /* PostCollectionViewCell.swift in Sources */, 302BB626267E447900FD74F5 /* PostInputViewController+UITextFieldDelegate.swift in Sources */, 30BC8BA02662B8A700F7E6A5 /* StorageType.swift in Sources */, + 3B7AC037268C35D90055B340 /* NewCommentViewController.swift in Sources */, 307A305B2661B7A20020DF8B /* FeedCollectionViewController.swift in Sources */, 304E06CC2674442800A99128 /* UIImage+encodeBase64.swift in Sources */, 30BC8BA62662C02300F7E6A5 /* CacheImage.swift in Sources */, @@ -461,6 +491,7 @@ 307A306526629B990020DF8B /* AuthorView.swift in Sources */, 30C77CB4266AF47300A888DC /* Credentials.swift in Sources */, 30BC8BA22662BB0000F7E6A5 /* DataContainer.swift in Sources */, + 3B7AC059269A8B090055B340 /* Author.swift in Sources */, E021984923FA35E00025C28E /* SceneDelegate.swift in Sources */, 30C77CB0266AD69700A888DC /* CurrentUserService.swift in Sources */, 30C77CB6266AF48300A888DC /* CurrentUser.swift in Sources */, @@ -468,6 +499,7 @@ 304E06CF267468DA00A99128 /* UIColor+Pastel.swift in Sources */, 304E06C42674133D00A99128 /* String+isBlank.swift in Sources */, 30BC8BA42662BDEF00F7E6A5 /* ImageStore.swift in Sources */, + 3B7AC020268952B40055B340 /* Comment.swift in Sources */, 3033795B267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.swift in Sources */, 30B9B93B268CA9E6007B1942 /* UIImage+PixelBuffer.swift in Sources */, 303379592675371C0066D94A /* FeedCollectionViewController+UICollectionViewDataSource .swift in Sources */, diff --git a/Secretly/Base.lproj/Main.storyboard b/Secretly/Base.lproj/Main.storyboard index 4ed5b13..b552c5f 100644 --- a/Secretly/Base.lproj/Main.storyboard +++ b/Secretly/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -72,7 +72,7 @@ - + @@ -81,19 +81,20 @@ - + - + - + + @@ -127,6 +128,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -170,6 +261,7 @@ + @@ -182,12 +274,18 @@ + + + + + + diff --git a/Secretly/Models/Author.swift b/Secretly/Models/Author.swift new file mode 100644 index 0000000..bcae534 --- /dev/null +++ b/Secretly/Models/Author.swift @@ -0,0 +1,13 @@ +// +// Author.swift +// Secretly +// +// Created by Fernanda Hernandez on 10/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation +struct Author: Codable { + let name: String + let avatarUrl: String +} diff --git a/Secretly/Models/Comment.swift b/Secretly/Models/Comment.swift new file mode 100644 index 0000000..d0f29d2 --- /dev/null +++ b/Secretly/Models/Comment.swift @@ -0,0 +1,32 @@ +// +// Comment.swift +// Secretly +// +// Created by Fernanda Hernandez on 27/06/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/Network/HttpClient.swift b/Secretly/Network/HttpClient.swift index 0554709..824bd5f 100644 --- a/Secretly/Network/HttpClient.swift +++ b/Secretly/Network/HttpClient.swift @@ -54,7 +54,9 @@ struct HttpClient { builder.body = body if let token = AmacaConfig.shared.apiToken { builder.headers = ["Authorization": "Bearer \(token)"] + } + return builder.request() } } diff --git a/Secretly/Services/ComentService.swift b/Secretly/Services/ComentService.swift new file mode 100644 index 0000000..4227d74 --- /dev/null +++ b/Secretly/Services/ComentService.swift @@ -0,0 +1,27 @@ +// +// ComentService.swift +// Secretly +// +// Created by Fernanda Hernandez on 25/06/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct CreatCommentsService { + private var commentEndpoint: RestClient + + init(id: Int) { + + if(id == 0){ + print("a ver que pasa") + } + commentEndpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(id)/comments") + } + func create(_ model: Comment, complete: @escaping (Result) -> Void ) { + try? commentEndpoint.create(model: model) { result in + DispatchQueue.main.async { complete(result) } + } + } + +} diff --git a/Secretly/Services/CurrentUserService.swift b/Secretly/Services/CurrentUserService.swift index 6579aa9..27fe5a2 100644 --- a/Secretly/Services/CurrentUserService.swift +++ b/Secretly/Services/CurrentUserService.swift @@ -27,6 +27,7 @@ struct CurrentUserService { fakeEndpoint.show { result in guard let fake = try? result.get() else { return } let currentUser = CurrentUser(username: fake.username) + signUp(currentUser) { completion($0) } } } @@ -48,5 +49,6 @@ struct CurrentUserService { fileprivate func storeToken(_ result: Result) { guard let res = try? result.get(), let token = res.token else { return } AmacaConfig.shared.setApiToken(token) + print("tokeeen: \(token)") } } diff --git a/Secretly/Services/FeedCommentService.swift b/Secretly/Services/FeedCommentService.swift new file mode 100644 index 0000000..5a247d9 --- /dev/null +++ b/Secretly/Services/FeedCommentService.swift @@ -0,0 +1,29 @@ +// +// FeedCommentService.swift +// Secretly +// +// Created by Fernanda Hernandez on 01/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation +struct FeedCommentService { + private var feedCommenTEndpoint: RestClient? + + + + init(post: Post?){ + guard let post = post, let postId = post.id else {return} + self.feedCommenTEndpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(postId)/comments") + + } + + func load(completion: @escaping ([Comment]) -> Void) { + feedCommenTEndpoint?.list { result in + guard let comments = try? result.get() else { return } + DispatchQueue.main.async { completion(comments) } + } + } + +} + diff --git a/Secretly/ViewControllers/CommentCollectionViewCell.swift b/Secretly/ViewControllers/CommentCollectionViewCell.swift new file mode 100644 index 0000000..8135b2e --- /dev/null +++ b/Secretly/ViewControllers/CommentCollectionViewCell.swift @@ -0,0 +1,35 @@ +// +// CommentCollectionViewCell.swift +// Secretly +// +// Created by Fernanda Hernandez on 02/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +class CommentCollectionViewCell: UICollectionViewCell { + + static let reuseIdentifier = "commentXIB" + + @IBOutlet weak var contentLabel: UILabel! + @IBOutlet weak var authorView: AuthorCommentView! + + var comment: Comment? { + didSet { + updateView() + } + } + + override func awakeFromNib() { + super.awakeFromNib() + updateView() + } + + func updateView() { + guard let comment = comment else {return } + self.contentLabel.text = comment.content + self.authorView.author = comment.author + } + +} diff --git a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift index 15100dd..40bb51d 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift +++ b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDataSource .swift @@ -19,8 +19,9 @@ extension FeedCollectionViewController: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PostCollectionViewCell.reuseIdentifier, for: indexPath) as! PostCollectionViewCell - + cell.viewControlle = self cell.post = self.posts?[indexPath.row] + return cell } } diff --git a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegate.swift b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegate.swift index 2b373e7..230b596 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegate.swift +++ b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegate.swift @@ -23,18 +23,18 @@ extension FeedCollectionViewController: UICollectionViewDelegate { } */ - /* + // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item - override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { + func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool { return false } - override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { - return false + func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool { + return true } - override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { - + func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) { + print("holaaaaaaaaaaaa") } - */ + } diff --git a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift index e6c7ccc..5944097 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift +++ b/Secretly/ViewControllers/FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift @@ -12,4 +12,6 @@ extension FeedCollectionViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.bounds.width, height: 300) } + + } diff --git a/Secretly/ViewControllers/FeedCollectionViewController.swift b/Secretly/ViewControllers/FeedCollectionViewController.swift index 9180d42..38b1c6e 100644 --- a/Secretly/ViewControllers/FeedCollectionViewController.swift +++ b/Secretly/ViewControllers/FeedCollectionViewController.swift @@ -9,8 +9,13 @@ import UIKit import CoreLocation -class FeedCollectionViewController: UIViewController { +class FeedCollectionViewController: UIViewController{ + let feedService = FeedService() + let refreshControl = UIRefreshControl() + let postInputView = PostInputViewController() + + var posts: [Post]? { didSet { self.collectionView.reloadData() @@ -18,9 +23,7 @@ class FeedCollectionViewController: UIViewController { } } - let refreshControl = UIRefreshControl() - let postInputView = PostInputViewController() - + @IBOutlet weak var collectionView: UICollectionView! override func viewDidLoad() { @@ -39,11 +42,13 @@ class FeedCollectionViewController: UIViewController { collectionView.addSubview(refreshControl) refreshControl.addTarget(self, action: #selector(self.loadPosts), for: UIControl.Event.valueChanged) + } @objc func loadPosts() { feedService.load { [unowned self] posts in self.posts = posts } + print("dani mi amor:\(posts?.count)") } @IBAction @@ -54,6 +59,7 @@ class FeedCollectionViewController: UIViewController { } extension FeedCollectionViewController: PostInputViewDelegate { + func didCreatePost(post: Post?) { guard let upost = post else { return } self.posts?.insert(upost, at: 0) diff --git a/Secretly/ViewControllers/NewCommentViewController.swift b/Secretly/ViewControllers/NewCommentViewController.swift new file mode 100644 index 0000000..adaed35 --- /dev/null +++ b/Secretly/ViewControllers/NewCommentViewController.swift @@ -0,0 +1,154 @@ +// +// NewCommentViewController.swift +// Secretly +// +// Created by Fernanda Hernandez on 30/06/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +class NewCommentViewController: UIViewController { + + let refreshControl = UIRefreshControl() + + var postSelected: Post? { + didSet { + loadComments() + } + } + var comments: [Comment]?{ + didSet { + self.commentCollection?.reloadData() + self.refreshControl.endRefreshing() + } + } + + @IBOutlet weak var commentContent: UITextField! + @IBOutlet weak var commentCollection: UICollectionView! + + + @IBAction func close(_ sender: Any) { + dismiss(animated: true, completion: nil) + + comments = [] + } + @IBAction func onTapCreateComment(_ sender: Any) { + do { + if(commentContent.text == ""){ + showAlert() + } + else{ + try self.createComment() + self.commentCollection.reloadData() + commentContent.text = "" + + if(comments!.count <= 4){ + dismiss(animated: true, completion: nil) + + } + else{ + showAlertOk() + } + } + + } catch let err { + self.errorAlert(err) + } + } + + override func viewDidLoad() { + super.viewDidLoad() + loadSetUp() + } + + func loadSetUp(){ + self.commentCollection.reloadData() + let nib = UINib(nibName: String(describing: CommentCollectionViewCell.self), bundle: nil) + commentCollection.register(nib, forCellWithReuseIdentifier: CommentCollectionViewCell.reuseIdentifier) + commentCollection.delegate = self + commentCollection.dataSource = self + commentCollection.prefetchDataSource = self + commentCollection.addSubview(refreshControl) + refreshControl.addTarget(self, action: #selector(self.loadComments), for: UIControl.Event.valueChanged) + + } + private func buildComment() throws -> Comment { + guard let postText = commentContent.text else { + throw EmptyPostError() + } + var post = Comment( + content: postText + ) + + return post + } + private func errorAlert(_ error: Error) { + let err = error as? Titleable + let alert = UIAlertController(title: (err?.title ?? "Server Error"), message: error.localizedDescription, preferredStyle: .alert) + let okAction = UIAlertAction(title: "Ok", style: .default) + alert.addAction(okAction) + self.present(alert, animated: true, completion: nil) + } + + private func createComment() throws { + let id = 0 + let commentService = CreatCommentsService(id: (postSelected?.id!)!) + let comment = try self.buildComment() + commentService.create(comment) { [unowned self] result in + switch result { + case .success(let comment): + print("se subió chido") + case .failure(let err): + self.errorAlert(err) + showAlert() + } + } + } + + func showAlert(){ + let alert = UIAlertController(title: "", message: "Comentario vacío", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Aceptar", style: .cancel, handler: {_ in + print("") + })) + present(alert, animated: true) + } + func showAlertOk(){ + let alert = UIAlertController(title: "", message: "Comentario subido exitosamente", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "Aceptar", style: .cancel, handler: {_ in + print("puchurraste cancelar") + })) + present(alert, animated: true) + } + + @objc + func loadComments() { + var feedCommentService = FeedCommentService(post: postSelected!) + feedCommentService.load { [unowned self] comments in self.comments = comments } + } +} + +extension NewCommentViewController: UICollectionViewDataSource{ + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return comments?.count ?? 0 + } + + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CommentCollectionViewCell.reuseIdentifier, for: indexPath) as! CommentCollectionViewCell + cell.comment = self.comments?[indexPath.row] + return cell + } + +} + +extension NewCommentViewController: UICollectionViewDataSourcePrefetching{ + func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) { + guard let indexPath = indexPaths.last else { return } + print("================\(indexPath.row)=================") + } +} +extension NewCommentViewController: UICollectionViewDelegateFlowLayout{ + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + return CGSize(width: collectionView.bounds.width, height: 150) + } +} diff --git a/Secretly/Views/PostCollectionViewCell.swift b/Secretly/ViewControllers/PostCollectionViewCell.swift similarity index 57% rename from Secretly/Views/PostCollectionViewCell.swift rename to Secretly/ViewControllers/PostCollectionViewCell.swift index ee08d53..0ec9217 100644 --- a/Secretly/Views/PostCollectionViewCell.swift +++ b/Secretly/ViewControllers/PostCollectionViewCell.swift @@ -9,10 +9,16 @@ import UIKit class PostCollectionViewCell: UICollectionViewCell { + static let reuseIdentifier = "feedPostCell" + let storyboard = UIStoryboard(name: "Main", bundle: nil) + let coomentview = NewCommentViewController() + + weak var viewControlle: UIViewController? + lazy var loggedInView: NewCommentViewController = storyboard.instantiateViewController(withIdentifier: "NewCommentViewController") as! NewCommentViewController var post: Post? { didSet { - updateView() + updateView() } } @IBOutlet weak var authorView: AuthorView! @@ -20,14 +26,24 @@ class PostCollectionViewCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView! @IBOutlet weak var likeState: UIImageView! @IBOutlet weak var commentCounter: UILabel! + + @IBAction func commentsView(_ sender: UIStoryboardSegue) { + + loggedInView.commentCollection = nil + loggedInView.comments = [] + loggedInView.postSelected = post + viewControlle?.modalPresentationStyle = .fullScreen + viewControlle?.present(loggedInView, animated: true, completion: nil) + + } override func awakeFromNib() { super.awakeFromNib() } - func updateView() { + func updateView() { imageView.image = nil - guard let post = post else { return } + guard let post = post else {return } if let color = UIColor(hex: post.backgroundColor) { self.backgroundColor = color } @@ -39,3 +55,10 @@ class PostCollectionViewCell: UICollectionViewCell { self.authorView.author = post.user } } + + + + + + + diff --git a/Secretly/Views/AuthorCommentView.swift b/Secretly/Views/AuthorCommentView.swift new file mode 100644 index 0000000..14f803e --- /dev/null +++ b/Secretly/Views/AuthorCommentView.swift @@ -0,0 +1,85 @@ +// +// AuthorCommentView.swift +// Secretly +// +// Created by Fernanda Hernandez on 10/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import UIKit + +class AuthorCommentView: UIView { + var author: Author? { + didSet { + updateContent() + } + } + + let stack: UIStackView = { + let sv = UIStackView() + sv.alignment = .fill + sv.distribution = .fill + sv.axis = .horizontal + sv.spacing = CGFloat(10.0) + sv.translatesAutoresizingMaskIntoConstraints = false + return sv + }() + + let avatarImg: UIImageView = { + let iv = UIImageView() + iv.image = UIImage(systemName: "person.fill") + iv.contentMode = .scaleAspectFill + iv.clipsToBounds = true + iv.translatesAutoresizingMaskIntoConstraints = false + return iv + }() + + let usernameLbl: UILabel = { + let lbl = UILabel() + lbl.text = "Jane Doe" + lbl.font = UIFont.systemFont(ofSize: 18, weight: .semibold) + lbl.textColor = .black + lbl.translatesAutoresizingMaskIntoConstraints = false + return lbl + }() + + convenience init() { + self.init(frame: .zero) + } + + override init(frame: CGRect) { + super.init(frame: frame) + setupConstraints() + } + + required init?(coder: NSCoder) { + super.init(coder: coder) + setupConstraints() + } + + func setupConstraints() { + self.backgroundColor = .clear + addSubview(stack) + stack.addArrangedSubview(avatarImg) + stack.addArrangedSubview(usernameLbl) + + NSLayoutConstraint.activate([ + stack.topAnchor.constraint(equalTo: self.topAnchor, constant: 3), + stack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 3), + stack.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 3), + stack.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 3) + ]) + + NSLayoutConstraint.activate([ + avatarImg.widthAnchor.constraint(equalTo: avatarImg.heightAnchor) + ]) + avatarImg.layer.cornerRadius = self.frame.height / 2.0 + } + + func updateContent() { + guard let author = author else { return } + usernameLbl.text = author.name + ImageLoader.load(author.avatarUrl) { [unowned self] img in self.avatarImg.image = img } + } + +} diff --git a/Secretly/Views/AuthorView.swift b/Secretly/Views/AuthorView.swift index 9d32ae3..b9095e3 100644 --- a/Secretly/Views/AuthorView.swift +++ b/Secretly/Views/AuthorView.swift @@ -57,14 +57,6 @@ class AuthorView: UIView { setupConstraints() } - /* - // Only override draw() if you perform custom drawing. - // An empty implementation adversely affects performance during animation. - override func draw(_ rect: CGRect) { - // Drawing code - } - */ - func setupConstraints() { self.backgroundColor = .clear addSubview(stack) diff --git a/Secretly/Views/CommentCollectionViewCell.xib b/Secretly/Views/CommentCollectionViewCell.xib new file mode 100644 index 0000000..107488a --- /dev/null +++ b/Secretly/Views/CommentCollectionViewCell.xib @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Secretly/Views/PostCollectionViewCell.xib b/Secretly/Views/PostCollectionViewCell.xib index 7b3a4e2..0706702 100644 --- a/Secretly/Views/PostCollectionViewCell.xib +++ b/Secretly/Views/PostCollectionViewCell.xib @@ -1,8 +1,8 @@ - + - + @@ -11,27 +11,27 @@ - + - + - + - + - + @@ -39,7 +39,7 @@ - + @@ -56,12 +56,19 @@ - + + @@ -83,7 +90,7 @@ - + @@ -91,7 +98,7 @@ - + diff --git a/Secretly/ViewControllers/PostInputViewController.xib b/Secretly/Views/PostInputViewController.xib similarity index 99% rename from Secretly/ViewControllers/PostInputViewController.xib rename to Secretly/Views/PostInputViewController.xib index 7fd920c..239ea4f 100644 --- a/Secretly/ViewControllers/PostInputViewController.xib +++ b/Secretly/Views/PostInputViewController.xib @@ -1,8 +1,8 @@ - + - +