Skip to content

Commit

Permalink
Allow only admin to delete group
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-amisha-i committed Apr 5, 2024
1 parent f5310d8 commit 9f2c972
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
20 changes: 14 additions & 6 deletions Data/Data/Helper/Firebase/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class StorageManager: ObservableObject {

let metadata = StorageMetadata()
metadata.contentType = "image/jpg"
var cancellable: StorageUploadTask?

return Future { promise in
storageRef.putData(imageData, metadata: metadata) { _, error in
cancellable = storageRef.putData(imageData, metadata: metadata) { _, error in
if let error {
LogE("StorageManager: Error while uploading file: \(error.localizedDescription)")
promise(.failure(.databaseError))
Expand All @@ -45,20 +46,27 @@ public class StorageManager: ObservableObject {
} else if let imageUrl = url?.absoluteString {
LogD("StorageManager: Image successfully uploaded to Firebase!")
promise(.success(imageUrl))
} else {
promise(.failure(.unexpectedError))
}
}
}
}
}
.handleEvents(receiveCancel: {
cancellable?.cancel()
})
.eraseToAnyPublisher()
}

public func updateImage(for type: ImageStoreType, id: String, url: String, imageData: Data) -> AnyPublisher<String, ServiceError> {
self.deleteImage(imageUrl: url)
.flatMap { _ in
self.uploadImage(for: type, id: id, imageData: imageData)
}
.eraseToAnyPublisher()
deleteImage(imageUrl: url).catch { error -> AnyPublisher<Void, ServiceError> in
return Fail(error: error).eraseToAnyPublisher()
}
.flatMap { _ in
self.uploadImage(for: type, id: id, imageData: imageData)
}
.eraseToAnyPublisher()
}

public func deleteImage(imageUrl: String) -> AnyPublisher<Void, ServiceError> {
Expand Down
5 changes: 1 addition & 4 deletions Data/Data/Repository/ShareCodeRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ public class ShareCodeRepository: ObservableObject {
guard let self else { promise(.failure(.unexpectedError)); return }
self.fetchSharedCode(code: code)
.sink { result in
switch result {
case .failure(let error):
if case .failure(let error) = result {
promise(.failure(error))
case .finished:
promise(.success(true))
}
} receiveValue: { code in
promise(.success(code == nil))
Expand Down
2 changes: 1 addition & 1 deletion Data/Data/Store/UserStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class UserStore: ObservableObject {
}

func updateUser(user: AppUser) -> AnyPublisher<Void, ServiceError> {
Future { [weak self] promise in
Future<Void, ServiceError> { [weak self] promise in
guard let self else {
promise(.failure(.unexpectedError))
return
Expand Down
10 changes: 7 additions & 3 deletions Splito/UI/Home/Groups/Group/Group Setting/GroupSettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct GroupSettingView: View {
viewModel.handleMemberTap(member: user)
}

GroupAdvanceSettingsView(onLeaveGroupTap: viewModel.handleLeaveGroupTap,
GroupAdvanceSettingsView(isDisable: !viewModel.isAdmin,
onLeaveGroupTap: viewModel.handleLeaveGroupTap,
onDeleteGroupTap: viewModel.handleDeleteGroupTap)
}
}
Expand Down Expand Up @@ -115,6 +116,7 @@ private struct GroupMembersView: View {

private struct GroupAdvanceSettingsView: View {

var isDisable: Bool
var onLeaveGroupTap: () -> Void
var onDeleteGroupTap: () -> Void

Expand All @@ -127,7 +129,7 @@ private struct GroupAdvanceSettingsView: View {
GroupListEditCellView(icon: "arrow.left.square", text: "Leave group",
isDistructive: true, onTap: onLeaveGroupTap)

GroupListEditCellView(icon: "trash", text: "Delete group",
GroupListEditCellView(icon: "trash", text: "Delete group", isDisable: isDisable,
isDistructive: true, onTap: onDeleteGroupTap)
}
.padding(.horizontal, 22)
Expand All @@ -138,6 +140,7 @@ private struct GroupListEditCellView: View {

var icon: String
var text: String
var isDisable: Bool = false
var isDistructive: Bool = false

var onTap: () -> Void
Expand All @@ -153,10 +156,11 @@ private struct GroupListEditCellView: View {
}
.frame(height: 40)
.padding(.leading, 16)
.foregroundColor(isDistructive ? awarenessColor : primaryText)
.foregroundColor(isDisable ? disableText : (isDistructive ? awarenessColor : primaryText))
.onTouchGesture {
onTap()
}
.disabled(isDisable)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GroupSettingViewModel: BaseViewModel, ObservableObject {
private let groupId: String
private let router: Router<AppRoute>

@Published var isAdmin = false
@Published var showLeaveGroupDialog = false
@Published var showRemoveMemberDialog = false

Expand Down Expand Up @@ -50,7 +51,6 @@ class GroupSettingViewModel: BaseViewModel, ObservableObject {
}

func fetchGroupMembers() {
currentViewState = .loading
groupRepository.fetchMembersBy(groupId: groupId)
.sink { [weak self] completion in
if case .failure(let error) = completion {
Expand All @@ -59,10 +59,16 @@ class GroupSettingViewModel: BaseViewModel, ObservableObject {
} receiveValue: { [weak self] users in
guard let self else { return }
self.members = users
self.checkForGroupAdmin()
self.currentViewState = .initial
}.store(in: &cancelable)
}

func checkForGroupAdmin() {
guard let userId = preference.user?.id, let group else { return }
isAdmin = userId == group.createdBy
}

// MARK: - User Actions

func handleEditGroupTap() {
Expand Down

0 comments on commit 9f2c972

Please sign in to comment.