diff --git a/Secretly.xcodeproj/project.pbxproj b/Secretly.xcodeproj/project.pbxproj index 8dd4829..e36d53d 100644 --- a/Secretly.xcodeproj/project.pbxproj +++ b/Secretly.xcodeproj/project.pbxproj @@ -56,6 +56,9 @@ 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 */; }; + 5B89708B269A491D0080C464 /* Like.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B89708A269A491D0080C464 /* Like.swift */; }; + 5B89708D269A4A3E0080C464 /* LikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B89708C269A4A3E0080C464 /* LikeService.swift */; }; + 5B897092269A913A0080C464 /* LikeServiceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B897091269A913A0080C464 /* LikeServiceTest.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 +128,9 @@ 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 = ""; }; + 5B89708A269A491D0080C464 /* Like.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Like.swift; sourceTree = ""; }; + 5B89708C269A4A3E0080C464 /* LikeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LikeService.swift; sourceTree = ""; }; + 5B897091269A913A0080C464 /* LikeServiceTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LikeServiceTest.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 = ""; }; @@ -199,8 +205,8 @@ children = ( 307A305C2661CD510020DF8B /* PostCollectionViewCell.swift */, 307A305D2661CD510020DF8B /* PostCollectionViewCell.xib */, - 307A306426629B990020DF8B /* AuthorView.swift */, 302BB61B267D7CC800FD74F5 /* PreviewPostVIew.swift */, + 307A306426629B990020DF8B /* AuthorView.swift */, ); path = Views; sourceTree = ""; @@ -222,6 +228,7 @@ 30C77CAF266AD69700A888DC /* CurrentUserService.swift */, 304E06C726742BDA00A99128 /* CreatePostService.swift */, 304E06C926742CC500A99128 /* FeedService.swift */, + 5B89708C269A4A3E0080C464 /* LikeService.swift */, ); path = Services; sourceTree = ""; @@ -248,6 +255,7 @@ 307A30572661AD540020DF8B /* User.swift */, 30C77CB3266AF47300A888DC /* Credentials.swift */, 30C77CB5266AF48300A888DC /* CurrentUser.swift */, + 5B89708A269A491D0080C464 /* Like.swift */, ); path = Models; sourceTree = ""; @@ -299,6 +307,7 @@ children = ( E021985D23FA35E20025C28E /* SecretlyTests.swift */, E021985F23FA35E20025C28E /* Info.plist */, + 5B897091269A913A0080C464 /* LikeServiceTest.swift */, ); path = SecretlyTests; sourceTree = ""; @@ -428,6 +437,7 @@ E021984B23FA35E00025C28E /* WelcomeViewController.swift in Sources */, 3072FBDF2680FA5A00B35C8C /* ImageProcessor.swift in Sources */, 302BB622267E38E800FD74F5 /* PostInputViewController+UIImagePickerControllerDelegate.swift in Sources */, + 5B89708B269A491D0080C464 /* Like.swift in Sources */, 302B5845267E658E007133E6 /* HttpResponse.swift in Sources */, 302B584A267E658E007133E6 /* RestClient.swift in Sources */, 302B5848267E658E007133E6 /* HttpClient.swift in Sources */, @@ -466,6 +476,7 @@ 30C77CB6266AF48300A888DC /* CurrentUser.swift in Sources */, 302BB624267E3A8700FD74F5 /* PostInputViewController+UIColorPickerViewControllerDelegate.swift in Sources */, 304E06CF267468DA00A99128 /* UIColor+Pastel.swift in Sources */, + 5B89708D269A4A3E0080C464 /* LikeService.swift in Sources */, 304E06C42674133D00A99128 /* String+isBlank.swift in Sources */, 30BC8BA42662BDEF00F7E6A5 /* ImageStore.swift in Sources */, 3033795B267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.swift in Sources */, @@ -481,6 +492,7 @@ buildActionMask = 2147483647; files = ( E021985E23FA35E20025C28E /* SecretlyTests.swift in Sources */, + 5B897092269A913A0080C464 /* LikeServiceTest.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -635,13 +647,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D3XL2U7DQC; + DEVELOPMENT_TEAM = CF8GW6MT8Z; INFOPLIST_FILE = Secretly/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = mx.unam.ioslab.Secretly; + PRODUCT_BUNDLE_IDENTIFIER = fernando.Secretly; PRODUCT_NAME = Secretly; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 1; @@ -653,13 +665,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D3XL2U7DQC; + DEVELOPMENT_TEAM = CF8GW6MT8Z; INFOPLIST_FILE = Secretly/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = mx.unam.ioslab.Secretly; + PRODUCT_BUNDLE_IDENTIFIER = fernando.Secretly; PRODUCT_NAME = Secretly; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 1; @@ -672,7 +684,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D3XL2U7DQC; + DEVELOPMENT_TEAM = CF8GW6MT8Z; INFOPLIST_FILE = SecretlyTests/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 14.1; LD_RUNPATH_SEARCH_PATHS = ( @@ -694,7 +706,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; BUNDLE_LOADER = "$(TEST_HOST)"; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D3XL2U7DQC; + DEVELOPMENT_TEAM = CF8GW6MT8Z; INFOPLIST_FILE = SecretlyTests/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 14.1; LD_RUNPATH_SEARCH_PATHS = ( diff --git a/Secretly/Info.plist b/Secretly/Info.plist index 154ea82..51ca4f3 100644 --- a/Secretly/Info.plist +++ b/Secretly/Info.plist @@ -2,12 +2,6 @@ - NSPhotoLibraryUsageDescription - To add some context to your posts - NSCameraUsageDescription - To add some context to your posts - NSLocationWhenInUseUsageDescription - To georeference posts within a region CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -26,6 +20,12 @@ 1 LSRequiresIPhoneOS + NSCameraUsageDescription + To add some context to your posts + NSLocationWhenInUseUsageDescription + To georeference posts within a region + NSPhotoLibraryUsageDescription + To add some context to your posts UIApplicationSceneManifest UIApplicationSupportsMultipleScenes diff --git a/Secretly/Models/Like.swift b/Secretly/Models/Like.swift new file mode 100644 index 0000000..81c408b --- /dev/null +++ b/Secretly/Models/Like.swift @@ -0,0 +1,15 @@ +// +// Like.swift +// Secretly +// +// Created by Proteco on 10/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct Like: Restable { + let id: Int + let createdAt: Date + let updatedAt: Date +} diff --git a/Secretly/Models/Post.swift b/Secretly/Models/Post.swift index eba5ff0..2c13f19 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? + var likesCount: Int? + var liked: Bool - 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, liked: Bool, likesCount: Int? = nil) { 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.likesCount = nil + self.liked = false } func encode(to encoder: Encoder) throws { diff --git a/Secretly/Network/HttpResponse.swift b/Secretly/Network/HttpResponse.swift index eb0543a..1ea6e75 100644 --- a/Secretly/Network/HttpResponse.swift +++ b/Secretly/Network/HttpResponse.swift @@ -20,6 +20,19 @@ struct HttpResponse { } func result(for data: Data?) -> Result { - return status.result().map { _ in data } + //#if DEBUG + if let udata = data, !udata.isEmpty { + let currentData = String(data: udata, encoding: .utf8) + debugPrint("Response: \(status) \(self.httpUrlResponse.statusCode) \(self.httpUrlResponse.url!) -d \(String(describing: currentData))") + } else { + debugPrint("Response: \(status) \(self.httpUrlResponse.statusCode) \(self.httpUrlResponse.url!)") + } + //#endif + if let udata = data, !udata.isEmpty { + return status.result().map { _ in data } + } else { + return status.result().map { _ in nil } + } } + } diff --git a/Secretly/Network/RestClient.swift b/Secretly/Network/RestClient.swift index 3048ccf..4f24f61 100644 --- a/Secretly/Network/RestClient.swift +++ b/Secretly/Network/RestClient.swift @@ -50,6 +50,13 @@ struct RestClient { complete(newResult) } } + + func create(complete: @escaping (Result) -> Void) throws { + client.post(path: path, body: nil) { result in + let newResult = result.flatMap { parse(data: $0) } + complete(newResult) + } + } func create(model: T, complete: @escaping (Result) -> Void) throws { let data = try encoder.encode(model) @@ -61,7 +68,14 @@ struct RestClient { func update(model: T, complete: @escaping (Result) -> Void) throws { let data = try encoder.encode(model) - client.put(path: "\(path)/\(model.id)", body: data) { result in + client.put(path: path, body: data) { result in + let newResult = result.flatMap { parse(data: $0) } + complete(newResult) + } + } + + func delete(complete: @escaping (Result) -> Void) { + client.delete(path: path) { result in let newResult = result.flatMap { parse(data: $0) } complete(newResult) } diff --git a/Secretly/Services/LikeService.swift b/Secretly/Services/LikeService.swift new file mode 100644 index 0000000..748d3d3 --- /dev/null +++ b/Secretly/Services/LikeService.swift @@ -0,0 +1,42 @@ +// +// LikeService.swift +// Secretly +// +// Created by Proteco on 10/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import Foundation + +struct LikeService { + private var endpoint: RestClient? + + init(post: Post?) { + guard let post = post, let postId = post.id else { + self.endpoint = nil + return + } + self.endpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(postId)/likes") + } + + init(id: Int?) { + guard let postId = id else { + self.endpoint = nil + return + } + self.endpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(postId)/likes") + } + + func create(_ model: Like, complete: @escaping (Result) -> Void) { + try? endpoint?.create() { result in + DispatchQueue.main.async { complete(result) } + } + } + + func delete(_ model: Like, complete: @escaping (Result) -> Void) { + try? endpoint?.delete() { result in + DispatchQueue.main.async { complete(result) } + } + } + +} diff --git a/Secretly/ViewControllers/CreatePostViewController.swift b/Secretly/ViewControllers/CreatePostViewController.swift index 4091252..2f657f7 100644 --- a/Secretly/ViewControllers/CreatePostViewController.swift +++ b/Secretly/ViewControllers/CreatePostViewController.swift @@ -19,7 +19,7 @@ class CreatePostViewController: UIViewController { @IBAction func createPost(_ sender: Any?) { - let post = Post(content: contentField.text!, backgroundColor: colorField.text!) + let post = Post(content: contentField.text!, backgroundColor: colorField.text!, liked: false, likesCount: 0) let postsEndpoint = RestClient(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts") do { diff --git a/Secretly/ViewControllers/PostInputViewController.swift b/Secretly/ViewControllers/PostInputViewController.swift index 676ce35..c25fc35 100644 --- a/Secretly/ViewControllers/PostInputViewController.swift +++ b/Secretly/ViewControllers/PostInputViewController.swift @@ -141,7 +141,8 @@ class PostInputViewController: UIViewController, UINavigationControllerDelegate content: postText, backgroundColor: previewPost.backgroundColor?.hexString ?? "#3366CC", latitude: currentLocation?.latitude, - longitude: currentLocation?.longitude + longitude: currentLocation?.longitude, + liked: false ) if let uimage = previewPost.image { post.imageData = uimage.encodeBase64() diff --git a/Secretly/Views/PostCollectionViewCell.swift b/Secretly/Views/PostCollectionViewCell.swift index ee08d53..2e4a5ae 100644 --- a/Secretly/Views/PostCollectionViewCell.swift +++ b/Secretly/Views/PostCollectionViewCell.swift @@ -10,9 +10,11 @@ import UIKit class PostCollectionViewCell: UICollectionViewCell { static let reuseIdentifier = "feedPostCell" + var likeService: LikeService? var post: Post? { didSet { updateView() + likeService = LikeService(post: post) } } @IBOutlet weak var authorView: AuthorView! @@ -20,12 +22,23 @@ class PostCollectionViewCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView! @IBOutlet weak var likeState: UIImageView! @IBOutlet weak var commentCounter: UILabel! + @IBOutlet weak var likeCounter: UILabel! override func awakeFromNib() { super.awakeFromNib() + + let tapLikeState = UITapGestureRecognizer(target: self, action: #selector(tapLike)) + self.likeState.isUserInteractionEnabled = true + self.likeState.addGestureRecognizer(tapLikeState) + + let doubleTapImageView = UITapGestureRecognizer(target: self, action: #selector(doubleTapImageViewLike)) + doubleTapImageView.numberOfTapsRequired = 2 + self.imageView.isUserInteractionEnabled = true + self.imageView.addGestureRecognizer(doubleTapImageView) } func updateView() { + status() imageView.image = nil guard let post = post else { return } if let color = UIColor(hex: post.backgroundColor) { @@ -33,9 +46,46 @@ class PostCollectionViewCell: UICollectionViewCell { } self.contentLabel.text = post.content self.commentCounter.text = String(describing: post.commentsCount ?? 0) + self.likeCounter.text = String(describing: post.likesCount ?? 0) if let postImg = post.image { ImageLoader.load(postImg.mediumUrl) { img in self.imageView.image = img } } self.authorView.author = post.user } + + func status() { + guard let post = post else {return} + if post.liked { + likeState.image = UIImage(systemName: "heart.fill") + } else { + likeState.image = UIImage(systemName: "heart") + } + } + + @objc private func tapLike(){ + self.like() + } + + @objc private func doubleTapImageViewLike(_ gesture: UITapGestureRecognizer){ + self.like() + } + + private func like(){ + guard let post = post, let id = post.id else {return} + let updateLike = Like(id: id, createdAt: Date(), updatedAt: Date()) + if !post.liked { + likeService?.create(updateLike) { result in + self.post?.liked = true + self.likeState.image = UIImage(systemName: "heart.fill") + self.likeCounter.text = String(describing: post.likesCount! + 1) + } + } else { + likeService?.delete(updateLike) { result in + self.post?.liked = false + self.likeState.image = UIImage(systemName: "heart") + self.likeCounter.text = String(describing: post.likesCount ?? 0 - 1) + } + } + } + } diff --git a/Secretly/Views/PostCollectionViewCell.xib b/Secretly/Views/PostCollectionViewCell.xib index 7b3a4e2..bd58bf8 100644 --- a/Secretly/Views/PostCollectionViewCell.xib +++ b/Secretly/Views/PostCollectionViewCell.xib @@ -62,12 +62,22 @@ + + @@ -76,6 +86,7 @@ + @@ -89,6 +100,7 @@ + diff --git a/SecretlyTests/LikeServiceTest.swift b/SecretlyTests/LikeServiceTest.swift new file mode 100644 index 0000000..0fde5c4 --- /dev/null +++ b/SecretlyTests/LikeServiceTest.swift @@ -0,0 +1,24 @@ +// +// LikeServiceTest.swift +// SecretlyTests +// +// Created by Proteco on 10/07/21. +// Copyright © 2021 3zcurdia. All rights reserved. +// + +import XCTest +@testable import Secretly + +class LikeServiceTest: XCTestCase { + + func testCreateLike() { + let createLike = LikeService(id: 70) + XCTAssertNoThrow(createLike) + } + + func testDelteTest() { + let deleteLike = LikeService(id: 70) + XCTAssertNoThrow(deleteLike) + } + +}