mirror of
https://github.com/T8RIN/ImageToolbox.git
synced 2025-05-20 06:56:22 +08:00
Code refactor
This commit is contained in:
@ -24,6 +24,7 @@ import android.net.Uri
|
|||||||
import android.webkit.MimeTypeMap
|
import android.webkit.MimeTypeMap
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
|
import com.t8rin.logger.makeLog
|
||||||
import dagger.Lazy
|
import dagger.Lazy
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -241,7 +242,7 @@ internal class AndroidShareProvider @Inject constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.getOrNull()?.toString()
|
}.onFailure { it.makeLog("cacheData") }.getOrNull()?.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun shareData(
|
override suspend fun shareData(
|
||||||
|
@ -395,6 +395,31 @@ internal class AndroidFileController @Inject constructor(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun transferBytes(
|
||||||
|
fromUri: String,
|
||||||
|
to: Writeable
|
||||||
|
): SaveResult = withContext(ioDispatcher) {
|
||||||
|
runSuspendCatching {
|
||||||
|
context.contentResolver.openInputStream(fromUri.toUri())?.buffered()?.use { input ->
|
||||||
|
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||||
|
while (input.read(buffer) != -1) {
|
||||||
|
to.writeBytes(buffer)
|
||||||
|
}
|
||||||
|
} ?: throw IllegalAccessException("File inaccessible")
|
||||||
|
}.onSuccess {
|
||||||
|
return@withContext SaveResult.Success(
|
||||||
|
message = null,
|
||||||
|
savingPath = ""
|
||||||
|
)
|
||||||
|
}.onFailure {
|
||||||
|
to.makeLog("File Controller write")
|
||||||
|
it.makeLog("File Controller write")
|
||||||
|
return@withContext SaveResult.Error.Exception(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
return@withContext SaveResult.Error.Exception(IllegalStateException())
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun <O : Any> saveObject(
|
override suspend fun <O : Any> saveObject(
|
||||||
key: String,
|
key: String,
|
||||||
value: O,
|
value: O,
|
||||||
|
@ -50,6 +50,11 @@ interface FileController {
|
|||||||
toUri: String
|
toUri: String
|
||||||
): SaveResult
|
): SaveResult
|
||||||
|
|
||||||
|
suspend fun transferBytes(
|
||||||
|
fromUri: String,
|
||||||
|
to: Writeable
|
||||||
|
): SaveResult
|
||||||
|
|
||||||
suspend fun <O : Any> saveObject(
|
suspend fun <O : Any> saveObject(
|
||||||
key: String,
|
key: String,
|
||||||
value: O,
|
value: O,
|
||||||
|
@ -94,14 +94,15 @@ internal class AndroidAudioCoverRetriever @Inject constructor(
|
|||||||
override suspend fun loadCover(
|
override suspend fun loadCover(
|
||||||
audioData: ByteArray
|
audioData: ByteArray
|
||||||
): Result<String> {
|
): Result<String> {
|
||||||
|
val audioUri = shareProvider.cacheData(
|
||||||
|
writeData = {
|
||||||
|
it.writeBytes(audioData)
|
||||||
|
},
|
||||||
|
filename = "Audio_data_${System.currentTimeMillis()}.mp3"
|
||||||
|
) ?: return Result.failure(NullPointerException(getString(R.string.filename_is_not_set)))
|
||||||
|
|
||||||
return loadCover(
|
return loadCover(
|
||||||
shareProvider.cacheData(
|
audioUri = audioUri
|
||||||
writeData = {
|
|
||||||
it.writeBytes(audioData)
|
|
||||||
},
|
|
||||||
filename = "Audio_data_${System.currentTimeMillis()}.mp3"
|
|
||||||
)
|
|
||||||
?: return Result.failure(NullPointerException(getString(R.string.filename_is_not_set)))
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user