1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-25 17:44:24 +00:00

Begin creating rooms

This commit is contained in:
Steffo 2023-12-02 04:06:29 +01:00
parent 8d32d17d32
commit 5ca508f645
Signed by: steffo
GPG key ID: 2A24051445686895
4 changed files with 64 additions and 5 deletions

View file

@ -1,10 +1,17 @@
package eu.steffo.twom.create package eu.steffo.twom.create
import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
class CreateActivity : ComponentActivity() { class CreateActivity : ComponentActivity() {
companion object {
const val NAME_EXTRA = "name"
const val DESCRIPTION_EXTRA = "description"
const val AVATAR_EXTRA = "avatar"
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -15,8 +22,16 @@ class CreateActivity : ComponentActivity() {
setResult(RESULT_CANCELED) setResult(RESULT_CANCELED)
finish() finish()
}, },
onClickCreate = { onClickCreate = { name: String, description: String, avatarUri: Uri? ->
setResult(RESULT_OK) val resultIntent = Intent()
resultIntent.putExtra(NAME_EXTRA, name)
resultIntent.putExtra(DESCRIPTION_EXTRA, description)
if (avatarUri != null) {
// Kotlin cannot use nullable types in Java interop generics
resultIntent.putExtra(AVATAR_EXTRA, avatarUri)
}
setResult(RESULT_OK, resultIntent)
finish() finish()
}, },
) )

View file

@ -39,7 +39,7 @@ import eu.steffo.twom.theme.TwoMPadding
@Preview @Preview
fun CreateActivityContent( fun CreateActivityContent(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onClickCreate: () -> Unit = {}, onClickCreate: (name: String, description: String, avatarUri: Uri?) -> Unit = { _, _, _ -> },
) { ) {
val context = LocalContext.current val context = LocalContext.current
@ -105,7 +105,9 @@ fun CreateActivityContent(
Button( Button(
modifier = Modifier modifier = Modifier
.fillMaxWidth(), .fillMaxWidth(),
onClick = onClickCreate, onClick = {
onClickCreate(name, description, avatarUri)
},
) { ) {
Text(stringResource(R.string.create_complete_text)) Text(stringResource(R.string.create_complete_text))
} }

View file

@ -1,5 +1,6 @@
package eu.steffo.twom.create package eu.steffo.twom.create
import android.net.Uri
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
@ -11,7 +12,7 @@ import eu.steffo.twom.theme.TwoMTheme
@Preview @Preview
fun CreateActivityScaffold( fun CreateActivityScaffold(
onClickBack: () -> Unit = {}, onClickBack: () -> Unit = {},
onClickCreate: () -> Unit = {}, onClickCreate: (name: String, description: String, avatarUri: Uri?) -> Unit = { _, _, _ -> },
) { ) {
TwoMTheme { TwoMTheme {
Scaffold( Scaffold(
@ -21,6 +22,7 @@ fun CreateActivityScaffold(
content = { content = {
CreateActivityContent( CreateActivityContent(
modifier = Modifier.padding(it), modifier = Modifier.padding(it),
onClickCreate = onClickCreate,
) )
} }
) )

View file

@ -1,6 +1,7 @@
package eu.steffo.twom.main package eu.steffo.twom.main
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
@ -15,6 +16,8 @@ import eu.steffo.twom.matrix.TwoMMatrix
import eu.steffo.twom.room.RoomActivity import eu.steffo.twom.room.RoomActivity
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomPreset
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
@ -137,6 +140,43 @@ class MainActivity : ComponentActivity() {
private fun onCreate(result: ActivityResult) { private fun onCreate(result: ActivityResult) {
Log.d("Main", "Received result from create activity: $result") Log.d("Main", "Received result from create activity: $result")
if (result.resultCode == RESULT_OK) {
val intent: Intent = result.data!!
val name = intent.getStringExtra(CreateActivity.NAME_EXTRA)
val description = intent.getStringExtra(CreateActivity.DESCRIPTION_EXTRA)
@Suppress("DEPRECATION") val avatarUri =
intent.getParcelableExtra<Uri>(CreateActivity.AVATAR_EXTRA)
if (name == null) {
Log.w("Main", "Result from create activity did not have `name` extra set")
return
}
if (description == null) {
Log.w("Main", "Result from create activity did not have `description` extra set")
return
}
lifecycleScope.launch {
val currentSession = session
val createRoomParams = CreateRoomParams()
createRoomParams.name = name
createRoomParams.topic = description
createRoomParams.avatarUri = avatarUri
createRoomParams.preset = CreateRoomPreset.PRESET_PRIVATE_CHAT
createRoomParams.roomType = TwoMMatrix.ROOM_TYPE
Log.d(
"Main",
"Creating room '$name' with description '$description' and avatar '$avatarUri'..."
)
val roomId = currentSession!!.roomService().createRoom(createRoomParams)
Log.d(
"Main",
"Created room '$name' with description '$description' and avatar '$avatarUri': $roomId"
)
}
}
} }
private fun resetContent() { private fun resetContent() {