forked from android/compose-samples
/
MainViewModel.kt
110 lines (94 loc) · 3.75 KB
/
MainViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.compose.jetchat
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.compose.jetchat.conversation.ConversationUiState
import com.example.compose.jetchat.conversation.Message
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
/**
* Used to communicate between screens.
*/
class MainViewModel : ViewModel() {
private val _drawerShouldBeOpened = MutableStateFlow(false)
val drawerShouldBeOpened = _drawerShouldBeOpened.asStateFlow()
fun openDrawer() {
_drawerShouldBeOpened.value = true
}
fun resetOpenDrawerAction() {
_drawerShouldBeOpened.value = false
}
//----------Below code added for OpenAI integration-------------
var uiState = ConversationUiState(
initialMessages = listOf(Message("bot", "Welcome to JetchatGPT!", "8:07 pm")),
channelName = "#jetchatgpt",
channelMembers = 2
)
private var openAIWrapper = OpenAIWrapper()
var botIsTyping by mutableStateOf(false)
private set
fun onMessageSent(content: String) {
// add user message to chat history
addMessage("me", content)
// start typing animation while request loads
botIsTyping = true
// fetch openai response and add to chat history
viewModelScope.launch {
// if user message contains "image" keyword, target image endpoint, otherwise target chat endpoint
if (content.contains("image", ignoreCase = true)) {
var responseContent: String
var imageUrl: String? = null
try {
imageUrl = openAIWrapper.imageURL(content)
responseContent = "Generated image:"
} catch (e: Exception) {
responseContent = "Sorry, there was an error processing your request: ${e.message}"
}
botIsTyping = false
addMessage(author = "bot", content = responseContent, imageUrl = imageUrl)
} else {
val chatResponse = try {
openAIWrapper.chat(content)
} catch (e: Exception) {
"Sorry, there was an error processing your request: ${e.message}"
}
botIsTyping = false
addMessage(author = "bot", content = chatResponse)
}
}
}
private fun addMessage(author: String, content: String, imageUrl: String? = null) {
// calculate message timestamp
val currentTime = Calendar.getInstance().time
val dateFormat = SimpleDateFormat("h:mm a", Locale.getDefault())
val timeNow = dateFormat.format(currentTime)
val message = Message(
author = author,
content = content,
timestamp = timeNow,
imageUrl = imageUrl
)
uiState.addMessage(message)
}
}