Skip to content

Commit

Permalink
Add content file system
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Corry <[email protected]>
  • Loading branch information
kylecorry31 committed Nov 9, 2024
1 parent 45c5dd1 commit d510a63
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
extra.apply {
set("groupId", "com.kylecorry.andromeda")
set("versionName", "10.5.2")
set("versionName", "11.0.0")
set("targetVersion", 35)
set("compileVersion", 35)
}
Expand Down
1 change: 1 addition & 0 deletions files/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
implementation project(':core')
implementation libs.androidx.core.ktx
implementation libs.kotlinx.coroutines.android
implementation libs.androidx.documentfile
coreLibraryDesugaring libs.desugar
testImplementation libs.junit
androidTestImplementation libs.androidx.junit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kylecorry.andromeda.files

import android.net.Uri
import java.time.Instant

data class ContentDocument(
val uri: Uri,
val displayName: String?,
val mimeType: String?,
val canRead: Boolean,
val canWrite: Boolean,
val lastModified: Instant,
val size: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.kylecorry.andromeda.files

import android.content.Context
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import java.time.Instant
import kotlin.collections.toList

class ContentFileSystem(context: Context, treeUri: Uri) {

private val tree = DocumentFile.fromTreeUri(context, treeUri)

fun createFile(displayName: String, mimeType: String): ContentDocument? {
return tree?.createFile(mimeType, displayName)?.toContentDocument()
}

fun getFile(displayName: String): ContentDocument? {
return findFile(displayName)?.toContentDocument()
}

fun deleteFile(displayName: String): Boolean {
return findFile(displayName)?.delete() == true
}

fun listFiles(): List<DocumentFile> {
return tree?.listFiles()?.toList() ?: emptyList()
}

fun canWrite(displayName: String? = null): Boolean {
return if (displayName == null) {
tree?.canWrite() == true
} else {
findFile(displayName)?.canWrite() == true
}
}

fun canRead(displayName: String? = null): Boolean {
return if (displayName == null) {
tree?.canRead() == true
} else {
findFile(displayName)?.canRead() == true
}
}

private fun findFile(displayName: String): DocumentFile? {
return tree?.findFile(displayName)
}

companion object {
fun getDocumentFromUri(context: Context, uri: Uri): ContentDocument? {
return DocumentFile.fromSingleUri(context, uri)?.toContentDocument()
}

private fun DocumentFile.toContentDocument(): ContentDocument {
return ContentDocument(
uri,
name,
type,
canRead(),
canWrite(),
Instant.ofEpochMilli(lastModified()),
length()
)
}
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
androidLibraryVersion = "8.2.2"
androidXDocumentFileVersion = "1.0.1"
appCompatVersion = "1.7.0"
cameraCamera2Version = "1.3.4"
constraintlayoutVersion = "2.1.4"
Expand Down Expand Up @@ -37,6 +38,7 @@ androidx-camera-lifecycle = { module = "androidx.camera:camera-lifecycle", versi
androidx-camera-view = { module = "androidx.camera:camera-view", version.ref = "cameraCamera2Version" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayoutVersion" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtxVersion" }
androidx-documentfile = { module = "androidx.documentfile:documentfile", version.ref = "androidXDocumentFileVersion" }
androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferencesVersion" }
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCoreVersion" }
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junit" }
Expand Down

0 comments on commit d510a63

Please sign in to comment.