Skip to content

Commit

Permalink
Added reason field & show in activity log
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Nov 29, 2024
1 parent 218dcff commit 7dfe413
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
7 changes: 5 additions & 2 deletions Data/Data/Model/ActivityLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public struct ActivityLog: Codable, Identifiable, Hashable {
public let expenseName: String?
public let payerName: String?
public let receiverName: String?
public let paymentReason: String?
public let amount: Double?

public init(type: ActivityType, groupId: String, activityId: String, groupName: String, actionUserName: String,
recordedOn: Timestamp, previousGroupName: String? = nil, removedMemberName: String? = nil,
expenseName: String? = nil, payerName: String? = nil, receiverName: String? = nil, amount: Double? = nil) {
recordedOn: Timestamp, previousGroupName: String? = nil, removedMemberName: String? = nil, expenseName: String? = nil,
payerName: String? = nil, receiverName: String? = nil, paymentReason: String? = nil, amount: Double? = nil) {
self.type = type
self.groupId = groupId
self.activityId = activityId
Expand All @@ -43,6 +44,7 @@ public struct ActivityLog: Codable, Identifiable, Hashable {
self.expenseName = expenseName
self.payerName = payerName
self.receiverName = receiverName
self.paymentReason = paymentReason
self.amount = amount
}

Expand All @@ -59,6 +61,7 @@ public struct ActivityLog: Codable, Identifiable, Hashable {
case expenseName = "expense_name"
case payerName = "payer_name"
case receiverName = "receiver_name"
case paymentReason = "payment_reason"
case amount
}
}
Expand Down
1 change: 1 addition & 0 deletions Data/Data/Repository/ActivityLogRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct ActivityLogContext {
var currentUser: AppUser?
var payerName: String?
var receiverName: String?
var paymentReason: String?
var previousGroupName: String?
var removedMemberName: String?
}
16 changes: 8 additions & 8 deletions Data/Data/Repository/TransactionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public class TransactionRepository: ObservableObject {
private func hasTransactionChanged(_ transaction: Transactions, oldTransaction: Transactions) -> Bool {
return oldTransaction.payerId != transaction.payerId || oldTransaction.receiverId != transaction.receiverId ||
oldTransaction.updatedBy != transaction.updatedBy || oldTransaction.note != transaction.note ||
oldTransaction.imageUrl != transaction.imageUrl || oldTransaction.amount != transaction.amount ||
oldTransaction.date.dateValue() != transaction.date.dateValue() ||
oldTransaction.imageUrl != transaction.imageUrl || oldTransaction.reason != transaction.reason ||
oldTransaction.amount != transaction.amount || oldTransaction.date.dateValue() != transaction.date.dateValue() ||
oldTransaction.updatedAt.dateValue() != transaction.updatedAt.dateValue() || oldTransaction.isActive != transaction.isActive
}

Expand All @@ -103,8 +103,9 @@ public class TransactionRepository: ObservableObject {
guard let self else { return nil }
let payerName = (user.id == transaction.payerId && memberId == transaction.payerId) ? (user.id == transaction.addedBy ? "You" : "you") : (memberId == transaction.payerId) ? "you" : members.payer.nameWithLastInitial
let receiverName = (memberId == transaction.receiverId) ? "you" : (memberId == transaction.receiverId) ? "you" : members.receiver.nameWithLastInitial
let context = ActivityLogContext(group: group, transaction: transaction, type: type, memberId: memberId,
currentUser: user, payerName: payerName, receiverName: receiverName)
let context = ActivityLogContext(group: group, transaction: transaction, type: type,
memberId: memberId, currentUser: user, payerName: payerName,
receiverName: receiverName, paymentReason: transaction.reason)

return await self.addActivityLog(context: context)
}
Expand All @@ -131,10 +132,9 @@ public class TransactionRepository: ObservableObject {
let actionUserName = (context.memberId == currentUser.id) ? "You" : currentUser.nameWithLastInitial
let amount: Double = (context.memberId == transaction.payerId) ? transaction.amount : (context.memberId == transaction.receiverId) ? -transaction.amount : 0

return ActivityLog(type: context.type, groupId: groupId, activityId: transactionId,
groupName: context.group?.name ?? "", actionUserName: actionUserName,
recordedOn: Timestamp(date: Date()), payerName: context.payerName,
receiverName: context.receiverName, amount: amount)
return ActivityLog(type: context.type, groupId: groupId, activityId: transactionId, groupName: context.group?.name ?? "",
actionUserName: actionUserName, recordedOn: Timestamp(date: Date()), payerName: context.payerName,
receiverName: context.receiverName, paymentReason: context.paymentReason, amount: amount)
}

private func addActivityLog(context: ActivityLogContext) async -> Error? {
Expand Down
31 changes: 25 additions & 6 deletions Splito/UI/Home/ActivityLog/ActivityLogView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private struct ActivityLogDescriptionView: View {
let type: ActivityType
let payerName: String
var receiverName: String
var paymentReason: String?
let groupName: String
var oldGroupName: String
let actionUserName: String
Expand All @@ -202,6 +203,7 @@ private struct ActivityLogDescriptionView: View {
self.type = activityLog.type
self.payerName = activityLog.payerName ?? "Someone"
self.receiverName = activityLog.receiverName ?? "Someone"
self.paymentReason = activityLog.paymentReason
self.groupName = activityLog.groupName
self.oldGroupName = activityLog.previousGroupName ?? ""
self.actionUserName = activityLog.actionUserName
Expand Down Expand Up @@ -276,18 +278,35 @@ private struct ActivityLogDescriptionView: View {
if actionUserName != payerName && actionUserName != receiverName {
transactionDescription()
} else if actionUserName != payerName {
highlightedText(actionUserName) + disabledText(" recorded a payment from ") +
highlightedText(payerName) + disabledText(" in") + highlightedText(" \"\(groupName)\".")
if let paymentReason, !paymentReason.isEmpty {
highlightedText(actionUserName) + disabledText(" recorded a payment from ") +
highlightedText(payerName) + disabledText(" for") + highlightedText(" \"\(paymentReason)\"") +
disabledText(" in") + highlightedText(" \"\(groupName)\".")
} else {
highlightedText(actionUserName) + disabledText(" recorded a payment from ") +
highlightedText(payerName) + disabledText(" in") + highlightedText(" \"\(groupName)\".")
}
} else {
highlightedText(payerName) + disabledText(" paid ") + highlightedText(receiverName) +
disabledText(" in") + highlightedText(" \"\(groupName)\".")
if let paymentReason, !paymentReason.isEmpty {
highlightedText(payerName) + disabledText(" paid ") + highlightedText(receiverName) + disabledText(" for") +
highlightedText(" \"\(paymentReason)\"") + disabledText(" in") + highlightedText(" \"\(groupName)\".")
} else {
highlightedText(payerName) + disabledText(" paid ") + highlightedText(receiverName) +
disabledText(" in") + highlightedText(" \"\(groupName)\".")
}
}
}

@ViewBuilder
private func transactionDescription(action: String = "added") -> some View {
highlightedText(actionUserName) + disabledText(" \(action) a payment from ") + highlightedText(payerName) +
disabledText(" to ") + highlightedText(receiverName) + disabledText(" in") + highlightedText(" \"\(groupName)\".")
if let paymentReason, !paymentReason.isEmpty {
highlightedText(actionUserName) + disabledText(" \(action) a payment from ") + highlightedText(payerName) +
disabledText(" to ") + highlightedText(receiverName) + disabledText(" for") +
highlightedText(" \"\(paymentReason)\"") + disabledText(" in") + highlightedText(" \"\(groupName)\".")
} else {
highlightedText(actionUserName) + disabledText(" \(action) a payment from ") + highlightedText(payerName) +
disabledText(" to ") + highlightedText(receiverName) + disabledText(" in") + highlightedText(" \"\(groupName)\".")
}
}

private func highlightedText(_ text: String) -> Text {
Expand Down
3 changes: 2 additions & 1 deletion Splito/UI/Home/Expense/Note/AddNoteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ struct AddNoteView: View {
ToolbarItem(placement: .topBarTrailing) {
CheckmarkButton(showLoader: viewModel.showLoader) {
Task {
let isActionSucceed = await viewModel.handleSaveNoteAction(tempNote: tempNote, tempPaymentReason: tempPaymentReason)
let isActionSucceed = await viewModel.handleSaveNoteAction(tempNote: tempNote,
tempPaymentReason: tempPaymentReason)
if isActionSucceed {
dismiss()
} else {
Expand Down

0 comments on commit 7dfe413

Please sign in to comment.