wip: wiring network client
This commit is contained in:
@@ -9,7 +9,6 @@ import androidx.compose.foundation.layout.wrapContentSize
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
@@ -24,7 +23,6 @@ import xyz.magicalbits.smsremote.components.JetchatAppBar
|
||||
import xyz.magicalbits.smsremote.theme.JetchatTheme
|
||||
import kotlin.getValue
|
||||
import androidx.navigation.findNavController
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class DeviceFragment : Fragment() {
|
||||
private val viewModel: DeviceViewModel by viewModels()
|
||||
@@ -33,8 +31,10 @@ class DeviceFragment : Fragment() {
|
||||
override fun onAttach(context: Context) {
|
||||
super.onAttach(context)
|
||||
// Consider using safe args plugin
|
||||
val deviceName = arguments?.getString("deviceName")
|
||||
viewModel.setDeviceId(deviceName)
|
||||
val deviceId = arguments?.getString("deviceId")
|
||||
val name = arguments?.getString("name")
|
||||
val type = arguments?.getString("type")
|
||||
viewModel.setDeviceData(deviceId, name, type)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterial3Api::class)
|
||||
@@ -65,6 +65,7 @@ class DeviceFragment : Fragment() {
|
||||
|
||||
JetchatTheme {
|
||||
if (deviceData == null) {
|
||||
println("calling device error")
|
||||
DeviceError()
|
||||
} else {
|
||||
val navController: NavController = rootView.findNavController()
|
||||
|
||||
@@ -4,26 +4,39 @@ import androidx.compose.runtime.Immutable
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import xyz.magicalbits.smsremote.data.a14Device
|
||||
import xyz.magicalbits.smsremote.data.iPhoneDevice
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.magicalbits.smsremote.network.NetworkClient
|
||||
|
||||
class DeviceViewModel : ViewModel() {
|
||||
private var deviceId: String = ""
|
||||
private val _deviceData = MutableLiveData<DeviceScreenState>()
|
||||
val deviceData: LiveData<DeviceScreenState> = _deviceData
|
||||
|
||||
fun setDeviceId(newDeviceId: String?) {
|
||||
if (newDeviceId != null) {
|
||||
fun setDeviceData(newDeviceId: String?, name: String?, type: String?) {
|
||||
if (newDeviceId != null && name != null) {
|
||||
deviceId = newDeviceId
|
||||
}
|
||||
|
||||
// placeholder since there's no API reading logic yet
|
||||
_deviceData.value =
|
||||
if (deviceId == "Samsung A14") {
|
||||
a14Device
|
||||
} else {
|
||||
iPhoneDevice
|
||||
var simCardDtoList: List<NetworkClient.SimCardDto> = listOf()
|
||||
viewModelScope.launch {
|
||||
val networkClient = NetworkClient()
|
||||
simCardDtoList = networkClient.getSimCardsByAccessKey(deviceId)
|
||||
println("sims: $simCardDtoList")
|
||||
}.invokeOnCompletion {
|
||||
// FIXME waiting for the response causes brief moment of DeviceError() before _deviceData is updated ...
|
||||
// a solution: caching SIM phone numbers of all discovered devices locally on startup and updating them
|
||||
// only on startup (implicit behavior) or with a pull-down refresh action (not done yet)
|
||||
|
||||
// placeholder since there's no API reading logic yet
|
||||
_deviceData.value =
|
||||
DeviceScreenState(
|
||||
deviceId = deviceId,
|
||||
name = name,
|
||||
phoneNumbers = simCardDtoList.map { it.phone_number },
|
||||
)
|
||||
println("sims live: ${_deviceData.value!!.phoneNumbers}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user