refactor(android): prepare SettingsScreen composable for standalone preview

This commit is contained in:
Harsh Shandilya 2024-09-16 13:12:08 +05:30
parent 2fdbff6f12
commit 245437c739
2 changed files with 14 additions and 12 deletions

View file

@ -283,13 +283,15 @@ fun LobstersPostsScreen(
} }
composable<Settings> { composable<Settings> {
SettingsScreen( SettingsScreen(
context = context, openInputStream = context.contentResolver::openInputStream,
openOutputStream = context.contentResolver::openOutputStream,
openLibrariesScreen = { navController.navigate(AboutLibraries) }, openLibrariesScreen = { navController.navigate(AboutLibraries) },
importPosts = viewModel::importPosts, importPosts = viewModel::importPosts,
exportPostsAsJson = viewModel::exportPostsAsJson, exportPostsAsJson = viewModel::exportPostsAsJson,
exportPostsAsHtml = viewModel::exportPostsAsHtml, exportPostsAsHtml = viewModel::exportPostsAsHtml,
snackbarHostState = snackbarHostState, snackbarHostState = snackbarHostState,
contentPadding = contentPadding, contentPadding = contentPadding,
modifier = Modifier.fillMaxSize(),
) )
} }
composable<AboutLibraries> { composable<AboutLibraries> {

View file

@ -6,7 +6,7 @@
*/ */
package dev.msfjarvis.claw.android.ui.screens package dev.msfjarvis.claw.android.ui.screens
import android.content.Context import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable 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.PaddingValues
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -51,9 +50,10 @@ private const val HTML_MIME_TYPE = "application/html"
@Composable @Composable
fun SettingsScreen( fun SettingsScreen(
context: Context,
openLibrariesScreen: () -> Unit, openLibrariesScreen: () -> Unit,
snackbarHostState: SnackbarHostState, snackbarHostState: SnackbarHostState,
openInputStream: (Uri) -> InputStream?,
openOutputStream: (Uri) -> OutputStream?,
importPosts: suspend (InputStream) -> Unit, importPosts: suspend (InputStream) -> Unit,
exportPostsAsJson: suspend (OutputStream) -> Unit, exportPostsAsJson: suspend (OutputStream) -> Unit,
exportPostsAsHtml: suspend (OutputStream) -> Unit, exportPostsAsHtml: suspend (OutputStream) -> Unit,
@ -61,7 +61,7 @@ fun SettingsScreen(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
Box(modifier = modifier.padding(contentPadding).fillMaxSize()) { Box(modifier = modifier.padding(contentPadding)) {
Column { Column {
ListItem( ListItem(
headlineContent = { Text("Data transfer") }, headlineContent = { Text("Data transfer") },
@ -77,11 +77,11 @@ fun SettingsScreen(
horizontalArrangement = Arrangement.spacedBy(32.dp), horizontalArrangement = Arrangement.spacedBy(32.dp),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) { ) {
ImportPosts(context, coroutineScope, snackbarHostState, importPosts) ImportPosts(openInputStream, coroutineScope, snackbarHostState, importPosts)
ExportPosts( ExportPosts(
context,
coroutineScope, coroutineScope,
snackbarHostState, snackbarHostState,
openOutputStream,
exportPostsAsJson, exportPostsAsJson,
exportPostsAsHtml, exportPostsAsHtml,
) )
@ -105,7 +105,7 @@ fun SettingsScreen(
@Composable @Composable
private fun RowScope.ImportPosts( private fun RowScope.ImportPosts(
context: Context, openInputStream: (Uri) -> InputStream?,
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
snackbarHostState: SnackbarHostState, snackbarHostState: SnackbarHostState,
importPosts: suspend (InputStream) -> Unit, importPosts: suspend (InputStream) -> Unit,
@ -118,7 +118,7 @@ private fun RowScope.ImportPosts(
return@rememberLauncherForActivityResult return@rememberLauncherForActivityResult
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openInputStream(uri)?.use { stream -> openInputStream(uri)?.use { stream ->
importPosts(stream) importPosts(stream)
snackbarHostState.showSnackbarDismissing("Successfully imported posts") snackbarHostState.showSnackbarDismissing("Successfully imported posts")
} }
@ -135,9 +135,9 @@ private fun RowScope.ImportPosts(
@Composable @Composable
private fun RowScope.ExportPosts( private fun RowScope.ExportPosts(
context: Context,
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
snackbarHostState: SnackbarHostState, snackbarHostState: SnackbarHostState,
openOutputStream: (Uri) -> OutputStream?,
exportPostsAsJson: suspend (OutputStream) -> Unit, exportPostsAsJson: suspend (OutputStream) -> Unit,
exportPostsAsHtml: suspend (OutputStream) -> Unit, exportPostsAsHtml: suspend (OutputStream) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
@ -150,7 +150,7 @@ private fun RowScope.ExportPosts(
return@rememberLauncherForActivityResult return@rememberLauncherForActivityResult
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openOutputStream(uri)?.use { stream -> openOutputStream(uri)?.use { stream ->
exportPostsAsJson(stream) exportPostsAsJson(stream)
snackbarHostState.showSnackbarDismissing("Successfully exported posts") snackbarHostState.showSnackbarDismissing("Successfully exported posts")
} }
@ -164,7 +164,7 @@ private fun RowScope.ExportPosts(
return@rememberLauncherForActivityResult return@rememberLauncherForActivityResult
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openOutputStream(uri)?.use { stream -> openOutputStream(uri)?.use { stream ->
exportPostsAsHtml(stream) exportPostsAsHtml(stream)
snackbarHostState.showSnackbarDismissing("Successfully exported posts") snackbarHostState.showSnackbarDismissing("Successfully exported posts")
} }