Skip to content

Commit

Permalink
Add parentId name&Changed repo name& handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 10, 2025
1 parent dc67b79 commit f2d6fc8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
16 changes: 8 additions & 8 deletions Data/Data.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
21CF56512D1E804000B47A6D /* DeepLinkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21CF56502D1E804000B47A6D /* DeepLinkManager.swift */; };
21D8D0832C0857F10061B365 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D8D0822C0857F10061B365 /* Constants.swift */; };
21F41AFC2D312C6500D37DBB /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F41AFB2D312C6500D37DBB /* Comment.swift */; };
21F41AFE2D312EDE00D37DBB /* CommentStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F41AFD2D312EDE00D37DBB /* CommentStore.swift */; };
21F41B002D31311A00D37DBB /* CommentRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F41AFF2D31311A00D37DBB /* CommentRepository.swift */; };
21F41AFE2D312EDE00D37DBB /* ExpenseCommentStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F41AFD2D312EDE00D37DBB /* ExpenseCommentStore.swift */; };
21F41B002D31311A00D37DBB /* ExpenseCommentRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21F41AFF2D31311A00D37DBB /* ExpenseCommentRepository.swift */; };
64E499C5CFAEA368EC21313F /* Pods_DataTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1571EA0A08D442FEF7C09424 /* Pods_DataTests.framework */; };
7EF3A291581F7EA20CB1042D /* Pods_Data.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E91B3E23688435064A60C0C4 /* Pods_Data.framework */; };
D826C0DF2BDA95C900AAA449 /* Timestamp+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D826C0DE2BDA95C900AAA449 /* Timestamp+Extension.swift */; };
Expand Down Expand Up @@ -85,8 +85,8 @@
21CF56502D1E804000B47A6D /* DeepLinkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeepLinkManager.swift; sourceTree = "<group>"; };
21D8D0822C0857F10061B365 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
21F41AFB2D312C6500D37DBB /* Comment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Comment.swift; sourceTree = "<group>"; };
21F41AFD2D312EDE00D37DBB /* CommentStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentStore.swift; sourceTree = "<group>"; };
21F41AFF2D31311A00D37DBB /* CommentRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommentRepository.swift; sourceTree = "<group>"; };
21F41AFD2D312EDE00D37DBB /* ExpenseCommentStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpenseCommentStore.swift; sourceTree = "<group>"; };
21F41AFF2D31311A00D37DBB /* ExpenseCommentRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpenseCommentRepository.swift; sourceTree = "<group>"; };
5B14CF1A2EEF27479BF50566 /* Pods-DataTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DataTests.debug.xcconfig"; path = "Target Support Files/Pods-DataTests/Pods-DataTests.debug.xcconfig"; sourceTree = "<group>"; };
803094012FE4C155F2A347B6 /* Pods-DataTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DataTests.release.xcconfig"; path = "Target Support Files/Pods-DataTests/Pods-DataTests.release.xcconfig"; sourceTree = "<group>"; };
BED6C37AA3F8FD2A6350DE1C /* Pods-Data.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Data.debug.xcconfig"; path = "Target Support Files/Pods-Data/Pods-Data.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -192,7 +192,7 @@
D83B15082B999789004A5F4F /* GroupRepository.swift */,
D85E86E42BAB088F002EDF76 /* ExpenseRepository.swift */,
D8CF5A242C199ADB0014E3AD /* TransactionRepository.swift */,
21F41AFF2D31311A00D37DBB /* CommentRepository.swift */,
21F41AFF2D31311A00D37DBB /* ExpenseCommentRepository.swift */,
21559CAF2CBD2B400039F127 /* ActivityLogRepository.swift */,
D8D14A532BA092F400F45FF2 /* ShareCodeRepository.swift */,
2163D3B52D269E2A004B4F20 /* FeedbackRepository.swift */,
Expand Down Expand Up @@ -265,7 +265,7 @@
D8A7CA762BA5AB800014EC67 /* GroupStore.swift */,
D8910E372BB6D1D300877CE0 /* ExpenseStore.swift */,
D8CF5A222C1989630014E3AD /* TransactionStore.swift */,
21F41AFD2D312EDE00D37DBB /* CommentStore.swift */,
21F41AFD2D312EDE00D37DBB /* ExpenseCommentStore.swift */,
21559CAD2CBD2AED0039F127 /* ActivityLogStore.swift */,
D8A7CA7A2BA5B6AC0014EC67 /* ShareCodeStore.swift */,
2163D3B32D269E17004B4F20 /* FeedbackStore.swift */,
Expand Down Expand Up @@ -541,7 +541,7 @@
D88721432B99F133009DC5BE /* StorageManager.swift in Sources */,
D89DBE4A2B8CBEAE00E5F1BD /* ServiceError.swift in Sources */,
D8613F682BFDECC4008BD53D /* Query+Extension.swift in Sources */,
21F41B002D31311A00D37DBB /* CommentRepository.swift in Sources */,
21F41B002D31311A00D37DBB /* ExpenseCommentRepository.swift in Sources */,
D89DBE482B8CBE4C00E5F1BD /* AppUser.swift in Sources */,
D8D14A522BA0917D00F45FF2 /* SharedCode.swift in Sources */,
D89DBE462B8CBE0F00E5F1BD /* UserRepository.swift in Sources */,
Expand All @@ -551,7 +551,7 @@
D8AC25C32B7F390B00CEAAD3 /* AppAssembly.swift in Sources */,
21D8D0832C0857F10061B365 /* Constants.swift in Sources */,
D8AC25C12B7F38D300CEAAD3 /* Injector.swift in Sources */,
21F41AFE2D312EDE00D37DBB /* CommentStore.swift in Sources */,
21F41AFE2D312EDE00D37DBB /* ExpenseCommentStore.swift in Sources */,
D8D14A542BA092F500F45FF2 /* ShareCodeRepository.swift in Sources */,
D8A7CA7B2BA5B6AC0014EC67 /* ShareCodeStore.swift in Sources */,
21CF56512D1E804000B47A6D /* DeepLinkManager.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions Data/Data/DI/AppAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public class AppAssembly: Assembly {
TransactionStore.init()
}.inObjectScope(.container)

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

container.register(FeedbackStore.self) { _ in
Expand Down Expand Up @@ -95,8 +95,8 @@ public class AppAssembly: Assembly {
TransactionRepository.init()
}.inObjectScope(.container)

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

container.register(FeedbackRepository.self) { _ in
Expand Down
8 changes: 4 additions & 4 deletions Data/Data/Model/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ public struct Comment: Codable, Hashable {

@DocumentID public var id: String? // Automatically generated ID by Firestore

public var expenseId: String
public var parentId: String
public var comment: String
public var commentedBy: String
public var commentedAt: Timestamp

public init(expenseId: String, comment: String, commentedBy: String, commentedAt: Timestamp = Timestamp()) {
self.expenseId = expenseId
public init(parentId: String, comment: String, commentedBy: String, commentedAt: Timestamp = Timestamp()) {
self.parentId = parentId
self.comment = comment
self.commentedBy = commentedBy
self.commentedAt = commentedAt
}

enum CodingKeys: String, CodingKey {
case id
case expenseId = "expense_id"
case parentId = "parent_id"
case comment = "comment"
case commentedBy = "commented_by"
case commentedAt = "commented_at"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//
// CommentRepository.swift
// ExpenseCommentRepository.swift
// Data
//
// Created by Nirali Sonani on 10/01/25.
//

import FirebaseFirestore

public class CommentRepository: ObservableObject {
public class ExpenseCommentRepository: ObservableObject {

@Inject private var store: CommentStore
@Inject private var store: ExpenseCommentStore
@Inject private var preference: SplitoPreference
@Inject private var activityLogRepository: ActivityLogRepository

Expand Down Expand Up @@ -66,7 +66,7 @@ public class CommentRepository: ObservableObject {
}
}

private func createActivityLogForExpenseComment(context: ActivityLogContext) -> ActivityLog? {
private func createActivityLogForComment(context: ActivityLogContext) -> ActivityLog? {
guard let group = context.group, let groupId = group.id, let expense = context.expense,
let expenseId = expense.id, let currentUser = context.currentUser,
let memberId = context.memberId else { return nil }
Expand All @@ -80,7 +80,7 @@ public class CommentRepository: ObservableObject {
}

private func addActivityLog(context: ActivityLogContext) async -> Error? {
if let activity = createActivityLogForExpenseComment(context: context), let memberId = context.memberId {
if let activity = createActivityLogForComment(context: context), let memberId = context.memberId {
do {
try await activityLogRepository.addActivityLog(userId: memberId, activity: activity)
LogD("CommentRepository: \(#function) Activity log added successfully for \(memberId).")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// CommentStore.swift
// ExpenseCommentStore.swift
// Data
//
// Created by Nirali Sonani on 10/01/25.
//

import FirebaseFirestore

class CommentStore: ObservableObject {
class ExpenseCommentStore: ObservableObject {

private let COLLECTION_NAME: String = "groups"
private let SUB_COLLECTION_NAME: String = "expenses"
Expand All @@ -30,13 +30,19 @@ class CommentStore: ObservableObject {
var newComment = comment
newComment.id = documentRef.documentID

try documentRef.setData(from: newComment)
do {
try documentRef.setData(from: newComment)
} catch {
LogE("CommentStore: \(#function) Failed to add comment: \(error).")
throw error
}

return newComment
}

func fetchCommentsBy(groupId: String, expenseId: String, limit: Int, lastDocument: DocumentSnapshot?) async throws -> (data: [Comment], lastDocument: DocumentSnapshot?) {
var query = commentReference(groupId: groupId, expenseId: expenseId)
.order(by: "commented_at", descending: true)
.order(by: "commented_at", descending: false)
.limit(to: limit)

if let lastDocument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ private struct ExpenseCommentListView: View {
}

ExpenseCommentCellView(comment: comment, memberName: memberName,
memberProfileUrl: viewModel.getMemberDataBy(id: comment.commentedBy)?.imageUrl,
isLastComment: viewModel.comments.last?.id == comment.id)
memberProfileUrl: viewModel.getMemberDataBy(id: comment.commentedBy)?.imageUrl,
isLastComment: viewModel.comments.last?.id == comment.id)
}

if viewModel.hasMoreComments {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
@Inject private var userRepository: UserRepository
@Inject private var groupRepository: GroupRepository
@Inject private var expenseRepository: ExpenseRepository
@Inject private var commentRepository: CommentRepository
@Inject private var commentRepository: ExpenseCommentRepository

@Published private(set) var expense: Expense?
@Published private(set) var comments: [Comment] = []
Expand Down Expand Up @@ -170,7 +170,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
Task { [weak self] in
do {
self?.showLoader = true
let comment = Comment(expenseId: expenseId, comment: self?.comment.trimming(spaces: .leadingAndTrailing) ?? "", commentedBy: userId)
let comment = Comment(parentId: expenseId, comment: self?.comment.trimming(spaces: .leadingAndTrailing) ?? "", commentedBy: userId)
let newComment = try await self?.commentRepository.addComment(group: group, expense: expense, comment: comment)

if let newComment {
Expand All @@ -183,7 +183,7 @@ class ExpenseDetailsViewModel: BaseViewModel, ObservableObject {
LogD("ExpenseDetailsViewModel: \(#function) Expense comment added successfully.")
} catch {
self?.showLoader = false
LogE("ExpenseDetailsViewModel: \(#function) Failed to add comment: \(error).")
LogE("ExpenseDetailsViewModel: \(#function) Failed to add expense comment: \(error).")
self?.showToastForError()
}
}
Expand Down

0 comments on commit f2d6fc8

Please sign in to comment.