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 @@ -8,6 +8,8 @@ import com.greybox.projectmesh.DeviceStatusManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.delay
import org.kodein.di.DI
import com.greybox.projectmesh.server.AppServer
Expand All @@ -32,8 +34,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 +58,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 +82,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 +192,32 @@ 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 {
Comment thread
Kteja03 marked this conversation as resolved.
try {
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