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
20 changes: 18 additions & 2 deletions Secretly.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
0409C25B26981ADA008301E0 /* UpdatePostService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0409C25A26981ADA008301E0 /* UpdatePostService.swift */; };
0409C25D26981AE9008301E0 /* CreateLikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0409C25C26981AE9008301E0 /* CreateLikeService.swift */; };
0409C25F26981AF9008301E0 /* DeleteLikeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0409C25E26981AF9008301E0 /* DeleteLikeService.swift */; };
0409C26126981B15008301E0 /* Like.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0409C26026981B15008301E0 /* Like.swift */; };
302B5845267E658E007133E6 /* HttpResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 302B583D267E658E007133E6 /* HttpResponse.swift */; };
302B5846267E658E007133E6 /* AmacaConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 302B583E267E658E007133E6 /* AmacaConfig.swift */; };
302B5847267E658E007133E6 /* StatusCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 302B583F267E658E007133E6 /* StatusCode.swift */; };
Expand Down Expand Up @@ -76,6 +80,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0409C25A26981ADA008301E0 /* UpdatePostService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdatePostService.swift; sourceTree = "<group>"; };
0409C25C26981AE9008301E0 /* CreateLikeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateLikeService.swift; sourceTree = "<group>"; };
0409C25E26981AF9008301E0 /* DeleteLikeService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteLikeService.swift; sourceTree = "<group>"; };
0409C26026981B15008301E0 /* Like.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Like.swift; sourceTree = "<group>"; };
302B583D267E658E007133E6 /* HttpResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpResponse.swift; sourceTree = "<group>"; };
302B583E267E658E007133E6 /* AmacaConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AmacaConfig.swift; sourceTree = "<group>"; };
302B583F267E658E007133E6 /* StatusCode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusCode.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +230,9 @@
30C77CAF266AD69700A888DC /* CurrentUserService.swift */,
304E06C726742BDA00A99128 /* CreatePostService.swift */,
304E06C926742CC500A99128 /* FeedService.swift */,
0409C25A26981ADA008301E0 /* UpdatePostService.swift */,
0409C25C26981AE9008301E0 /* CreateLikeService.swift */,
0409C25E26981AF9008301E0 /* DeleteLikeService.swift */,
);
path = Services;
sourceTree = "<group>";
Expand All @@ -248,6 +259,7 @@
307A30572661AD540020DF8B /* User.swift */,
30C77CB3266AF47300A888DC /* Credentials.swift */,
30C77CB5266AF48300A888DC /* CurrentUser.swift */,
0409C26026981B15008301E0 /* Like.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -446,6 +458,7 @@
302B5846267E658E007133E6 /* AmacaConfig.swift in Sources */,
302BB61C267D7CC800FD74F5 /* PreviewPostVIew.swift in Sources */,
3033795D267537B40066D94A /* FeedCollectionViewController+UICollectionViewDelegateFlowLayout .swift in Sources */,
0409C26126981B15008301E0 /* Like.swift in Sources */,
30BC8BA82662CEBA00F7E6A5 /* Checksum.swift in Sources */,
30FD0E722659645A006E309A /* Faker.swift in Sources */,
30B9B939268CA989007B1942 /* Nudity.mlmodel in Sources */,
Expand All @@ -454,10 +467,12 @@
302BB626267E447900FD74F5 /* PostInputViewController+UITextFieldDelegate.swift in Sources */,
30BC8BA02662B8A700F7E6A5 /* StorageType.swift in Sources */,
307A305B2661B7A20020DF8B /* FeedCollectionViewController.swift in Sources */,
0409C25B26981ADA008301E0 /* UpdatePostService.swift in Sources */,
304E06CC2674442800A99128 /* UIImage+encodeBase64.swift in Sources */,
30BC8BA62662C02300F7E6A5 /* CacheImage.swift in Sources */,
302B5847267E658E007133E6 /* StatusCode.swift in Sources */,
302B584B267E658E007133E6 /* RequestBuilder.swift in Sources */,
0409C25F26981AF9008301E0 /* DeleteLikeService.swift in Sources */,
307A306526629B990020DF8B /* AuthorView.swift in Sources */,
30C77CB4266AF47300A888DC /* Credentials.swift in Sources */,
30BC8BA22662BB0000F7E6A5 /* DataContainer.swift in Sources */,
Expand All @@ -467,6 +482,7 @@
302BB624267E3A8700FD74F5 /* PostInputViewController+UIColorPickerViewControllerDelegate.swift in Sources */,
304E06CF267468DA00A99128 /* UIColor+Pastel.swift in Sources */,
304E06C42674133D00A99128 /* String+isBlank.swift in Sources */,
0409C25D26981AE9008301E0 /* CreateLikeService.swift in Sources */,
30BC8BA42662BDEF00F7E6A5 /* ImageStore.swift in Sources */,
3033795B267537490066D94A /* FeedCollectionViewController+UICollectionViewDataSourcePrefetching.swift in Sources */,
30B9B93B268CA9E6007B1942 /* UIImage+PixelBuffer.swift in Sources */,
Expand Down Expand Up @@ -635,13 +651,13 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = D3XL2U7DQC;
DEVELOPMENT_TEAM = KE5G7XBPRL;
INFOPLIST_FILE = Secretly/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = mx.unam.ioslab.Secretly;
PRODUCT_BUNDLE_IDENTIFIER = Toun.Secretly;
PRODUCT_NAME = Secretly;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 1;
Expand Down
12 changes: 6 additions & 6 deletions Secretly/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPhotoLibraryUsageDescription</key>
<string>To add some context to your posts </string>
<key>NSCameraUsageDescription</key>
<string>To add some context to your posts</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>To georeference posts within a region </string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand All @@ -26,6 +20,12 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSCameraUsageDescription</key>
<string>To add some context to your posts</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>To georeference posts within a region </string>
<key>NSPhotoLibraryUsageDescription</key>
<string>To add some context to your posts </string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
21 changes: 21 additions & 0 deletions Secretly/Models/Like.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Like.swift
// Secretly
//
// Created by Antonio Lara Navarrete on 09/07/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation
struct Like:Restable {
let id: Int?
let createdAt: Date?
let updatedAt: Date?
init(id:Int?,createdAt:Date?,updatedAt:Date?) {
self.id=id
self.createdAt=createdAt
self.updatedAt=updatedAt
}


}
7 changes: 6 additions & 1 deletion Secretly/Models/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ struct Post: Restable {
let longitude: Double?
let createdAt: Date?
let updatedAt: Date?
let likesCount:Int?
let 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,likesCount:Int?,liked:Bool?) {
self.content = content
self.backgroundColor = backgroundColor
self.id = nil
Expand All @@ -34,6 +37,8 @@ struct Post: Restable {
self.commentsCount = nil
self.createdAt = nil
self.updatedAt = nil
self.likesCount = nil
self.liked = nil
}

func encode(to encoder: Encoder) throws {
Expand Down
19 changes: 4 additions & 15 deletions Secretly/Network/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ struct HttpClient {
}

private func request(method: String, path: String, body: Data?, complete: @escaping ResultResponse) {
guard let req = buildRequest(method: method, path: path, body: body) else {
complete(.failure(RequestError.invalidRequest))
return
}
guard let req = RequestBuilder.build(baseUrl: self.baseUrl, method: method, path: path, body: body) else {
complete(.failure(RequestError.invalidRequest))
return
}

session.dataTask(with: req) { (data, response, error) in
if let error = error {
Expand All @@ -46,15 +46,4 @@ struct HttpClient {
complete(result)
}.resume()
}

private func buildRequest(method: String, path: String, body: Data?) -> URLRequest? {
var builder = RequestBuilder(baseUrl: self.baseUrl)
builder.method = method
builder.path = path
builder.body = body
if let token = AmacaConfig.shared.apiToken {
builder.headers = ["Authorization": "Bearer \(token)"]
}
return builder.request()
}
}
7 changes: 7 additions & 0 deletions Secretly/Network/HttpResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ struct HttpResponse {
}

func result(for data: Data?) -> Result<Data?, Error> {
#if DEBUG
debugPrint(status)
print("\(self.httpUrlResponse.statusCode) \(httpUrlResponse.url!)")
if let unwrapedData = data, let currentData = String(data: unwrapedData, encoding: .utf8) {
print(currentData)
}
#endif
return status.result().map { _ in data }
}
}
26 changes: 23 additions & 3 deletions Secretly/Network/RequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@
//

import Foundation

struct RequestBuilder {
struct RequestBuilder: CustomDebugStringConvertible {
static func build(baseUrl: String, method: String, path: String, body: Data?) -> URLRequest? {
var builder = RequestBuilder(baseUrl: baseUrl)
builder.method = method
builder.path = path
builder.body = body
if let token = AmacaConfig.shared.apiToken {
builder.headers = ["Authorization": "Bearer \(token)"]
}
#if DEBUG
debugPrint(builder)
#endif
return builder.request()
}
enum ContentMode {
case jsonApp

Expand All @@ -33,7 +45,15 @@ struct RequestBuilder {
public var body: Data?
public var headers: [String: String]?
public var contentMode: ContentMode = .jsonApp

var debugDescription: String {
let currentUrl: String = url()?.debugDescription ?? "Invalid url"
let currentHeaders: String = headers?.debugDescription ?? ""
if let unwrapedbody = body, let currentBody = String(data: unwrapedbody, encoding: .utf8) {
return "\(method.uppercased()) \(currentUrl) -H \(currentHeaders) -d \(currentBody)"
} else {
return "\(method.uppercased()) \(currentUrl) -H \(currentHeaders)"
}
}
init(baseUrl: String) {
self.urlComponents = URLComponents(string: baseUrl)!
}
Expand Down
4 changes: 2 additions & 2 deletions Secretly/Network/RestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ struct RestClient<T: Restable> {

func update(model: T, complete: @escaping (Result<T?, Error>) -> 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(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
22 changes: 22 additions & 0 deletions Secretly/Services/CreateLikeService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// CreateLikeService.swift
// Secretly
//
// Created by Antonio Lara Navarrete on 09/07/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation

struct CreateLikeService {
private var endpoint: RestClient<Like>
init(id:Int) {
endpoint = RestClient<Like>(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(id)/likes")
}

func create(_ model: Like, complete: @escaping (Result<Like?, Error>) -> Void ) {
try? endpoint.create(model: model) { result in
DispatchQueue.main.async { complete(result) }
}
}
}
21 changes: 21 additions & 0 deletions Secretly/Services/DeleteLikeService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DeleteLikeService.swift
// Secretly
//
// Created by Antonio Lara Navarrete on 09/07/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//
import Foundation

struct DeleteLikeService {
private var endpoint: RestClient<Like>
init(id:Int) {
endpoint = RestClient<Like>(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts/\(id)/likes")
}

func delete(_ model: Like, complete: @escaping (Result<Like?, Error>) -> Void ) {
endpoint.delete(model: model) { result in
DispatchQueue.main.async { complete(result) }
}
}
}
23 changes: 23 additions & 0 deletions Secretly/Services/UpdatePostService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// UpdatePostService.swift
// Secretly
//
// Created by Antonio Lara Navarrete on 09/07/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//

import Foundation

struct UpdatePostService {
private var endpoint: RestClient<Post>

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

func update(_ model: Post, complete: @escaping (Result<Post?, Error>) -> Void ) {
try? endpoint.update(model: model) { result in
DispatchQueue.main.async { complete(result) }
}
}
}
2 changes: 1 addition & 1 deletion Secretly/ViewControllers/CreatePostViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!,likesCount: 0,liked:false)
let postsEndpoint = RestClient<Post>(client: AmacaConfig.shared.httpClient, path: "/api/v1/posts")

do {
Expand Down
4 changes: 3 additions & 1 deletion Secretly/ViewControllers/PostInputViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class PostInputViewController: UIViewController, UINavigationControllerDelegate
content: postText,
backgroundColor: previewPost.backgroundColor?.hexString ?? "#3366CC",
latitude: currentLocation?.latitude,
longitude: currentLocation?.longitude
longitude: currentLocation?.longitude,
likesCount: 0,
liked:false
)
if let uimage = previewPost.image {
post.imageData = uimage.encodeBase64()
Expand Down
Loading