Skip to content

Commit 0393970

Browse files
committed
Add deleting profile.
1 parent f1f7662 commit 0393970

File tree

7 files changed

+126
-21
lines changed

7 files changed

+126
-21
lines changed

Sources/SPProfiling/Interface/Profile/CurrentProfileController.swift

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ class CurrentProfileController: NativeProfileController {
6363
})
6464
])
6565

66+
NotificationCenter.default.addObserver(forName: SPProfiling.didChangedAuthState, object: nil, queue: nil) { [weak self] _ in
67+
guard let self = self else { return }
68+
if (ProfileModel.currentProfile?.id != self.profileModel.id) {
69+
self.dismissAnimated()
70+
}
71+
}
72+
6673
NotificationCenter.default.addObserver(forName: SPProfiling.didReloadedProfile, object: nil, queue: nil) { [weak self] _ in
6774
guard let self = self else { return }
6875
if let profileModel = ProfileModel.currentProfile {
@@ -124,22 +131,27 @@ class CurrentProfileController: NativeProfileController {
124131
action: { [weak self] _, indexPath in
125132
guard let self = self else { return }
126133
let sourceView = self.tableView.cellForRow(at: indexPath) ?? UIView()
134+
let cell = self.tableView.cellForRow(at: indexPath) as? SPTableViewCell
135+
cell?.setLoading(true)
136+
127137
UIAlertController.confirm(
128138
title: Texts.Profile.Actions.SignOut.Confirm.title,
129139
description: Texts.Profile.Actions.SignOut.Confirm.description,
130140
actionTitle: Texts.Profile.Actions.SignOut.title,
131141
cancelTitle: Texts.cancel,
132142
desctructive: true,
133-
action: { [weak self] in
143+
completion: { [weak self] confirmed in
134144
guard let self = self else { return }
135-
let cell = self.tableView.cellForRow(at: indexPath) as? SPTableViewCell
136-
cell?.setLoading(true)
137-
ProfileModel.currentProfile?.signOut { error in
138-
if let error = error {
139-
SPAlert.present(message: error.localizedDescription, haptic: .error)
140-
} else {
141-
self.dismissAnimated()
145+
if confirmed {
146+
ProfileModel.currentProfile?.signOut { error in
147+
if let error = error {
148+
SPAlert.present(message: error.localizedDescription, haptic: .error)
149+
} else {
150+
self.dismissAnimated()
151+
}
152+
cell?.setLoading(false)
142153
}
154+
} else {
143155
cell?.setLoading(false)
144156
}
145157
},
@@ -163,21 +175,25 @@ class CurrentProfileController: NativeProfileController {
163175
action: { [weak self] _, indexPath in
164176
guard let self = self else { return }
165177
let sourceView = self.tableView.cellForRow(at: indexPath) ?? UIView()
178+
let cell = self.tableView.cellForRow(at: indexPath) as? SPTableViewCell
179+
cell?.setLoading(true)
166180
UIAlertController.confirmDouble(
167181
title: Texts.Profile.Actions.Delete.Confirm.title,
168182
description: Texts.Profile.Actions.Delete.Confirm.description,
169183
actionTitle: Texts.Profile.Actions.Delete.title,
170184
cancelTitle: Texts.cancel,
171185
desctructive: true,
172-
action: { [weak self] in
186+
completion: { [weak self] confirmed in
173187
guard let self = self else { return }
174-
let cell = self.tableView.cellForRow(at: indexPath) as? SPTableViewCell
175-
cell?.setLoading(true)
176-
self.profileModel.delete { error in
177-
cell?.setLoading(false)
178-
if let error = error {
179-
SPAlert.present(message: error.localizedDescription, haptic: .error)
188+
if confirmed {
189+
self.profileModel.delete(on: self) { error in
190+
cell?.setLoading(false)
191+
if let error = error {
192+
SPAlert.present(message: error.localizedDescription, haptic: .error)
193+
}
180194
}
195+
} else {
196+
cell?.setLoading(false)
181197
}
182198
},
183199
sourceView: sourceView,

Sources/SPProfiling/Models/Errors/DeleteProfileError.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,42 @@
2020
// SOFTWARE.
2121

2222
import Foundation
23+
import SPFirebaseFirestore
2324

2425
public enum DeleteProfileError: LocalizedError {
2526

2627
case notEnoughPermissions
28+
case notReachable
29+
case unknown
2730

2831
public var errorDescription: String? {
2932
switch self {
3033
case .notEnoughPermissions: return "notEnoughPermissions"
34+
case .notReachable: return Texts.Error.not_reachable
35+
case .unknown: return Texts.Error.unknown
36+
}
37+
}
38+
39+
public static func convert(_ error: Error) -> EditProfileError {
40+
let error = SPFirebaseFirestoreError.get(by: error)
41+
switch error {
42+
case .OK: return .unknown
43+
case .cancelled: return .notReachable
44+
case .unknown: return .unknown
45+
case .invalidArgument: return .unknown
46+
case .deadlineExceeded: return .unknown
47+
case .notFound: return .unknown
48+
case .alreadyExists: return .unknown
49+
case .permissionDenied: return .notEnoughPermissions
50+
case .resourceExhausted: return .unknown
51+
case .failedPrecondition: return .unknown
52+
case .aborted: return .notReachable
53+
case .outOfRange: return .unknown
54+
case .unimplemented: return .unknown
55+
case .internal: return .unknown
56+
case .unavailable: return .notReachable
57+
case .dataLoss: return .notReachable
58+
case .unauthenticated: return .notEnoughPermissions
3159
}
3260
}
3361
}

Sources/SPProfiling/Models/ProfileAction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public enum ProfileAction {
2525

2626
case getProfile
2727
case editProfile(_ profileModel: ProfileModel)
28+
case deleteProfile(_ profileModel: ProfileModel)
2829
case getProfilveAvatar
2930
case setProfilveAvatar(_ profileModel: ProfileModel)
3031
}

Sources/SPProfiling/ProfileModelExtension.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ extension ProfileModel {
5353
SPProfiling.signOut(completion: completion)
5454
}
5555

56-
#warning("implement delete profile and all data")
57-
public func delete(completion: @escaping (DeleteProfileError?)->Void) {
58-
delay(2) {
59-
completion(.notEnoughPermissions)
60-
}
56+
public func delete(on controller: UIViewController, completion: @escaping (Error?)->Void) {
57+
SPProfiling.deleteProfile(on: controller, completion: completion)
6158
}
6259

6360
// MARK: - Data
@@ -116,6 +113,8 @@ extension ProfileModel {
116113
return nil
117114
case .editProfile(let profileModel):
118115
return self.id == profileModel.id ? nil : EditProfileError.notEnoughPermissions
116+
case .deleteProfile(let profileModel):
117+
return self.id == profileModel.id ? nil : EditProfileError.notEnoughPermissions
119118
case .setProfilveAvatar(let profileModel):
120119
return self.id == profileModel.id ? nil : SetAvatarError.notEnoughPermissions
121120
}

Sources/SPProfiling/SPProfiling.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,33 @@ public class SPProfiling {
9999
}
100100
}
101101

102+
static func deleteProfile(on controller: UIViewController, completion: @escaping (Error?)->Void) {
103+
104+
let deleteAction = {
105+
SPProfiling.Profile.deleteProfile { error in
106+
if let error = error {
107+
completion(error)
108+
} else {
109+
Auth.delete { error in
110+
completion(error)
111+
}
112+
}
113+
}
114+
}
115+
116+
if Auth.isAnonymous ?? true {
117+
deleteAction()
118+
} else {
119+
Auth.signInAppleForConfirm(on: controller) { error in
120+
if let error = error {
121+
completion(error)
122+
} else {
123+
deleteAction()
124+
}
125+
}
126+
}
127+
}
128+
102129
// MARK: - Private
103130

104131
private static func configureDataServices() {

Sources/SPProfiling/Services/Auth.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ class Auth {
8080
})
8181
}
8282

83+
static func delete(completion: @escaping (AuthError?)->Void) {
84+
SPFirebaseAuth.delete { error in
85+
if let error = error {
86+
completion(AuthError.convert(error))
87+
} else {
88+
signInAnonymously() { error in
89+
completion(error)
90+
}
91+
}
92+
}
93+
}
94+
8395
// MARK: - Data
8496

8597
static var userID: String? { SPFirebaseAuth.userID }

Sources/SPProfiling/Services/Profile+Actions.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ extension SPProfiling.Profile {
9898
completion?(error)
9999
return
100100
}
101-
102101
let name = ProfileModel.cleanProfileName(name)
103102
if let error = ProfileModel.middlewareProfileName(name) {
104103
completion?(error)
@@ -188,5 +187,28 @@ extension SPProfiling.Profile {
188187
}
189188
}
190189
}
190+
191+
static func deleteProfile(completion: ((Error?)->Void)?) {
192+
guard let currentProfileModel = currentProfile else {
193+
completion?(DeleteProfileError.notEnoughPermissions)
194+
return
195+
}
196+
if let error = currentProfileModel.middleware(.deleteProfile(currentProfileModel)) {
197+
completion?(error)
198+
return
199+
}
200+
201+
202+
reset()
203+
204+
Firestore.profile_document(userID: currentProfileModel.id).delete { error in
205+
if let error = error {
206+
completion?(DeleteProfileError.convert(error))
207+
} else {
208+
print("document deleted")
209+
completion?(nil)
210+
}
211+
}
212+
}
191213
}
192214

0 commit comments

Comments
 (0)