Skip to content

Commit

Permalink
Remove member and its related classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-amisha-i committed Mar 28, 2024
1 parent ef07391 commit ba1ff01
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 429 deletions.
12 changes: 0 additions & 12 deletions Data/Data.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
D89DBE532B8DC9F700E5F1BD /* AppRoute.swift in Sources */ = {isa = PBXBuildFile; fileRef = D89DBE522B8DC9F700E5F1BD /* AppRoute.swift */; };
D8A7CA752BA5AB670014EC67 /* UserStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A7CA742BA5AB670014EC67 /* UserStore.swift */; };
D8A7CA772BA5AB800014EC67 /* GroupStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A7CA762BA5AB800014EC67 /* GroupStore.swift */; };
D8A7CA792BA5AB920014EC67 /* MemberStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A7CA782BA5AB920014EC67 /* MemberStore.swift */; };
D8A7CA7B2BA5B6AC0014EC67 /* ShareCodeStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A7CA7A2BA5B6AC0014EC67 /* ShareCodeStore.swift */; };
D8A7CA802BA867F80014EC67 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A7CA7F2BA867F80014EC67 /* String+Extension.swift */; };
D8AC25BB2B7F327A00CEAAD3 /* SplitoPreference.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AC25BA2B7F327A00CEAAD3 /* SplitoPreference.swift */; };
Expand All @@ -33,8 +32,6 @@
D8AC25C32B7F390B00CEAAD3 /* AppAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8AC25C22B7F390B00CEAAD3 /* AppAssembly.swift */; };
D8D14A522BA0917D00F45FF2 /* SharedCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D14A512BA0917D00F45FF2 /* SharedCode.swift */; };
D8D14A542BA092F500F45FF2 /* ShareCodeRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D14A532BA092F400F45FF2 /* ShareCodeRepository.swift */; };
D8D14A5A2BA1E6E400F45FF2 /* MemberRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D14A592BA1E6E400F45FF2 /* MemberRepository.swift */; };
D8D14A5C2BA1E73600F45FF2 /* Member.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D14A5B2BA1E73600F45FF2 /* Member.swift */; };
D8D42AA92B872726009B345D /* DDLogProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D42AA82B872726009B345D /* DDLogProvider.swift */; };
D8DF8BBF2B7A315100165138 /* Data.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D8DF8BB62B7A315000165138 /* Data.framework */; };
D8DF8BC42B7A315100165138 /* DataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8DF8BC32B7A315100165138 /* DataTests.swift */; };
Expand Down Expand Up @@ -71,7 +68,6 @@
D89DBE522B8DC9F700E5F1BD /* AppRoute.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppRoute.swift; sourceTree = "<group>"; };
D8A7CA742BA5AB670014EC67 /* UserStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserStore.swift; sourceTree = "<group>"; };
D8A7CA762BA5AB800014EC67 /* GroupStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupStore.swift; sourceTree = "<group>"; };
D8A7CA782BA5AB920014EC67 /* MemberStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemberStore.swift; sourceTree = "<group>"; };
D8A7CA7A2BA5B6AC0014EC67 /* ShareCodeStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareCodeStore.swift; sourceTree = "<group>"; };
D8A7CA7F2BA867F80014EC67 /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
D8AC25BA2B7F327A00CEAAD3 /* SplitoPreference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplitoPreference.swift; sourceTree = "<group>"; };
Expand All @@ -80,8 +76,6 @@
D8AC25C22B7F390B00CEAAD3 /* AppAssembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppAssembly.swift; sourceTree = "<group>"; };
D8D14A512BA0917D00F45FF2 /* SharedCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SharedCode.swift; sourceTree = "<group>"; };
D8D14A532BA092F400F45FF2 /* ShareCodeRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareCodeRepository.swift; sourceTree = "<group>"; };
D8D14A592BA1E6E400F45FF2 /* MemberRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemberRepository.swift; sourceTree = "<group>"; };
D8D14A5B2BA1E73600F45FF2 /* Member.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Member.swift; sourceTree = "<group>"; };
D8D42A9E2B870BBA009B345D /* BaseStyle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = BaseStyle.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D8D42AA82B872726009B345D /* DDLogProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDLogProvider.swift; sourceTree = "<group>"; };
D8DF8BB62B7A315000165138 /* Data.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Data.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -140,7 +134,6 @@
children = (
D89DBE452B8CBE0F00E5F1BD /* UserRepository.swift */,
D83B15082B999789004A5F4F /* GroupRepository.swift */,
D8D14A592BA1E6E400F45FF2 /* MemberRepository.swift */,
D85E86E42BAB088F002EDF76 /* ExpenseRepository.swift */,
D8D14A532BA092F400F45FF2 /* ShareCodeRepository.swift */,
);
Expand All @@ -153,7 +146,6 @@
D89DBE272B88802800E5F1BD /* Country.swift */,
D89DBE472B8CBE4C00E5F1BD /* AppUser.swift */,
D83B15042B9996C0004A5F4F /* Groups.swift */,
D8D14A5B2BA1E73600F45FF2 /* Member.swift */,
D85E86DD2BAB0292002EDF76 /* Expense.swift */,
D8D14A512BA0917D00F45FF2 /* SharedCode.swift */,
);
Expand Down Expand Up @@ -199,7 +191,6 @@
D8AC25BA2B7F327A00CEAAD3 /* SplitoPreference.swift */,
D8A7CA742BA5AB670014EC67 /* UserStore.swift */,
D8A7CA762BA5AB800014EC67 /* GroupStore.swift */,
D8A7CA782BA5AB920014EC67 /* MemberStore.swift */,
D8A7CA7A2BA5B6AC0014EC67 /* ShareCodeStore.swift */,
);
path = Store;
Expand Down Expand Up @@ -480,16 +471,13 @@
D8A7CA802BA867F80014EC67 /* String+Extension.swift in Sources */,
D89DBE282B88802800E5F1BD /* Country.swift in Sources */,
D83B15092B999789004A5F4F /* GroupRepository.swift in Sources */,
D8D14A5A2BA1E6E400F45FF2 /* MemberRepository.swift in Sources */,
D89DBE532B8DC9F700E5F1BD /* AppRoute.swift in Sources */,
D8A7CA752BA5AB670014EC67 /* UserStore.swift in Sources */,
D89DBE402B8C9E7400E5F1BD /* RouterView.swift in Sources */,
D85E86E52BAB088F002EDF76 /* ExpenseRepository.swift in Sources */,
D8A7CA772BA5AB800014EC67 /* GroupStore.swift in Sources */,
D8AC25BE2B7F359B00CEAAD3 /* FirebaseProvider.swift in Sources */,
D89DBE1D2B872F0B00E5F1BD /* NonceGenerator.swift in Sources */,
D8D14A5C2BA1E73600F45FF2 /* Member.swift in Sources */,
D8A7CA792BA5AB920014EC67 /* MemberStore.swift in Sources */,
D85E86DE2BAB0292002EDF76 /* Expense.swift in Sources */,
D8D42AA92B872726009B345D /* DDLogProvider.swift in Sources */,
);
Expand Down
8 changes: 0 additions & 8 deletions Data/Data/DI/AppAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public class AppAssembly: Assembly {
GroupStore.init()
}.inObjectScope(.container)

