load conversation from API instead of FakeData
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
package xyz.magicalbits.smsremote.conversation
|
||||
|
||||
import androidx.compose.runtime.toMutableStateList
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.magicalbits.smsremote.network.NetworkClient
|
||||
|
||||
class ConversationViewModel : ViewModel() {
|
||||
private var phoneNumber: String = ""
|
||||
private val _conversationData = MutableLiveData<ConversationScreenState>()
|
||||
val conversationData: LiveData<ConversationScreenState> = _conversationData
|
||||
|
||||
fun setConversationData(phoneNumber: String?) {
|
||||
if (phoneNumber != null) {
|
||||
this.phoneNumber = phoneNumber
|
||||
|
||||
var messageDtoList: List<NetworkClient.SmsMessageDto> = listOf()
|
||||
viewModelScope.launch {
|
||||
val networkClient = NetworkClient()
|
||||
messageDtoList = networkClient.getSmsMessagesByLocalPhoneNumber(phoneNumber)
|
||||
}.invokeOnCompletion {
|
||||
_conversationData.value =
|
||||
ConversationScreenState(
|
||||
phoneNumber = this.phoneNumber,
|
||||
initialMessages = messageDtoList.map {
|
||||
Message(
|
||||
if (it.msg_type == "INCOMING") {
|
||||
it.remote_phone_number
|
||||
} else {
|
||||
it.local_phone_number
|
||||
},
|
||||
it.content,
|
||||
// FIXME convert to HH:MM AM/PM
|
||||
it.ts_sent.toString(),
|
||||
null,
|
||||
)
|
||||
},
|
||||
)
|
||||
println("sims live: ${_conversationData.value!!.initialMessages}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class ConversationScreenState(
|
||||
val phoneNumber: String,
|
||||
val initialMessages: List<Message>,
|
||||
) {
|
||||
private val _messages: MutableList<Message> = initialMessages.toMutableStateList()
|
||||
|
||||
val messages: List<Message> = _messages
|
||||
|
||||
fun addMessage(msg: Message) {
|
||||
_messages.add(0, msg)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user