mirror of
https://github.com/bitfireAT/davx5-ose
synced 2024-07-22 03:01:24 +00:00
Replace DavUtils.lastSegmentOfUrl
by UrlUtils.lastSegment
(#767)
* Added `ReplaceWith` to `lastSegmentOfUrl` Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> * Replaced all usages Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> * Rollback replacement Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> * Added tests for UrlUtils Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> * Got rid of `DavUtils.lastSegmentOfUrl` Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> * Turned into variable Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> * Fixed method call Signed-off-by: Arnau Mora <arnyminerz@proton.me> * Fixed method call Signed-off-by: Arnau Mora <arnyminerz@proton.me> --------- Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me> Signed-off-by: Arnau Mora <arnyminerz@proton.me> Co-authored-by: Ricki Hirner <hirner@bitfire.at>
This commit is contained in:
parent
39f8f2e475
commit
a7c04c2bf7
|
@ -16,6 +16,7 @@ import at.bitfire.davdroid.network.HttpClient
|
|||
import at.bitfire.davdroid.resource.LocalResource
|
||||
import at.bitfire.davdroid.settings.AccountSettings
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
|
@ -81,7 +82,7 @@ class TestSyncManager(
|
|||
assertEquals(assertDownloadRemote.keys.toList(), bunch)
|
||||
|
||||
for ((url, eTag) in assertDownloadRemote) {
|
||||
val fileName = DavUtils.lastSegmentOfUrl(url)
|
||||
val fileName = url.lastSegment
|
||||
var localEntry = localCollection.entries.firstOrNull { it.fileName == fileName }
|
||||
if (localEntry == null) {
|
||||
val newEntry = LocalTestResource().also {
|
||||
|
|
|
@ -23,6 +23,7 @@ import at.bitfire.dav4jvm.property.webdav.CurrentUserPrivilegeSet
|
|||
import at.bitfire.dav4jvm.property.webdav.DisplayName
|
||||
import at.bitfire.dav4jvm.property.webdav.ResourceType
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
|
@ -211,7 +212,7 @@ data class Collection(
|
|||
}
|
||||
|
||||
// calculated properties
|
||||
fun title() = displayName ?: DavUtils.lastSegmentOfUrl(url)
|
||||
fun title() = displayName ?: url.lastSegment
|
||||
fun readOnly() = forceReadOnly || !privWriteContent
|
||||
|
||||
}
|
|
@ -38,6 +38,6 @@ data class HomeSet(
|
|||
var displayName: String? = null
|
||||
) {
|
||||
|
||||
fun title() = displayName ?: url.lastSegment()
|
||||
fun title() = displayName ?: url.lastSegment
|
||||
|
||||
}
|
|
@ -25,6 +25,7 @@ import at.bitfire.davdroid.log.Logger
|
|||
import at.bitfire.davdroid.settings.AccountSettings
|
||||
import at.bitfire.davdroid.syncadapter.AccountUtils
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.davdroid.util.setAndVerifyUserData
|
||||
import at.bitfire.vcard4android.AndroidAddressBook
|
||||
import at.bitfire.vcard4android.AndroidContact
|
||||
|
@ -108,7 +109,7 @@ open class LocalAddressBook(
|
|||
|
||||
val sb = StringBuilder(info.displayName.let {
|
||||
if (it.isNullOrEmpty())
|
||||
DavUtils.lastSegmentOfUrl(info.url)
|
||||
info.url.lastSegment
|
||||
else
|
||||
it
|
||||
})
|
||||
|
|
|
@ -16,6 +16,7 @@ import at.bitfire.davdroid.db.Collection
|
|||
import at.bitfire.davdroid.db.SyncState
|
||||
import at.bitfire.davdroid.log.Logger
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.ical4android.AndroidCalendar
|
||||
import at.bitfire.ical4android.AndroidCalendarFactory
|
||||
import at.bitfire.ical4android.BatchOperation
|
||||
|
@ -54,7 +55,8 @@ class LocalCalendar private constructor(
|
|||
private fun valuesFromCollectionInfo(info: Collection, withColor: Boolean): ContentValues {
|
||||
val values = ContentValues()
|
||||
values.put(Calendars.NAME, info.url.toString())
|
||||
values.put(Calendars.CALENDAR_DISPLAY_NAME, if (info.displayName.isNullOrBlank()) DavUtils.lastSegmentOfUrl(info.url) else info.displayName)
|
||||
values.put(Calendars.CALENDAR_DISPLAY_NAME,
|
||||
if (info.displayName.isNullOrBlank()) info.url.lastSegment else info.displayName)
|
||||
|
||||
if (withColor)
|
||||
values.put(Calendars.CALENDAR_COLOR, info.color ?: Constants.DAVDROID_GREEN_RGBA)
|
||||
|
|
|
@ -13,6 +13,7 @@ import at.bitfire.davdroid.db.Principal
|
|||
import at.bitfire.davdroid.db.SyncState
|
||||
import at.bitfire.davdroid.log.Logger
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.ical4android.JtxCollection
|
||||
import at.bitfire.ical4android.JtxCollectionFactory
|
||||
import at.bitfire.ical4android.JtxICalObject
|
||||
|
@ -33,7 +34,10 @@ class LocalJtxCollection(account: Account, client: ContentProviderClient, id: Lo
|
|||
fun valuesFromCollection(info: Collection, account: Account, owner: Principal?, withColor: Boolean) =
|
||||
ContentValues().apply {
|
||||
put(JtxContract.JtxCollection.URL, info.url.toString())
|
||||
put(JtxContract.JtxCollection.DISPLAYNAME, info.displayName ?: DavUtils.lastSegmentOfUrl(info.url))
|
||||
put(
|
||||
JtxContract.JtxCollection.DISPLAYNAME,
|
||||
info.displayName ?: info.url.lastSegment
|
||||
)
|
||||
put(JtxContract.JtxCollection.DESCRIPTION, info.description)
|
||||
if (owner != null)
|
||||
put(JtxContract.JtxCollection.OWNER, owner.url.toString())
|
||||
|
|
|
@ -14,6 +14,7 @@ import at.bitfire.davdroid.db.Collection
|
|||
import at.bitfire.davdroid.db.SyncState
|
||||
import at.bitfire.davdroid.log.Logger
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.ical4android.DmfsTaskList
|
||||
import at.bitfire.ical4android.DmfsTaskListFactory
|
||||
import at.bitfire.ical4android.TaskProvider
|
||||
|
@ -53,7 +54,8 @@ class LocalTaskList private constructor(
|
|||
private fun valuesFromCollectionInfo(info: Collection, withColor: Boolean): ContentValues {
|
||||
val values = ContentValues(3)
|
||||
values.put(TaskLists._SYNC_ID, info.url.toString())
|
||||
values.put(TaskLists.LIST_NAME, if (info.displayName.isNullOrBlank()) DavUtils.lastSegmentOfUrl(info.url) else info.displayName)
|
||||
values.put(TaskLists.LIST_NAME,
|
||||
if (info.displayName.isNullOrBlank()) info.url.lastSegment else info.displayName)
|
||||
|
||||
if (withColor)
|
||||
values.put(TaskLists.LIST_COLOR, info.color ?: Constants.DAVDROID_GREEN_RGBA)
|
||||
|
|
|
@ -27,6 +27,7 @@ import at.bitfire.davdroid.resource.LocalEvent
|
|||
import at.bitfire.davdroid.resource.LocalResource
|
||||
import at.bitfire.davdroid.settings.AccountSettings
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.ical4android.Event
|
||||
import at.bitfire.ical4android.InvalidCalendarException
|
||||
import at.bitfire.ical4android.util.DateUtils
|
||||
|
@ -182,7 +183,12 @@ class CalendarSyncManager(
|
|||
val iCal = calendarData?.iCalendar
|
||||
?: throw DavException("Received multi-get response without calendar data")
|
||||
|
||||
processVEvent(DavUtils.lastSegmentOfUrl(response.href), eTag, scheduleTag, StringReader(iCal))
|
||||
processVEvent(
|
||||
response.href.lastSegment,
|
||||
eTag,
|
||||
scheduleTag,
|
||||
StringReader(iCal)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import at.bitfire.davdroid.syncadapter.groups.CategoriesStrategy
|
|||
import at.bitfire.davdroid.syncadapter.groups.VCard4Strategy
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.DavUtils.sameTypeAs
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.vcard4android.Contact
|
||||
import at.bitfire.vcard4android.GroupMethod
|
||||
import ezvcard.VCardVersion
|
||||
|
@ -304,7 +305,13 @@ class ContactsSyncManager(
|
|||
val card = addressData?.card
|
||||
?: throw DavException("Received multi-get response without address data")
|
||||
|
||||
processCard(DavUtils.lastSegmentOfUrl(response.href), eTag, StringReader(card), isJCard, resourceDownloader)
|
||||
processCard(
|
||||
response.href.lastSegment,
|
||||
eTag,
|
||||
StringReader(card),
|
||||
isJCard,
|
||||
resourceDownloader
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import at.bitfire.davdroid.resource.LocalJtxICalObject
|
|||
import at.bitfire.davdroid.resource.LocalResource
|
||||
import at.bitfire.davdroid.settings.AccountSettings
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.ical4android.InvalidCalendarException
|
||||
import at.bitfire.ical4android.JtxICalObject
|
||||
import okhttp3.HttpUrl
|
||||
|
@ -111,7 +112,7 @@ class JtxSyncManager(
|
|||
val iCal = calendarData?.iCalendar
|
||||
?: throw DavException("Received multi-get response without task data")
|
||||
|
||||
processICalObject(DavUtils.lastSegmentOfUrl(response.href), eTag, StringReader(iCal))
|
||||
processICalObject(response.href.lastSegment, eTag, StringReader(iCal))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import at.bitfire.davdroid.resource.LocalTask
|
|||
import at.bitfire.davdroid.resource.LocalTaskList
|
||||
import at.bitfire.davdroid.settings.AccountSettings
|
||||
import at.bitfire.davdroid.util.DavUtils
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import at.bitfire.ical4android.InvalidCalendarException
|
||||
import at.bitfire.ical4android.Task
|
||||
import okhttp3.HttpUrl
|
||||
|
@ -111,7 +112,7 @@ class TasksSyncManager(
|
|||
val iCal = calendarData?.iCalendar
|
||||
?: throw DavException("Received multi-get response without task data")
|
||||
|
||||
processVTodo(DavUtils.lastSegmentOfUrl(response.href), eTag, StringReader(iCal))
|
||||
processVTodo(response.href.lastSegment, eTag, StringReader(iCal))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class CollectionScreenModel @AssistedInject constructor(
|
|||
val owner: Flow<String?> = collection.map { collection ->
|
||||
collection?.ownerId?.let { ownerId ->
|
||||
val principal = principalDao.getAsync(ownerId)
|
||||
principal.displayName ?: principal.url.lastSegment()
|
||||
principal.displayName ?: principal.url.lastSegment
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -60,15 +60,6 @@ object DavUtils {
|
|||
return String.format(Locale.ROOT, "#%06X%02X", color, alpha)
|
||||
}
|
||||
|
||||
@Deprecated("Use HttpUrl.lastSegment in UrlUtils")
|
||||
fun lastSegmentOfUrl(url: HttpUrl): String {
|
||||
// the list returned by HttpUrl.pathSegments() is unmodifiable, so we have to create a copy
|
||||
val segments = LinkedList(url.pathSegments)
|
||||
segments.reverse()
|
||||
|
||||
return segments.firstOrNull { it.isNotEmpty() } ?: "/"
|
||||
}
|
||||
|
||||
fun prepareLookup(context: Context, lookup: Lookup) {
|
||||
if (Build.VERSION.SDK_INT >= 29) {
|
||||
/* Since Android 10, there's a native DnsResolver API that allows to send SRV queries without
|
||||
|
|
|
@ -6,5 +6,5 @@ package at.bitfire.davdroid.util
|
|||
|
||||
import okhttp3.HttpUrl
|
||||
|
||||
fun HttpUrl.lastSegment(): String =
|
||||
pathSegments.lastOrNull { it.isNotEmpty() } ?: "/"
|
||||
val HttpUrl.lastSegment: String
|
||||
get() = pathSegments.lastOrNull { it.isNotEmpty() } ?: "/"
|
|
@ -18,9 +18,6 @@ import org.xbill.DNS.SRVRecord
|
|||
|
||||
class DavUtilsTest {
|
||||
|
||||
val exampleURL = "http://example.com/"
|
||||
|
||||
|
||||
@Test
|
||||
fun testAcceptAnything() {
|
||||
assertEquals("*/*", DavUtils.acceptAnything(null))
|
||||
|
@ -34,14 +31,6 @@ class DavUtilsTest {
|
|||
assertEquals("#000000FF", DavUtils.ARGBtoCalDAVColor(0xFF000000.toInt()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLastSegmentOfUrl() {
|
||||
assertEquals("/", DavUtils.lastSegmentOfUrl(exampleURL.toHttpUrl()))
|
||||
assertEquals("dir", DavUtils.lastSegmentOfUrl((exampleURL + "dir").toHttpUrl()))
|
||||
assertEquals("dir", DavUtils.lastSegmentOfUrl((exampleURL + "dir/").toHttpUrl()))
|
||||
assertEquals("file.html", DavUtils.lastSegmentOfUrl((exampleURL + "dir/file.html").toHttpUrl()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testParent() {
|
||||
// with trailing slash
|
||||
|
|
18
app/src/test/kotlin/at/bitfire/davdroid/UrlUtilsTest.kt
Normal file
18
app/src/test/kotlin/at/bitfire/davdroid/UrlUtilsTest.kt
Normal file
|
@ -0,0 +1,18 @@
|
|||
package at.bitfire.davdroid
|
||||
|
||||
import at.bitfire.davdroid.util.lastSegment
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
class UrlUtilsTest {
|
||||
private val exampleURL = "http://example.com/"
|
||||
|
||||
@Test
|
||||
fun testLastSegmentOfUrl() {
|
||||
Assert.assertEquals("/", exampleURL.toHttpUrl().lastSegment)
|
||||
Assert.assertEquals("dir", (exampleURL + "dir").toHttpUrl().lastSegment)
|
||||
Assert.assertEquals("dir", (exampleURL + "dir/").toHttpUrl().lastSegment)
|
||||
Assert.assertEquals("file.html", (exampleURL + "dir/file.html").toHttpUrl().lastSegment)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue