This repository was archived by the owner on Sep 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCreatePostViewController.swift
More file actions
59 lines (50 loc) · 1.88 KB
/
CreatePostViewController.swift
File metadata and controls
59 lines (50 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// CreatePostViewController.swift
// Secretly
//
// Created by Luis Ezcurdia on 05/06/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//
import UIKit
class CreatePostViewController: UIViewController {
@IBOutlet weak var contentField: UITextField!
@IBOutlet weak var colorField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
colorField.text = "#3366CC"
}
@IBAction
func createPost(_ sender: Any?) {
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 {
try postsEndpoint.create(model: post) { [unowned self] result in
switch result {
case .success(let post):
print("there is a new post \(post?.id ?? 0)")
case .failure(let err):
DispatchQueue.main.async {
self.errorAlert(err)
}
}
}
} catch let err {
self.errorAlert(err)
}
}
func errorAlert(_ error: Error) {
let err = error as? Titleable
let alert = UIAlertController(title: (err?.title ?? "Error"), message: error.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}