From f1e6622316de6c17854b1e9ad5fbf41c9a0e292f Mon Sep 17 00:00:00 2001 From: Yocelin Garcia Date: Sat, 26 Jun 2021 13:38:46 -0500 Subject: [PATCH 01/16] Add data model for likes nad the likes service files --- Secretly.xcodeproj/project.pbxproj | 12 +++++++ Secretly/Base.lproj/Main.storyboard | 4 +-- Secretly/Models/Author.swift | 20 +++++++++++ Secretly/Models/Like.swift | 26 ++++++++++++++ Secretly/Models/Post.swift | 6 +++- Secretly/Services/LikeService.swift | 39 +++++++++++++++++++++ Secretly/Views/PostCollectionViewCell.swift | 38 +++++++++++++++++++- Secretly/Views/PostCollectionViewCell.xib | 13 +++++-- 8 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 Secretly/Models/Author.swift create mode 100644 Secretly/Models/Like.swift create mode 100644 Secretly/Services/LikeService.swift diff --git a/Secretly.xcodeproj/project.pbxproj b/Secretly.xcodeproj/project.pbxproj index 9084066..4dce578 100644 --- a/Secretly.xcodeproj/project.pbxproj +++ b/Secretly.xcodeproj/project.pbxproj @@ -60,6 +60,9 @@ E021985023FA35E20025C28E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E021984F23FA35E20025C28E /* Assets.xcassets */; }; E021985323FA35E20025C28E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E021985123FA35E20025C28E /* LaunchScreen.storyboard */; }; E021985E23FA35E20025C28E /* SecretlyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E021985D23FA35E20025C28E /* SecretlyTests.swift */; }; + F17F4C76268650A60070BB2A /* LikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17F4C75268650A60070BB2A /* LikeService.swift */; }; + F17F4C7A2686521C0070BB2A /* Like.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17F4C792686521C0070BB2A /* Like.swift */; }; + F17F4C7E268654160070BB2A /* Author.swift in Sources */ = {isa = PBXBuildFile; fileRef = F17F4C7D268654160070BB2A /* Author.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -130,6 +133,9 @@ E021985923FA35E20025C28E /* SecretlyTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SecretlyTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; E021985D23FA35E20025C28E /* SecretlyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretlyTests.swift; sourceTree = ""; }; E021985F23FA35E20025C28E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + F17F4C75268650A60070BB2A /* LikeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LikeService.swift; sourceTree = ""; }; + F17F4C792686521C0070BB2A /* Like.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Like.swift; sourceTree = ""; }; + F17F4C7D268654160070BB2A /* Author.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Author.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -220,6 +226,7 @@ 30C77CAF266AD69700A888DC /* CurrentUserService.swift */, 304E06C726742BDA00A99128 /* CreatePostService.swift */, 304E06C926742CC500A99128 /* FeedService.swift */, + F17F4C75268650A60070BB2A /* LikeService.swift */, ); path = Services; sourceTree = ""; @@ -233,6 +240,8 @@ 307A30572661AD540020DF8B /* User.swift */, 30C77CB3266AF47300A888DC /* Credentials.swift */, 30C77CB5266AF48300A888DC /* CurrentUser.swift */, + F17F4C792686521C0070BB2A /* Like.swift */, + F17F4C7D268654160070BB2A /* Author.swift */, ); path = Models; sourceTree = ""; @@ -419,12 +428,14 @@ 307A30622661E8A90020DF8B /* UIColor+Hex.swift in Sources */, 307A30542661ABD60020DF8B /* Post.swift in Sources */, 304E06C626741A5100A99128 /* UIColor+theme.swift in Sources */, + F17F4C76268650A60070BB2A /* LikeService.swift in Sources */, 304E06C826742BDA00A99128 /* CreatePostService.swift in Sources */, E021984723FA35E00025C28E /* AppDelegate.swift in Sources */, 30337955267536980066D94A /* PostInputViewController+CLLocationManagerDelegate.swift in Sources */, 307A30562661AD480020DF8B /* Image.swift in Sources */, 307A30582661AD540020DF8B /* User.swift in Sources */, 302B5849267E658E007133E6 /* RequestError.swift in Sources */, + F17F4C7E268654160070BB2A /* Author.swift in Sources */, 302B5846267E658E007133E6 /* AmacaConfig.swift in Sources */, 302BB61C267D7CC800FD74F5 /* PreviewPostVIew.swift in Sources */, 3033795D267537B40066D94A /* FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift in Sources */, @@ -448,6 +459,7 @@ 302BB624267E3A8700FD74F5 /* PostInputViewController+UIColorPickerViewControllerDelegate.swift in Sources */, 304E06CF267468DA00A99128 /* UIColor+Pastel.swift in Sources */, 304E06C42674133D00A99128 /* String+isBlank.swift in Sources */, + F17F4C7A2686521C0070BB2A /* Like.swift in Sources */, 30BC8BA42662BDEF00F7E6A5 /* ImageStore.swift in Sources */, 3033795B267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.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..9cbb258 100644 --- a/Secretly/Base.lproj/Main.storyboard +++ b/Secretly/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + diff --git a/Secretly/Models/Author.swift b/Secretly/Models/Author.swift new file mode 100644 index 0000000..3eab3af --- /dev/null +++ b/Secretly/Models/Author.swift @@ -0,0 +1,20 @@ +// +// Author.swift +// Secretly +// +// Created by Yocelin Garcia Romero on 25/06/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation +import UIKit + +struct Author: Restable { + let name: String? + let id: String? + + init(id: String, name: String){ + self.name = name + self.id = id + } +} diff --git a/Secretly/Models/Like.swift b/Secretly/Models/Like.swift new file mode 100644 index 0000000..6327016 --- /dev/null +++ b/Secretly/Models/Like.swift @@ -0,0 +1,26 @@ +// +// Like.swift +// Secretly +// +// Created by Yocelin Garcia Romero on 25/06/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation +import UIKit + +struct Like { + let author: Author? + let createdAt: Date? + let updatedAt: Date? + + init(createdAt: Date, updatedAt: Date) { + self.createdAt = createdAt + self.updatedAt = updatedAt + self.author = nil + } +} + +extension Like: Restable { + var id: String { "" } +} diff --git a/Secretly/Models/Post.swift b/Secretly/Models/Post.swift index eba5ff0..b77480d 100644 --- a/Secretly/Models/Post.swift +++ b/Secretly/Models/Post.swift @@ -21,8 +21,10 @@ struct Post: Restable { let longitude: Double? let createdAt: Date? let updatedAt: Date? + let likes: [Like]? + let likesCount: Int? - init(content: String, backgroundColor: String, latitude: Double? = nil, longitude: Double? = nil, image: UIImage? = nil) { + init(content: String, backgroundColor: String, latitude: Double? = nil, longitude: Double? = nil, image: UIImage? = nil, likesCount: Int? = 0) { self.content = content self.backgroundColor = backgroundColor self.id = nil @@ -34,6 +36,8 @@ struct Post: Restable { self.commentsCount = nil self.createdAt = nil self.updatedAt = nil + self.likes = nil + self.likesCount = likesCount } func encode(to encoder: Encoder) throws { diff --git a/Secretly/Services/LikeService.swift b/Secretly/Services/LikeService.swift new file mode 100644 index 0000000..059ac15 --- /dev/null +++ b/Secretly/Services/LikeService.swift @@ -0,0 +1,39 @@ +// +// LikeService.swift +// Secretly +// +// Created by Luis Ezcurdia on 11/06/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct LikeService { + private var likeEndpoint: RestClient + private var postId: Int + + init(postId: Int) { + self.postId = postId + likeEndpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(self.postId)/like") + } + + func delete(_ model: Like, complete: @escaping (Result) -> Void ) { + likeEndpoint.delete(model: model) { result in + DispatchQueue.main.async { complete(result) } + } + } + + func load(completion: @escaping ([Like]) -> Void) { + likeEndpoint.list { result in + guard let likes = try? result.get() else { return } + DispatchQueue.main.async { completion(likes) } + } + } + + func create(_ model: Like, complete: @escaping (Result) -> Void ) { + try? likeEndpoint.create(model: model) { + result in + DispatchQueue.main.async { complete(result) } + } + } +} diff --git a/Secretly/Views/PostCollectionViewCell.swift b/Secretly/Views/PostCollectionViewCell.swift index ee08d53..32f7ff3 100644 --- a/Secretly/Views/PostCollectionViewCell.swift +++ b/Secretly/Views/PostCollectionViewCell.swift @@ -20,7 +20,8 @@ class PostCollectionViewCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView! @IBOutlet weak var likeState: UIImageView! @IBOutlet weak var commentCounter: UILabel! - + @IBOutlet weak var likeMsm: UILabel! + override func awakeFromNib() { super.awakeFromNib() } @@ -37,5 +38,40 @@ class PostCollectionViewCell: UICollectionViewCell { ImageLoader.load(postImg.mediumUrl) { img in self.imageView.image = img } } self.authorView.author = post.user + self.likeMsm.text = getLikeMessage(post: post) + // self.likeState = setLikeState(post: post) } + + func getLikeMessage(post: Post) -> String { + let likeCount = post.likesCount ?? 0 + let likeAuthors = post.likes + + var authorsResume = "" + + if(likeCount == 1) { + authorsResume = "\(likeAuthors?[0].author?.name ?? "") has liked this photo" + } + + if(likeCount >= 2){ + let nameOne = likeAuthors?[0].author?.name ?? "" + let nameTwo = likeAuthors?[1].author?.name ?? "" + authorsResume = "\(nameOne), \(nameTwo) and \(likeCount) more, liked this photo" + } + + return authorsResume + } + + /* func setLikeState(post: Post) -> UIImageView { + let userName = post.user?.username + let likeAuthors = post.likes + + let userLikedItsPhoto = likeAuthors!.filter({$0.author?.name == userName}) + + var image = UIImageView(named: "image") + if(userLikedItsPhoto.count == 1){ + image = + } + + return image + } */ } diff --git a/Secretly/Views/PostCollectionViewCell.xib b/Secretly/Views/PostCollectionViewCell.xib index 7b3a4e2..30a07ea 100644 --- a/Secretly/Views/PostCollectionViewCell.xib +++ b/Secretly/Views/PostCollectionViewCell.xib @@ -1,8 +1,8 @@ - + - + @@ -62,6 +62,12 @@ + @@ -69,6 +75,7 @@ + @@ -79,6 +86,7 @@ + @@ -89,6 +97,7 @@ + From 40b71e469da81f81bfbc6d72d294b184695fa77f Mon Sep 17 00:00:00 2001 From: Yocelin Garcia Date: Thu, 1 Jul 2021 02:03:03 -0500 Subject: [PATCH 02/16] Update like and comment ui elements from uiimageview to uibutton --- Secretly/Models/Post.swift | 6 +- .../PostInputViewController.xib | 4 +- Secretly/Views/PostCollectionViewCell.swift | 34 ++++---- Secretly/Views/PostCollectionViewCell.xib | 87 ++++++++----------- 4 files changed, 56 insertions(+), 75 deletions(-) diff --git a/Secretly/Models/Post.swift b/Secretly/Models/Post.swift index b77480d..12aa2cd 100644 --- a/Secretly/Models/Post.swift +++ b/Secretly/Models/Post.swift @@ -22,9 +22,8 @@ struct Post: Restable { let createdAt: Date? let updatedAt: Date? let likes: [Like]? - let likesCount: Int? - init(content: String, backgroundColor: String, latitude: Double? = nil, longitude: Double? = nil, image: UIImage? = nil, likesCount: Int? = 0) { + init(content: String, backgroundColor: String, latitude: Double? = nil, longitude: Double? = nil, image: UIImage? = nil, likes: [Like]? = nil) { self.content = content self.backgroundColor = backgroundColor self.id = nil @@ -36,8 +35,7 @@ struct Post: Restable { self.commentsCount = nil self.createdAt = nil self.updatedAt = nil - self.likes = nil - self.likesCount = likesCount + self.likes = likes } func encode(to encoder: Encoder) throws { diff --git a/Secretly/ViewControllers/PostInputViewController.xib b/Secretly/ViewControllers/PostInputViewController.xib index 7fd920c..34921f1 100644 --- a/Secretly/ViewControllers/PostInputViewController.xib +++ b/Secretly/ViewControllers/PostInputViewController.xib @@ -1,8 +1,8 @@ - + - + diff --git a/Secretly/Views/PostCollectionViewCell.swift b/Secretly/Views/PostCollectionViewCell.swift index 32f7ff3..6ccc125 100644 --- a/Secretly/Views/PostCollectionViewCell.swift +++ b/Secretly/Views/PostCollectionViewCell.swift @@ -18,7 +18,7 @@ class PostCollectionViewCell: UICollectionViewCell { @IBOutlet weak var authorView: AuthorView! @IBOutlet weak var contentLabel: UILabel! @IBOutlet weak var imageView: UIImageView! - @IBOutlet weak var likeState: UIImageView! + @IBOutlet weak var likeState: UIButton! @IBOutlet weak var commentCounter: UILabel! @IBOutlet weak var likeMsm: UILabel! @@ -39,11 +39,25 @@ class PostCollectionViewCell: UICollectionViewCell { } self.authorView.author = post.user self.likeMsm.text = getLikeMessage(post: post) - // self.likeState = setLikeState(post: post) + self.likeState.isEnabled = setLikeState(post: post) + } + + func setLikeState(post: Post) -> Bool { + let userName = post.user?.username ?? "" + let likeAuthors = post.likes ?? [] + + let userLikedItsPhoto = likeAuthors.filter({$0.author?.name == userName}) + + var buttonStatus = true + if(userLikedItsPhoto.count == 1){ + buttonStatus = false + } + + return buttonStatus } func getLikeMessage(post: Post) -> String { - let likeCount = post.likesCount ?? 0 + let likeCount = post.likes?.count ?? 0 let likeAuthors = post.likes var authorsResume = "" @@ -60,18 +74,4 @@ class PostCollectionViewCell: UICollectionViewCell { return authorsResume } - - /* func setLikeState(post: Post) -> UIImageView { - let userName = post.user?.username - let likeAuthors = post.likes - - let userLikedItsPhoto = likeAuthors!.filter({$0.author?.name == userName}) - - var image = UIImageView(named: "image") - if(userLikedItsPhoto.count == 1){ - image = - } - - return image - } */ } diff --git a/Secretly/Views/PostCollectionViewCell.xib b/Secretly/Views/PostCollectionViewCell.xib index 30a07ea..2e3b16c 100644 --- a/Secretly/Views/PostCollectionViewCell.xib +++ b/Secretly/Views/PostCollectionViewCell.xib @@ -17,79 +17,62 @@ - + - - - - + - - - - - - - - - - - - - - - - - - - + + + + + + + + @@ -98,7 +81,7 @@ - + From 8e0ec1b07afc2758873bfbb0ddf97bf12d5be179 Mon Sep 17 00:00:00 2001 From: Yocelin Garcia Date: Fri, 2 Jul 2021 00:08:57 -0500 Subject: [PATCH 03/16] Fix post xib aligment --- Secretly/Views/PostCollectionViewCell.xib | 28 ++++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Secretly/Views/PostCollectionViewCell.xib b/Secretly/Views/PostCollectionViewCell.xib index 2e3b16c..e826579 100644 --- a/Secretly/Views/PostCollectionViewCell.xib +++ b/Secretly/Views/PostCollectionViewCell.xib @@ -21,24 +21,25 @@ -