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:
parent
51d2a83ee6
commit
d66bbeb277
14 changed files with 203 additions and 74 deletions
|
@ -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>
|
||||
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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,20 +51,11 @@ 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,9 +21,11 @@ import org.matrix.android.sdk.api.session.Session
|
|||
fun MatrixActivityScaffold(
|
||||
onClickLogin: () -> Unit = {},
|
||||
onClickLogout: () -> Unit = {},
|
||||
onClickRoom: (roomId: String) -> Unit = {},
|
||||
session: Session? = null,
|
||||
) {
|
||||
TwoMTheme {
|
||||
CompositionLocalProvider(LocalSession provides session) {
|
||||
Scaffold(
|
||||
topBar = {
|
||||
CenterAlignedTopAppBar(
|
||||
|
@ -30,7 +34,6 @@ fun MatrixActivityScaffold(
|
|||
},
|
||||
actions = {
|
||||
ProfileIconButton(
|
||||
session = session,
|
||||
onClickLogout = onClickLogout
|
||||
)
|
||||
},
|
||||
|
@ -45,9 +48,10 @@ fun MatrixActivityScaffold(
|
|||
} else {
|
||||
MainActivityRoomListControl(
|
||||
modifier = Modifier.padding(it),
|
||||
session = session,
|
||||
onClickRoom = onClickRoom,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
)
|
||||
|
|
47
app/src/main/java/eu/steffo/twom/main/RoomListItem.kt
Normal file
47
app/src/main/java/eu/steffo/twom/main/RoomListItem.kt
Normal 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)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
|
@ -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,
|
||||
if (session == null || url == null || avatar == null) {
|
||||
DefaultAvatar(
|
||||
modifier = modifier,
|
||||
contentDescription = contentDescription
|
||||
)
|
||||
} else {
|
||||
Image(
|
||||
modifier = modifier,
|
||||
bitmap = avatar!!,
|
||||
contentDescription = contentDescription,
|
||||
)
|
||||
}
|
||||
}
|
17
app/src/main/java/eu/steffo/twom/matrix/DefaultAvatar.kt
Normal file
17
app/src/main/java/eu/steffo/twom/matrix/DefaultAvatar.kt
Normal 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,
|
||||
)
|
||||
}
|
6
app/src/main/java/eu/steffo/twom/matrix/LocalSession.kt
Normal file
6
app/src/main/java/eu/steffo/twom/matrix/LocalSession.kt
Normal 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 }
|
|
@ -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,
|
||||
)
|
||||
|
|
10
app/src/main/java/eu/steffo/twom/room/RoomActivity.kt
Normal file
10
app/src/main/java/eu/steffo/twom/room/RoomActivity.kt
Normal 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"
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
</resources>
|
5
app/src/main/res/values/themes.xml
Normal file
5
app/src/main/res/values/themes.xml
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<style name="Theme.TwoM" parent="@style/Theme.Material3.DayNight"></style>
|
||||
</resources>
|
Loading…
Reference in a new issue