mirror of
https://github.com/Steffo99/twom.git
synced 2024-11-25 09:34:25 +00:00
So close, yet so far away
This commit is contained in:
parent
43f4b266aa
commit
38f0ecbf1c
7 changed files with 99 additions and 46 deletions
|
@ -50,13 +50,22 @@ class MainActivity : ComponentActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openSession() {
|
private fun openSession() {
|
||||||
Log.d("Main", "If possible, opening session: $session")
|
val currentSession = session
|
||||||
session?.open()
|
Log.d("Main", "If possible, opening session: $currentSession")
|
||||||
|
if (currentSession != null) {
|
||||||
|
Log.d("Main", "Opening session: $currentSession")
|
||||||
|
currentSession.open()
|
||||||
|
currentSession.syncService().startSync(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun closeSession() {
|
private fun closeSession() {
|
||||||
Log.d("Main", "If possible, closing session: $session")
|
val currentSession = session
|
||||||
session?.close()
|
Log.d("Main", "If possible, closing session: $currentSession")
|
||||||
|
if (currentSession != null) {
|
||||||
|
Log.d("Main", "Closing session: $currentSession")
|
||||||
|
currentSession.close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onClickLogin() {
|
private fun onClickLogin() {
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
package eu.steffo.twom.main
|
|
||||||
|
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import eu.steffo.twom.R
|
|
||||||
import eu.steffo.twom.theme.TwoMPadding
|
|
||||||
import org.matrix.android.sdk.api.session.Session
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun MainActivityLoggedInControl(
|
|
||||||
session: Session,
|
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
) {
|
|
||||||
Column(modifier) {
|
|
||||||
Row(TwoMPadding.base) {
|
|
||||||
Text(LocalContext.current.getString(R.string.loggedin_text))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
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
|
||||||
|
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.res.stringResource
|
||||||
|
import eu.steffo.twom.R
|
||||||
|
import eu.steffo.twom.matrix.Avatar
|
||||||
|
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
|
||||||
|
fun MainActivityRoomListControl(
|
||||||
|
session: Session,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
) {
|
||||||
|
var roomSummaries by remember { mutableStateOf<List<RoomSummary>?>(null) }
|
||||||
|
|
||||||
|
LaunchedEffect(session) 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) {
|
||||||
|
if (roomSummaries == null) {
|
||||||
|
Text(
|
||||||
|
modifier = TwoMPadding.base,
|
||||||
|
text = stringResource(R.string.loading)
|
||||||
|
)
|
||||||
|
} else if (roomSummaries!!.isEmpty()) {
|
||||||
|
Text(
|
||||||
|
modifier = TwoMPadding.base,
|
||||||
|
text = stringResource(R.string.room_list_empty_text)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
roomSummaries!!.forEach {
|
||||||
|
ListItem(
|
||||||
|
headlineContent = {
|
||||||
|
Text(it.name)
|
||||||
|
},
|
||||||
|
leadingContent = {
|
||||||
|
Avatar(
|
||||||
|
session = session,
|
||||||
|
url = it.avatarUrl,
|
||||||
|
contentDescription = "" // TODO: Is this correct?
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
Divider()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -37,16 +37,16 @@ fun MatrixActivityScaffold(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
if (session != null) {
|
if (session == null) {
|
||||||
MainActivityLoggedInControl(
|
|
||||||
modifier = Modifier.padding(it),
|
|
||||||
session = session,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
MatrixActivityNotLoggedInControl(
|
MatrixActivityNotLoggedInControl(
|
||||||
modifier = Modifier.padding(it),
|
modifier = Modifier.padding(it),
|
||||||
onClickLogin = onClickLogin,
|
onClickLogin = onClickLogin,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
MainActivityRoomListControl(
|
||||||
|
modifier = Modifier.padding(it),
|
||||||
|
session = session,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ import androidx.compose.ui.platform.LocalContext
|
||||||
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.matrix.Avatar
|
import eu.steffo.twom.matrix.UserAvatar
|
||||||
import org.matrix.android.sdk.api.session.Session
|
import org.matrix.android.sdk.api.session.Session
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -41,7 +41,7 @@ fun ProfileIconButton(
|
||||||
contentDescription = LocalContext.current.getString(R.string.account_label),
|
contentDescription = LocalContext.current.getString(R.string.account_label),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
Avatar(
|
UserAvatar(
|
||||||
session = session,
|
session = session,
|
||||||
userId = session.myUserId,
|
userId = session.myUserId,
|
||||||
contentDescription = LocalContext.current.getString(R.string.account_label),
|
contentDescription = LocalContext.current.getString(R.string.account_label),
|
||||||
|
|
|
@ -16,32 +16,28 @@ import org.matrix.android.sdk.api.session.Session
|
||||||
@Composable
|
@Composable
|
||||||
fun Avatar(
|
fun Avatar(
|
||||||
session: Session,
|
session: Session,
|
||||||
userId: String,
|
url: String,
|
||||||
contentDescription: String,
|
contentDescription: String,
|
||||||
) {
|
) {
|
||||||
var avatar by remember { mutableStateOf<ImageBitmap?>(null) }
|
var avatar by remember { mutableStateOf<ImageBitmap?>(null) }
|
||||||
var fetched by remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
LaunchedEffect(session, userId) GetAvatar@{
|
LaunchedEffect(session, url) GetAvatar@{
|
||||||
Log.d("Avatar", "Trying to retrieve the avatar url for $userId...")
|
Log.d("Avatar", "Downloading avatar at: $url")
|
||||||
val avatarUrl = session.profileService().getAvatarUrl(userId)
|
|
||||||
Log.d("Avatar", "Avatar URL for $userId is: $avatarUrl")
|
|
||||||
val avatarFile = session.fileService().downloadFile(
|
val avatarFile = session.fileService().downloadFile(
|
||||||
fileName = "avatar",
|
fileName = "avatar",
|
||||||
url = avatarUrl.getOrNull(),
|
url = url,
|
||||||
mimeType = null,
|
mimeType = null,
|
||||||
elementToDecrypt = null,
|
elementToDecrypt = null,
|
||||||
)
|
)
|
||||||
// TODO: Should I check the MIME type? And the size of the image?
|
// TODO: Should I check the MIME type? And the size of the image?
|
||||||
Log.d("Avatar", "Avatar file for $userId is: $avatarFile")
|
Log.d("Avatar", "File for $url is: $avatarFile")
|
||||||
val avatarBitmap = BitmapFactory.decodeFile(avatarFile.absolutePath)
|
val avatarBitmap = BitmapFactory.decodeFile(avatarFile.absolutePath)
|
||||||
Log.d("Avatar", "Avatar bitmap for $userId is: $avatarBitmap")
|
Log.d("Avatar", "Bitmap for $url is: $avatarBitmap")
|
||||||
avatar = avatarBitmap.asImageBitmap()
|
avatar = avatarBitmap.asImageBitmap()
|
||||||
fetched = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Image(
|
Image(
|
||||||
bitmap = if (fetched && avatar != null) avatar!! else TwoMMatrix.defaultAvatar,
|
bitmap = if (avatar != null) avatar!! else TwoMMatrix.defaultAvatar,
|
||||||
contentDescription = contentDescription
|
contentDescription = contentDescription
|
||||||
)
|
)
|
||||||
}
|
}
|
|
@ -24,4 +24,6 @@
|
||||||
<string name="loggedin_text">You are logged in.</string>
|
<string name="loggedin_text">You are logged in.</string>
|
||||||
<string name="account_label">My account</string>
|
<string name="account_label">My account</string>
|
||||||
<string name="profile_logout_text">Logout</string>
|
<string name="profile_logout_text">Logout</string>
|
||||||
|
<string name="room_list_empty_text">There are no rooms.</string>
|
||||||
|
<string name="loading">Loading...</string>
|
||||||
</resources>
|
</resources>
|
Loading…
Reference in a new issue