-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMyProfilePageVC+Actions.swift
208 lines (186 loc) · 8.03 KB
/
MyProfilePageVC+Actions.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// MyProfilePageVC+Actions.swift
// Commun
//
// Created by Chung Tran on 10/29/19.
// Copyright © 2019 Commun Limited. All rights reserved.
//
import Foundation
import RxSwift
import SwiftyGif
extension MyProfilePageVC {
@objc func changeCoverBtnDidTouch(_ sender: Any) {
openActionSheet(cover: true)
}
@objc func changeAvatarBtnDidTouch(_ sender: Any) {
openActionSheet(cover: false)
}
@objc func walletDidTouch() {
let state = (viewModel as! MyProfilePageViewModel).balancesVM.state.value
switch state {
case .error:
(viewModel as! MyProfilePageViewModel).balancesVM.reload()
case .listEnded, .loading(false):
let walletVC = CommunWalletVC()
show(walletVC, sender: nil)
default:
break
}
}
// MARK: - Covers + Avatar
func openActionSheet(cover: Bool) {
showCMActionSheet(
title: "change \(cover ? "cover photo": "avatar")".localized().uppercaseFirst,
actions: [
.default(
title: "choose from gallery".localized().uppercaseFirst,
iconName: "photo_solid",
handle: {[unowned self] in
cover ? self.onUpdateCover() : self.onUpdateAvatar()
}),
.default(
title: "remove current \(cover ? "cover photo" : "avatar")".localized().uppercaseFirst,
iconName: "delete",
tintColor: .appRedColor,
handle: {[unowned self] in
cover ? self.onUpdateCover(delete: true) : self.onUpdateAvatar(delete: true)
}
)
])
}
func onUpdateCover(delete: Bool = false) {
// Save originalImage for reverse when update failed
let originImageUrl = ResponseAPIContentGetProfile.current?.coverUrl
// If deleting
if delete {
coverImageView.image = .placeholder
ResponseAPIContentGetProfile.updateCurrentUserProfile(coverUrl: "")
BlockchainManager.instance.updateProfile(params: ["cover_url": ""])
.subscribe(onError: {[weak self] (error) in
ResponseAPIContentGetProfile.updateCurrentUserProfile(coverUrl: originImageUrl)
self?.showError(error)
})
.disposed(by: disposeBag)
return
}
// If updating
showCoverImagePicker(joinedDateString: self.viewModel.profile.value?.registration?.time) { (image) in
self.coverImageView.image = image
self.coverImageView.showLoading(cover: false, spinnerColor: .appWhiteColor)
RestAPIManager.instance.uploadImage(image)
.flatMap { url -> Single<String> in
BlockchainManager.instance.updateProfile(params: ["cover_url": url]).andThen(.just(url))
}
.subscribe(onSuccess: { [weak self] (url) in
self?.coverImageView.hideLoading()
ResponseAPIContentGetProfile.updateCurrentUserProfile(coverUrl: url)
}, onError: { [weak self] (error) in
self?.coverImageView.hideLoading()
ResponseAPIContentGetProfile.updateCurrentUserProfile(coverUrl: originImageUrl)
self?.showError(error)
})
.disposed(by: self.disposeBag)
}
}
func onUpdateAvatar(delete: Bool = false) {
// Save image for reversing when update failed
let originImageUrl = ResponseAPIContentGetProfile.current?.avatarUrl
// On deleting
if delete {
headerView.avatarImageView.image = UIImage(named: "empty-avatar")
ResponseAPIContentGetProfile.updateCurrentUserProfile(avatarUrl: "")
BlockchainManager.instance.updateProfile(params: ["avatar_url": ""])
.subscribe(onError: {[weak self] error in
ResponseAPIContentGetProfile.updateCurrentUserProfile(avatarUrl: originImageUrl)
self?.showError(error)
})
.disposed(by: disposeBag)
return
}
// On updating
let chooseAvatarVC = ProfileChooseAvatarVC.fromStoryboard("ProfileChooseAvatarVC", withIdentifier: "ProfileChooseAvatarVC")
self.present(chooseAvatarVC, animated: true, completion: nil)
return chooseAvatarVC.viewModel.didSelectImage
.filter {$0 != nil}
.map {$0!}
// Upload image
.flatMap { image -> Single<String> in
self.headerView.avatarImageView.showLoading(cover: false, spinnerColor: .appWhiteColor)
self.headerView.avatarImageView.image = image
return RestAPIManager.instance.uploadImage(image)
}
// Save to db
.flatMap {BlockchainManager.instance.updateProfile(params: ["avatar_url": $0]).andThen(Single<String>.just($0))}
// Catch error and reverse image
.subscribe(onNext: { [weak self] (url) in
self?.headerView.avatarImageView.hideLoading()
ResponseAPIContentGetProfile.updateCurrentUserProfile(avatarUrl: url)
}, onError: { [weak self] (error) in
self?.headerView.avatarImageView.hideLoading()
ResponseAPIContentGetProfile.updateCurrentUserProfile(avatarUrl: originImageUrl)
self?.showError(error)
})
.disposed(by: disposeBag)
}
// MARK: - Biography
@objc func addBioButtonDidTouch(_ sender: Any) {
self.onUpdateBio(new: true)
}
@objc func bioLabelDidTouch(_ sender: Any) {
showCMActionSheet(
title: "\("change".localized().uppercaseFirst) \("profile description".localized())",
actions: [
.default(
title: "edit".localized().uppercaseFirst,
iconName: "edit",
handle: {
self.onUpdateBio()
}
),
.default(
title: "delete".localized().uppercaseFirst,
iconName: "delete",
tintColor: .appRedColor,
handle: {
self.onUpdateBio(delete: true)
}
)
]
)
}
func onUpdateBio(new: Bool = false, delete: Bool = false) {
// Save original bio for reversing
let originalBio = headerView.descriptionLabel.text
// On deleting
if delete {
headerView.descriptionLabel.text = nil
BlockchainManager.instance.updateProfile(params: ["biography": ""])
.subscribe(onCompleted: {
ResponseAPIContentGetProfile.updateCurrentUserProfile(bio: "")
}, onError: {[weak self] error in
self?.showError(error)
self?.headerView.descriptionLabel.text = originalBio
})
.disposed(by: disposeBag)
return
}
let editBioVC = MyProfileEditBioVC()
if !new {
editBioVC.bio = headerView.descriptionLabel.text
}
self.present(editBioVC, animated: true, completion: nil)
editBioVC.didConfirm
.flatMap {bio -> Single<String> in
self.headerView.descriptionLabel.text = bio
return BlockchainManager.instance.updateProfile(params: ["biography": bio])
.andThen(.just(bio))
}
.subscribe(onNext: { (bio) in
ResponseAPIContentGetProfile.updateCurrentUserProfile(bio: bio)
}, onError: {[weak self] (error) in
self?.headerView.descriptionLabel.text = originalBio
self?.showError(error)
})
.disposed(by: disposeBag)
}
}