mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
chore: KMP audit — commonize code, centralize utilities, eliminate dead abstractions (#5133)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
50ade01e55
commit
72b981f73b
132 changed files with 2186 additions and 916 deletions
|
|
@ -16,9 +16,11 @@
|
|||
*/
|
||||
package org.meshtastic.core.service
|
||||
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
|
@ -27,10 +29,15 @@ import kotlin.test.assertNotNull
|
|||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class AndroidFileServiceTest {
|
||||
private val testDispatchers =
|
||||
UnconfinedTestDispatcher().let { dispatcher ->
|
||||
CoroutineDispatchers(io = dispatcher, main = dispatcher, default = dispatcher)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInitialization() = runTest {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val service = AndroidFileService(context)
|
||||
val service = AndroidFileService(context, testDispatchers)
|
||||
assertNotNull(service)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ package org.meshtastic.core.service
|
|||
|
||||
import android.app.Application
|
||||
import co.touchlab.kermit.Logger
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import com.eygraber.uri.toAndroidUri
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.BufferedSink
|
||||
import okio.BufferedSource
|
||||
|
|
@ -26,15 +26,16 @@ import okio.buffer
|
|||
import okio.sink
|
||||
import okio.source
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.MeshtasticUri
|
||||
import org.meshtastic.core.common.util.toAndroidUri
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.repository.FileService
|
||||
import java.io.FileOutputStream
|
||||
|
||||
@Single
|
||||
class AndroidFileService(private val context: Application) : FileService {
|
||||
override suspend fun write(uri: MeshtasticUri, block: suspend (BufferedSink) -> Unit): Boolean =
|
||||
withContext(Dispatchers.IO) {
|
||||
class AndroidFileService(private val context: Application, private val dispatchers: CoroutineDispatchers) :
|
||||
FileService {
|
||||
override suspend fun write(uri: CommonUri, block: suspend (BufferedSink) -> Unit): Boolean =
|
||||
withContext(dispatchers.io) {
|
||||
try {
|
||||
val pfd = context.contentResolver.openFileDescriptor(uri.toAndroidUri(), "wt")
|
||||
if (pfd == null) {
|
||||
|
|
@ -51,8 +52,8 @@ class AndroidFileService(private val context: Application) : FileService {
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun read(uri: MeshtasticUri, block: suspend (BufferedSource) -> Unit): Boolean =
|
||||
withContext(Dispatchers.IO) {
|
||||
override suspend fun read(uri: CommonUri, block: suspend (BufferedSource) -> Unit): Boolean =
|
||||
withContext(dispatchers.io) {
|
||||
try {
|
||||
val success =
|
||||
context.contentResolver.openInputStream(uri.toAndroidUri())?.use { inputStream ->
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
package org.meshtastic.core.service
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.BufferedSink
|
||||
import okio.BufferedSource
|
||||
|
|
@ -25,17 +24,18 @@ import okio.buffer
|
|||
import okio.sink
|
||||
import okio.source
|
||||
import org.koin.core.annotation.Single
|
||||
import org.meshtastic.core.common.util.MeshtasticUri
|
||||
import org.meshtastic.core.common.util.CommonUri
|
||||
import org.meshtastic.core.di.CoroutineDispatchers
|
||||
import org.meshtastic.core.repository.FileService
|
||||
import java.io.File
|
||||
|
||||
@Single
|
||||
class JvmFileService : FileService {
|
||||
override suspend fun write(uri: MeshtasticUri, block: suspend (BufferedSink) -> Unit): Boolean =
|
||||
withContext(Dispatchers.IO) {
|
||||
class JvmFileService(private val dispatchers: CoroutineDispatchers) : FileService {
|
||||
override suspend fun write(uri: CommonUri, block: suspend (BufferedSink) -> Unit): Boolean =
|
||||
withContext(dispatchers.io) {
|
||||
try {
|
||||
// Treat uriString as a local file path
|
||||
val file = File(uri.uriString)
|
||||
// Treat URI string as a local file path
|
||||
val file = File(uri.toString())
|
||||
file.parentFile?.mkdirs()
|
||||
file.sink().buffer().use { sink -> block(sink) }
|
||||
true
|
||||
|
|
@ -45,10 +45,10 @@ class JvmFileService : FileService {
|
|||
}
|
||||
}
|
||||
|
||||
override suspend fun read(uri: MeshtasticUri, block: suspend (BufferedSource) -> Unit): Boolean =
|
||||
withContext(Dispatchers.IO) {
|
||||
override suspend fun read(uri: CommonUri, block: suspend (BufferedSource) -> Unit): Boolean =
|
||||
withContext(dispatchers.io) {
|
||||
try {
|
||||
val file = File(uri.uriString)
|
||||
val file = File(uri.toString())
|
||||
file.source().buffer().use { source -> block(source) }
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue