Skip to content
This repository was archived by the owner on Sep 8, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Secretly.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
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 */; };
CF00550026D7054000585549 /* Like.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF0054FF26D7054000585549 /* Like.swift */; };
CFF9124926D7232C0001E9FF /* LikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = CFF9124826D7232C0001E9FF /* LikeService.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 */; };
Expand Down Expand Up @@ -125,6 +127,8 @@
30C77CB5266AF48300A888DC /* CurrentUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUser.swift; sourceTree = "<group>"; };
30C77CB7266BD44300A888DC /* CreatePostViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreatePostViewController.swift; sourceTree = "<group>"; };
30FD0E712659645A006E309A /* Faker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Faker.swift; sourceTree = "<group>"; };
CF0054FF26D7054000585549 /* Like.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Like.swift; sourceTree = "<group>"; };
CFF9124826D7232C0001E9FF /* LikeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LikeService.swift; sourceTree = "<group>"; };
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 = "<group>"; };
E021984823FA35E00025C28E /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +226,7 @@
30C77CAF266AD69700A888DC /* CurrentUserService.swift */,
304E06C726742BDA00A99128 /* CreatePostService.swift */,
304E06C926742CC500A99128 /* FeedService.swift */,
CFF9124826D7232C0001E9FF /* LikeService.swift */,
);
path = Services;
sourceTree = "<group>";
Expand All @@ -248,6 +253,7 @@
307A30572661AD540020DF8B /* User.swift */,
30C77CB3266AF47300A888DC /* Credentials.swift */,
30C77CB5266AF48300A888DC /* CurrentUser.swift */,
CF0054FF26D7054000585549 /* Like.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -428,6 +434,7 @@
E021984B23FA35E00025C28E /* WelcomeViewController.swift in Sources */,
3072FBDF2680FA5A00B35C8C /* ImageProcessor.swift in Sources */,
302BB622267E38E800FD74F5 /* PostInputViewController+UIImagePickerControllerDelegate.swift in Sources */,
CF00550026D7054000585549 /* Like.swift in Sources */,
302B5845267E658E007133E6 /* HttpResponse.swift in Sources */,
302B584A267E658E007133E6 /* RestClient.swift in Sources */,
302B5848267E658E007133E6 /* HttpClient.swift in Sources */,
Expand Down Expand Up @@ -466,6 +473,7 @@
30C77CB6266AF48300A888DC /* CurrentUser.swift in Sources */,
302BB624267E3A8700FD74F5 /* PostInputViewController+UIColorPickerViewControllerDelegate.swift in Sources */,
304E06CF267468DA00A99128 /* UIColor+Pastel.swift in Sources */,
CFF9124926D7232C0001E9FF /* LikeService.swift in Sources */,
304E06C42674133D00A99128 /* String+isBlank.swift in Sources */,
30BC8BA42662BDEF00F7E6A5 /* ImageStore.swift in Sources */,
3033795B267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.swift in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions Secretly/Models/Like.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Like.swift
// Secretly
//
// Created by maxwell on 25/08/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation

struct Like: Restable {
var id: Int
let likeableType: String
let likeableId: Int
let createdAt: String
let updatedAt: String
var user: User
}
23 changes: 19 additions & 4 deletions Secretly/Models/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct Post: Restable {
let commentsCount: Int?
let latitude: Double?
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) {
self.content = content
Expand All @@ -32,8 +32,8 @@ struct Post: Restable {
self.longitude = longitude
self.user = nil
self.commentsCount = nil
self.createdAt = nil
self.updatedAt = nil
self.likesCount = nil
self.liked = nil
}

func encode(to encoder: Encoder) throws {
Expand All @@ -43,5 +43,20 @@ struct Post: Restable {
try container.encode(imageData, forKey: .imageData)
try container.encode(latitude, forKey: .latitude)
try container.encode(longitude, forKey: .longitude)
try container.encode(likesCount, forKey: .likesCount)
try container.encode(liked, forKey: .liked)

}

mutating func toggleLike(state:String){
if state == "inc"{
likesCount = (likesCount ?? 0) + 1
liked = true
} else if state == "dec"{
likesCount = (likesCount ?? 0) - 1
liked = false
} else {
print("Estado no reconocido")
}
}
}
6 changes: 5 additions & 1 deletion Secretly/Network/HttpResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct HttpResponse {
}

func result(for data: Data?) -> Result<Data?, Error> {
return status.result().map { _ in data }
if let data = data, !data.isEmpty {
return status.result().map { _ in data }
} else {
return status.result().map { _ in nil }
}
}
}
14 changes: 14 additions & 0 deletions Secretly/Network/RestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ struct RestClient<T: Restable> {
complete(newResult)
}
}

