1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-21 23:54:26 +00:00

Progress but also no progress

This commit is contained in:
Steffo 2023-11-30 05:15:42 +01:00
parent 51d2a83ee6
commit d66bbeb277
Signed by: steffo
GPG key ID: 2A24051445686895
14 changed files with 203 additions and 74 deletions

View file

@ -33,7 +33,7 @@
<activity
android:name=".main.MainActivity"
android:exported="true"
android:theme="@android:style/Theme.NoTitleBar">
android:theme="@style/Theme.TwoM">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -43,11 +43,11 @@
<activity
android:name=".login.LoginActivity"
android:theme="@android:style/Theme.NoTitleBar" />
android:theme="@style/Theme.TwoM" />
<activity
android:name=".homeserver.SelectHomeserverActivity"
android:theme="@android:style/Theme.NoTitleBar" />
android:name=".room.RoomActivity"
android:theme="@style/Theme.TwoM" />
</application>

View file

@ -11,12 +11,14 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import eu.steffo.twom.login.LoginActivity
import eu.steffo.twom.matrix.TwoMMatrix
import eu.steffo.twom.room.RoomActivity
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.Session
class MainActivity : ComponentActivity() {
private lateinit var loginLauncher: ActivityResultLauncher<Intent>
private lateinit var roomLauncher: ActivityResultLauncher<Intent>
private var session: Session? = null
@ -35,6 +37,12 @@ class MainActivity : ComponentActivity() {
this::onLogin
)
roomLauncher =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
this::onRoom
)
resetContent()
}
@ -70,7 +78,8 @@ class MainActivity : ComponentActivity() {
private fun onClickLogin() {
Log.d("Main", "Clicked login, launching login activity...")
loginLauncher.launch(Intent(applicationContext, LoginActivity::class.java))
val intent = Intent(applicationContext, LoginActivity::class.java)
loginLauncher.launch(intent)
}
private fun onLogin(result: ActivityResult) {
@ -102,12 +111,24 @@ class MainActivity : ComponentActivity() {
}
}
private fun onClickRoom(roomId: String) {
Log.d("Main", "Clicked a room, launching room activity...")
val intent = Intent(applicationContext, RoomActivity::class.java)
intent.putExtra(RoomActivity.ROOM_ID_EXTRA, roomId)
loginLauncher.launch(intent)
}
private fun onRoom(result: ActivityResult) {
Log.d("Main", "Received result from room activity: $result")
}
private fun resetContent() {
Log.d("Main", "Recomposing...")
setContent {
MatrixActivityScaffold(
onClickLogin = this::onClickLogin,
onClickLogout = this::onClickLogout,
onClickRoom = this::onClickRoom,
session = session,
)
}

View file

@ -2,8 +2,6 @@ package eu.steffo.twom.main
import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Divider
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
@ -13,28 +11,31 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.matrix.Avatar
import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.theme.TwoMPadding
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
@Composable
@Preview
fun MainActivityRoomListControl(
session: Session,
modifier: Modifier = Modifier,
onClickRoom: (roomId: String) -> Unit = {},
) {
val session = LocalSession.current
var roomSummaries by remember { mutableStateOf<List<RoomSummary>?>(null) }
LaunchedEffect(session) GetRoomSummaries@{
if (session == null) {
Log.d("RoomList", "Not doing anything, session is null.")
return@GetRoomSummaries
}
Log.d("RoomList", "Getting room summaries...")
val queryParamsBuilder = roomSummaryQueryParams()
roomSummaries = session.roomService().getRoomSummaries(queryParamsBuilder)
Log.d("RoomList", "Obtained room summaries: $roomSummaries")
val sanityCheck = session.roomService().getRoom("!MdYm4p7umKo4DYX71m:candy.steffo.eu")
Log.d("RoomList", "Sanity check: $sanityCheck")
}
Column(modifier) {
@ -50,19 +51,10 @@ fun MainActivityRoomListControl(
)
} else {
roomSummaries!!.forEach {
ListItem(
headlineContent = {
Text(it.name)
},
leadingContent = {
Avatar(
session = session,
url = it.avatarUrl,
contentDescription = "" // TODO: Is this correct?
)
}
RoomListItem(
onClickRoom = onClickRoom,
roomSummary = it,
)
Divider()
}
}
}

View file

@ -6,10 +6,12 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.theme.TwoMTheme
import org.matrix.android.sdk.api.session.Session
@ -19,34 +21,36 @@ import org.matrix.android.sdk.api.session.Session
fun MatrixActivityScaffold(
onClickLogin: () -> Unit = {},
onClickLogout: () -> Unit = {},
onClickRoom: (roomId: String) -> Unit = {},
session: Session? = null,
) {
TwoMTheme {
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = {
Text(LocalContext.current.getString(R.string.app_name))
},
actions = {
ProfileIconButton(
session = session,
onClickLogout = onClickLogout
)
},
)
}
) {
if (session == null) {
MatrixActivityNotLoggedInControl(
modifier = Modifier.padding(it),
onClickLogin = onClickLogin,
)
} else {
MainActivityRoomListControl(
modifier = Modifier.padding(it),
session = session,
)
CompositionLocalProvider(LocalSession provides session) {
Scaffold(
topBar = {
CenterAlignedTopAppBar(
title = {
Text(LocalContext.current.getString(R.string.app_name))
},
actions = {
ProfileIconButton(
onClickLogout = onClickLogout
)
},
)
}
) {
if (session == null) {
MatrixActivityNotLoggedInControl(
modifier = Modifier.padding(it),
onClickLogin = onClickLogin,
)
} else {
MainActivityRoomListControl(
modifier = Modifier.padding(it),
onClickRoom = onClickRoom,
)
}
}
}
}

View file

@ -18,16 +18,16 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R
import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.matrix.UserAvatar
import org.matrix.android.sdk.api.session.Session
@Composable
@Preview(showBackground = true)
fun ProfileIconButton(
modifier: Modifier = Modifier,
session: Session? = null,
onClickLogout: () -> Unit = {},
) {
val session = LocalSession.current
var expanded by remember { mutableStateOf(false) }
Box {
@ -42,7 +42,6 @@ fun ProfileIconButton(
)
} else {
UserAvatar(
session = session,
userId = session.myUserId,
contentDescription = LocalContext.current.getString(R.string.account_label),
)

View file

@ -0,0 +1,47 @@
package eu.steffo.twom.main
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import eu.steffo.twom.matrix.Avatar
import org.matrix.android.sdk.api.session.room.model.RoomSummary
@Composable
fun RoomListItem(
roomSummary: RoomSummary,
onClickRoom: (roomId: String) -> Unit,
) {
ListItem(
modifier = Modifier.clickable {
onClickRoom(roomSummary.roomId)
},
headlineContent = {
Text(roomSummary.name)
},
leadingContent = {
Box(
modifier = Modifier
.size(40.dp)
.clip(MaterialTheme.shapes.medium)
) {
Avatar(
url = roomSummary.avatarUrl,
)
}
},
supportingContent = {
val canonicalAlias = roomSummary.canonicalAlias
if (canonicalAlias != null) {
Text(canonicalAlias)
}
},
)
}

View file

@ -9,19 +9,34 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import org.matrix.android.sdk.api.session.Session
import androidx.compose.ui.tooling.preview.Preview
@Composable
@Preview
fun Avatar(
session: Session,
url: String,
contentDescription: String,
modifier: Modifier = Modifier,
url: String? = "",
contentDescription: String = "",
) {
val session = LocalSession.current
var avatar by remember { mutableStateOf<ImageBitmap?>(null) }
LaunchedEffect(session, url) GetAvatar@{
if (session == null) {
Log.d("Avatar", "Not doing anything, session is null.")
return@GetAvatar
}
if (url == null) {
Log.d("Avatar", "URL is null, not downloading anything.")
return@GetAvatar
}
if (url.isEmpty()) {
Log.d("Avatar", "URL is a zero-length string, not downloading anything.")
return@GetAvatar
}
Log.d("Avatar", "Downloading avatar at: $url")
val avatarFile = session.fileService().downloadFile(
fileName = "avatar",
@ -36,8 +51,16 @@ fun Avatar(
avatar = avatarBitmap.asImageBitmap()
}
Image(
bitmap = if (avatar != null) avatar!! else TwoMMatrix.defaultAvatar,
contentDescription = contentDescription
)
if (session == null || url == null || avatar == null) {
DefaultAvatar(
modifier = modifier,
contentDescription = contentDescription
)
} else {
Image(
modifier = modifier,
bitmap = avatar!!,
contentDescription = contentDescription,
)
}
}

View file

@ -0,0 +1,17 @@
package eu.steffo.twom.matrix
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
@Composable
fun DefaultAvatar(
modifier: Modifier = Modifier,
contentDescription: String = "",
) {
Image(
modifier = Modifier,
bitmap = TwoMMatrix.defaultAvatar,
contentDescription = contentDescription,
)
}

View file

@ -0,0 +1,6 @@
package eu.steffo.twom.matrix
import androidx.compose.runtime.staticCompositionLocalOf
import org.matrix.android.sdk.api.session.Session
val LocalSession = staticCompositionLocalOf<Session?> { null }

View file

@ -1,37 +1,45 @@
package eu.steffo.twom.matrix
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import org.matrix.android.sdk.api.session.Session
import androidx.compose.ui.Modifier
@Composable
fun UserAvatar(
session: Session,
userId: String,
contentDescription: String,
modifier: Modifier = Modifier,
userId: String = "",
contentDescription: String = "",
) {
val session = LocalSession.current
var avatarUrl by rememberSaveable { mutableStateOf<String?>(null) }
LaunchedEffect(session, userId) GetAvatarUrl@{
if (session == null) {
Log.d("UserAvatar", "Not doing anything, session is null.")
return@GetAvatarUrl
}
if (userId.isEmpty()) {
Log.d("UserAvatar", "Not doing anything, userId is empty.")
return@GetAvatarUrl
}
Log.d("UserAvatar", "Retrieving avatar url for: $userId...")
avatarUrl = session.profileService().getAvatarUrl(userId).getOrNull()
Log.d("UserAvatar", "Retrieved avatar url for $userId: $avatarUrl")
}
if (avatarUrl == null) {
Image(
bitmap = TwoMMatrix.defaultAvatar,
DefaultAvatar(
modifier = modifier,
contentDescription = contentDescription,
)
} else {
Avatar(
session = session,
modifier = modifier,
url = avatarUrl!!,
contentDescription = contentDescription,
)

View file

@ -0,0 +1,10 @@
package eu.steffo.twom.room
import androidx.activity.ComponentActivity
class RoomActivity : ComponentActivity() {
companion object {
const val ROOM_ID_EXTRA = "roomId"
}
}

View file

@ -30,8 +30,8 @@ fun TwoMTheme(
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
window.statusBarColor = colorScheme.surface.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme
}
}

View file

@ -1,3 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.TwoM" parent="@style/Theme.Material3.DayNight"></style>
</resources>