-
-
Notifications
You must be signed in to change notification settings - Fork 88
Implemented sort order for DAV documents #1434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rfc2822
merged 26 commits into
main-ose
from
1431-file-order-issue-when-accessing-audio-files-via-android-document-provider-saf-via-playbook
May 22, 2025
Merged
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
3aaa870
Implemented sort order
ArnyminerZ d7bdcb0
Fixed column name
ArnyminerZ f1edf6e
Implemented sort order
ArnyminerZ 5b905dc
Fixed column name
ArnyminerZ 20e9391
Merge branch 'main-ose' into 1431-file-order-issue-when-accessing-aud…
ArnyminerZ 39f9588
Merge remote-tracking branch 'origin/1431-file-order-issue-when-acces…
ArnyminerZ 4de50e2
Improved issues
ArnyminerZ 8e2ea85
Add test for WebDavDocumentDao.getChildren for ORDER BY
rfc2822 268ff49
Merge branch 'main-ose' into 1431-file-order-issue-when-accessing-aud…
ArnyminerZ f3095d5
Converted getChildren into a raw query
ArnyminerZ 65af1a9
Fix formatting of SQL query in WebDavDocumentDao
rfc2822 3a5aae9
Refactoring
ArnyminerZ 8245287
Drop comment
ArnyminerZ 05dc9a6
Merge branch 'main-ose' into 1431-file-order-issue-when-accessing-aud…
ArnyminerZ fa2f7d7
Changed default sort to show directories first
ArnyminerZ 4154091
Merge branch 'main-ose' into 1431-file-order-issue-when-accessing-aud…
ArnyminerZ 32c354b
Fixed tests
ArnyminerZ 8699634
Merge remote-tracking branch 'origin/1431-file-order-issue-when-acces…
ArnyminerZ 1baa0e4
Switched to query constructor instead of in-place
ArnyminerZ 50f6d48
Changed log method
ArnyminerZ dbcf233
Add DocumentSortByMapper to handle orderBy mapping for WebDavDocument…
rfc2822 0043732
Rename DavDocumentsProviderTest to DocumentSortByMapperTest and updat…
rfc2822 9e7d29c
Refactor sorting and mapping
rfc2822 a33ce92
Add "order by name" as last criterion
rfc2822 1eb59ae
Remove default sort order constant from DocumentSortByMapper and use …
rfc2822 0f9f1b4
Adapt comments
rfc2822 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
app/src/androidTest/kotlin/at/bitfire/davdroid/db/WebDavDocumentDaoTest.kt
This file contains hidden or 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,80 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid.db | ||
|
||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import kotlinx.coroutines.test.runTest | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.util.logging.Level | ||
import java.util.logging.Logger | ||
import javax.inject.Inject | ||
|
||
@HiltAndroidTest | ||
class WebDavDocumentDaoTest { | ||
|
||
@get:Rule | ||
val hiltRule = HiltAndroidRule(this) | ||
|
||
@Inject | ||
lateinit var db: AppDatabase | ||
|
||
@Inject | ||
lateinit var logger: Logger | ||
|
||
@Before | ||
fun setUp() { | ||
hiltRule.inject() | ||
} | ||
|
||
|
||
@Test | ||
fun testGetChildren() = runTest { | ||
val mountDao = db.webDavMountDao() | ||
val dao = db.webDavDocumentDao() | ||
|
||
val mount = WebDavMount(id = 1, name = "Test", url = "https://example.com/".toHttpUrl()) | ||
db.webDavMountDao().insert(mount) | ||
|
||
val root = WebDavDocument( | ||
id = 1, | ||
mountId = mount.id, | ||
parentId = null, | ||
name = "Root Document" | ||
) | ||
dao.insertOrReplace(root) | ||
dao.insertOrReplace(WebDavDocument(id = 0, mountId = mount.id, parentId = root.id, name = "Name 1", displayName = "DisplayName 2")) | ||
dao.insertOrReplace(WebDavDocument(id = 0, mountId = mount.id, parentId = root.id, name = "Name 2", displayName = "DisplayName 1")) | ||
dao.insertOrReplace(WebDavDocument(id = 0, mountId = mount.id, parentId = root.id, name = "Name 3", displayName = "Directory 1", isDirectory = true)) | ||
try { | ||
dao.getChildren(root.id, orderBy = "name DESC").let { result -> | ||
logger.log(Level.INFO, "getChildren single sort Result", result) | ||
|
||
assertEquals(listOf( | ||
"Name 3", | ||
"Name 2", | ||
"Name 1" | ||
), result.map { it.name }) | ||
} | ||
|
||
dao.getChildren(root.id, orderBy = "isDirectory DESC, name ASC").let { result -> | ||
logger.log(Level.INFO, "getChildren multiple sort Result", result) | ||
|
||
assertEquals(listOf( | ||
"Name 3", | ||
"Name 1", | ||
"Name 2" | ||
), result.map { it.name }) | ||
} | ||
} finally { | ||
mountDao.deleteAsync(mount) | ||
} | ||
} | ||
|
||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
91 changes: 91 additions & 0 deletions
91
app/src/main/kotlin/at/bitfire/davdroid/webdav/DocumentSortByMapper.kt
This file contains hidden or 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,91 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid.webdav | ||
|
||
import android.provider.DocumentsContract.Document | ||
import at.bitfire.davdroid.db.WebDavDocument | ||
import java.util.logging.Logger | ||
import javax.inject.Inject | ||
|
||
class DocumentSortByMapper @Inject constructor( | ||
private val logger: Logger | ||
) { | ||
|
||
/** | ||
* Contains a map that maps the column names from the documents provider to [WebDavDocument]. | ||
*/ | ||
private val columnsMap = mapOf( | ||
Document.COLUMN_DOCUMENT_ID to "id", | ||
Document.COLUMN_DISPLAY_NAME to "displayName", | ||
Document.COLUMN_MIME_TYPE to "mimeType", | ||
Document.COLUMN_SIZE to "size", | ||
Document.COLUMN_LAST_MODIFIED to "lastModified" | ||
) | ||
|
||
/** | ||
* Maps an incoming `orderBy` column from a [android.content.ContentProvider] query to | ||
* a validated SQL ORDER BY-clause for [WebDavDocument]s. | ||
* | ||
* @param orderBy orderBy of content provider documents | ||
* | ||
* @return value of the ORDER BY-clause, like "name ASC" | ||
*/ | ||
fun mapContentProviderToSql(orderBy: String): String { | ||
// Map incoming orderBy to a list of pair of column and direction (true for ASC), like | ||
// [ Pair("displayName", Boolean), … ] | ||
val requestedFields = orderBy | ||
// Split by commas to divide each order column | ||
.split(',') | ||
// Trim any leading or trailing spaces | ||
.map { it.trim() } | ||
|
||
val requestedCriteria = mutableListOf<Pair<String, Boolean>>() | ||
rfc2822 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (field in requestedFields) { | ||
val idx = field.indexOfFirst { it == ' ' } | ||
if (idx == -1) | ||
// no whitespace, only name → use ASC as default, like in SQL | ||
requestedCriteria += field to true | ||
else { | ||
// whitespace, name and sort order | ||
val name = field.substring(0, idx) | ||
val directionStr = field.substring(idx).trim() | ||
val ascending = directionStr.equals("ASC", true) | ||
requestedCriteria += name to ascending | ||
} | ||
} | ||
|
||
// If displayName doesn't appear in sort order, append it in case that the other criteria generate | ||
// the same order. For instance, if files are ordered by ascending size and all have 0 bytes, then | ||
// another order by name is useful. | ||
if (!requestedCriteria.any { it.first == Document.COLUMN_DISPLAY_NAME }) | ||
requestedCriteria += Document.COLUMN_DISPLAY_NAME to true | ||
|
||
// Generate SQL | ||
val sqlSortBy = mutableListOf<String>() // list of valid SQL ORDER BY elements like "displayName ASC" | ||
for ((requestedColumn, ascending) in requestedCriteria) { | ||
// Only take columns that are registered in the columns map | ||
val sqlFieldName = columnsMap[requestedColumn] | ||
if (sqlFieldName == null) { | ||
logger.warning("Ignoring unknown column in sortOrder: $requestedColumn") | ||
continue | ||
} | ||
|
||
// Finally, convert the column name from document to room, including the sort direction. | ||
sqlSortBy += "$sqlFieldName ${if (ascending) "ASC" else "DESC"}" | ||
} | ||
return sqlSortBy.joinToString(", ") | ||
} | ||
|
||
|
||
companion object { | ||
|
||
/** | ||
* Default ORDER BY value to use when content provider doesn't specify a sort order. | ||
*/ | ||
const val DEFAULT_ORDER = "isDirectory DESC, name ASC" | ||
|
||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
app/src/test/kotlin/at/bitfire/davdroid/webdav/DocumentSortByMapperTest.kt
This file contains hidden or 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,52 @@ | ||
/* | ||
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details. | ||
*/ | ||
|
||
package at.bitfire.davdroid.webdav | ||
|
||
import android.provider.DocumentsContract.Document | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Test | ||
import java.util.logging.Logger | ||
|
||
class DocumentSortByMapperTest { | ||
|
||
private val mapper = DocumentSortByMapper(Logger.getGlobal()) | ||
|
||
@Test | ||
fun test_MapContentProviderToSql() { | ||
// Valid column without direction | ||
assertEquals( | ||
"displayName ASC", | ||
mapper.mapContentProviderToSql(Document.COLUMN_DISPLAY_NAME) | ||
) | ||
// Valid column with direction | ||
assertEquals( | ||
"displayName DESC", | ||
mapper.mapContentProviderToSql("${Document.COLUMN_DISPLAY_NAME} DESC") | ||
) | ||
// Valid column with direction and multiple spaces | ||
assertEquals( | ||
"displayName ASC", | ||
mapper.mapContentProviderToSql("${Document.COLUMN_DISPLAY_NAME} ASC") | ||
) | ||
// Invalid column without direction | ||
assertEquals( | ||
"displayName ASC", | ||
mapper.mapContentProviderToSql("invalid") | ||
) | ||
// Invalid column with direction | ||
assertEquals( | ||
"displayName ASC", | ||
mapper.mapContentProviderToSql("invalid ASC") | ||
) | ||
// Valid and invalid columns with and without directions | ||
assertEquals( | ||
"displayName ASC, mimeType DESC", | ||
mapper.mapContentProviderToSql( | ||
"${Document.COLUMN_DISPLAY_NAME}, invalid DESC, ${Document.COLUMN_MIME_TYPE} DESC" | ||
) | ||
) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.