Skip to content
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

Feat/conversations by device #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

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

1 change: 1 addition & 0 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

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

3 changes: 2 additions & 1 deletion .idea/misc.xml

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

17 changes: 17 additions & 0 deletions .idea/runConfigurations.xml

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Overall, Compose ChatGPT Kotlin is a powerful and flexible chatbot solution that


## Requirements
- Android Studio Arctic Fox or later
- Android Studio Ladybug or later
- OpenAI API Key
- Firebase

Expand Down
18 changes: 9 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {

android {
namespace 'com.chatgptlite.wanted'
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "com.chatgptlite.wanted"
Expand Down Expand Up @@ -50,7 +50,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
kotlinCompilerExtensionVersion '1.5.11'
}
packagingOptions {
resources {
Expand All @@ -63,18 +63,18 @@ dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.ui:ui:1.7.5"
implementation "androidx.compose.ui:ui-tooling-preview:1.7.5"
implementation 'androidx.compose.material3:material3:1.1.0-beta01'
implementation 'androidx.compose.ui:ui-viewbinding:1.4.0'
implementation 'androidx.compose.ui:ui-util:1.4.0'
implementation 'androidx.compose.material:material-icons-extended:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.7.5"
debugImplementation "androidx.compose.ui:ui-tooling:1.7.5"
debugImplementation "androidx.compose.ui:ui-test-manifest:1.7.5"
def nav_version = "2.5.3"
implementation("androidx.navigation:navigation-compose:$nav_version")
implementation("io.coil-kt:coil-compose:2.3.0")
Expand All @@ -92,9 +92,9 @@ dependencies {
implementation 'com.google.firebase:firebase-firestore-ktx'

// Dependency injection
implementation "com.google.dagger:hilt-android:2.44"
implementation "com.google.dagger:hilt-android:2.48"
implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
kapt "com.google.dagger:hilt-compiler:2.44"
kapt "com.google.dagger:hilt-compiler:2.48"
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.chatgptlite.wanted.data.remote
import com.chatgptlite.wanted.models.ConversationModel

interface ConversationRepository {
suspend fun fetchConversations() : MutableList<ConversationModel>
suspend fun fetchConversations(deviceId: String) : MutableList<ConversationModel>
fun newConversation(conversation: ConversationModel) : ConversationModel
suspend fun deleteConversation(conversationId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ class ConversationRepositoryImpl @Inject constructor(
private val fsInstance: FirebaseFirestore,
) : ConversationRepository {

override suspend fun fetchConversations(): MutableList<ConversationModel> {

if (getFireBaseSnapShot().documents.isNotEmpty()) {
val documents = getFireBaseSnapShot().documents
override suspend fun fetchConversations(deviceId: String): MutableList<ConversationModel> {
val snapshot = fsInstance.collection(conversationCollection)
.whereEqualTo("deviceId", deviceId)
.get()
.await()

return documents.map {
if (snapshot.documents.isNotEmpty()) {
return snapshot.documents.mapNotNull {
it.toObject(ConversationModel::class.java)
}.toList() as MutableList<ConversationModel>
}.toMutableList()
}

return mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import java.util.*
data class ConversationModel(
var id: String = Date().time.toString(),
var title: String = "",
var deviceId: String = "",
var createdAt: Date = Date(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.material3.*
import androidx.compose.material3.DrawerValue.Closed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import com.chatgptlite.wanted.ui.conversations.ConversationViewModel
import com.chatgptlite.wanted.ui.theme.BackGroundColor
Expand All @@ -24,9 +25,10 @@ fun AppScaffold(
content: @Composable () -> Unit,
) {
val scope = rememberCoroutineScope()
val context = LocalContext.current

scope.launch {
conversationViewModel.initialize()
conversationViewModel.initialize(context = context)
}

ModalNavigationDrawer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package com.chatgptlite.wanted.ui.conversations

import android.annotation.SuppressLint
import android.content.Context
import android.provider.Settings
import androidx.lifecycle.ViewModel
import com.chatgptlite.wanted.data.remote.ConversationRepository
import com.chatgptlite.wanted.data.remote.MessageRepository
Expand Down Expand Up @@ -31,6 +34,7 @@ class ConversationViewModel @Inject constructor(
MutableStateFlow(HashMap())
private val _isFetching: MutableStateFlow<Boolean> = MutableStateFlow(false)
private val _isFabExpanded = MutableStateFlow(false)
private val _deviceId: MutableStateFlow<String> = MutableStateFlow("")

val currentConversationState: StateFlow<String> = _currentConversation.asStateFlow()
val conversationsState: StateFlow<MutableList<ConversationModel>> = _conversations.asStateFlow()
Expand All @@ -41,12 +45,12 @@ class ConversationViewModel @Inject constructor(

private var stopReceivingResults = false

suspend fun initialize(context: Context) {
_deviceId.value = _getDeviceUniqueId(context)


suspend fun initialize() {
_isFetching.value = true

_conversations.value = conversationRepo.fetchConversations()
_conversations.value = conversationRepo.fetchConversations(deviceId = _deviceId.value)

if (_conversations.value.isNotEmpty()) {
_currentConversation.value = _conversations.value.first().id
Expand Down Expand Up @@ -113,6 +117,7 @@ class ConversationViewModel @Inject constructor(
val newConversation: ConversationModel = ConversationModel(
id = _currentConversation.value,
title = title,
deviceId = _deviceId.value,
createdAt = Date(),
)

Expand Down Expand Up @@ -223,10 +228,17 @@ class ConversationViewModel @Inject constructor(

_messages.value = messagesMap
}

fun stopReceivingResults() {
stopReceivingResults = true
}

private fun setFabExpanded(expanded: Boolean) {
_isFabExpanded.value = expanded
}

@SuppressLint("HardwareIds")
private fun _getDeviceUniqueId(context: Context): String {
return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
}
}
Loading