mirror of
https://github.com/Steffo99/twom.git
synced 2024-11-21 15:44:26 +00:00
Refactor Main to something actually readable
This commit is contained in:
parent
c56c0124e8
commit
77dd9373e9
4 changed files with 180 additions and 172 deletions
|
@ -4,199 +4,66 @@ import android.os.Bundle
|
|||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.core.net.toFile
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import eu.steffo.twom.main.components.MainScaffold
|
||||
import eu.steffo.twom.matrix.utils.TwoMMatrix
|
||||
import kotlinx.coroutines.launch
|
||||
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
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomStateEvent
|
||||
|
||||
|
||||
private const val TAG = "MainActivity"
|
||||
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private var session: Session? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
TwoMMatrix.ensureMatrix(applicationContext)
|
||||
|
||||
fetchLastSession()
|
||||
openSession()
|
||||
resetContent()
|
||||
initSession()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
|
||||
closeSession()
|
||||
deinitSession()
|
||||
}
|
||||
|
||||
private fun fetchLastSession() {
|
||||
Log.d("Main", "Fetching the last successfully authenticated session...")
|
||||
session = TwoMMatrix.matrix.authenticationService().getLastAuthenticatedSession()
|
||||
}
|
||||
|
||||
private fun openSession() {
|
||||
val currentSession = session
|
||||
Log.d("Main", "If possible, opening session: $currentSession")
|
||||
if (currentSession != null) {
|
||||
Log.d("Main", "Opening session: $currentSession")
|
||||
currentSession.open()
|
||||
currentSession.syncService().startSync(true)
|
||||
currentSession.addListener(OpenSessionListener(this::resetContent))
|
||||
|
||||
Log.d(
|
||||
"Main",
|
||||
"Opened session, recomposing..."
|
||||
)
|
||||
resetContent()
|
||||
}
|
||||
}
|
||||
|
||||
private class OpenSessionListener(private val resetContent: () -> Unit) : Session.Listener {
|
||||
private class SessionChangeListener(private val resetContent: () -> Unit) : Session.Listener {
|
||||
override fun onSessionStarted(session: Session) {
|
||||
Log.d(TAG, "Session has started!")
|
||||
resetContent()
|
||||
}
|
||||
|
||||
override fun onSessionStopped(session: Session) {
|
||||
Log.d(TAG, "Session has stopped!")
|
||||
resetContent()
|
||||
}
|
||||
}
|
||||
|
||||
private fun closeSession() {
|
||||
val currentSession = session
|
||||
Log.d("Main", "If possible, closing session: $currentSession")
|
||||
if (currentSession != null) {
|
||||
Log.d("Main", "Closing session: $currentSession")
|
||||
currentSession.close()
|
||||
}
|
||||
private fun initSession() {
|
||||
Log.i(TAG, "Initializing session...")
|
||||
TwoMMatrix.ensureMatrix(applicationContext)
|
||||
TwoMMatrix.setupSession()
|
||||
TwoMMatrix.session?.addListener(SessionChangeListener { resetContent() })
|
||||
}
|
||||
|
||||
private fun deinitSession() {
|
||||
Log.i(TAG, "Deinitializing session...")
|
||||
TwoMMatrix.teardownSession()
|
||||
}
|
||||
|
||||
private fun resetContent() {
|
||||
Log.d("Main", "Recomposing...")
|
||||
Log.v(TAG, "Recomposing...")
|
||||
setContent {
|
||||
// TODO: Check this with a clearer mind
|
||||
MainScaffold(
|
||||
session = session,
|
||||
session = TwoMMatrix.session,
|
||||
processLogin = {
|
||||
Log.d(
|
||||
"Main",
|
||||
"Login activity returned a successful result, trying to get session..."
|
||||
)
|
||||
fetchLastSession()
|
||||
openSession()
|
||||
|
||||
initSession()
|
||||
},
|
||||
processLogout = {
|
||||
val signedOutSession = session!!
|
||||
|
||||
Log.d("Main", "Clicked logout, recomposing...")
|
||||
session = null
|
||||
resetContent()
|
||||
|
||||
Log.d("Main", "Done recomposing, now signing out...")
|
||||
lifecycleScope.launch {
|
||||
signedOutSession.signOutService().signOut(true)
|
||||
TwoMMatrix.session?.signOutService()?.signOut(true)
|
||||
}
|
||||
|
||||
Log.d("Main", "Done logging out!")
|
||||
},
|
||||
processCreate = {
|
||||
lifecycleScope.launch {
|
||||
val name = it.name
|
||||
val description = it.description
|
||||
val avatarUri = it.avatarUri
|
||||
|
||||
val currentSession = session
|
||||
|
||||
val createRoomParams = CreateRoomParams()
|
||||
|
||||
createRoomParams.name = name
|
||||
createRoomParams.topic = description
|
||||
createRoomParams.preset = CreateRoomPreset.PRESET_PRIVATE_CHAT
|
||||
createRoomParams.roomType = TwoMMatrix.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
|
||||
// Do we really want everyone to set RSVPs? Maybe m.room.member could be used 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"
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package eu.steffo.twom.main.components
|
||||
|
||||
import android.util.Log
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.launch
|
||||
import androidx.compose.material.icons.Icons
|
||||
|
@ -8,22 +9,126 @@ import androidx.compose.material3.ExtendedFloatingActionButton
|
|||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.core.net.toFile
|
||||
import eu.steffo.twom.R
|
||||
import eu.steffo.twom.configureroom.activities.ConfigureRoomActivity
|
||||
import eu.steffo.twom.matrix.complocals.LocalSession
|
||||
import eu.steffo.twom.matrix.utils.TwoMMatrix
|
||||
import kotlinx.coroutines.launch
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomPreset
|
||||
import org.matrix.android.sdk.api.session.room.model.create.CreateRoomStateEvent
|
||||
|
||||
private const val TAG = "CreateRoomFAB"
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
fun CreateRoomFAB(
|
||||
modifier: Modifier = Modifier,
|
||||
onCreateConfigured: (configuration: ConfigureRoomActivity.Configuration) -> Unit = {},
|
||||
) {
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
// Hide if no session is available
|
||||
val session = LocalSession.current ?: return
|
||||
|
||||
val launcher =
|
||||
rememberLauncherForActivityResult(ConfigureRoomActivity.CreateContract()) {
|
||||
if (it != null) {
|
||||
onCreateConfigured(it)
|
||||
rememberLauncherForActivityResult(ConfigureRoomActivity.CreateContract()) CreateRoom@{
|
||||
it ?: return@CreateRoom
|
||||
|
||||
scope.launch {
|
||||
Log.d(TAG, "Configuring room creation parameters...")
|
||||
val createRoomParams = CreateRoomParams()
|
||||
|
||||
createRoomParams.name = it.name
|
||||
Log.d(TAG, "Room name is: ${createRoomParams.name}")
|
||||
|
||||
createRoomParams.topic = it.description
|
||||
Log.d(TAG, "Room description is: ${createRoomParams.topic}")
|
||||
|
||||
createRoomParams.preset = CreateRoomPreset.PRESET_PRIVATE_CHAT
|
||||
Log.d(TAG, "Room preset is: ${createRoomParams.preset}")
|
||||
|
||||
createRoomParams.roomType = TwoMMatrix.ROOM_TYPE
|
||||
Log.d(TAG, "Room type is: ${createRoomParams.roomType}")
|
||||
|
||||
when (it.avatarUri?.toFile()?.isFile) {
|
||||
false -> {
|
||||
Log.e(TAG, "Avatar is no longer in cache, not setting...")
|
||||
}
|
||||
|
||||
true -> {
|
||||
createRoomParams.avatarUri = it.avatarUri
|
||||
Log.d(TAG, "Room avatar is: ${createRoomParams.avatarUri}")
|
||||
}
|
||||
|
||||
null -> {
|
||||
Log.d(TAG, "Room avatar is: <not set>")
|
||||
}
|
||||
}
|
||||
|
||||
Log.v(TAG, "Constructing initial power levels event...")
|
||||
val powerLevelsEvent = 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
|
||||
// Do we really want everyone to set RSVPs? Maybe m.room.member could be used instead...?
|
||||
"eu.steffo.twom.rsvp" to 0,
|
||||
),
|
||||
|
||||
"users" to mapOf(
|
||||
// Give ourselves admin permissions
|
||||
session.myUserId to 100,
|
||||
)
|
||||
)
|
||||
)
|
||||
Log.v(TAG, "Initial power levels event is: $powerLevelsEvent")
|
||||
|
||||
createRoomParams.initialStates = mutableListOf(
|
||||
powerLevelsEvent
|
||||
)
|
||||
Log.d(TAG, "Initial states are: ${createRoomParams.initialStates}")
|
||||
|
||||
Log.i(TAG, "Creating room: ${createRoomParams.name}")
|
||||
val roomId = session.roomService().createRoom(createRoomParams)
|
||||
|
||||
Log.d(
|
||||
TAG,
|
||||
"Successfully created room: ${createRoomParams.name}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import androidx.compose.runtime.Composable
|
|||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import eu.steffo.twom.configureroom.activities.ConfigureRoomActivity
|
||||
import eu.steffo.twom.matrix.complocals.LocalSession
|
||||
import eu.steffo.twom.theme.components.TwoMTheme
|
||||
import org.matrix.android.sdk.api.session.Session
|
||||
|
@ -14,10 +13,9 @@ import org.matrix.android.sdk.api.session.Session
|
|||
@Composable
|
||||
@Preview
|
||||
fun MainScaffold(
|
||||
session: Session? = null,
|
||||
processLogin: () -> Unit = {},
|
||||
processLogout: () -> Unit = {},
|
||||
processCreate: (it: ConfigureRoomActivity.Configuration) -> Unit = {},
|
||||
session: Session? = null,
|
||||
) {
|
||||
TwoMTheme {
|
||||
CompositionLocalProvider(LocalSession provides session) {
|
||||
|
@ -29,11 +27,7 @@ fun MainScaffold(
|
|||
)
|
||||
},
|
||||
floatingActionButton = {
|
||||
if (session != null) {
|
||||
CreateRoomFAB(
|
||||
onCreateConfigured = processCreate,
|
||||
)
|
||||
}
|
||||
CreateRoomFAB()
|
||||
},
|
||||
content = {
|
||||
if (session == null) {
|
||||
|
|
|
@ -4,6 +4,9 @@ import android.content.Context
|
|||
import android.util.Log
|
||||
import org.matrix.android.sdk.api.Matrix
|
||||
import org.matrix.android.sdk.api.MatrixConfiguration
|
||||
import org.matrix.android.sdk.api.session.Session
|
||||
|
||||
private const val TAG = "TwoMMatrix"
|
||||
|
||||
/**
|
||||
* Object containing the global state of the application.
|
||||
|
@ -23,7 +26,7 @@ object TwoMMatrix {
|
|||
*/
|
||||
fun ensureMatrix(context: Context): Matrix? {
|
||||
if (!this::matrix.isInitialized) {
|
||||
Log.d("Matrix", "Initializing Matrix...")
|
||||
Log.i(TAG, "Initializing Matrix...")
|
||||
matrix = Matrix(
|
||||
context = context.applicationContext,
|
||||
matrixConfiguration = MatrixConfiguration(
|
||||
|
@ -36,6 +39,45 @@ object TwoMMatrix {
|
|||
return null
|
||||
}
|
||||
|
||||
val session: Session?
|
||||
get() = matrix.authenticationService().getLastAuthenticatedSession()
|
||||
|
||||
fun setupSession() {
|
||||
Log.d(TAG, "Attempting to setup session...")
|
||||
val session = this.session
|
||||
|
||||
if (session == null) {
|
||||
Log.d(TAG, "Session is null, nothing to setup.")
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(TAG, "Session is: $session")
|
||||
|
||||
Log.i(TAG, "Opening session: $session")
|
||||
session.open()
|
||||
|
||||
Log.i(TAG, "Starting to sync with session: $session")
|
||||
session.syncService().startSync(true)
|
||||
}
|
||||
|
||||
fun teardownSession() {
|
||||
Log.d(TAG, "Attempting to teardown session...")
|
||||
val session = this.session
|
||||
|
||||
if (session == null) {
|
||||
Log.d(TAG, "Session is null, nothing to teardown.")
|
||||
return
|
||||
}
|
||||
|
||||
Log.d(TAG, "Session is: $session")
|
||||
|
||||
Log.i(TAG, "Stopping to sync with session: $session")
|
||||
session.syncService().stopSync()
|
||||
|
||||
Log.i(TAG, "Closing session: $session")
|
||||
session.close()
|
||||
}
|
||||
|
||||
const val ROOM_TYPE = "eu.steffo.twom.happening"
|
||||
|
||||
const val RSVP_STATE_TYPE = "eu.steffo.twom.rsvp"
|
||||
|
|
Loading…
Reference in a new issue