very basic screen for the room creation
This commit is contained in:
parent
7d0f3e88e0
commit
7ed38b2624
6 changed files with 147 additions and 20 deletions
|
@ -13,7 +13,7 @@ import androidx.activity.compose.setContent
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import com.faraphel.tasks_valider.connectivity.wifi_p2p.WifiP2pHelper
|
import com.faraphel.tasks_valider.connectivity.wifi_p2p.WifiP2pHelper
|
||||||
import com.faraphel.tasks_valider.database.Database
|
import com.faraphel.tasks_valider.database.Database
|
||||||
import com.faraphel.tasks_valider.ui.screen.RoomScreen
|
import com.faraphel.tasks_valider.ui.screen.room.RoomScreen
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
|
@ -119,10 +119,7 @@ class WifiP2pHelper(
|
||||||
*
|
*
|
||||||
* Connect to another device, allowing for a communication using Sockets
|
* Connect to another device, allowing for a communication using Sockets
|
||||||
*/
|
*/
|
||||||
fun connect(deviceAddress: String, listener: WifiP2pManager.ActionListener? = null) {
|
fun connect(config: WifiP2pConfig, listener: WifiP2pManager.ActionListener? = null) {
|
||||||
val config = WifiP2pConfig().apply {
|
|
||||||
this.deviceAddress = deviceAddress
|
|
||||||
}
|
|
||||||
this.manager.connect(this.channel, config, listener)
|
this.manager.connect(this.channel, config, listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,4 +159,55 @@ class WifiP2pHelper(
|
||||||
fun requestGroupInfo(listener: WifiP2pManager.GroupInfoListener? = null) {
|
fun requestGroupInfo(listener: WifiP2pManager.GroupInfoListener? = null) {
|
||||||
this.manager.requestGroupInfo(this.channel, listener)
|
this.manager.requestGroupInfo(this.channel, listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper around WifiP2pManager::createGroup
|
||||||
|
*
|
||||||
|
* Create a new WiFi-Direct group.
|
||||||
|
*/
|
||||||
|
fun createGroup(listener: WifiP2pManager.ActionListener? = null) {
|
||||||
|
this.manager.createGroup(this.channel, listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper around WifiP2pManager::removeGroup
|
||||||
|
*
|
||||||
|
* Disconnect from the current WiFi-Direct group.
|
||||||
|
*/
|
||||||
|
fun removeGroup(listener: WifiP2pManager.ActionListener? = null) {
|
||||||
|
this.manager.removeGroup(this.channel, listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new WiFi-Direct group.
|
||||||
|
* If already connected to a group, quit it first.
|
||||||
|
*
|
||||||
|
* @param listener: the createGroup listener
|
||||||
|
* @param onFailure: error handler for the removeGroup event
|
||||||
|
*/
|
||||||
|
fun recreateGroup(
|
||||||
|
listener: WifiP2pManager.ActionListener? = null,
|
||||||
|
onFailure: ((Int) -> Unit)? = null
|
||||||
|
) {
|
||||||
|
// get the current group information
|
||||||
|
this.requestGroupInfo { group ->
|
||||||
|
// if a group exist, quit it
|
||||||
|
if (group != null)
|
||||||
|
this.removeGroup(object : WifiP2pManager.ActionListener {
|
||||||
|
override fun onSuccess() {
|
||||||
|
// once left, create the group
|
||||||
|
this@WifiP2pHelper.createGroup(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(reason: Int) {
|
||||||
|
// call the handler
|
||||||
|
onFailure?.invoke(reason)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
else
|
||||||
|
// create the group
|
||||||
|
this.createGroup(listener)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,17 +1,19 @@
|
||||||
package com.faraphel.tasks_valider.ui.screen
|
package com.faraphel.tasks_valider.ui.screen.room
|
||||||
|
|
||||||
import android.net.wifi.p2p.WifiP2pDeviceList
|
import android.net.wifi.p2p.WifiP2pDeviceList
|
||||||
import android.net.wifi.p2p.WifiP2pManager
|
import android.net.wifi.p2p.WifiP2pManager
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import com.faraphel.tasks_valider.connectivity.wifi_p2p.WifiP2pHelper
|
import com.faraphel.tasks_valider.connectivity.wifi_p2p.WifiP2pHelper
|
||||||
import com.faraphel.tasks_valider.ui.widgets.connectivity.WifiP2pDeviceListWidget
|
import com.faraphel.tasks_valider.ui.widgets.connectivity.WifiP2pDeviceListWidget
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This screen allow the user to join a room
|
||||||
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun RoomClientScreen(wifiP2pHelper: WifiP2pHelper) {
|
fun RoomClientScreen(wifiP2pHelper: WifiP2pHelper) {
|
||||||
val peers = remember { mutableStateOf<WifiP2pDeviceList?>(null) }
|
val peers = remember { mutableStateOf<WifiP2pDeviceList?>(null) }
|
|
@ -0,0 +1,61 @@
|
||||||
|
package com.faraphel.tasks_valider.ui.screen.room
|
||||||
|
|
||||||
|
import android.net.wifi.p2p.WifiP2pManager
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.material3.*
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import com.faraphel.tasks_valider.connectivity.wifi_p2p.WifiP2pHelper
|
||||||
|
import com.faraphel.tasks_valider.ui.screen.tasks.TaskGroupScreen
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This screen allow the user to create a room
|
||||||
|
*/
|
||||||
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@Composable
|
||||||
|
fun RoomHostScreen(wifiP2pHelper: WifiP2pHelper) {
|
||||||
|
val expanded = remember { mutableStateOf(false) }
|
||||||
|
val isCreated = remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
// if the group have been created, display the group screen
|
||||||
|
if (isCreated.value) {
|
||||||
|
TaskGroupScreen()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Column {
|
||||||
|
ExposedDropdownMenuBox(
|
||||||
|
expanded = expanded.value,
|
||||||
|
onExpandedChange = { value -> expanded.value = !value }
|
||||||
|
) {
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = { Text("ISRI") },
|
||||||
|
onClick = {}
|
||||||
|
)
|
||||||
|
DropdownMenuItem(
|
||||||
|
text = { Text("MIAGE") },
|
||||||
|
onClick = {}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
Button(onClick = {
|
||||||
|
// create a new WiFi-Direct group
|
||||||
|
wifiP2pHelper.recreateGroup(object : WifiP2pManager.ActionListener {
|
||||||
|
override fun onSuccess() {
|
||||||
|
Log.i("room", "group created !")
|
||||||
|
// mark the group as created
|
||||||
|
isCreated.value = true
|
||||||
|
}
|
||||||
|
override fun onFailure(reason: Int) {
|
||||||
|
Log.e("room", "error while creating group. Reason: $reason")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}) {
|
||||||
|
Text("Create")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.faraphel.tasks_valider.ui.screen
|
package com.faraphel.tasks_valider.ui.screen.room
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
|
@ -10,7 +10,6 @@ import com.faraphel.tasks_valider.connectivity.wifi_p2p.WifiP2pHelper
|
||||||
|
|
||||||
|
|
||||||
enum class RoomScreenType {
|
enum class RoomScreenType {
|
||||||
UNDEFINED,
|
|
||||||
HOST,
|
HOST,
|
||||||
CLIENT
|
CLIENT
|
||||||
}
|
}
|
||||||
|
@ -21,10 +20,21 @@ enum class RoomScreenType {
|
||||||
*/
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun RoomScreen(wifiP2pHelper: WifiP2pHelper) {
|
fun RoomScreen(wifiP2pHelper: WifiP2pHelper) {
|
||||||
val roomMode = remember { mutableStateOf(RoomScreenType.UNDEFINED) }
|
val roomMode = remember { mutableStateOf<RoomScreenType?>(null) }
|
||||||
|
|
||||||
when (roomMode.value) {
|
when (roomMode.value) {
|
||||||
RoomScreenType.UNDEFINED -> Column {
|
RoomScreenType.CLIENT -> {
|
||||||
|
// Client mode
|
||||||
|
RoomClientScreen(wifiP2pHelper)
|
||||||
|
}
|
||||||
|
RoomScreenType.HOST -> {
|
||||||
|
// Host mode
|
||||||
|
RoomHostScreen(wifiP2pHelper)
|
||||||
|
}
|
||||||
|
|
||||||
|
null -> {
|
||||||
|
// let the user decide which room mode he which to use
|
||||||
|
Column {
|
||||||
Button(onClick = { roomMode.value = RoomScreenType.CLIENT }) {
|
Button(onClick = { roomMode.value = RoomScreenType.CLIENT }) {
|
||||||
Text(text = "Join a room")
|
Text(text = "Join a room")
|
||||||
}
|
}
|
||||||
|
@ -32,13 +42,6 @@ fun RoomScreen(wifiP2pHelper: WifiP2pHelper) {
|
||||||
Text(text = "Create a room")
|
Text(text = "Create a room")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RoomScreenType.CLIENT -> {
|
|
||||||
// Client mode
|
|
||||||
RoomClientScreen(wifiP2pHelper)
|
|
||||||
}
|
|
||||||
RoomScreenType.HOST -> {
|
|
||||||
// Host mode
|
|
||||||
TODO("Faraphel: host mode not implemented")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.faraphel.tasks_valider.ui.screen.tasks
|
||||||
|
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This screen let the user decide which student team he wants to interact with
|
||||||
|
*/
|
||||||
|
@Composable
|
||||||
|
fun TaskGroupScreen() {
|
||||||
|
Text(text = "Task Group")
|
||||||
|
}
|
Loading…
Reference in a new issue