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 */; };
92CC860426E33B7F00BE71F5 /* Like.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92CC860326E33B7F00BE71F5 /* Like.swift */; };
92CC860826E34AF700BE71F5 /* LikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92CC860726E34AF700BE71F5 /* 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>"; };
92CC860326E33B7F00BE71F5 /* Like.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Like.swift; sourceTree = "<group>"; };
92CC860726E34AF700BE71F5 /* 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 */,
92CC860726E34AF700BE71F5 /* LikeService.swift */,
);
path = Services;
sourceTree = "<group>";
Expand All @@ -248,6 +253,7 @@
307A30572661AD540020DF8B /* User.swift */,
30C77CB3266AF47300A888DC /* Credentials.swift */,
30C77CB5266AF48300A888DC /* CurrentUser.swift */,
92CC860326E33B7F00BE71F5 /* 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 */,
92CC860426E33B7F00BE71F5 /* 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 */,
92CC860826E34AF700BE71F5 /* LikeService.swift in Sources */,
304E06C42674133D00A99128 /* String+isBlank.swift in Sources */,
30BC8BA42662BDEF00F7E6A5 /* ImageStore.swift in Sources */,
3033795B267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.swift in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions Secretly.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions Secretly/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<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"/>
Expand Down
22 changes: 22 additions & 0 deletions Secretly/Models/Like.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Like.swift
// Secretly
//
// Created by Berenice Medel on 04/09/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation

struct Like: Restable {
let id, userID, postId: Int?
let createdAt, updatedAt: String?

init(postId: Int){
self.id = nil
self.postId = postId
self.createdAt = nil
self.updatedAt = nil
self.userID = nil
}
}
4 changes: 4 additions & 0 deletions Secretly/Models/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ struct Post: Restable {
var imageData: String?
let user: User?
let commentsCount: Int?
let likesCount: Int?
let latitude: Double?
let longitude: Double?
let createdAt: Date?
let updatedAt: Date?
var liked: Bool?

init(content: String, backgroundColor: String, latitude: Double? = nil, longitude: Double? = nil, image: UIImage? = nil) {
self.content = content
Expand All @@ -32,8 +34,10 @@ struct Post: Restable {
self.longitude = longitude
self.user = nil
self.commentsCount = nil
self.likesCount = nil
self.createdAt = nil
self.updatedAt = nil
self.liked = nil
}

func encode(to encoder: Encoder) throws {
Expand Down
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 udata = data, !udata.isEmpty {
return status.result().map { _ in data }
} else {
return status.result().map { _ in nil }
}
}
}
2 changes: 1 addition & 1 deletion Secretly/Network/RestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ struct RestClient<T: Restable> {
}

func delete(model: T, complete: @escaping (Result<T?, Error>) -> Void) {
client.delete(path: "\(path)/\(model.id)") { result in
client.delete(path: "\(path)") { result in
let newResult = result.flatMap { parse(data: $0) }
complete(newResult)
}
Expand Down
30 changes: 30 additions & 0 deletions Secretly/Services/LikeService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// LikeService.swift
// Secretly
//
// Created by Berenice Medel on 04/09/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation


struct LikeService {
private var endpoint: RestClient<Like>

init(post: Post) {
self.endpoint = RestClient<Like>(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(post.id!)/likes")
}

func create(_ model: Like, complete: @escaping (Result<Like?, Error>) -> Void ) {
try? endpoint.create(model: model) { result in
DispatchQueue.main.async { complete(result) }
}
}

func delete(_ model: Like, complete: @escaping (Result<Like?, Error>) -> Void ) {
try? endpoint.delete(model: model) { result in
DispatchQueue.main.async { complete(result) }
}
}
}
64 changes: 62 additions & 2 deletions Secretly/Views/PostCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit

class PostCollectionViewCell: UICollectionViewCell {
static let reuseIdentifier = "feedPostCell"

var post: Post? {
didSet {
updateView()
Expand All @@ -18,13 +19,63 @@ 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 commentCounter: UILabel!

@IBOutlet weak var likeState: UIButton!
@IBOutlet weak var likeCounter: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
}

@IBAction func onLikeClicked(_ sender: Any) {
let like = Like(postId: (self.post?.id)!)
let likeService = LikeService(post: post!)
if (post?.liked)!{
deleteLike(like: like, likeService: likeService)
}else{
createLike(like: like, likeService: likeService)
}
}

func createLike(like: Like, likeService: LikeService){
likeService.create(like) {
[unowned self] result in
switch result {
case .success(let like):
print("there is a new like \(like?.id ?? 0)")
DispatchQueue.main.async {
self.post!.liked = true
self.likeState.tintColor = .red
self.likeState.setImage(UIImage(systemName: "heart.fill"), for: .normal)
let newCounter =
Int(self.likeCounter.text!)! + 1
self.likeCounter.text = String(newCounter)
}
case .failure(let err):
print(err.localizedDescription)
}
}
}

func deleteLike(like: Like, likeService: LikeService){
likeService.delete(like) {
[unowned self] result in
switch result {
case .success(let _):
print("like deleted")
DispatchQueue.main.async {
let newCounter =
Int(self.likeCounter.text!)! - 1
self.likeCounter.text = String(newCounter)
self.likeState.tintColor = .white
self.likeState.setImage(UIImage(systemName: "heart"), for: .normal)
self.post!.liked = false
}
case .failure(let err):
print(err.localizedDescription)
}
}
}
func updateView() {
imageView.image = nil
guard let post = post else { return }
Expand All @@ -33,9 +84,18 @@ 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
if post.liked!{
self.likeState.tintColor = .red
self.likeState.setImage(UIImage(systemName: "heart.fill"), for: .normal)
}else{
self.likeState.tintColor = .white
self.likeState.setImage(UIImage(systemName: "heart"), for: .normal)
}
}

}
35 changes: 22 additions & 13 deletions Secretly/Views/PostCollectionViewCell.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<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 Down Expand Up @@ -30,14 +30,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,34 +54,51 @@
<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="0lh-5R-KgJ">
<rect key="frame" x="10" y="275" width="21" height="22"/>
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<state key="normal" image="heart" catalog="system"/>
<connections>
<action selector="onLikeClicked:" destination="gTV-IL-0wX" eventType="touchUpInside" id="qdf-7H-40a"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="SR4-kG-ocT">
<rect key="frame" x="34" y="278" width="5" 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="0lh-5R-KgJ" firstAttribute="leading" secondItem="gTV-IL-0wX" secondAttribute="leading" constant="10" id="2kY-cH-WU4"/>
<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 firstAttribute="bottom" secondItem="SR4-kG-ocT" secondAttribute="bottom" constant="10" id="Cn6-gU-63n"/>
<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 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="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"/>
<constraint firstAttribute="bottom" secondItem="LcI-IM-E3b" secondAttribute="bottom" id="ic7-9y-GCI"/>
<constraint firstItem="SR4-kG-ocT" firstAttribute="leading" secondItem="0lh-5R-KgJ" secondAttribute="trailing" constant="3" id="lE8-5g-caM"/>
<constraint firstItem="gUg-yt-vKP" firstAttribute="top" secondItem="gTV-IL-0wX" secondAttribute="top" id="mch-9a-c32"/>
<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="0lh-5R-KgJ" secondAttribute="bottom" constant="3" id="sVZ-GL-XJF"/>
<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="SR4-kG-ocT" id="F7H-t7-THc"/>
<outlet property="likeState" destination="0lh-5R-KgJ" id="gjd-Q9-YTP"/>
</connections>
<point key="canvasLocation" x="389.85507246376812" y="182.8125"/>
</collectionViewCell>
Expand Down