-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #121 from vinceglb/improve-filekit-api
♻️ Improve FileKit API
- Loading branch information
Showing
139 changed files
with
2,274 additions
and
1,976 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ on: | |
push: | ||
branches: | ||
- main | ||
- next | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
filekit-coil/src/androidMain/kotlin/io/github/vinceglb/filekit/coil/FileKitCoil.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.vinceglb.filekit.coil | ||
|
||
import io.github.vinceglb.filekit.PlatformFile | ||
|
||
public actual val PlatformFile.coilModel: Any | ||
get() = uri |
6 changes: 6 additions & 0 deletions
6
filekit-coil/src/appleMain/kotlin/io/github/vinceglb/filekit/coil/FileKitCoil.apple.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.vinceglb.filekit.coil | ||
|
||
import io.github.vinceglb.filekit.PlatformFile | ||
|
||
public actual val PlatformFile.coilModel: Any | ||
get() = nsUrl |
7 changes: 7 additions & 0 deletions
7
filekit-coil/src/commonMain/kotlin/io/github/vinceglb/filekit/coil/FileKitCoil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.github.vinceglb.filekit.coil | ||
|
||
import androidx.compose.runtime.Composable | ||
import io.github.vinceglb.filekit.PlatformFile | ||
|
||
@Composable | ||
public expect fun rememberPlatformFileCoilModel(file: PlatformFile): Any? |
6 changes: 6 additions & 0 deletions
6
filekit-coil/src/jvmMain/kotlin/io/github/vinceglb/filekit/coil/FileKitCoil.jvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.vinceglb.filekit.coil | ||
|
||
import io.github.vinceglb.filekit.PlatformFile | ||
|
||
public actual val PlatformFile.coilModel: Any | ||
get() = file |
10 changes: 10 additions & 0 deletions
10
filekit-coil/src/nonWebMain/kotlin/io/github/vinceglb/filekit/coil/FileKitCoil.nonWeb.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.vinceglb.filekit.coil | ||
|
||
import androidx.compose.runtime.Composable | ||
import io.github.vinceglb.filekit.PlatformFile | ||
|
||
public expect val PlatformFile.coilModel: Any | ||
|
||
@Composable | ||
public actual fun rememberPlatformFileCoilModel(file: PlatformFile): Any? = | ||
file.coilModel |
21 changes: 21 additions & 0 deletions
21
filekit-coil/src/webMain/kotlin/io/github/vinceglb/filekit/coil/FileKitCoil.web.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.vinceglb.filekit.coil | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import io.github.vinceglb.filekit.PlatformFile | ||
import io.github.vinceglb.filekit.readBytes | ||
|
||
@Composable | ||
public actual fun rememberPlatformFileCoilModel(file: PlatformFile): Any? { | ||
var bytes by remember(file) { mutableStateOf<ByteArray?>(null) } | ||
|
||
LaunchedEffect(file) { | ||
bytes = file.readBytes() | ||
} | ||
|
||
return bytes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
filekit-core/src/androidMain/kotlin/io/github/vinceglb/filekit/FileKit.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.vinceglb.filekit | ||
|
||
import android.content.Context | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.result.ActivityResultRegistry | ||
import java.lang.ref.WeakReference | ||
|
||
// TODO add a FileKitInitializer? Remove registry from FileKit? | ||
public actual object FileKit { | ||
public var registry: ActivityResultRegistry? = null | ||
private set | ||
|
||
private var _context: WeakReference<Context?> = WeakReference(null) | ||
public val context: Context? | ||
get() = _context.get() | ||
|
||
public fun init(activity: ComponentActivity) { | ||
_context = WeakReference(activity.applicationContext) | ||
registry = activity.activityResultRegistry | ||
} | ||
|
||
public fun init(context: Context, registry: ActivityResultRegistry) { | ||
_context = WeakReference(context) | ||
FileKit.registry = registry | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
filekit-core/src/androidMain/kotlin/io/github/vinceglb/filekit/PlatformFile.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.github.vinceglb.filekit | ||
|
||
import android.content.ContentResolver | ||
import android.content.Context | ||
import android.net.Uri | ||
import android.provider.OpenableColumns | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
|
||
public actual data class PlatformFile( | ||
val uri: Uri, | ||
internal val context: Context, | ||
) | ||
|
||
public actual val PlatformFile.name: String | ||
get() = context.getFileName(uri) ?: throw IllegalStateException("Failed to get file name") | ||
|
||
public actual val PlatformFile.path: String | ||
get() = context.contentResolver.let { | ||
it.query(uri, null, null, null, null)?.use { cursor -> | ||
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); | ||
cursor.moveToFirst() | ||
val name = cursor.getString(nameIndex) | ||
File(context.filesDir, name).path | ||
} | ||
} ?: throw IllegalStateException("Failed to get file path") | ||
|
||
public actual val PlatformFile.size: Long | ||
get() = runCatching { | ||
context.contentResolver.query(uri, null, null, null, null) | ||
?.use { cursor -> | ||
cursor.moveToFirst() | ||
cursor.getColumnIndex(OpenableColumns.SIZE).let(cursor::getLong) | ||
} | ||
}.getOrNull() ?: throw IllegalStateException("Failed to get file size") | ||
|
||
public actual suspend fun PlatformFile.readBytes(): ByteArray = withContext(Dispatchers.IO) { | ||
context | ||
.contentResolver | ||
.openInputStream(uri) | ||
.use { stream -> stream?.readBytes() } | ||
?: throw IllegalStateException("Failed to read file") | ||
} | ||
|
||
public actual fun PlatformFile.getStream(): PlatformInputStream { | ||
return context | ||
.contentResolver | ||
.openInputStream(uri) | ||
?.let { PlatformInputStream(it) } | ||
?: throw IllegalStateException("Failed to open stream") | ||
} | ||
|
||
private fun Context.getFileName(uri: Uri): String? = when (uri.scheme) { | ||
ContentResolver.SCHEME_CONTENT -> getContentFileName(uri) | ||
else -> uri.path?.let(::File)?.name | ||
} | ||
|
||
private fun Context.getContentFileName(uri: Uri): String? = runCatching { | ||
contentResolver.query(uri, null, null, null, null)?.use { cursor -> | ||
cursor.moveToFirst() | ||
return@use cursor | ||
.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME) | ||
.let(cursor::getString) | ||
} | ||
}.getOrNull() |
5 changes: 2 additions & 3 deletions
5
...lekit/core/PlatformInputStream.android.kt → ...lb/filekit/PlatformInputStream.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.