Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ data class NetworkScreenModel(
class NetworkScreenViewModel(di:DI, savedStateHandle: SavedStateHandle): ViewModel() {
// _uiState will be updated whenever there is a change in the UI state
private val _uiState = MutableStateFlow(NetworkScreenModel())

// uiState is a read-only property that shows the current UI state
val uiState: Flow<NetworkScreenModel> = _uiState.asStateFlow()

// di is used to get the AndroidVirtualNode instance
private val node: AndroidVirtualNode by di.instance()
private val appServer: AppServer by di.instance()
Expand All @@ -54,7 +56,8 @@ class NetworkScreenViewModel(di:DI, savedStateHandle: SavedStateHandle): ViewMod

// For each disconnected node, notify DeviceStatusManager
disconnectedNodes.forEach { nodeAddress ->
val ipAddress = InetAddress.getByAddress(nodeAddress.addressToByteArray()).hostAddress
val ipAddress =
InetAddress.getByAddress(nodeAddress.addressToByteArray()).hostAddress
DeviceStatusManager.handleNetworkDisconnect(ipAddress)
Log.d("NetworkScreenViewModel", "Detected disconnection of node: $ipAddress")
}
Expand All @@ -77,11 +80,11 @@ class NetworkScreenViewModel(di:DI, savedStateHandle: SavedStateHandle): ViewMod
allNodes = allNodesWithTest,
// update the ssid of the connecting station
connectingInProgressSsid =
if (nodeState.wifiState.wifiStationState.status == WifiStationState.Status.CONNECTING) {
nodeState.wifiState.wifiStationState.config?.ssid
} else {
null
}
if (nodeState.wifiState.wifiStationState.status == WifiStationState.Status.CONNECTING) {
nodeState.wifiState.wifiStationState.config?.ssid
} else {
null
}
)
}

Expand Down Expand Up @@ -187,13 +190,30 @@ class NetworkScreenViewModel(di:DI, savedStateHandle: SavedStateHandle): ViewMod
delay(30000)
}
}
}

*/
fun getDeviceName(wifiAddress: Int) {
viewModelScope.launch {
val inetAddress = InetAddress.getByAddress(wifiAddress.addressToByteArray())
appServer.sendDeviceName(inetAddress)
}

fun onNodeSelected(ipAddress: String) {
viewModelScope.launch {
try {
val addr = InetAddress.getByName(ipAddress)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

InetAddress.getByName(ipAddress) is a blocking network call. Since viewModelScope.launch defaults to the main dispatcher, this could lead to a NetworkOnMainThreadException and cause your app to crash. You should perform this operation on a background thread using withContext(Dispatchers.IO).

You will also need to add import kotlinx.coroutines.Dispatchers and import kotlinx.coroutines.withContext at the top of the file.

Suggested change
val addr = InetAddress.getByName(ipAddress)
val addr = withContext(Dispatchers.IO) { InetAddress.getByName(ipAddress) }

appServer.requestRemoteUserInfo(addr)
appServer.pushUserInfoTo(addr)
} catch (e: Exception) {
Log.e("NetworkScreenViewModel", "Failed to request user info for $ipAddress", e)
}
}
}
}

fun getDeviceName(wifiAddress: Int) {
viewModelScope.launch {
val inetAddress = InetAddress.getByAddress(wifiAddress.addressToByteArray())
appServer.sendDeviceName(inetAddress)
}
}
}




15 changes: 3 additions & 12 deletions app/src/main/java/com/greybox/projectmesh/views/NetworkScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalSavedStateRegistryOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import com.greybox.projectmesh.ViewModelFactory
import com.greybox.projectmesh.viewModel.HomeScreenViewModel
import com.greybox.projectmesh.viewModel.NetworkScreenModel
import com.greybox.projectmesh.viewModel.NetworkScreenViewModel
import org.kodein.di.compose.localDI
import com.greybox.projectmesh.extension.WifiListItem
import com.greybox.projectmesh.server.AppServer
import java.net.InetAddress
import org.kodein.di.instance


@Composable
fun NetworkScreen(
Expand All @@ -30,8 +27,7 @@ fun NetworkScreen(
) {
// declare the UI state, we can use the uiState to access the current state of the viewModel
val uiState: NetworkScreenModel by viewModel.uiState.collectAsState(initial = NetworkScreenModel())
val di = localDI()
val appServer: AppServer by di.instance()


// display all the connected station
LazyColumn{
Expand All @@ -43,12 +39,7 @@ fun NetworkScreen(
wifiAddress = eachItem.key,
wifiEntry = eachItem.value,
onClick = { ipAddress ->
//request user info when clicking
val addr = InetAddress.getByName(ipAddress)
appServer.requestRemoteUserInfo(addr)
appServer.pushUserInfoTo(addr)

//Navigate to Ping Screen
viewModel.onNodeSelected(ipAddress)
onNodeClick(ipAddress)
}
)
Expand Down