From 245437c739f83fdf9d30bf3eea46af5568c9a6cf Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 16 Sep 2024 13:12:08 +0530 Subject: [PATCH] refactor(android): prepare `SettingsScreen` composable for standalone preview --- .../android/ui/screens/LobstersPostsScreen.kt | 4 +++- .../claw/android/ui/screens/SettingsScreen.kt | 22 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt index 3936975a..2a43d6f5 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/LobstersPostsScreen.kt @@ -283,13 +283,15 @@ fun LobstersPostsScreen( } composable { SettingsScreen( - context = context, + openInputStream = context.contentResolver::openInputStream, + openOutputStream = context.contentResolver::openOutputStream, openLibrariesScreen = { navController.navigate(AboutLibraries) }, importPosts = viewModel::importPosts, exportPostsAsJson = viewModel::exportPostsAsJson, exportPostsAsHtml = viewModel::exportPostsAsHtml, snackbarHostState = snackbarHostState, contentPadding = contentPadding, + modifier = Modifier.fillMaxSize(), ) } composable { diff --git a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/SettingsScreen.kt b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/SettingsScreen.kt index 4b33598f..a21dd0f5 100644 --- a/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/SettingsScreen.kt +++ b/android/src/main/kotlin/dev/msfjarvis/claw/android/ui/screens/SettingsScreen.kt @@ -6,7 +6,7 @@ */ package dev.msfjarvis.claw.android.ui.screens -import android.content.Context +import android.net.Uri import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.clickable @@ -16,7 +16,6 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.RowScope -import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding @@ -51,9 +50,10 @@ private const val HTML_MIME_TYPE = "application/html" @Composable fun SettingsScreen( - context: Context, openLibrariesScreen: () -> Unit, snackbarHostState: SnackbarHostState, + openInputStream: (Uri) -> InputStream?, + openOutputStream: (Uri) -> OutputStream?, importPosts: suspend (InputStream) -> Unit, exportPostsAsJson: suspend (OutputStream) -> Unit, exportPostsAsHtml: suspend (OutputStream) -> Unit, @@ -61,7 +61,7 @@ fun SettingsScreen( modifier: Modifier = Modifier, ) { val coroutineScope = rememberCoroutineScope() - Box(modifier = modifier.padding(contentPadding).fillMaxSize()) { + Box(modifier = modifier.padding(contentPadding)) { Column { ListItem( headlineContent = { Text("Data transfer") }, @@ -77,11 +77,11 @@ fun SettingsScreen( horizontalArrangement = Arrangement.spacedBy(32.dp), modifier = Modifier.fillMaxWidth(), ) { - ImportPosts(context, coroutineScope, snackbarHostState, importPosts) + ImportPosts(openInputStream, coroutineScope, snackbarHostState, importPosts) ExportPosts( - context, coroutineScope, snackbarHostState, + openOutputStream, exportPostsAsJson, exportPostsAsHtml, ) @@ -105,7 +105,7 @@ fun SettingsScreen( @Composable private fun RowScope.ImportPosts( - context: Context, + openInputStream: (Uri) -> InputStream?, coroutineScope: CoroutineScope, snackbarHostState: SnackbarHostState, importPosts: suspend (InputStream) -> Unit, @@ -118,7 +118,7 @@ private fun RowScope.ImportPosts( return@rememberLauncherForActivityResult } coroutineScope.launch { - context.contentResolver.openInputStream(uri)?.use { stream -> + openInputStream(uri)?.use { stream -> importPosts(stream) snackbarHostState.showSnackbarDismissing("Successfully imported posts") } @@ -135,9 +135,9 @@ private fun RowScope.ImportPosts( @Composable private fun RowScope.ExportPosts( - context: Context, coroutineScope: CoroutineScope, snackbarHostState: SnackbarHostState, + openOutputStream: (Uri) -> OutputStream?, exportPostsAsJson: suspend (OutputStream) -> Unit, exportPostsAsHtml: suspend (OutputStream) -> Unit, modifier: Modifier = Modifier, @@ -150,7 +150,7 @@ private fun RowScope.ExportPosts( return@rememberLauncherForActivityResult } coroutineScope.launch { - context.contentResolver.openOutputStream(uri)?.use { stream -> + openOutputStream(uri)?.use { stream -> exportPostsAsJson(stream) snackbarHostState.showSnackbarDismissing("Successfully exported posts") } @@ -164,7 +164,7 @@ private fun RowScope.ExportPosts( return@rememberLauncherForActivityResult } coroutineScope.launch { - context.contentResolver.openOutputStream(uri)?.use { stream -> + openOutputStream(uri)?.use { stream -> exportPostsAsHtml(stream) snackbarHostState.showSnackbarDismissing("Successfully exported posts") }