Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Dec 4, 2024
1 parent aee85c4 commit ccd2bc9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Data/Data/Model/Groups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public struct Groups: Codable, Identifiable {
public var hasExpenses: Bool
public var isActive: Bool

public init(name: String, createdBy: String, updatedBy: String, imageUrl: String? = nil, members: [String], balances: [GroupMemberBalance],
createdAt: Timestamp, updatedAt: Timestamp = Timestamp(), hasExpenses: Bool = false, isActive: Bool = true) {
public init(name: String, createdBy: String, updatedBy: String, imageUrl: String? = nil,
members: [String], balances: [GroupMemberBalance], createdAt: Timestamp = Timestamp(),
updatedAt: Timestamp = Timestamp(), hasExpenses: Bool = false, isActive: Bool = true) {
self.name = name
self.createdBy = createdBy
self.updatedBy = updatedBy
Expand Down
2 changes: 1 addition & 1 deletion Data/Data/Repository/GroupRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public class GroupRepository: ObservableObject {
group.updatedBy = userId
group.updatedAt = Timestamp()

return try await updateGroup(group: group, type: .groupDeleted)
try await updateGroup(group: group, type: .groupDeleted)
}

public func updateGroup(group: Groups, type: ActivityType, removedMember: AppUser? = nil) async throws {
Expand Down
4 changes: 2 additions & 2 deletions Splito/UI/Home/Groups/Create Group/CreateGroupViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ class CreateGroupViewModel: BaseViewModel, ObservableObject {
guard let userId = preference.user?.id else { return false }

let memberBalance = GroupMemberBalance(id: userId, balance: 0, totalSummary: [])
let group = Groups(name: groupName.trimming(spaces: .leadingAndTrailing), createdBy: userId, updatedBy: userId,
members: [userId], balances: [memberBalance], createdAt: Timestamp())
let group = Groups(name: groupName.trimming(spaces: .leadingAndTrailing), createdBy: userId,
updatedBy: userId, members: [userId], balances: [memberBalance])

do {
showLoader = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,9 @@ class GroupPaymentViewModel: BaseViewModel, ObservableObject {
NotificationCenter.default.post(name: .updateTransaction, object: self.transaction)
}

if let transaction = self.transaction {
guard hasTransactionChanged(transaction, oldTransaction: oldTransaction) else { return true }
await updateGroupMemberBalance(updateType: .Update(oldTransaction: oldTransaction))
}
guard let transaction = self.transaction else { return false }
guard hasTransactionChanged(transaction, oldTransaction: oldTransaction) else { return true }
await updateGroupMemberBalance(updateType: .Update(oldTransaction: oldTransaction))

showLoader = false
LogD("GroupPaymentViewModel: \(#function) Payment updated successfully.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ class GroupTransactionListViewModel: BaseViewModel, ObservableObject {
// MARK: - Data Loading
private func fetchGroup() async {
do {
self.group = try await groupRepository.fetchGroupBy(id: groupId)
let group = try await groupRepository.fetchGroupBy(id: groupId)
guard let group else {
currentViewState = .initial
return
}
self.group = group
currentViewState = .initial
LogD("GroupTransactionListViewModel: \(#function) Group fetched successfully.")
} catch {
Expand Down
5 changes: 3 additions & 2 deletions Splito/UI/Home/Groups/GroupListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ class GroupListViewModel: BaseViewModel, ObservableObject {
func fetchCurrentUser() {
guard let userId = preference.user?.id else { return }

Task {
Task { [weak self] in
do {
preference.user = try await userRepository.fetchUserBy(userID: userId)
guard let self else { return }
self.preference.user = try await self.userRepository.fetchUserBy(userID: userId)
} catch {
LogE("GroupListViewModel: \(#function) Failed to fetch current user: \(error).")
}
Expand Down

0 comments on commit ccd2bc9

Please sign in to comment.