refactor(android): enforce DI encapsulation

This commit is contained in:
Harsh Shandilya 2023-07-17 22:36:11 +05:30
parent 4c95680f3b
commit 415ddae9f7
3 changed files with 19 additions and 10 deletions

View file

@ -259,7 +259,8 @@ fun LobstersApp(
composable(route = Destinations.DataTransfer.route) { composable(route = Destinations.DataTransfer.route) {
DataTransferScreen( DataTransferScreen(
context = context, context = context,
dataTransferRepository = viewModel.dataTransferRepository, importPosts = viewModel::importPosts,
exportPosts = viewModel::exportPosts,
snackbarHostState = snackbarHostState, snackbarHostState = snackbarHostState,
) )
} }

View file

@ -25,7 +25,8 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import dev.msfjarvis.claw.android.viewmodel.DataTransferRepository import java.io.InputStream
import java.io.OutputStream
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -34,14 +35,15 @@ private const val MIME_TYPE = "application/json"
@Composable @Composable
fun DataTransferScreen( fun DataTransferScreen(
context: Context, context: Context,
dataTransferRepository: DataTransferRepository, importPosts: suspend (InputStream) -> Unit,
exportPosts: suspend (OutputStream) -> Unit,
snackbarHostState: SnackbarHostState, snackbarHostState: SnackbarHostState,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
Column(modifier = modifier) { Column(modifier = modifier) {
ImportOption(context, coroutineScope, dataTransferRepository, snackbarHostState) ImportOption(context, coroutineScope, importPosts, snackbarHostState)
ExportOption(context, coroutineScope, dataTransferRepository, snackbarHostState) ExportOption(context, coroutineScope, exportPosts, snackbarHostState)
} }
} }
@ -49,7 +51,7 @@ fun DataTransferScreen(
private fun ImportOption( private fun ImportOption(
context: Context, context: Context,
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
dataTransferRepository: DataTransferRepository, importPosts: suspend (InputStream) -> Unit,
snackbarHostState: SnackbarHostState, snackbarHostState: SnackbarHostState,
) { ) {
val importAction = val importAction =
@ -60,7 +62,7 @@ private fun ImportOption(
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openInputStream(uri)?.use { stream -> context.contentResolver.openInputStream(uri)?.use { stream ->
dataTransferRepository.importPosts(stream) importPosts(stream)
snackbarHostState.showSnackbarDismissing("Successfully imported posts") snackbarHostState.showSnackbarDismissing("Successfully imported posts")
} }
} }
@ -78,7 +80,7 @@ private fun ImportOption(
private fun ExportOption( private fun ExportOption(
context: Context, context: Context,
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
dataTransferRepository: DataTransferRepository, exportPosts: suspend (OutputStream) -> Unit,
snackbarHostState: SnackbarHostState, snackbarHostState: SnackbarHostState,
) { ) {
val exportAction = val exportAction =
@ -89,7 +91,7 @@ private fun ExportOption(
} }
coroutineScope.launch { coroutineScope.launch {
context.contentResolver.openOutputStream(uri)?.use { stream -> context.contentResolver.openOutputStream(uri)?.use { stream ->
dataTransferRepository.exportPosts(stream) exportPosts(stream)
snackbarHostState.showSnackbarDismissing("Successfully exported posts") snackbarHostState.showSnackbarDismissing("Successfully exported posts")
} }
} }

View file

@ -21,6 +21,8 @@ import dev.msfjarvis.claw.core.injection.IODispatcher
import dev.msfjarvis.claw.database.local.SavedPost import dev.msfjarvis.claw.database.local.SavedPost
import dev.msfjarvis.claw.model.Comment import dev.msfjarvis.claw.model.Comment
import java.io.IOException import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.time.LocalDateTime import java.time.LocalDateTime
import java.time.Month import java.time.Month
@ -42,7 +44,7 @@ constructor(
private val savedPostsRepository: SavedPostsRepository, private val savedPostsRepository: SavedPostsRepository,
private val commentsRepository: CommentsRepository, private val commentsRepository: CommentsRepository,
private val linkMetadataRepository: LinkMetadataRepository, private val linkMetadataRepository: LinkMetadataRepository,
val dataTransferRepository: DataTransferRepository, private val dataTransferRepository: DataTransferRepository,
private val pagingSourceFactory: LobstersPagingSource.Factory, private val pagingSourceFactory: LobstersPagingSource.Factory,
@IODispatcher private val ioDispatcher: CoroutineDispatcher, @IODispatcher private val ioDispatcher: CoroutineDispatcher,
) : ViewModel() { ) : ViewModel() {
@ -124,6 +126,10 @@ constructor(
} }
} }
suspend fun importPosts(input: InputStream) = dataTransferRepository.importPosts(input)
suspend fun exportPosts(output: OutputStream) = dataTransferRepository.exportPosts(output)
/** /**
* Parses a given [String] into a [LocalDateTime]. This method is only intended to be used for * Parses a given [String] into a [LocalDateTime]. This method is only intended to be used for
* dates in the format returned by the Lobsters API, and is not a general purpose parsing * dates in the format returned by the Lobsters API, and is not a general purpose parsing