diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1748393..2d9d1f4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -40,6 +40,11 @@ + + + + \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/HomeserverActivity.kt b/app/src/main/java/eu/steffo/twom/HomeserverActivity.kt new file mode 100644 index 0000000..f856787 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/HomeserverActivity.kt @@ -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() + } + } + } +} diff --git a/app/src/main/java/eu/steffo/twom/MainActivity.kt b/app/src/main/java/eu/steffo/twom/MainActivity.kt index 67d7efd..054882e 100644 --- a/app/src/main/java/eu/steffo/twom/MainActivity.kt +++ b/app/src/main/java/eu/steffo/twom/MainActivity.kt @@ -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") } } } diff --git a/app/src/main/java/eu/steffo/twom/global/TwoMMatrix.kt b/app/src/main/java/eu/steffo/twom/global/TwoMMatrix.kt new file mode 100644 index 0000000..ec9ab2c --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/global/TwoMMatrix.kt @@ -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) + } +} diff --git a/app/src/main/java/eu/steffo/twom/ui/scaffold/TwoMMatrixProvider.kt b/app/src/main/java/eu/steffo/twom/global/TwoMRoomDisplayNameFallbackProvider.kt similarity index 58% rename from app/src/main/java/eu/steffo/twom/ui/scaffold/TwoMMatrixProvider.kt rename to app/src/main/java/eu/steffo/twom/global/TwoMRoomDisplayNameFallbackProvider.kt index 89589d2..1d91ca1 100644 --- a/app/src/main/java/eu/steffo/twom/ui/scaffold/TwoMMatrixProvider.kt +++ b/app/src/main/java/eu/steffo/twom/global/TwoMRoomDisplayNameFallbackProvider.kt @@ -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 { null } -val LocalSession = compositionLocalOf { 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) } -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/steffo/twom/ui/fragment/HomeserverFragment.kt b/app/src/main/java/eu/steffo/twom/ui/fragment/HomeserverFragment.kt index 96ea246..8a28fad 100644 --- a/app/src/main/java/eu/steffo/twom/ui/fragment/HomeserverFragment.kt +++ b/app/src/main/java/eu/steffo/twom/ui/fragment/HomeserverFragment.kt @@ -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(