func create(complete: @escaping (Result<T?, Error>)-> Void) throws {
client.post(path: path, body: nil) { result in
let newResult = result.flatMap { parse(data: $0)}
complete(newResult)
}
}

func update(model: T, complete: @escaping (Result<T?, Error>) -> Void) throws {
let data = try encoder.encode(model)
Expand All @@ -73,6 +80,13 @@ struct RestClient<T: Restable> {
complete(newResult)
}
}

func delete(complete: @escaping (Result<T?, Error>) -> Void) {
client.delete(path: path) { result in
let newResult = result.flatMap { parse(data: $0) }
complete(newResult)
}
}

private func parseList(data: Data?) -> Result<[T], Error> {
if let data = data {
Expand Down
36 changes: 36 additions & 0 deletions Secretly/Services/LikeService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// LikeService.swift
// Secretly
//
// Created by maxwell on 25/08/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation

struct LikeService {
let endPoint: RestClient<Like>?
var active = false

init(post: Post?) {
guard let post = post, let postId = post.id else {
self.endPoint = nil
return
}
self.endPoint = RestClient<Like>(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(postId)/likes")
self.active = post.liked ?? false
}

mutating func action(complete: @escaping (Result<Like?, Error>)-> Void){
if self.active {
endPoint?.delete{ result in
DispatchQueue.main.async { complete(result) }
}
} else {
try? endPoint?.create{ result in
DispatchQueue.main.async { complete(result) }
}
}
active = !active
}
}
29 changes: 27 additions & 2 deletions Secretly/Views/PostCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ 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!
@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 likeCounter: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
}
Expand All @@ -37,5 +40,27 @@ class PostCollectionViewCell: UICollectionViewCell {
ImageLoader.load(postImg.mediumUrl) { img in self.imageView.image = img }
}
self.authorView.author = post.user

let imageName = post.liked ?? false ? "heart.fill" : "heart"
likeState.setImage(UIImage(systemName: imageName), for: .normal)

self.likeCounter.text = "\(self.post?.likesCount ?? 0 ) likes"

}

@IBAction func likeOnTap(_ sender: UIButton) {
likeService?.action{ [unowned self] result in
var imageName = "heart"
switch result {
case .success(nil):
post?.toggleLike(state: "dec")
case .success:
imageName = "heart.fill"
post?.toggleLike(state: "inc")
case .failure:
print("No se pudo actualizar el like")
}
likeState.setImage(UIImage(systemName: imageName ),for: .normal)
}
}
}
40 changes: 29 additions & 11 deletions Secretly/Views/PostCollectionViewCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand All @@ -19,6 +20,7 @@
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="LcI-IM-E3b">
<rect key="frame" x="0.0" y="0.0" width="400" height="300"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="height" constant="300" id="FVk-cX-Y87"/>
<constraint firstAttribute="width" secondItem="LcI-IM-E3b" secondAttribute="height" multiplier="4:3" id="IcI-he-Eb8"/>
Expand All @@ -30,14 +32,6 @@
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="heart" catalog="system" translatesAutoresizingMaskIntoConstraints="NO" id="qBu-h3-372">
<rect key="frame" x="3" y="278.5" width="20" height="17.5"/>
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="width" constant="20" id="FZr-RK-fSl"/>
<constraint firstAttribute="height" constant="20" id="aph-kx-k7x"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1200 " lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ier-Sw-bbQ">
<rect key="frame" x="370" y="282" width="27" height="12"/>
<constraints>
Expand All @@ -62,17 +56,38 @@
<constraint firstAttribute="height" constant="40" id="FP9-i4-4aV"/>
</constraints>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="NKD-wH-ehQ">
<rect key="frame" x="3" y="278" width="20" height="20"/>
<constraints>
<constraint firstAttribute="width" constant="20" id="AHZ-y0-NCQ"/>
<constraint firstAttribute="height" constant="20" id="byE-ec-dTd"/>
</constraints>
<color key="tintColor" name="Champagne Pink"/>
<state key="normal" image="heart" catalog="system"/>
<connections>
<action selector="likeOnTap:" destination="gTV-IL-0wX" eventType="touchUpInside" id="3zQ-qs-Wg8"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1000 likes" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="HyU-Gz-t62">
<rect key="frame" x="26" y="281" width="49" height="12"/>
<fontDescription key="fontDescription" type="system" pointSize="10"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</view>
<viewLayoutGuide key="safeArea" id="ZTg-uK-7eu"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="gUg-yt-vKP" secondAttribute="trailing" id="1Ox-jh-Fdw"/>
<constraint firstItem="LcI-IM-E3b" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" id="ALT-n0-JLg"/>
<constraint firstAttribute="trailing" secondItem="ier-Sw-bbQ" secondAttribute="trailing" constant="3" id="Chd-rw-tMw"/>
<constraint firstItem="NKD-wH-ehQ" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" constant="3" id="PGc-LK-fHx"/>
<constraint firstItem="LcI-IM-E3b" firstAttribute="top" secondItem="gTV-IL-0wX" secondAttribute="top" id="Saq-lM-0ue"/>
<constraint firstAttribute="trailing" secondItem="Mh0-Gr-U1B" secondAttribute="trailing" constant="20" id="TmC-rH-rgp"/>
<constraint firstAttribute="bottom" secondItem="NKD-wH-ehQ" secondAttribute="bottom" constant="2" id="Twa-PG-939"/>
<constraint firstItem="gUg-yt-vKP" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" id="Udj-7I-U3z"/>
<constraint firstItem="qBu-h3-372" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" constant="3" id="Wk2-tC-y2S"/>
<constraint firstItem="HyU-Gz-t62" firstAttribute="leading" secondItem="NKD-wH-ehQ" secondAttribute="trailing" constant="3" id="X1w-9U-g3e"/>
<constraint firstAttribute="bottom" secondItem="HyU-Gz-t62" secondAttribute="bottom" constant="7" id="YVi-Hd-1GC"/>
<constraint firstItem="Mh0-Gr-U1B" firstAttribute="centerY" secondItem="gTV-IL-0wX" secondAttribute="centerY" id="cdi-uV-CwA"/>
<constraint firstAttribute="bottom" secondItem="ier-Sw-bbQ" secondAttribute="bottom" constant="6" id="dEi-5s-5Xo"/>
<constraint firstItem="ier-Sw-bbQ" firstAttribute="leading" secondItem="Zus-SL-Va3" secondAttribute="trailing" constant="3" id="dc0-TJ-Axz"/>
Expand All @@ -81,22 +96,25 @@
<constraint firstAttribute="trailing" secondItem="LcI-IM-E3b" secondAttribute="trailing" id="mm7-gQ-FEH"/>
<constraint firstItem="Mh0-Gr-U1B" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" constant="20" id="pwh-eS-Ehf"/>
<constraint firstAttribute="bottom" secondItem="Zus-SL-Va3" secondAttribute="bottom" constant="2" id="t8V-mY-lku"/>
<constraint firstAttribute="bottom" secondItem="qBu-h3-372" secondAttribute="bottom" constant="3" id="wei-7x-Bcq"/>
</constraints>
<size key="customSize" width="400" height="300"/>
<connections>
<outlet property="authorView" destination="gUg-yt-vKP" id="cZF-pT-x7d"/>
<outlet property="commentCounter" destination="ier-Sw-bbQ" id="Kr2-a4-New"/>
<outlet property="contentLabel" destination="Mh0-Gr-U1B" id="tTs-p1-j7k"/>
<outlet property="imageView" destination="LcI-IM-E3b" id="vWk-OE-zm9"/>
<outlet property="likeState" destination="qBu-h3-372" id="Qyd-rc-8ZQ"/>
<outlet property="likeCounter" destination="HyU-Gz-t62" id="aCE-DI-VBS"/>
<outlet property="likeState" destination="NKD-wH-ehQ" id="79Q-RO-Sms"/>
</connections>
<point key="canvasLocation" x="389.85507246376812" y="182.8125"/>
</collectionViewCell>
</objects>
<resources>
<image name="heart" catalog="system" width="128" height="109"/>
<image name="message.fill" catalog="system" width="128" height="113"/>
<namedColor name="Champagne Pink">
<color red="0.85882352941176465" green="0.792156862745098" blue="0.74117647058823533" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
Expand Down