container.register(MemberStore.self) { _ in
MemberStore.init()
}.inObjectScope(.container)

container.register(ShareCodeStore.self) { _ in
ShareCodeStore.init()
}.inObjectScope(.container)
Expand All @@ -55,10 +51,6 @@ public class AppAssembly: Assembly {
GroupRepository.init()
}.inObjectScope(.container)

container.register(MemberRepository.self) { _ in
MemberRepository.init()
}.inObjectScope(.container)

container.register(ShareCodeRepository.self) { _ in
ShareCodeRepository.init()
}.inObjectScope(.container)
Expand Down
4 changes: 2 additions & 2 deletions Data/Data/Model/Expense.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public struct Expense: Codable {
let amount: Double
let date: Date
let paidBy: AppUser
let splitTo: [Member] // Reference to users involved in the split
let splitTo: [String] // Reference to users involved in the split
let splitType: SplitType

public init(name: String, amount: Double, date: Date, paidBy: AppUser, splitTo: [Member], splitType: SplitType) {
public init(name: String, amount: Double, date: Date, paidBy: AppUser, splitTo: [String], splitType: SplitType) {
self.name = name
self.amount = amount
self.date = date
Expand Down
4 changes: 2 additions & 2 deletions Data/Data/Model/Groups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public struct Groups: Codable, Identifiable {

public var name: String
public var createdBy: String
public var members: [Member]
public var members: [String]
public var imageUrl: String?
public var createdAt: Timestamp

public init(name: String, createdBy: String, members: [Member], imageUrl: String? = nil, createdAt: Timestamp) {
public init(name: String, createdBy: String, members: [String], imageUrl: String? = nil, createdAt: Timestamp) {
self.name = name
self.createdBy = createdBy
self.members = members
Expand Down
36 changes: 0 additions & 36 deletions Data/Data/Model/Member.swift

This file was deleted.

169 changes: 33 additions & 136 deletions Data/Data/Repository/GroupRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,112 +13,44 @@ public class GroupRepository: ObservableObject {

@Inject private var preference: SplitoPreference
@Inject private var storageManager: StorageManager
@Inject private var memberRepository: MemberRepository

private var cancelables = Set<AnyCancellable>()

public func createGroup(group: Groups, imageData: Data?) -> AnyPublisher<String, ServiceError> {
Future { [weak self] promise in

guard let self else { return }

self.createGroupInStore(group: group)
.flatMap { docId -> AnyPublisher<(String, Member), ServiceError> in
return self.addCreatorToMembers(groupId: docId)
.map { (docId, $0) }
.eraseToAnyPublisher()
}
.flatMap { docId, member -> AnyPublisher<String, ServiceError> in
var newGroup = group
newGroup.id = docId
newGroup.members.append(member)
return self.finalizeGroupCreation(group: newGroup, imageData: imageData)
}
.sink { completion in
if case let .failure(error) = completion {
promise(.failure(error))
}
} receiveValue: { docId in
promise(.success(docId))
}
.store(in: &self.cancelables)
}
.eraseToAnyPublisher()
}

private func createGroupInStore(group: Groups) -> AnyPublisher<String, ServiceError> {
Future { [weak self] promise in

guard let self else {
promise(.failure(.unexpectedError))
return
}
guard let self else { promise(.failure(.unexpectedError)); return }

self.store.createGroup(group: group) { docId in
guard let docId else {
promise(.failure(.databaseError))
return
}
promise(.success(docId))
}
}.eraseToAnyPublisher()
}

private func addCreatorToMembers(groupId: String) -> AnyPublisher<Member, ServiceError> {
Future { [weak self] promise in

guard let self, let userId = self.preference.user?.id else { return }
var member = Member(userId: userId, groupId: groupId)

self.memberRepository.addMemberToMembers(member: member) { memberId in
if let memberId {
member.id = memberId
promise(.success(member))
if let imageData {
var newGroup = group
newGroup.id = docId
self.uploadImage(imageData: imageData, group: newGroup)
.sink { completion in
switch completion {
case .finished:
return
case .failure(let error):
promise(.failure(error))
}
} receiveValue: { _ in
promise(.success(docId))
}.store(in: &self.cancelables)
} else {
promise(.failure(.databaseError))
promise(.success(docId))
}
}
}.eraseToAnyPublisher()
}

private func finalizeGroupCreation(group: Groups, imageData: Data?) -> AnyPublisher<String, ServiceError> {
Future { [weak self] promise in
guard let self, let groupId = group.id else { return }
if let imageData {
self.uploadImage(imageData: imageData, group: group)
.sink { completion in
switch completion {
case .finished:
return
case .failure(let error):
promise(.failure(error))
}
} receiveValue: { _ in
promise(.success(groupId))
}.store(in: &self.cancelables)
} else {
self.updateGroup(group: group)
.sink { completion in
switch completion {
case .finished:
return
case .failure(let error):
promise(.failure(error))
}
} receiveValue: { _ in
promise(.success(groupId))
}.store(in: &self.cancelables)
}
}.eraseToAnyPublisher()
}

private func uploadImage(imageData: Data, group: Groups) -> AnyPublisher<Void, ServiceError> {
Future { [weak self] promise in

guard let self, let groupId = group.id else {
promise(.failure(.unexpectedError))
return
}
guard let self, let groupId = group.id else { promise(.failure(.unexpectedError)); return }

self.storageManager.uploadImage(for: .group, id: groupId, imageData: imageData) { url in
guard let url else {
Expand All @@ -144,76 +76,41 @@ public class GroupRepository: ObservableObject {
}.eraseToAnyPublisher()
}

public func addMemberToGroup(groupId: String, memberId: String) -> AnyPublisher<String, ServiceError> {
Future { [weak self] promise in
guard let self else {
promise(.failure(.unexpectedError))
return
}

self.fetchGroupBy(id: groupId)
.flatMap { group -> AnyPublisher<(Groups?, Member?), ServiceError> in
return self.fetchMemberWith(id: memberId)
.map { (group, $0) }
.eraseToAnyPublisher()
}
.flatMap { group, member -> AnyPublisher<Void, ServiceError> in
guard var group, let member else {
return Fail(error: .dataNotFound).eraseToAnyPublisher()
}
group.members.append(member)
return self.updateGroup(group: group)
}
.sink { completion in
if case let .failure(error) = completion {
promise(.failure(error))
}
} receiveValue: { _ in
promise(.success(groupId))
}
.store(in: &self.cancelables)
}.eraseToAnyPublisher()
}

public func fetchMemberWith(id: String) -> AnyPublisher<Member?, ServiceError> {
Future { [weak self] promise in
guard let self else { return }
self.memberRepository.fetchMemberBy(id: id)
.sink { completion in
switch completion {
case .failure(let error):
promise(.failure(error))
case .finished:
break
}
} receiveValue: { member in
promise(.success(member))
}.store(in: &self.cancelables)
}.eraseToAnyPublisher()
}

public func updateGroup(group: Groups) -> AnyPublisher<Void, ServiceError> {
store.updateGroup(group: group)
}

public func fetchGroups(userId: String) -> AnyPublisher<[Groups], ServiceError> {
Future { [weak self] promise in
guard let self else { return }

self.store.fetchGroups(userId: userId)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(error))
}
} receiveValue: { [weak self] groups in
guard let self else { return }
} receiveValue: { groups in
// Show only those groups in which the user is part of
let filteredGroups = groups.filter { $0.createdBy == userId || $0.members.contains(where: { $0.userId == userId }) }
let filteredGroups = groups.filter { $0.createdBy == userId || $0.members.contains { $0 == userId } }
promise(.success(filteredGroups))
}.store(in: &self.cancelables)

}.eraseToAnyPublisher()
}

public func addMemberToGroup(memberId: String, groupId: String) -> AnyPublisher<Void, ServiceError> {
return fetchGroupBy(id: groupId)
.flatMap { group -> AnyPublisher<Void, ServiceError> in
guard let group else { return Fail(error: .dataNotFound).eraseToAnyPublisher() }

var newGroup = group
newGroup.members.append(memberId)

return self.updateGroup(group: newGroup)
}
.eraseToAnyPublisher()
}

public func fetchGroupBy(id: String) -> AnyPublisher<Groups?, ServiceError> {
store.fetchGroupBy(id: id)
}
Expand Down
Loading

0 comments on commit ba1ff01

Please sign in to comment.