Skip to content

Commit 0805f76

Browse files
committed
Merge branch 'main' of github.com:lambiengcode/compose_chatgpt_kotlin
2 parents a1b8729 + a88f58b commit 0805f76

File tree

13 files changed

+351
-95
lines changed

13 files changed

+351
-95
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.

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ The project also contains other files and directories at the root level, includi
9797
- [ ] stream: false
9898
- [ ] gpt-3.5-turbo
9999
- [ ] text-davinci-003, text-curie-001, text-babbage-001, text-ada-001
100-
- [ ] Stop generating
101-
- [ ] Delete conversation
100+
- [x] Stop generating
101+
- [x] Delete conversation
102102
- [ ] Settings for ChatGPT
103103
- [ ] Light/Dart Themes
104104

@@ -108,6 +108,10 @@ The project also contains other files and directories at the root level, includi
108108
- [OpenAI GPT-3 API](https://beta.openai.com/docs/api-reference/introduction)
109109
- [Kotlin Coroutines](https://kotlinlang.org/docs/coroutines-overview.html)
110110

111+
## Star History
112+
113+
[![Star History Chart](https://api.star-history.com/svg?repos=lambiengcode/compose-chatgpt-kotlin-android-chatbot&type=Date)](https://star-history.com/#lambiengcode/compose-chatgpt-kotlin-android-chatbot&Date)
114+
111115
## Contributing
112116
Contributions are welcome! Please feel free to submit a pull request or open an issue if you encounter any problems or have suggestions for improvements.
113117

app/src/main/java/com/chatgptlite/wanted/MainActivity.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class MainActivity : ComponentActivity() {
6363
focusManager.clearFocus()
6464
}
6565
}
66-
67-
ChatGPTLiteTheme() {
66+
val darkTheme = remember(key1 = "darkTheme") {
67+
mutableStateOf(true)
68+
}
69+
ChatGPTLiteTheme(darkTheme.value) {
6870
Surface(
6971
color = MaterialTheme.colorScheme.background,
7072
) {
@@ -79,18 +81,23 @@ class MainActivity : ComponentActivity() {
7981
scope.launch {
8082
drawerState.close()
8183
}
84+
},
85+
onIconClicked = {
86+
darkTheme.value = !darkTheme.value
8287
}
8388
) {
84-
Column( modifier = Modifier
85-
.fillMaxSize()) {
86-
AppBar (onClickMenu = {
87-
scope.launch {
88-
drawerState.open()
89-
}
90-
})
91-
Divider()
92-
Conversation()
93-
}
89+
Column(
90+
modifier = Modifier
91+
.fillMaxSize()
92+
) {
93+
AppBar(onClickMenu = {
94+
scope.launch {
95+
drawerState.open()
96+
}
97+
})
98+
Divider()
99+
Conversation()
100+
}
94101
}
95102
}
96103
}

app/src/main/java/com/chatgptlite/wanted/constants/Constants.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ const val urlToAvatarGPT = "https://gptapk.com/wp-content/uploads/2023/02/chatgp
88
const val urlToGithub = "https://github.com/lambiengcode"
99

1010
const val matchResultString = "\"text\":"
11-
const val matchResultTurboString = "\"content\":"
11+
const val matchResultTurboString = "\"content\":"
12+
const val conversationTestTag = "ConversationTestTag"

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()
8+
suspend fun deleteConversation(conversationId: String)
99
}

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.chatgptlite.wanted.data.remote
22

3+
import android.content.ContentValues
4+
import android.util.Log
35
import com.chatgptlite.wanted.constants.conversationCollection
6+
import com.chatgptlite.wanted.helpers.DataHolder
47
import com.chatgptlite.wanted.models.ConversationModel
8+
import com.google.firebase.firestore.FieldValue
59
import com.google.firebase.firestore.FirebaseFirestore
610
import com.google.firebase.firestore.Query
711
import com.google.firebase.firestore.QuerySnapshot
@@ -11,12 +15,11 @@ import javax.inject.Inject
1115
class ConversationRepositoryImpl @Inject constructor(
1216
private val fsInstance: FirebaseFirestore,
1317
) : ConversationRepository {
18+
1419
override suspend fun fetchConversations(): MutableList<ConversationModel> {
15-
val result: QuerySnapshot = fsInstance.collection(conversationCollection)
16-
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()
1720

18-
if (result.documents.isNotEmpty()) {
19-
val documents = result.documents
21+
if (getFireBaseSnapShot().documents.isNotEmpty()) {
22+
val documents = getFireBaseSnapShot().documents
2023

2124
return documents.map {
2225
it.toObject(ConversationModel::class.java)
@@ -31,8 +34,45 @@ class ConversationRepositoryImpl @Inject constructor(
3134
return conversation
3235
}
3336

34-
override fun deleteConversation() {
35-
TODO("Not yet implemented")
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()
49+
50+
val docRef = fsInstance
51+
.collection("conversations")
52+
.document(DataHolder.docPath)
53+
54+
// Remove the 'capital' field from the document
55+
val updates = hashMapOf<String, Any>(
56+
"id" to FieldValue.delete(),
57+
"title" to FieldValue.delete(),
58+
"createdAt" to FieldValue.delete()
59+
)
60+
docRef.update(updates)
61+
.addOnSuccessListener {
62+
Log.d(
63+
ContentValues.TAG,
64+
"DocumentSnapshot successfully deleted from message!"
65+
)
66+
}
67+
.addOnFailureListener { e ->
68+
Log.w(
69+
ContentValues.TAG,
70+
"Error deleting document", e
71+
)
72+
}
3673
}
74+
private suspend fun getFireBaseSnapShot() =
75+
fsInstance.collection(conversationCollection)
76+
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()
3777

3878
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import kotlinx.coroutines.flow.Flow
66
interface MessageRepository {
77
fun fetchMessages(conversationId: String): Flow<List<MessageModel>>
88
fun createMessage(message: MessageModel): MessageModel
9-
fun deleteMessage(message: MessageModel)
9+
fun deleteMessage()
1010
}

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.chatgptlite.wanted.data.remote
22

3+
import android.content.ContentValues
4+
import android.util.Log
35
import com.chatgptlite.wanted.constants.messageCollection
6+
import com.chatgptlite.wanted.helpers.DataHolder
47
import com.chatgptlite.wanted.models.MessageModel
8+
import com.google.firebase.firestore.FieldValue
59
import com.google.firebase.firestore.FirebaseFirestore
610
import com.google.firebase.firestore.Query
711
import com.google.firebase.firestore.QuerySnapshot
@@ -14,9 +18,13 @@ import javax.inject.Inject
1418
class MessageRepositoryImpl @Inject constructor(
1519
private val fsInstance: FirebaseFirestore,
1620
) : MessageRepository {
17-
override fun fetchMessages(conversationId: String): Flow<List<MessageModel>> = callbackFlow {
18-
val result: QuerySnapshot =
19-
fsInstance.collection(messageCollection).whereEqualTo("conversationId", conversationId)
21+
private lateinit var result: QuerySnapshot
22+
override fun fetchMessages(conversationId: String): Flow<List<MessageModel>> =
23+
callbackFlow {
24+
result =
25+
fsInstance
26+
.collection(messageCollection)
27+
.whereEqualTo("conversationId", conversationId)
2028
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()
2129

2230
if (result.documents.isNotEmpty()) {
@@ -44,7 +52,32 @@ class MessageRepositoryImpl @Inject constructor(
4452
return message
4553
}
4654

47-
override fun deleteMessage(message: MessageModel) {
48-
TODO("Not yet implemented")
55+
override fun deleteMessage() {
56+
val docRef = fsInstance
57+
.collection("messages")
58+
.document(DataHolder.docPath)
59+
60+
// Remove the fields from the document
61+
val updates = hashMapOf<String, Any>(
62+
"answer" to FieldValue.delete(),
63+
"conversationId" to FieldValue.delete(),
64+
"createdAt" to FieldValue.delete(),
65+
"id" to FieldValue.delete(),
66+
"question" to FieldValue.delete()
67+
)
68+
docRef.update(updates)
69+
.addOnSuccessListener {
70+
Log.d(
71+
ContentValues.TAG,
72+
"DocumentSnapshot successfully deleted from message!"
73+
)
74+
}
75+
.addOnFailureListener { e ->
76+
Log.w(
77+
ContentValues.TAG,
78+
"Error deleting document", e
79+
)
80+
}
81+
4982
}
5083
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.chatgptlite.wanted.helpers
2+
3+
object DataHolder {
4+
var docPath: String = ""
5+
}

0 commit comments

Comments
 (0)