@@ -6,6 +6,11 @@ import android.util.Log
6
6
import androidx.lifecycle.LiveData
7
7
import androidx.lifecycle.MutableLiveData
8
8
import androidx.lifecycle.ViewModel
9
+ import androidx.lifecycle.viewModelScope
10
+ import kotlinx.coroutines.delay
11
+ import kotlinx.coroutines.flow.MutableStateFlow
12
+ import kotlinx.coroutines.flow.StateFlow
13
+ import kotlinx.coroutines.launch
9
14
10
15
class ChatViewModel : ViewModel () {
11
16
private val TAG = " ChatViewModel"
@@ -49,42 +54,86 @@ class ChatViewModel : ViewModel() {
49
54
val mockResponse =
50
55
" 天空呈现蓝色的原因主要与光的散射有关。当太阳光进入大气层后,大气中的气体分子和悬浮微粒会对阳光进行散射" .toCharArray()
51
56
val sb = StringBuffer ()
57
+ sendData(sb, mockResponse)
58
+ }
52
59
53
- val subThread = true
54
- if (subThread) {
55
- Thread {
56
- sendData(sb, mockResponse, subThread)
57
- }.start()
58
- } else {
59
- sendData(sb, mockResponse, subThread)
60
+ private fun sendData (sb : StringBuffer , mockResponse : CharArray ) {
61
+ viewModelScope.launch {
62
+ for (c in mockResponse) {
63
+ val history = _messageList .value ? : ArrayList ()
64
+ val lastMsg = history.last()
65
+ sb.append(c)
66
+ kkk.postValue(sb.toString())
67
+ if (lastMsg.sender == " Bot" ) {
68
+ val newMsg = ChatMessage (" Bot" , sb.toString(), false )
69
+ history[history.size - 1 ] = newMsg
70
+
71
+ _messageList .value = ArrayList (history)
72
+
73
+ } else {
74
+ val newMsg = ChatMessage (" Bot" , sb.toString(), false )
75
+ history.add(newMsg)
76
+ _messageList .value = history
77
+ }
78
+ // _messageList.postValue(history)
79
+
80
+ delay(10 )
81
+ Log .d(TAG , " history ${_messageList .value} " )
82
+ }
60
83
}
61
84
62
- //
63
85
64
86
}
65
87
66
- private fun sendData (sb : StringBuffer , mockResponse : CharArray , subThread : Boolean ) {
67
-
68
- for (c in mockResponse) {
69
- val history = _messageList .value ? : ArrayList ()
70
- val lastMsg = history.last()
71
- sb.append(c)
72
- kkk.postValue(sb.toString())
73
- if (lastMsg.sender == " Bot" ) {
74
- val newMsg = ChatMessage (" Bot" , sb.toString(), false )
75
- history[history.size - 1 ] = newMsg
76
- } else {
77
- val newMsg = ChatMessage (" Bot" , sb.toString(), false )
78
- history.add(newMsg)
88
+ private val _messages = MutableStateFlow <List <ChatMessage >>(emptyList())
89
+ val messages: StateFlow <List <ChatMessage >> = _messages
90
+
91
+ fun addMessage (message : ChatMessage ) {
92
+ _messages .value = _messages .value + message
93
+ }
94
+
95
+ // 更新特定 index 的消息
96
+ fun updateMessageAt (index : Int , newMessage : ChatMessage ) {
97
+ _messages .value = _messages .value.toMutableList().apply {
98
+ if (index in indices) {
99
+ this [index] = newMessage
79
100
}
80
- Log .d(TAG , " history $history " )
101
+ }
102
+ }
81
103
82
- if (subThread) {
83
- _messageList .postValue(history)
84
- Thread .sleep(10 )
85
- } else {
86
- _messageList .value = history
104
+ // 模拟流式添加消息
105
+ fun startReceivingMessages () {
106
+ var i = 0
107
+ val mockResponse =
108
+ " 天空呈现蓝色的原因主要与光的散射有关。当太阳光进入大气层后,大气中的气体分子和悬浮微粒会对阳光进行散射" .toCharArray()
109
+ val sb = StringBuffer ()
110
+
111
+
112
+
113
+ viewModelScope.launch {
114
+ for (c in mockResponse) {
115
+ val history = _messages .value ? : ArrayList ()
116
+ val lastMsg = history.last()
117
+ sb.append(c)
118
+ kkk.postValue(sb.toString())
119
+ if (lastMsg.sender == " Bot" ) {
120
+ val newMsg = ChatMessage (" Bot" , sb.toString(), false )
121
+ updateMessageAt(history.size - 1 , newMsg)
122
+ } else {
123
+ val newMsg = ChatMessage (" Bot" , sb.toString(), false )
124
+ addMessage(newMsg)
125
+ }
126
+ Log .d(TAG , " history $history " )
127
+ delay(20 )
87
128
}
129
+
130
+
88
131
}
89
132
}
133
+
134
+ fun queryFlow (userQuery : String ) {
135
+ val userMsg = ChatMessage (" IAM四十二" , userQuery, true )
136
+ addMessage(userMsg)
137
+ startReceivingMessages()
138
+ }
90
139
}
0 commit comments