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

Checkpoint because GPG key generation distracted me from what I was doing

This commit is contained in:
Steffo 2024-01-13 10:31:59 +01:00
parent 01e744883f
commit d91d0cfc5f
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
11 changed files with 152 additions and 153 deletions

View file

@ -49,7 +49,7 @@
android:theme="@style/Theme.TwoM" /> android:theme="@style/Theme.TwoM" />
<activity <activity
android:name=".activities.CreateRoomActivity" android:name=".activities.ConfigureRoomActivity"
android:theme="@style/Theme.TwoM" /> android:theme="@style/Theme.TwoM" />
<activity <activity

View file

@ -2,6 +2,7 @@ package eu.steffo.twom.activities
import android.content.Context import android.content.Context
import android.content.Intent 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
@ -9,32 +10,38 @@ import androidx.activity.result.contract.ActivityResultContract
import eu.steffo.twom.composables.createroom.CreateRoomScaffold import eu.steffo.twom.composables.createroom.CreateRoomScaffold
data class CreateRoomActivityResult( class ConfigureRoomActivity : ComponentActivity() {
val name: String,
val description: String,
val avatarUri: String?,
)
class CreateRoomActivity : ComponentActivity() {
companion object { companion object {
const val NAME_EXTRA = "name" const val NAME_EXTRA = "name"
const val DESCRIPTION_EXTRA = "description" const val DESCRIPTION_EXTRA = "description"
const val AVATAR_EXTRA = "avatar" const val AVATAR_EXTRA = "avatar"
} }
class Contract : ActivityResultContract<Unit, CreateRoomActivityResult?>() { data class Result(
val name: String,
val description: String,
val avatarUri: Uri?,
)
class Contract : ActivityResultContract<Unit, Result?>() {
override fun createIntent(context: Context, input: Unit): Intent { override fun createIntent(context: Context, input: Unit): Intent {
return Intent(context, CreateRoomActivity::class.java) return Intent(context, ConfigureRoomActivity::class.java)
} }
override fun parseResult(resultCode: Int, intent: Intent?): CreateRoomActivityResult? { override fun parseResult(resultCode: Int, intent: Intent?): Result? {
return when (resultCode) { return when (resultCode) {
RESULT_OK -> CreateRoomActivityResult( RESULT_OK -> {
name = intent!!.getStringExtra(NAME_EXTRA)!!, intent!!
description = intent.getStringExtra(DESCRIPTION_EXTRA)!!, val name = intent.getStringExtra(NAME_EXTRA)!!
avatarUri = intent.getStringExtra(AVATAR_EXTRA), val description = intent.getStringExtra(DESCRIPTION_EXTRA)!!
) val avatar = intent.getStringExtra(AVATAR_EXTRA)
Result(
name = name,
description = description,
avatarUri = if (avatar != null) Uri.parse(avatar) else null,
)
}
else -> null else -> null
} }

View file

@ -16,7 +16,7 @@ class InviteUserActivity : ComponentActivity() {
class Contract : ActivityResultContract<Unit, String?>() { class Contract : ActivityResultContract<Unit, String?>() {
override fun createIntent(context: Context, input: Unit): Intent { override fun createIntent(context: Context, input: Unit): Intent {
return Intent(context, CreateRoomActivity::class.java) return Intent(context, ConfigureRoomActivity::class.java)
} }
override fun parseResult(resultCode: Int, intent: Intent?): String? { override fun parseResult(resultCode: Int, intent: Intent?): String? {

View file

@ -1,12 +1,9 @@
package eu.steffo.twom.activities package eu.steffo.twom.activities
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
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResult
import androidx.core.net.toFile import androidx.core.net.toFile
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import eu.steffo.twom.composables.main.MainScaffold import eu.steffo.twom.composables.main.MainScaffold
@ -74,118 +71,6 @@ class MainActivity : ComponentActivity() {
} }
} }
private fun onCreate(result: ActivityResult) {
Log.d("Main", "Received result from create activity: $result")
if (result.resultCode == RESULT_OK) {
val intent: Intent = result.data!!
val name = intent.getStringExtra(CreateRoomActivity.NAME_EXTRA)
val description = intent.getStringExtra(CreateRoomActivity.DESCRIPTION_EXTRA)
@Suppress("DEPRECATION") val avatarUri =
intent.getParcelableExtra<Uri>(CreateRoomActivity.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.preset = CreateRoomPreset.PRESET_PRIVATE_CHAT
createRoomParams.roomType = TwoMGlobals.ROOM_TYPE
createRoomParams.initialStates = mutableListOf(
CreateRoomStateEvent(
type = "m.room.power_levels",
content = mapOf(
// Users start with a power level of 0
"users_default" to 0,
// Allow only the party creator to send arbitrary events
"events_default" to 100,
// Allow only the party creator to send arbitrary states
"state_default" to 100,
// Allow only party officers to send invites
"invite" to 50,
// Allow only party officers to kick invitees
"kick" to 50,
// Allow only party officers to ban invitees
"ban" to 50,
// Allow only party officers to redact other people's events
"redact" to 50,
"notifications" to mapOf(
// Allow only party officers to ping the room
"room" to 50,
),
"events" to mapOf(
// Allow party officers to rename the room
"m.room.name" to 50,
// Allow party officers to change the room avatar
"m.room.avatar" to 50,
// Allow party officers to change the room topic
"m.room.topic" to 50,
// Allow everyone to redact their own states
"m.room.redaction" to 0,
// Allow everyone to set RSVPs
// FIXME: Do we really want everyone to set RSVPs? Maybe we should use m.room.member instead?
"eu.steffo.twom.rsvp" to 0,
),
"users" to mapOf(
// Give ourselves admin permissions
session!!.myUserId to 100,
)
)
)
)
when (avatarUri?.toFile()?.isFile) {
false -> {
Log.e(
"Main",
"Avatar has been deleted from cache before room could possibly be created, ignoring..."
)
}
true -> {
Log.d(
"Main",
"Avatar seems to exist at: $avatarUri"
)
createRoomParams.avatarUri = avatarUri
}
null -> {
Log.d(
"Main",
"Avatar was not set, ignoring..."
)
}
}
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() {
Log.d("Main", "Recomposing...") Log.d("Main", "Recomposing...")
setContent { setContent {
@ -215,6 +100,99 @@ class MainActivity : ComponentActivity() {
Log.d("Main", "Done logging out!") Log.d("Main", "Done logging out!")
}, },
processCreate = { name, description, avatarUri ->
lifecycleScope.launch {
val currentSession = session
val createRoomParams = CreateRoomParams()
createRoomParams.name = name
createRoomParams.topic = description
createRoomParams.preset = CreateRoomPreset.PRESET_PRIVATE_CHAT
createRoomParams.roomType = TwoMGlobals.ROOM_TYPE
createRoomParams.initialStates = mutableListOf(
CreateRoomStateEvent(
type = "m.room.power_levels",
content = mapOf(
// Users start with a power level of 0
"users_default" to 0,
// Allow only the party creator to send arbitrary events
"events_default" to 100,
// Allow only the party creator to send arbitrary states
"state_default" to 100,
// Allow only party officers to send invites
"invite" to 50,
// Allow only party officers to kick invitees
"kick" to 50,
// Allow only party officers to ban invitees
"ban" to 50,
// Allow only party officers to redact other people's events
"redact" to 50,
"notifications" to mapOf(
// Allow only party officers to ping the room
"room" to 50,
),
"events" to mapOf(
// Allow party officers to rename the room
"m.room.name" to 50,
// Allow party officers to change the room avatar
"m.room.avatar" to 50,
// Allow party officers to change the room topic
"m.room.topic" to 50,
// Allow everyone to redact their own states
"m.room.redaction" to 0,
// Allow everyone to set RSVPs
// FIXME: Do we really want everyone to set RSVPs? Maybe we should use m.room.member instead?
"eu.steffo.twom.rsvp" to 0,
),
"users" to mapOf(
// Give ourselves admin permissions
session!!.myUserId to 100,
)
)
)
)
when (avatarUri?.toFile()?.isFile) {
false -> {
Log.e(
"Main",
"Avatar has been deleted from cache before room could possibly be created, ignoring..."
)
}
true -> {
Log.d(
"Main",
"Avatar seems to exist at: $avatarUri"
)
createRoomParams.avatarUri = avatarUri
}
null -> {
Log.d(
"Main",
"Avatar was not set, ignoring..."
)
}
}
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"
)
}
}
) )
} }
} }

View file

@ -10,7 +10,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.activities.CreateRoomActivity import eu.steffo.twom.activities.ConfigureRoomActivity
import eu.steffo.twom.composables.theme.TwoMTheme import eu.steffo.twom.composables.theme.TwoMTheme
@Composable @Composable
@ -21,11 +21,11 @@ fun CreateRoomScaffold() {
fun submitActivity(name: String, description: String, avatarUri: Uri?) { fun submitActivity(name: String, description: String, avatarUri: Uri?) {
val resultIntent = Intent() val resultIntent = Intent()
resultIntent.putExtra(CreateRoomActivity.NAME_EXTRA, name) resultIntent.putExtra(ConfigureRoomActivity.NAME_EXTRA, name)
resultIntent.putExtra(CreateRoomActivity.DESCRIPTION_EXTRA, description) resultIntent.putExtra(ConfigureRoomActivity.DESCRIPTION_EXTRA, description)
// Kotlin cannot use nullable types in Java interop generics // Kotlin cannot use nullable types in Java interop generics
if (avatarUri != null) { if (avatarUri != null) {
resultIntent.putExtra(CreateRoomActivity.AVATAR_EXTRA, avatarUri) resultIntent.putExtra(ConfigureRoomActivity.AVATAR_EXTRA, avatarUri)
} }
activity.setResult(ComponentActivity.RESULT_OK, resultIntent) activity.setResult(ComponentActivity.RESULT_OK, resultIntent)
activity.finish() activity.finish()

View file

@ -1,5 +1,6 @@
package eu.steffo.twom.composables.main package eu.steffo.twom.composables.main
import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.launch import androidx.activity.result.launch
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
@ -12,16 +13,16 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.activities.CreateRoomActivity import eu.steffo.twom.activities.ConfigureRoomActivity
@Composable @Composable
@Preview @Preview
fun CreateRoomFAB( fun CreateRoomFAB(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onCreateParamsSelected: (name: String, description: String, avatarUri: String?) -> Unit = { _, _, _ -> }, onCreateParamsSelected: (name: String, description: String, avatarUri: Uri?) -> Unit = { _, _, _ -> },
) { ) {
val launcher = val launcher =
rememberLauncherForActivityResult(CreateRoomActivity.Contract()) { rememberLauncherForActivityResult(ConfigureRoomActivity.Contract()) {
if (it != null) { if (it != null) {
onCreateParamsSelected(it.name, it.description, it.avatarUri) onCreateParamsSelected(it.name, it.description, it.avatarUri)
} }

View file

@ -1,5 +1,6 @@
package eu.steffo.twom.composables.main package eu.steffo.twom.composables.main
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
@ -15,7 +16,7 @@ import org.matrix.android.sdk.api.session.Session
fun MainScaffold( fun MainScaffold(
processLogin: () -> Unit = {}, processLogin: () -> Unit = {},
processLogout: () -> Unit = {}, processLogout: () -> Unit = {},
processCreate: (name: String, description: String, avatarUri: String?) -> Unit = { _, _, _ -> }, processCreate: (name: String, description: String, avatarUri: Uri?) -> Unit = { _, _, _ -> },
session: Session? = null, session: Session? = null,
) { ) {
TwoMTheme { TwoMTheme {

View file

@ -16,14 +16,14 @@ import eu.steffo.twom.utils.RSVPAnswer
@Preview @Preview
fun RSVPChipRow( fun RSVPChipRow(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
value: RSVPAnswer = RSVPAnswer.UNKNOWN, value: RSVPAnswer = RSVPAnswer.LOADING,
onChange: (answer: RSVPAnswer) -> Unit = {}, onChange: (answer: RSVPAnswer) -> Unit = {},
) { ) {
fun toggleSwitch(representing: RSVPAnswer): () -> Unit { fun toggleSwitch(representing: RSVPAnswer): () -> Unit {
return { return {
onChange( onChange(
when (value) { when (value) {
representing -> RSVPAnswer.UNKNOWN representing -> RSVPAnswer.NONE
else -> representing else -> representing
} }
) )

View file

@ -12,6 +12,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.composables.errorhandling.ErrorIconButton import eu.steffo.twom.composables.errorhandling.ErrorIconButton
import eu.steffo.twom.composables.errorhandling.ErrorText
import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.errorhandling.LocalizableError
import eu.steffo.twom.composables.navigation.BackIconButton import eu.steffo.twom.composables.navigation.BackIconButton
@ -26,6 +27,16 @@ fun ViewRoomTopBar(
isLoading: Boolean = false, isLoading: Boolean = false,
error: LocalizableError? = null, error: LocalizableError? = null,
) { ) {
val roomSummaryRequest = LocalRoomSummary.current
val isLoading = (roomSummaryRequest == null)
val roomSummary = roomSummaryRequest.getOrNull()
if (roomSummary == null) {
ErrorText(stringResource(R.string.room_error_roomsummary_notfound))
return
}
TopAppBar( TopAppBar(
modifier = modifier, modifier = modifier,
navigationIcon = { navigationIcon = {

View file

@ -65,7 +65,7 @@ fun observeRSVP(room: Room, member: RoomMemberSummary): RSVP {
val answerField = content[TwoMGlobals.RSVP_STATE_ANSWER_FIELD] val answerField = content[TwoMGlobals.RSVP_STATE_ANSWER_FIELD]
?: return RSVP( ?: return RSVP(
event = event, event = event,
answer = RSVPAnswer.UNKNOWN, answer = RSVPAnswer.NONE,
comment = comment, comment = comment,
) )
@ -81,6 +81,7 @@ fun observeRSVP(room: Room, member: RoomMemberSummary): RSVP {
RSVPAnswer.LATER.value -> RSVPAnswer.LATER RSVPAnswer.LATER.value -> RSVPAnswer.LATER
RSVPAnswer.MAYBE.value -> RSVPAnswer.MAYBE RSVPAnswer.MAYBE.value -> RSVPAnswer.MAYBE
RSVPAnswer.NOWAY.value -> RSVPAnswer.NOWAY RSVPAnswer.NOWAY.value -> RSVPAnswer.NOWAY
RSVPAnswer.NONE.value -> RSVPAnswer.NONE
else -> RSVPAnswer.UNKNOWN else -> RSVPAnswer.UNKNOWN
} }

View file

@ -119,8 +119,8 @@ enum class RSVPAnswer {
// An option differing from the previous ones. // An option differing from the previous ones.
UNKNOWN { UNKNOWN {
override val value: String? override val value: String
get() = null get() = ""
override val staticColorRole: StaticColorRole override val staticColorRole: StaticColorRole
get() = NullishColorRole get() = NullishColorRole
@ -143,8 +143,8 @@ enum class RSVPAnswer {
// The answer is still being loaded. // The answer is still being loaded.
LOADING { LOADING {
override val value: String? override val value: String
get() = null get() = ""
override val staticColorRole: StaticColorRole override val staticColorRole: StaticColorRole
get() = NullishColorRole get() = NullishColorRole
@ -166,8 +166,8 @@ enum class RSVPAnswer {
// No answer has been provided yet. // No answer has been provided yet.
NONE { NONE {
override val value: String? override val value: String
get() = null get() = ""
override val staticColorRole: StaticColorRole override val staticColorRole: StaticColorRole
get() = NullishColorRole get() = NullishColorRole
@ -190,8 +190,8 @@ enum class RSVPAnswer {
// Has been invited, but has not accepted yet. // Has been invited, but has not accepted yet.
PENDING { PENDING {
override val value: String? override val value: String
get() = null get() = ""
override val staticColorRole: StaticColorRole override val staticColorRole: StaticColorRole
get() = NullishColorRole get() = NullishColorRole
@ -212,7 +212,7 @@ enum class RSVPAnswer {
stringResource(R.string.room_rsvp_nullish_placeholder) stringResource(R.string.room_rsvp_nullish_placeholder)
}; };
abstract val value: String? abstract val value: String
abstract val staticColorRole: StaticColorRole abstract val staticColorRole: StaticColorRole
abstract val icon: ImageVector abstract val icon: ImageVector