Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions android/src/main/java/com/nitrofs/FileDownloader.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.nitrofs

import android.os.Handler
import android.os.Looper
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.okhttp.OkHttp
Expand All @@ -11,6 +9,8 @@ import io.ktor.http.HttpMethod
import io.ktor.util.cio.writeChannel
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.copyAndClose
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

class FileDownloader {
Expand All @@ -30,7 +30,7 @@ class FileDownloader {
onDownload { totalBytesSent, contentLength ->
if (totalBytesSent > 0 && contentLength != null){
onProgress?.let {
Handler(Looper.getMainLooper()).post {
withContext(Dispatchers.Main) {
onProgress.invoke(totalBytesSent.toDouble(), contentLength.toDouble())
}
}
Expand Down
19 changes: 11 additions & 8 deletions android/src/main/java/com/nitrofs/HybridNitroFS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import com.margelo.nitro.nitrofs.NitroFile
import com.margelo.nitro.nitrofs.NitroFileEncoding
import com.margelo.nitro.nitrofs.NitroFileStat
import com.margelo.nitro.nitrofs.NitroUploadOptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers

class HybridNitroFS: HybridNitroFSSpec() {
val context = NitroModules.applicationContext ?: error("React Native context not found")
val nitroFsImpl = NitroFSImpl(context)
val ioScope = CoroutineScope(Dispatchers.IO)

override val BUNDLE_DIR: String
get() = ""
Expand All @@ -34,7 +37,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
data: String,
encoding: NitroFileEncoding
): Promise<Unit> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.writeFile(path, data, encoding)
} catch (e: Exception) {
Expand All @@ -48,7 +51,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
path: String,
encoding: NitroFileEncoding
): Promise<String> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.readFile(path, encoding)
} catch (e: Exception) {
Expand All @@ -62,7 +65,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
srcPath: String,
destPath: String
): Promise<Unit> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.copyFile(srcPath, destPath)
} catch (e: Exception) {
Expand All @@ -80,7 +83,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
}

override fun unlink(path: String): Promise<Boolean> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.unlink(path)
} catch (e: Exception) {
Expand All @@ -91,7 +94,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
}

override fun mkdir(path: String): Promise<Boolean> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.mkdir(path)
} catch (e: Exception) {
Expand All @@ -102,7 +105,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
}

override fun stat(path: String): Promise<NitroFileStat> {
return Promise.async {
return Promise.async(ioScope) {
nitroFsImpl.stat(path)
}
}
Expand All @@ -112,7 +115,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
uploadOptions: NitroUploadOptions,
onProgress: ((Double, Double) -> Unit)?
): Promise<Unit> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.uploadFile(file, uploadOptions, onProgress)
} catch (e: Exception) {
Expand All @@ -128,7 +131,7 @@ class HybridNitroFS: HybridNitroFSSpec() {
destinationPath: String,
onProgress: ((Double, Double) -> Unit)?
): Promise<NitroFile> {
return Promise.async {
return Promise.async(ioScope) {
try {
nitroFsImpl.downloadFile(serverUrl, fileName, destinationPath, onProgress)
NitroFile(
Expand Down
6 changes: 3 additions & 3 deletions android/src/main/java/com/nitrofs/NitroFileUploader.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.nitrofs

import android.os.Handler
import android.os.Looper
import io.ktor.client.HttpClient
import com.margelo.nitro.nitrofs.NitroUploadMethod
import com.margelo.nitro.nitrofs.NitroUploadOptions
Expand All @@ -14,6 +12,8 @@ import io.ktor.http.Headers
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.utils.io.streams.asInput
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

class NitroFileUploader {
Expand Down Expand Up @@ -45,7 +45,7 @@ class NitroFileUploader {
onUpload { totalBytesSent, totalBytes ->
if (totalBytesSent > 0 && totalBytes != null) {
onProgress?.let {
Handler(Looper.getMainLooper()).post {
withContext(Dispatchers.Main) {
it.invoke(totalBytesSent.toDouble(), totalBytes.toDouble())
}
}
Expand Down