OXYGENHUB
# 1823bc12eda1 oxygenhub-android / app / src / main / java / io / oxygenhub / android / MainActivity.kt
2.9 KiB · last changed Notifications when a proposal can be landed; the runner comes back after an update or restart (closes #20, refs #8) 10 days ago source blame history raw
57c08243 Andy McCall ↶ 1
package io.oxygenhub.android
2
ff65ea45 Andy McCall ↶ 3
import android.content.Intent
57c08243 Andy McCall ↶ 4
import android.os.Bundle
5
import androidx.activity.ComponentActivity
6
import androidx.activity.compose.setContent
7
import androidx.activity.enableEdgeToEdge
144cbf7b Andy McCall ↶ 8
import androidx.compose.runtime.getValue
9
import androidx.compose.runtime.key
ff65ea45 Andy McCall ↶ 10
import androidx.compose.runtime.mutableStateOf
11
import io.oxygenhub.android.alerts.LandAlerts
12
import io.oxygenhub.android.ui.nav.ProposalRoute
144cbf7b Andy McCall ↶ 13
import androidx.lifecycle.compose.collectAsStateWithLifecycle
14
import io.oxygenhub.android.ui.nav.SignedInApp
15
import io.oxygenhub.android.ui.nav.rememberViewModel
16
import io.oxygenhub.android.ui.signin.SignInScreen
17
import io.oxygenhub.android.ui.signin.SignInViewModel
57c08243 Andy McCall ↶ 18
import io.oxygenhub.android.ui.theme.OxygenHubTheme
19
import uniffi.rust_core.oxygenGreeting
20
21
class MainActivity : ComponentActivity() {
ff65ea45 Andy McCall ↶ 22
    /** A proposal a notification asked to open (#20), until it is shown. */
23
    private val opening = mutableStateOf<ProposalRoute?>(null)
24
57c08243 Andy McCall ↶ 25
    override fun onCreate(savedInstanceState: Bundle?) {
26
        super.onCreate(savedInstanceState)
ff65ea45 Andy McCall ↶ 27
        if (savedInstanceState == null) opening.value = proposalIn(intent)
57c08243 Andy McCall ↶ 28
        enableEdgeToEdge()
144cbf7b Andy McCall ↶ 29
        val container = (application as OxygenHubApp).container
30
        val sessions = container.sessions
57c08243 Andy McCall ↶ 31
        setContent {
32
            OxygenHubTheme {
144cbf7b Andy McCall ↶ 33
                val session by sessions.session.collectAsStateWithLifecycle()
34
                val signedIn = session
35
                val api = signedIn?.let { sessions.api() }
36
                if (signedIn == null || api == null) {
37
                    SignInScreen(
38
                        rememberViewModel { SignInViewModel(sessions, container::deviceAuth, container.clientName, DEFAULT_HUB) },
39
                    )
57c08243 Andy McCall ↶ 40
                } else {
144cbf7b Andy McCall ↶ 41
                    // A new session starts the signed-in app afresh: nothing of the last account's is kept.
42
                    key(signedIn) {
43
                        SignedInApp(
44
                            session = signedIn,
45
                            api = api,
46
                            nativeCore = { oxygenGreeting(signedIn.username) },
47
                            onSignOut = { sessions.signOut() },
ff65ea45 Andy McCall ↶ 48
                            open = opening.value,
49
                            onOpened = { opening.value = null },
144cbf7b Andy McCall ↶ 50
                        )
51
                    }
57c08243 Andy McCall ↶ 52
                }
53
            }
54
        }
ff65ea45 Andy McCall ↶ 55
    }
56
57
    override fun onNewIntent(intent: Intent) {
58
        super.onNewIntent(intent)
59
        proposalIn(intent)?.let { opening.value = it }
60
    }
61
62
    private fun proposalIn(intent: Intent?): ProposalRoute? {
63
        val repository = intent?.getStringExtra(LandAlerts.EXTRA_REPOSITORY) ?: return null
64
        val number = intent.getLongExtra(LandAlerts.EXTRA_NUMBER, 0)
65
        if ('/' !in repository || number <= 0) return null
66
        return ProposalRoute(repository.substringBefore('/'), repository.substringAfter('/'), number)
57c08243 Andy McCall ↶ 67
    }
68
}