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 pathPostInputViewController.swift
More file actions
167 lines (144 loc) · 5.09 KB
/
PostInputViewController.swift
File metadata and controls
167 lines (144 loc) · 5.09 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// PostInputViewController.swift
// Secretly
//
// Created by Luis Ezcurdia on 19/06/21.
// Copyright © 2021 3zcurdia. All rights reserved.
//
import UIKit
import CoreLocation
struct EmptyPostError: Error {}
protocol PostInputViewDelegate {
func didCreatePost(post: Post?)
}
class PostInputViewController: UIViewController, UINavigationControllerDelegate {
let createPostService = CreatePostService()
@IBOutlet weak var contentTxt: UITextField!
@IBOutlet weak var imgPickerButton: UIButton!
@IBOutlet weak var colorPickerButton: UIButton!
@IBOutlet weak var previewPost: PreviewPostView!
var source: Post?
var delegate: PostInputViewDelegate?
let locationManager = CLLocationManager()
var currentLocation: CLLocationCoordinate2D?
let imgPicker: UIImagePickerController = {
let view = UIImagePickerController()
view.mediaTypes = ["public.image"]
return view
}()
let colorPicker = UIColorPickerViewController()
override func viewDidLoad() {
super.viewDidLoad()
imgPickerButton.layer.cornerRadius = 8
colorPickerButton.layer.cornerRadius = 8
colorPicker.delegate = self
imgPicker.delegate = self
contentTxt.delegate = self
enableBasicLocationServices()
}
func enableBasicLocationServices() {
locationManager.delegate = self
switch locationManager.authorizationStatus {
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted, .denied:
print("Disable location features")
case .authorizedWhenInUse, .authorizedAlways:
print("Enable location features")
@unknown default:
fatalError()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
locationManager.startUpdatingLocation()
}
override func viewWillDisappear(_ animated: Bool) {
locationManager.stopUpdatingLocation()
super.viewWillDisappear(animated)
}
@IBAction
func didTapPublish(_ sender: Any) {
do {
try self.createPost()
self.dismiss(animated: true)
} catch let err {
self.errorAlert(err)
}
}
@IBAction
func didTapCancel(_ sender: Any) {
self.dismiss(animated: true)
}
@IBAction
func didTapImagePicker(_ sender: Any) {
present(imageSourceSelectAlert, animated: true)
}
private lazy var imageSourceSelectAlert: UIAlertController = {
let alert = UIAlertController(title: "Select source", message: "Select the image source for your post.", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: openCamera)
let libraryAction = UIAlertAction(title: "Library", style: .default, handler: openLibrary)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alert.addAction(cameraAction)
alert.addAction(libraryAction)
alert.addAction(cancelAction)
return alert
}()
private func openCamera(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
self.imgPicker.sourceType = .camera
} else {
self.imgPicker.sourceType = .photoLibrary
}
self.present(imgPicker, animated: true)
}
private func openLibrary(_ sender: Any) {
self.imgPicker.sourceType = .savedPhotosAlbum
self.present(imgPicker, animated: true)
}
@IBAction
func didTapColorPicker(_ sender: Any) {
colorPicker.selectedColor = previewPost.backgroundColor ?? .clear
present(colorPicker, animated: true)
}
private func createPost() throws {
let post = try self.buildPost()
createPostService.create(post) { [unowned self] result in
switch result {
case .success(let post):
delegate?.didCreatePost(post: post)
case .failure(let err):
self.errorAlert(err)
}
}
}
private func buildPost() throws -> Post {
guard let postText = contentTxt.text else {
throw EmptyPostError()
}
var post = Post(
content: postText,
backgroundColor: previewPost.backgroundColor?.hexString ?? "#3366CC",
latitude: currentLocation?.latitude,
longitude: currentLocation?.longitude,
likesCount: 0,
liked:false
)
if let uimage = previewPost.image {
post.imageData = uimage.encodeBase64()
}
return post
}
func errorAlert(_ error: Error) {
let err = error as? Titleable
let alert = UIAlertController(title: (err?.title ?? "Server Error"), message: error.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
public func clear() {
source = nil
contentTxt?.text = ""
previewPost?.clear()
}
}