-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserViewController.swift
140 lines (122 loc) · 4.88 KB
/
UserViewController.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
//
// UserViewController.swift
// Pluto-Example
//
// Created by Meng Li on 2019/8/10.
// Copyright © 2019 MuShare. All rights reserved.
//
import PlutoSDK
import Kingfisher
class UserViewController: UIViewController {
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
private lazy var imagePickerController: UIImagePickerController = {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.navigationBar.tintColor = .white
imagePickerController.navigationBar.titleTextAttributes = [
.foregroundColor : UIColor.white
]
return imagePickerController
}()
override func viewDidLoad() {
super.viewDidLoad()
Pluto.shared.myInfo(success: { [weak self] in
self?.set(user: $0)
}, error: {
print("Error loading user info: \($0)")
})
}
@IBAction func refresh(_ sender: Any) {
Pluto.shared.getAccessToken(isForceRefresh: true) { [weak self] in
self?.showAlert(title: "token", content: $0 ?? "")
}
}
@IBAction func showScopes(_ sender: Any) {
Pluto.shared.getScopes { [weak self] in
self?.showAlert(title: "scopes", content: $0.description)
}
}
private func set(user: PlutoUser) {
avatarImageView.kf.setImage(with: URL(string: user.avatar))
nameLabel.text = user.name
}
@IBAction func uploadAvatar(_ sender: Any) {
let alertController = UIAlertController(
title: "Update Avatar",
message: nil,
preferredStyle: .actionSheet
)
let takePhoto = UIAlertAction(title: "Take Photo", style: .default) { action in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
self.imagePickerController.sourceType = .camera
self.imagePickerController.cameraCaptureMode = .photo
self.imagePickerController.cameraDevice = .front
self.imagePickerController.allowsEditing = true
}
self.present(self.imagePickerController, animated: true)
}
let choosePhoto = UIAlertAction(title: "Select from Library", style: .default) { action in
self.imagePickerController.sourceType = .photoLibrary
self.imagePickerController.allowsEditing = true
self.present(self.imagePickerController, animated: true)
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(takePhoto)
alertController.addAction(choosePhoto)
alertController.addAction(cancel)
alertController.popoverPresentationController?.sourceView = avatarImageView
alertController.popoverPresentationController?.sourceRect = avatarImageView.bounds
present(alertController, animated: true)
}
@IBAction func updateName(_ sender: Any) {
let alertController = UIAlertController(
title: "Update user name",
message: nil,
preferredStyle: .alert
)
alertController.addTextField {
$0.textAlignment = .center
}
let addAction = UIAlertAction(title: "Submit", style: .default) { [unowned self] _ in
guard let name = alertController.textFields?[0].text else {
return
}
Pluto.shared.updateName(name: name, success: { [weak self] in
self?.nameLabel.text = name
}, error: { [weak self] in
switch $0 {
case .userIdExist:
self?.showAlert(title: "Error", content: "User ID already exists")
default:
print("Error updating user name: \($0)")
}
})
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(addAction)
alertController.addAction(cancelAction)
present(alertController, animated: true)
}
@IBAction func deleteAccount(_ sender: Any) {
Pluto.shared.deleteAccount(success: { [unowned self] in
self.dismiss(animated: true)
}, error: { error in
print(error)
})
}
}
extension UserViewController: UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else {
return
}
picker.dismiss(animated: true, completion: nil)
Pluto.shared.uploadAvatar(image: image, success: { [weak self] in
self?.avatarImageView.image = image
}, error: {
print("Error uploading avatar: \($0)")
})
}
}
extension UserViewController: UINavigationControllerDelegate {}