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

Some confusing progress

This commit is contained in:
Steffo 2023-11-19 19:33:54 +01:00
parent 579573ce7d
commit c42027be24
Signed by: steffo
GPG key ID: 2A24051445686895
6 changed files with 82 additions and 46 deletions

View file

@ -40,6 +40,11 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeserverActivity">
</activity>
</application>
</manifest>

View file

@ -0,0 +1,21 @@
package eu.steffo.twom
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import eu.steffo.twom.ui.fragment.HomeserverFragment
import eu.steffo.twom.ui.theme.TwoMTheme
class HomeserverActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
actionBar?.hide()
setContent {
TwoMTheme {
HomeserverFragment()
}
}
}
}

View file

@ -1,10 +1,11 @@
package eu.steffo.twom
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import eu.steffo.twom.ui.fragment.HomeserverFragment
import eu.steffo.twom.ui.scaffold.TwoMMatrixProvider
import androidx.compose.material3.Text
import eu.steffo.twom.global.TwoMMatrix
import eu.steffo.twom.ui.theme.TwoMTheme
@ -13,11 +14,18 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
actionBar?.hide()
TwoMMatrix.initMatrix(applicationContext)
TwoMMatrix.initSessionFromStorage()
// Do this in a better way
if(TwoMMatrix.session == null) {
val homeserverIntent = Intent(applicationContext, HomeserverActivity::class.java)
startActivity(homeserverIntent)
}
setContent {
TwoMMatrixProvider {
TwoMTheme {
HomeserverFragment()
}
TwoMTheme {
Text("Garasauto")
}
}
}

View file

@ -0,0 +1,38 @@
package eu.steffo.twom.global
import TwoMRoomDisplayNameFallbackProvider
import android.content.Context
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.session.Session
object TwoMMatrix {
lateinit var matrix: Matrix
private set
fun initMatrix(context: Context) {
matrix = Matrix(
context = context.applicationContext,
matrixConfiguration = MatrixConfiguration(
applicationFlavor = "TwoM",
roomDisplayNameFallbackProvider = TwoMRoomDisplayNameFallbackProvider(context.applicationContext)
)
)
}
var session: Session? = null
fun initSessionFromStorage() {
val lastSession = matrix.authenticationService().getLastAuthenticatedSession()
if(lastSession != null) {
session = lastSession
}
}
fun openSession() {
// FIXME: Possible race condition here?
session?.open()
session?.syncService()?.startSync(true)
}
}

View file

@ -1,41 +1,6 @@
package eu.steffo.twom.ui.scaffold
import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.platform.LocalContext
import eu.steffo.twom.R
import org.matrix.android.sdk.api.Matrix
import org.matrix.android.sdk.api.MatrixConfiguration
import org.matrix.android.sdk.api.provider.RoomDisplayNameFallbackProvider
import org.matrix.android.sdk.api.session.Session
val LocalMatrix = compositionLocalOf<Matrix?> { null }
val LocalSession = compositionLocalOf<Session?> { null }
@Composable
fun TwoMMatrixProvider(
content: @Composable () -> Unit = {},
) {
val matrix = Matrix(
context = LocalContext.current,
matrixConfiguration = MatrixConfiguration(
applicationFlavor = "TwoM",
roomDisplayNameFallbackProvider = TwoMRoomDisplayNameFallbackProvider(LocalContext.current)
)
)
// TODO: The session should be opened somewhere, I think.
val session = matrix.authenticationService().getLastAuthenticatedSession()
CompositionLocalProvider(
LocalMatrix provides matrix,
LocalSession provides session,
) {
content()
}
}
class TwoMRoomDisplayNameFallbackProvider(
@ -79,4 +44,4 @@ class TwoMRoomDisplayNameFallbackProvider(
override fun getNameForRoomInvite(): String {
return context.getString(R.string.room_name_fallback_invite)
}
}
}

View file

@ -19,9 +19,9 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import eu.steffo.twom.R
import eu.steffo.twom.global.TwoMMatrix
import eu.steffo.twom.ui.input.SelectHomeserverField
import eu.steffo.twom.ui.input.SelectHomeserverFieldState
import eu.steffo.twom.ui.scaffold.LocalMatrix
import eu.steffo.twom.ui.scaffold.TwoMTopAppBar
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@ -31,7 +31,7 @@ import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
@Preview
fun HomeserverFragment() {
val scope = rememberCoroutineScope()
val matrix = LocalMatrix.current
val matrix = TwoMMatrix.matrix
var homeserver by rememberSaveable { mutableStateOf("") }
var state by rememberSaveable { mutableStateOf(SelectHomeserverFieldState.Empty) }
@ -77,7 +77,7 @@ fun HomeserverFragment() {
delay(500L)
if(homeserver != it) return@ValidateFlows
val authenticationService = matrix!!.authenticationService()
val authenticationService = matrix.authenticationService()
state = SelectHomeserverFieldState.Validating
try {
@ -101,7 +101,6 @@ fun HomeserverFragment() {
else if(flowValid == false) LocalContext.current.getString(R.string.homeserver_error_notmatrix)
else null
,
enabled = (LocalMatrix.current != null),
)
}
Row(