load conversation from API instead of FakeData
This commit is contained in:
@@ -92,11 +92,12 @@ import xyz.magicalbits.smsremote.components.JetchatAppBar
|
||||
import xyz.magicalbits.smsremote.data.exampleUiState
|
||||
import xyz.magicalbits.smsremote.theme.JetchatTheme
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.magicalbits.smsremote.data.exampleUiStateNew
|
||||
|
||||
/**
|
||||
* Entry point for a conversation screen.
|
||||
*
|
||||
* @param uiState [ConversationUiState] that contains messages to display
|
||||
* @param uiState [ConversationScreenState] that contains messages to display
|
||||
* @param navigateToProfile User action when navigation to a profile is requested
|
||||
* @param modifier [Modifier] to apply to this layout node
|
||||
* @param onNavIconPressed Sends an event up when the user clicks on the menu
|
||||
@@ -104,7 +105,7 @@ import kotlinx.coroutines.launch
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun ConversationContent(
|
||||
uiState: ConversationUiState,
|
||||
uiState: ConversationScreenState,
|
||||
navigateToProfile: (String) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
onNavIconPressed: () -> Unit = { },
|
||||
@@ -167,8 +168,9 @@ fun ConversationContent(
|
||||
Scaffold(
|
||||
topBar = {
|
||||
ChannelNameBar(
|
||||
channelName = uiState.channelName,
|
||||
channelMembers = uiState.channelMembers,
|
||||
channelName = uiState.phoneNumber,
|
||||
// TODO remove?
|
||||
channelMembers = 2,
|
||||
onNavIconPressed = onNavIconPressed,
|
||||
scrollBehavior = scrollBehavior,
|
||||
)
|
||||
@@ -540,7 +542,7 @@ fun ClickableMessage(message: Message, isUserMe: Boolean, authorClicked: (String
|
||||
fun ConversationPreview() {
|
||||
JetchatTheme {
|
||||
ConversationContent(
|
||||
uiState = exampleUiState,
|
||||
uiState = exampleUiStateNew,
|
||||
navigateToProfile = { },
|
||||
)
|
||||
}
|
||||
|
||||
+8
-11
@@ -23,6 +23,8 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewGroup.LayoutParams
|
||||
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.fragment.app.Fragment
|
||||
@@ -31,11 +33,12 @@ import androidx.navigation.findNavController
|
||||
import xyz.magicalbits.smsremote.MainViewModel
|
||||
import xyz.magicalbits.smsremote.R
|
||||
import xyz.magicalbits.smsremote.data.exampleUiState
|
||||
import xyz.magicalbits.smsremote.data.exampleUiState2
|
||||
import xyz.magicalbits.smsremote.data.exampleUiStateNew
|
||||
import xyz.magicalbits.smsremote.theme.JetchatTheme
|
||||
|
||||
class ConversationFragment : Fragment() {
|
||||
private val activityViewModel: MainViewModel by activityViewModels()
|
||||
private val conversationViewModel: ConversationViewModel by activityViewModels()
|
||||
|
||||
var phoneNumber: String = ""
|
||||
|
||||
@@ -43,8 +46,9 @@ class ConversationFragment : Fragment() {
|
||||
super.onAttach(context)
|
||||
// Consider using safe args plugin
|
||||
val phoneNumber = arguments?.getString("phoneNumber")
|
||||
// viewModel.setDeviceId(deviceId)
|
||||
this.phoneNumber = phoneNumber!!
|
||||
// update view model with latest messages
|
||||
conversationViewModel.setConversationData(phoneNumber)
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
@@ -55,18 +59,11 @@ class ConversationFragment : Fragment() {
|
||||
ComposeView(inflater.context).apply {
|
||||
layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT)
|
||||
|
||||
val uiState =
|
||||
if (phoneNumber == "+420123456789") {
|
||||
exampleUiState
|
||||
} else {
|
||||
exampleUiState2
|
||||
}
|
||||
uiState.channelName = phoneNumber
|
||||
|
||||
setContent {
|
||||
val conversationData by conversationViewModel.conversationData.observeAsState()
|
||||
JetchatTheme {
|
||||
ConversationContent(
|
||||
uiState = uiState,
|
||||
uiState = conversationData ?: exampleUiStateNew,
|
||||
navigateToProfile = { user ->
|
||||
// Click callback
|
||||
val bundle = bundleOf("userId" to user)
|
||||
|
||||
+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