-
Notifications
You must be signed in to change notification settings - Fork 7
/
TabThree+ProfilePhoto.swift
150 lines (107 loc) · 4.84 KB
/
TabThree+ProfilePhoto.swift
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
//
// TabThreeHandler.swift
// DUPME
//
// Created by Hossein on 15/01/2017.
// Copyright © 2017 Dupify. All rights reserved.
//
import UIKit
import Firebase
import LBTAComponents
extension TabThreeViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
profileImageView.image = selectedImage
uploadProfileImageToDatabase()
}
dismiss(animated: true, completion: nil)
}
private func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func handleSelectProfileImageView(){
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
let alert = UIAlertController(title: "Profile Photo", message: nil, preferredStyle: .actionSheet)
let takePhotoAction = UIAlertAction(title: "Take Photo", style: .default, handler: {
action in
picker.sourceType = .camera
self.present(picker, animated: true, completion: nil)
})
let chooseFromGalleyAction = UIAlertAction(title: "Choose from Gallery", style: .default, handler: {
action in
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
})
let removePhotoAction = UIAlertAction(title: "Remove Current Photo", style: .default, handler: {
action in
alert.view.tintColor = .red
self.profileImageView.image = nil
self.removeProfileImageFromDatabase()
})
removePhotoAction.setValue(UIColor.red, forKey: "titleTextColor")
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(takePhotoAction)
alert.addAction(chooseFromGalleyAction)
if profileImageView.image != nil {
alert.addAction(removePhotoAction)
}
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
func loadProfileImageFromDatabase(){
let userID = FIRAuth.auth()!.currentUser!.uid
let ref = databaseRef.child("users/\(userID)")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
guard let imageURl = value?["profileImageURL"] else { return }
self.profileImageView.loadImage(urlString: imageURl as! String)
})
}
func loadUserDetailsFromDatabase(){
let userID = FIRAuth.auth()!.currentUser!.uid
let ref = databaseRef.child("users/\(userID)")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let userName = value?["name"] as? String
let email = value?["email"] as? String
self.userNameLable.text = userName
self.userEmailLable.text = email
})
}
private func removeProfileImageFromDatabase(){
let userID = FIRAuth.auth()!.currentUser!.uid
let ref = storageRef.child("Profile Images").child("\(userID).jpg")
ref.delete { (error) in
if error != nil {
print(error!)
return
}
let ref = self.databaseRef.child("users/\(userID)/profileImageURL")
ref.removeValue()
}
}
private func uploadProfileImageToDatabase(){
let userID = FIRAuth.auth()!.currentUser!.uid
let ref = storageRef.child("Profile Images").child("\(userID).jpg")
let myImage = UIImageJPEGRepresentation(self.profileImageView.image!, 0.8)
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
ref.put(myImage!, metadata: metadata, completion: { (metadata, error) in
if error != nil {
print(error!)
}
if let profileImageURL = metadata?.downloadURL()?.absoluteString {
let ref = self.databaseRef.child("users/\(userID)/profileImageURL")
ref.setValue(profileImageURL)
}
})
}
}