Skip to content

Commit 60bcc33

Browse files
committed
Changes fun deleteConversation parameter
1 parent 4771e9d commit 60bcc33

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

.idea/deploymentTargetDropDown.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/chatgptlite/wanted/data/remote/ConversationRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import com.chatgptlite.wanted.models.ConversationModel
55
interface ConversationRepository {
66
suspend fun fetchConversations() : MutableList<ConversationModel>
77
fun newConversation(conversation: ConversationModel) : ConversationModel
8-
fun deleteConversation(index: Int)
8+
suspend fun deleteConversation(conversationId: String)
99
}

app/src/main/java/com/chatgptlite/wanted/data/remote/ConversationRepositoryImpl.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ import javax.inject.Inject
1515
class ConversationRepositoryImpl @Inject constructor(
1616
private val fsInstance: FirebaseFirestore,
1717
) : ConversationRepository {
18-
private lateinit var result: QuerySnapshot
18+
1919
override suspend fun fetchConversations(): MutableList<ConversationModel> {
20-
result = fsInstance.collection(conversationCollection)
21-
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()
2220

23-
if (result.documents.isNotEmpty()) {
24-
val documents = result.documents
21+
if (getFireBaseSnapShot().documents.isNotEmpty()) {
22+
val documents = getFireBaseSnapShot().documents
2523

2624
return documents.map {
2725
it.toObject(ConversationModel::class.java)
@@ -36,8 +34,18 @@ class ConversationRepositoryImpl @Inject constructor(
3634
return conversation
3735
}
3836

39-
override fun deleteConversation(index: Int) {
40-
DataHolder.docPath = result.documents[index].id
37+
override suspend fun deleteConversation(conversationId: String) {
38+
var desiredKey: String? = null
39+
40+
getFireBaseSnapShot().documents.map { documentSnapshot ->
41+
val id = documentSnapshot.getString("id")
42+
if (id == conversationId) {
43+
desiredKey = documentSnapshot.id
44+
} else {
45+
null
46+
}
47+
}
48+
DataHolder.docPath = desiredKey.toString()
4149

4250
val docRef = fsInstance
4351
.collection("conversations")
@@ -62,7 +70,9 @@ class ConversationRepositoryImpl @Inject constructor(
6270
"Error deleting document", e
6371
)
6472
}
65-
6673
}
74+
private suspend fun getFireBaseSnapShot() =
75+
fsInstance.collection(conversationCollection)
76+
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()
6777

6878
}

app/src/main/java/com/chatgptlite/wanted/ui/common/AppDrawer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private fun ColumnScope.HistoryConversations(
169169
},
170170
onDeleteClicked = {
171171
scope.launch {
172-
conversationViewModel.deleteConversation(index)
172+
conversationViewModel.deleteConversation(conversations[index].id)
173173
conversationViewModel.deleteMessages(conversations[index].id)
174174
}
175175
}

app/src/main/java/com/chatgptlite/wanted/ui/conversations/ConversationViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ class ConversationViewModel @Inject constructor(
179179
messageRepo.deleteMessage()
180180
}
181181

182-
fun deleteConversation(index: Int) = conversationRepo.deleteConversation(index)
182+
suspend fun deleteConversation(conversationId: String) =
183+
conversationRepo.deleteConversation(conversationId)
183184

184185
private suspend fun fetchMessages() {
185186
if (_currentConversation.value.isEmpty() ||

0 commit comments

Comments
 (0)