Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions android/app/src/main/java/dev/opencode/mobile/Navigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ import dev.opencode.mobile.ui.componentgallery.components.TopBarGalleryScreen
import dev.opencode.mobile.ui.componentgallery.foundation.ColorGalleryScreen
import dev.opencode.mobile.ui.componentgallery.foundation.TypographyGalleryScreen
import dev.opencode.mobile.ui.components.OpenCodeBottomSheet
import dev.opencode.mobile.ui.connection.ConnectionOnboardingScreen
import dev.opencode.mobile.ui.connection.ConnectionSetupSheetContent
import dev.opencode.mobile.ui.connection.ConnectionSetupViewModel
import dev.opencode.mobile.domain.connection.ConnectionProfile
import dev.opencode.mobile.ui.main.MainScreen
import dev.opencode.mobile.ui.root.AppRootState
import dev.opencode.mobile.ui.settings.SettingsScreen
import dev.opencode.mobile.ui.settings.SettingsViewModel
import dev.opencode.mobile.ui.root.AppRootViewModel
import dev.opencode.mobile.ui.sessionchat.SessionChatScreen

Expand All @@ -60,6 +60,7 @@ fun MainNavigation() {
val rootState by rootViewModel.state.collectAsStateWithLifecycle()
var connectionSetupVisible by rememberSaveable { mutableStateOf(false) }
val setupViewModel: ConnectionSetupViewModel = hiltViewModel()
val settingsViewModel: SettingsViewModel = hiltViewModel()

fun presentConnectionSetup(initialProfile: ConnectionProfile?) {
setupViewModel.configure(initialProfile)
Expand All @@ -84,10 +85,6 @@ fun MainNavigation() {
when (rootState) {
AppRootState.Loading ->
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
AppRootState.Onboarding ->
ConnectionOnboardingScreen(
onConnectClick = { presentConnectionSetup(initialProfile = null) },
)
AppRootState.Home ->
NavDisplay(
modifier = Modifier.safeDrawingPadding(),
Expand All @@ -108,6 +105,7 @@ fun MainNavigation() {
onEditConnectionClick = { profile ->
presentConnectionSetup(initialProfile = profile)
},
onConnectClick = { presentConnectionSetup(initialProfile = null) },
)
}
}
Expand All @@ -120,6 +118,7 @@ fun MainNavigation() {
setupViewModel.configureFromCurrentProfile()
connectionSetupVisible = true
},
onClearData = settingsViewModel::clearAllData,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class DataStoreConnectionProfileStore
}
}

override suspend fun clearAll() {
context.connectionProfileDataStore.edit { prefs ->
prefs.clear()
}
}

private fun normalizeBaseUrl(baseUrl: String): String {
val trimmed = baseUrl.trim()
return if (trimmed.endsWith('/')) trimmed else "$trimmed/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ interface ConnectionProfileStore {
serverBaseUrl: String,
worktrees: List<String>,
)

suspend fun clearAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ class ConnectionSessionCoordinator
@Inject
constructor(
private val activeConnectionManager: ActiveConnectionManager,
private val connectionProfileStore: ConnectionProfileStore,
private val secureCredentialStore: SecureCredentialStore,
private val httpClient: OpencodeHttpClient,
private val sseHttpClient: OpencodeSseHttpClient,
) {
private val profileSavedEvents = MutableSharedFlow<Unit>(extraBufferCapacity = 1)
val profileSaved: SharedFlow<Unit> = profileSavedEvents.asSharedFlow()

private val profileClearedEvents = MutableSharedFlow<Unit>(extraBufferCapacity = 1)
val profileCleared: SharedFlow<Unit> = profileClearedEvents.asSharedFlow()

fun currentProfile(): ConnectionProfile? = activeConnectionManager.currentProfile()

suspend fun saveProfile(profile: ConnectionProfile) {
Expand All @@ -28,4 +33,16 @@ class ConnectionSessionCoordinator
sseHttpClient.rebuild()
profileSavedEvents.emit(Unit)
}

suspend fun clearConnectionData() {
val profile = activeConnectionManager.currentProfile()
if (profile != null) {
secureCredentialStore.deletePassword(profile.baseUrl)
}
connectionProfileStore.clearAll()
activeConnectionManager.disconnect()
httpClient.rebuild()
sseHttpClient.rebuild()
profileClearedEvents.tryEmit(Unit)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class DefaultMessageLiveStore
) : MessageLiveStore {
private val cache = ConcurrentHashMap<MessageObservationKey, SharedFlow<List<Message>>>()

override fun clearCache() {
cache.clear()
}

override fun observeMessages(
sessionId: String,
directory: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class DefaultSessionLiveStore
) : SessionLiveStore {
private val cache = ConcurrentHashMap<String, SharedFlow<List<Session>>>()

override fun clearCache() {
cache.clear()
}

override fun observeSessions(directory: String): Flow<List<Session>> =
cache.getOrPut(directory) { buildSharedFlow(directory) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class DefaultSessionStatusLiveStore
) : SessionStatusLiveStore {
private val cache = ConcurrentHashMap<String, SharedFlow<Map<String, SessionStatus>>>()

override fun clearCache() {
cache.clear()
}

override fun observeStatuses(directory: String): Flow<Map<String, SessionStatus>> =
cache.getOrPut(directory) { buildSharedFlow(directory) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ interface MessageLiveStore {
sessionId: String,
directory: String,
): Flow<List<Message>>
fun clearCache()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import kotlinx.coroutines.flow.Flow

interface SessionLiveStore {
fun observeSessions(directory: String): Flow<List<Session>>
fun clearCache()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import kotlinx.coroutines.flow.Flow

interface SessionStatusLiveStore {
fun observeStatuses(directory: String): Flow<Map<String, SessionStatus>>
fun clearCache()
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class ActiveConnectionManager
)
}

fun disconnect() {
connection = null
}

fun requireConnection(): ServerConnection = connection ?: error("No active server connection")

suspend fun applyProfile(profile: ConnectionProfile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ object ThemeManager {
resolvedDark = ThemeResolver.resolve(currentTheme.dark, isDark = true)
}

fun clearAll() {
currentTheme = loadBundledTheme()
resolve()
loadError = null
}

fun importTheme(jsonData: String) {
try {
val theme = json.decodeFromString<OpenCodeTheme>(jsonData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ import dev.opencode.mobile.ui.components.OpenCodeTopBarAction
import dev.opencode.mobile.ui.components.ProjectSessionSelector
import dev.opencode.mobile.ui.components.ProjectSessionSelectorProject
import dev.opencode.mobile.ui.components.ProjectSessionSelectorSession
import dev.opencode.mobile.ui.connection.ConnectionOnboardingScreen
import dev.opencode.mobile.ui.icons.OpenCodeIcons

@Composable
fun MainScreen(
onSettingsClick: () -> Unit,
onSessionClick: (SessionOpenTarget) -> Unit,
onEditConnectionClick: (dev.opencode.mobile.domain.connection.ConnectionProfile?) -> Unit,
onConnectClick: () -> Unit,
modifier: Modifier = Modifier,
viewModel: MainScreenViewModel = hiltViewModel(),
) {
Expand All @@ -54,6 +56,7 @@ fun MainScreen(
onSessionClick = onSessionClick,
onRetryProbe = viewModel::retryProbe,
onEditConnection = { viewModel.requestEditConnection(onEditConnectionClick) },
onConnectClick = onConnectClick,
onRefresh = viewModel::refresh,
onAddProjectClick = viewModel::showAddProjectSheet,
onFocusProject = viewModel::focusProject,
Expand All @@ -74,6 +77,7 @@ internal fun MainScreen(
onSettingsClick: () -> Unit,
onSessionClick: (SessionOpenTarget) -> Unit,
onEditConnection: () -> Unit,
onConnectClick: () -> Unit,
onRetryProbe: () -> Unit,
onRefresh: () -> Unit,
onAddProjectClick: () -> Unit,
Expand Down Expand Up @@ -128,6 +132,11 @@ internal fun MainScreen(
}

when (uiState) {
is MainScreenUiState.Disconnected -> {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
ConnectionOnboardingScreen(onConnectClick = onConnectClick)
}
}
MainScreenUiState.Probing,
MainScreenUiState.Loading,
-> {
Expand Down Expand Up @@ -297,7 +306,7 @@ private fun SessionSummary.toProjectSessionSelectorSession(): ProjectSessionSele

private fun showsLiveUpdatesStatus(uiState: MainScreenUiState): Boolean =
when (uiState) {
MainScreenUiState.Probing, MainScreenUiState.Loading -> false
MainScreenUiState.Disconnected, MainScreenUiState.Probing, MainScreenUiState.Loading -> false
else -> true
}

Expand Down Expand Up @@ -335,6 +344,7 @@ fun MainScreenPreview() {
onSettingsClick = {},
onSessionClick = {},
onEditConnection = {},
onConnectClick = {},
onRetryProbe = {},
onRefresh = {},
onAddProjectClick = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class MainScreenViewModel
refresh()
}
}
viewModelScope.launch {
connectionSessionCoordinator.profileCleared.collect {
_uiState.value = MainScreenUiState.Disconnected
}
}
}

fun refresh() {
Expand Down Expand Up @@ -183,6 +188,10 @@ class MainScreenViewModel
}

private suspend fun runHomeLoad(skipProbe: Boolean = false) {
if (activeConnectionManager.current() == null) {
_uiState.value = MainScreenUiState.Disconnected
return
}
if (!skipProbe) {
_uiState.value = MainScreenUiState.Probing
try {
Expand Down Expand Up @@ -413,6 +422,8 @@ class MainScreenViewModel
}

sealed interface MainScreenUiState {
data object Disconnected : MainScreenUiState

data object Probing : MainScreenUiState

data class ProbeFailed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import javax.inject.Inject

enum class AppRootState {
Loading,
Onboarding,
Home,
}

Expand Down Expand Up @@ -48,14 +47,9 @@ class AppRootViewModel

private suspend fun loadInitialState() {
activeConnectionManager.ensureLoaded()
if (activeConnectionManager.current() != null) {
httpClient.rebuild()
sseHttpClient.rebuild()
enterHome()
} else {
foregroundEventStream.stop()
_state.value = AppRootState.Onboarding
}
httpClient.rebuild()
sseHttpClient.rebuild()
enterHome()
}

private fun enterHome() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -24,8 +30,11 @@ fun SettingsScreen(
onComponentGalleryClick: () -> Unit,
onNavigateBack: () -> Unit,
onConnectionSetup: () -> Unit,
onClearData: () -> Unit,
modifier: Modifier = Modifier,
) {
var showClearConfirmation by remember { mutableStateOf(false) }

Column(modifier = modifier.fillMaxSize()) {
OpenCodeTopBar(
title = "Settings",
Expand All @@ -51,8 +60,38 @@ fun SettingsScreen(
startDecorator = StartDecorator.Icon(OpenCodeIcons.Architecture, "Component Gallery"),
onClick = onComponentGalleryClick,
)

OpenCodeListItem(
title = "Clear All Data",
subtitle = "Remove connection, cache, and imported themes",
startDecorator = StartDecorator.Icon(OpenCodeIcons.Close, "Clear All Data"),
onClick = { showClearConfirmation = true },
)
}
}

if (showClearConfirmation) {
AlertDialog(
onDismissRequest = { showClearConfirmation = false },
title = { Text("Clear all data?") },
text = { Text("This cannot be undone.") },
confirmButton = {
TextButton(
onClick = {
showClearConfirmation = false
onClearData()
},
) {
Text("Clear")
}
},
dismissButton = {
TextButton(onClick = { showClearConfirmation = false }) {
Text("Cancel")
}
},
)
}
}

@Composable
Expand Down Expand Up @@ -80,6 +119,7 @@ private fun SettingsScreenLightPreview() {
onComponentGalleryClick = {},
onNavigateBack = {},
onConnectionSetup = {},
onClearData = {},
)
}
}
Expand All @@ -92,6 +132,7 @@ private fun SettingsScreenDarkPreview() {
onComponentGalleryClick = {},
onNavigateBack = {},
onConnectionSetup = {},
onClearData = {},
)
}
}
Loading