Skip to content

Dev #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const val urlToAvatarGPT = "https://gptapk.com/wp-content/uploads/2023/02/chatgp
const val urlToGithub = "https://github.com/lambiengcode"

const val matchResultString = "\"text\":"
const val matchResultTurboString = "\"content\":"
const val matchResultTurboString = "\"content\":"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is different here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion my commit not clear. I have stored a val in here and made a push but found out it is not a good idea and removed it that is why noting has been changed in here.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import com.chatgptlite.wanted.models.ConversationModel
interface ConversationRepository {
suspend fun fetchConversations() : MutableList<ConversationModel>
fun newConversation(conversation: ConversationModel) : ConversationModel
fun deleteConversation()
suspend fun deleteConversation(conversationId: String)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.chatgptlite.wanted.data.remote

import android.content.ContentValues
import android.util.Log
import com.chatgptlite.wanted.constants.conversationCollection
import com.chatgptlite.wanted.helpers.DataHolder
import com.chatgptlite.wanted.models.ConversationModel
import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QuerySnapshot
Expand All @@ -11,12 +15,11 @@ import javax.inject.Inject
class ConversationRepositoryImpl @Inject constructor(
private val fsInstance: FirebaseFirestore,
) : ConversationRepository {

override suspend fun fetchConversations(): MutableList<ConversationModel> {
val result: QuerySnapshot = fsInstance.collection(conversationCollection)
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()

if (result.documents.isNotEmpty()) {
val documents = result.documents
if (getFireBaseSnapShot().documents.isNotEmpty()) {
val documents = getFireBaseSnapShot().documents

return documents.map {
it.toObject(ConversationModel::class.java)
Expand All @@ -31,8 +34,45 @@ class ConversationRepositoryImpl @Inject constructor(
return conversation
}

override fun deleteConversation() {
TODO("Not yet implemented")
override suspend fun deleteConversation(conversationId: String) {
var desiredKey: String? = null

getFireBaseSnapShot().documents.map { documentSnapshot ->
val id = documentSnapshot.getString("id")
if (id == conversationId) {
desiredKey = documentSnapshot.id
} else {
null
}
}
DataHolder.docPath = desiredKey.toString()

val docRef = fsInstance
.collection("conversations")
.document(DataHolder.docPath)

// Remove the 'capital' field from the document
val updates = hashMapOf<String, Any>(
"id" to FieldValue.delete(),
"title" to FieldValue.delete(),
"createdAt" to FieldValue.delete()
)
docRef.update(updates)
.addOnSuccessListener {
Log.d(
ContentValues.TAG,
"DocumentSnapshot successfully deleted from message!"
)
}
.addOnFailureListener { e ->
Log.w(
ContentValues.TAG,
"Error deleting document", e
)
}
}
private suspend fun getFireBaseSnapShot() =
fsInstance.collection(conversationCollection)
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import kotlinx.coroutines.flow.Flow
interface MessageRepository {
fun fetchMessages(conversationId: String): Flow<List<MessageModel>>
fun createMessage(message: MessageModel): MessageModel
fun deleteMessage(message: MessageModel)
fun deleteMessage()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.chatgptlite.wanted.data.remote

import android.content.ContentValues
import android.util.Log
import com.chatgptlite.wanted.constants.messageCollection
import com.chatgptlite.wanted.helpers.DataHolder
import com.chatgptlite.wanted.models.MessageModel
import com.google.firebase.firestore.FieldValue
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QuerySnapshot
Expand All @@ -14,9 +18,13 @@ import javax.inject.Inject
class MessageRepositoryImpl @Inject constructor(
private val fsInstance: FirebaseFirestore,
) : MessageRepository {
override fun fetchMessages(conversationId: String): Flow<List<MessageModel>> = callbackFlow {
val result: QuerySnapshot =
fsInstance.collection(messageCollection).whereEqualTo("conversationId", conversationId)
private lateinit var result: QuerySnapshot
override fun fetchMessages(conversationId: String): Flow<List<MessageModel>> =
callbackFlow {
result =
fsInstance
.collection(messageCollection)
.whereEqualTo("conversationId", conversationId)
.orderBy("createdAt", Query.Direction.DESCENDING).get().await()

if (result.documents.isNotEmpty()) {
Expand Down Expand Up @@ -44,7 +52,32 @@ class MessageRepositoryImpl @Inject constructor(
return message
}

override fun deleteMessage(message: MessageModel) {
TODO("Not yet implemented")
override fun deleteMessage() {
val docRef = fsInstance
.collection("messages")
.document(DataHolder.docPath)

// Remove the fields from the document
val updates = hashMapOf<String, Any>(
"answer" to FieldValue.delete(),
"conversationId" to FieldValue.delete(),
"createdAt" to FieldValue.delete(),
"id" to FieldValue.delete(),
"question" to FieldValue.delete()
)
docRef.update(updates)
.addOnSuccessListener {
Log.d(
ContentValues.TAG,
"DocumentSnapshot successfully deleted from message!"
)
}
.addOnFailureListener { e ->
Log.w(
ContentValues.TAG,
"Error deleting document", e
)
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.chatgptlite.wanted.helpers

object DataHolder {
var docPath: String = ""
}
74 changes: 69 additions & 5 deletions app/src/main/java/com/chatgptlite/wanted/ui/common/AppDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Message
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.WbSunny
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.outlined.AddComment
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -159,7 +156,7 @@ private fun ColumnScope.HistoryConversations(
.weight(1f, false),
) {
items(conversations.size) { index ->
ChatItem(
RecycleChatItem(
text = conversations[index].title,
Icons.Filled.Message,
selected = conversations[index].id == conversationId,
Expand All @@ -170,6 +167,12 @@ private fun ColumnScope.HistoryConversations(
conversationViewModel.onConversation(conversations[index])
}
},
onDeleteClicked = {
scope.launch {
conversationViewModel.deleteConversation(conversations[index].id)
conversationViewModel.deleteMessages(conversations[index].id)
}
}
)
}
}
Expand Down Expand Up @@ -240,6 +243,67 @@ private fun ChatItem(
)
}
}
@Composable
private fun RecycleChatItem(
text: String,
icon: ImageVector = Icons.Filled.Edit,
selected: Boolean,
onChatClicked: () -> Unit,
onDeleteClicked: () -> Unit
) {
val background = if (selected) {
Modifier.background(MaterialTheme.colorScheme.primaryContainer)
} else {
Modifier
}
Row(
modifier = Modifier
.height(56.dp)
.fillMaxWidth()
.padding(horizontal = 34.dp)
.clip(CircleShape)
.then(background)
.clickable(onClick = onChatClicked),
verticalAlignment = CenterVertically
) {
val iconTint = if (selected) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurfaceVariant
}
Icon(
icon,
tint = iconTint,
modifier = Modifier
.padding(start = 16.dp, top = 16.dp, bottom = 16.dp)
.size(25.dp),
contentDescription = null,
)
Text(
text,
style = MaterialTheme.typography.bodyMedium,
color = if (selected) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurface
},
modifier = Modifier.padding(start = 12.dp).fillMaxWidth(0.85f),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
Spacer(Modifier.weight(0.9f, true))
Icon(
imageVector = Icons.Filled.Delete,
contentDescription = "Delete",
tint = if (selected) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurface
},
modifier = Modifier.clickable { onDeleteClicked() }
)
}
}

@Composable
private fun ProfileItem(text: String, urlToImage: String?, onProfileClicked: () -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ class ConversationViewModel @Inject constructor(
var response: String = ""

for (message in messagesMap[conversationId]!!.reversed()) {
response += """
Human:${message.question.trim()}
Bot:${if (message.answer == "Let me thinking...") "" else message.answer.trim()}"""
response += """Human:${message.question.trim()}
|Bot:${
if (message.answer == "Let me thinking...") ""
else message.answer.trim()
}""".trimMargin()
}

return response
Expand All @@ -150,8 +152,7 @@ Bot:${if (message.answer == "Let me thinking...") "" else message.answer.trim()}

val response: MutableList<MessageTurbo> = mutableListOf(
MessageTurbo(
role = TurboRole.system,
content = "Markdown style if exists code"
role = TurboRole.system, content = "Markdown style if exists code"
)
)

Expand All @@ -166,8 +167,24 @@ Bot:${if (message.answer == "Let me thinking...") "" else message.answer.trim()}
return response.toList()
}

fun deleteMessages(conversationId: String) {

val conversations: MutableList<ConversationModel> = _conversations.value.toMutableList()
val conversationToRemove = conversations.find { it.id == conversationId }

if (conversationToRemove != null) {
conversations.remove(conversationToRemove)
_conversations.value = conversations
}
messageRepo.deleteMessage()
}

suspend fun deleteConversation(conversationId: String) =
conversationRepo.deleteConversation(conversationId)

private suspend fun fetchMessages() {
if (_currentConversation.value.isEmpty() || _messages.value[_currentConversation.value] != null) return
if (_currentConversation.value.isEmpty() ||
_messages.value[_currentConversation.value] != null) return

val flow: Flow<List<MessageModel>> = messageRepo.fetchMessages(_currentConversation.value)

Expand Down