From cac541ac6e971ee1de9202e47e9f342dac6eda65 Mon Sep 17 00:00:00 2001 From: Bruno Torres Date: Thu, 30 Jan 2020 22:22:23 -0600 Subject: [PATCH] se agregan los componentes necesarios para mandar likes a publicaciones --- Podfile.lock | 2 +- fakestagram.xcodeproj/project.pbxproj | 12 ++++++--- fakestagram/DataSource/RestClient.swift | 8 ++++-- fakestagram/Network/Client.swift | 2 +- fakestagram/Services/CreateLikeService.swift | 27 +++++++++++++++++++ fakestagram/Services/IndexService.swift | 7 ++++- .../CameraViewController.swift | 2 +- .../Views/PostCollectionViewCell.swift | 19 ++++++++++++- fakestagram/Views/PostCollectionViewCell.xib | 7 +++-- 9 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 fakestagram/Services/CreateLikeService.swift diff --git a/Podfile.lock b/Podfile.lock index 12505e8..b5bd883 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -17,4 +17,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 02c21c317ba9be4fdecdfff242f3fa20e9b269bd -COCOAPODS: 1.8.3 +COCOAPODS: 1.8.4 diff --git a/fakestagram.xcodeproj/project.pbxproj b/fakestagram.xcodeproj/project.pbxproj index fd88dc7..5e142c6 100644 --- a/fakestagram.xcodeproj/project.pbxproj +++ b/fakestagram.xcodeproj/project.pbxproj @@ -50,6 +50,7 @@ E0E8B971234916D100DA9D1A /* RestClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0E8B970234916D100DA9D1A /* RestClient.swift */; }; E0E8B974234918FF00DA9D1A /* Account.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0E8B973234918FF00DA9D1A /* Account.swift */; }; E0E8B9762349197100DA9D1A /* Post.swift in Sources */ = {isa = PBXBuildFile; fileRef = E0E8B9752349197100DA9D1A /* Post.swift */; }; + E13FE5C52389B5B5003BAA65 /* CreateLikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E13FE5C42389B5B5003BAA65 /* CreateLikeService.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -114,6 +115,7 @@ E0E8B970234916D100DA9D1A /* RestClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RestClient.swift; sourceTree = ""; }; E0E8B973234918FF00DA9D1A /* Account.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Account.swift; sourceTree = ""; }; E0E8B9752349197100DA9D1A /* Post.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Post.swift; sourceTree = ""; }; + E13FE5C42389B5B5003BAA65 /* CreateLikeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateLikeService.swift; sourceTree = ""; }; E669E31C92D68155914FC99D /* Pods_fakestagramTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_fakestagramTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; FFAA8B9133CEE464C903FDF5 /* Pods_fakestagram.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_fakestagram.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -154,6 +156,7 @@ children = ( E045844823762E10006574FA /* CreatePostService.swift */, E0458456237725B9006574FA /* IndexService.swift */, + E13FE5C42389B5B5003BAA65 /* CreateLikeService.swift */, ); path = Services; sourceTree = ""; @@ -520,6 +523,7 @@ E0E8B96C23481E1700DA9D1A /* HttpResponse.swift in Sources */, E045844F23765B0F006574FA /* ImageStore.swift in Sources */, E045844D23765677006574FA /* DataContainer.swift in Sources */, + E13FE5C52389B5B5003BAA65 /* CreateLikeService.swift in Sources */, E0E8B9652348018200DA9D1A /* Credentials.swift in Sources */, E0E8B971234916D100DA9D1A /* RestClient.swift in Sources */, E084FADC233A8734009AC50D /* SceneDelegate.swift in Sources */, @@ -689,13 +693,13 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D3XL2U7DQC; + DEVELOPMENT_TEAM = RMQP856RM3; INFOPLIST_FILE = fakestagram/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.3zcurdia.fakestagram; + PRODUCT_BUNDLE_IDENTIFIER = com.bruno.fakestagram; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "fakestagram/fakestagram-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -711,13 +715,13 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = D3XL2U7DQC; + DEVELOPMENT_TEAM = RMQP856RM3; INFOPLIST_FILE = fakestagram/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.3zcurdia.fakestagram; + PRODUCT_BUNDLE_IDENTIFIER = com.bruno.fakestagram; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OBJC_BRIDGING_HEADER = "fakestagram/fakestagram-Bridging-Header.h"; SWIFT_VERSION = 5.0; diff --git a/fakestagram/DataSource/RestClient.swift b/fakestagram/DataSource/RestClient.swift index f22198a..35383a0 100644 --- a/fakestagram/DataSource/RestClient.swift +++ b/fakestagram/DataSource/RestClient.swift @@ -23,9 +23,13 @@ class RestClient { } // eg. POST /posts/ - func create(_ data: T, success: @escaping (T?) -> Void) { + func create(id: String? = nil, _ data: T, success: @escaping (T?) -> Void) { + var path = basePath let payload = try? encoder.encode(data) - client.post(path: basePath, body: payload) { data in + if let uid = id { + path += "/\(uid)" + } + client.post(path: path, body: payload) { data in CodableSerializer(data: data).async(result: success) } } diff --git a/fakestagram/Network/Client.swift b/fakestagram/Network/Client.swift index 187f7ca..22177c6 100644 --- a/fakestagram/Network/Client.swift +++ b/fakestagram/Network/Client.swift @@ -41,7 +41,7 @@ class Client { debugPrint("Invalid request") return } - + print(req.url) session.dataTask(with: req) { (data, response, error) in if let error = error { debugPrint(error) diff --git a/fakestagram/Services/CreateLikeService.swift b/fakestagram/Services/CreateLikeService.swift new file mode 100644 index 0000000..9cf2215 --- /dev/null +++ b/fakestagram/Services/CreateLikeService.swift @@ -0,0 +1,27 @@ +// +// CreateLikeService.swift +// fakestagram +// +// Created by Bruno Torres on 23/11/19. +// Copyright © 2019 3zcurdia. All rights reserved. +// + +import Foundation + +struct CreateLike: Codable { + let id: String? +} + +class CreateLikeService{ + let clientLike = RestClient(client: Client.fakestagram, basePath: "/api/v1/posts") + + func createLike(id: String, success: @escaping (CreateLike?) -> Void){ + let newLike = CreateLike(id: id) + + clientLike.create(id: "\(id)/like", newLike, success: success) + print("------------------------") + clientLike.show(id: "\(id)/likes", success: success) + print("------------------------") + } + +} diff --git a/fakestagram/Services/IndexService.swift b/fakestagram/Services/IndexService.swift index 5c6cb55..76ec647 100644 --- a/fakestagram/Services/IndexService.swift +++ b/fakestagram/Services/IndexService.swift @@ -10,8 +10,13 @@ import Foundation class IndexService { let client = RestClient<[Post]>(client: Client.fakestagram, basePath: "/api/v1/posts") - + let clientLike = RestClient<[Like]>(client: Client.fakestagram, basePath: "/api/v1/posts") + func call(success: @escaping ([Post]?) -> Void) { client.show(success: success) } + + func callLike(id: String,success: @escaping ([Like]?) -> Void) { + //clientLike.show(id: id,success: success) + } } diff --git a/fakestagram/ViewControllers/CameraViewController.swift b/fakestagram/ViewControllers/CameraViewController.swift index a7996c7..8ecb278 100644 --- a/fakestagram/ViewControllers/CameraViewController.swift +++ b/fakestagram/ViewControllers/CameraViewController.swift @@ -99,7 +99,7 @@ class CameraViewController: UIViewController { func setupCaptureSession() { session.beginConfiguration() - let device = AVCaptureDevice.default(.builtInDualCamera, + let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)! guard let videoDeviceInput = try? AVCaptureDeviceInput(device: device), session.canAddInput(videoDeviceInput) else { return } diff --git a/fakestagram/Views/PostCollectionViewCell.swift b/fakestagram/Views/PostCollectionViewCell.swift index 1d5be7b..3eff76e 100644 --- a/fakestagram/Views/PostCollectionViewCell.swift +++ b/fakestagram/Views/PostCollectionViewCell.swift @@ -22,6 +22,9 @@ class PostCollectionViewCell: UICollectionViewCell { @IBOutlet weak var likeBttn: UIButton! @IBOutlet weak var commentBttn: UIButton! + let service = CreateLikeService() + let service2 = IndexService() + func reset() { self.imageView.image = nil self.likeCounter.text = "" @@ -38,5 +41,19 @@ class PostCollectionViewCell: UICollectionViewCell { self.imageView.image = img } } - + @IBAction func likeButton(_ sender: UIButton) { + print("like") + if let id = post?.id{ + service.createLike(id: "\(id)") { (like) in + print(like) + } + } + + /*service2.callLike(id: "\(id)") { [unowned self] data in + print(data) + + }*/ + + } + } diff --git a/fakestagram/Views/PostCollectionViewCell.xib b/fakestagram/Views/PostCollectionViewCell.xib index afff929..2fa7283 100644 --- a/fakestagram/Views/PostCollectionViewCell.xib +++ b/fakestagram/Views/PostCollectionViewCell.xib @@ -1,8 +1,8 @@ - + - + @@ -36,6 +36,9 @@