fix String.inc() and String.dec() not being inverse operations (#2355)

This commit is contained in:
Konrad Pozniak 2022-03-01 21:29:05 +01:00 committed by GitHub
commit b145fc9d50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 53 deletions

View file

@ -143,7 +143,7 @@ class CachedTimelineViewModel @Inject constructor(
val nextPlaceholderId = timelineDao.getNextPlaceholderIdAfter(activeAccount.id, placeholderId)
val response = api.homeTimeline(maxId = placeholderId.inc(), sinceId = nextPlaceholderId, limit = 20).await()
val response = api.homeTimeline(maxId = placeholderId.inc(), sinceId = nextPlaceholderId, limit = LOAD_AT_ONCE).await()
val statuses = response.body()
if (!response.isSuccessful || statuses == null) {

View file

@ -17,27 +17,39 @@ fun randomAlphanumericString(count: Int): String {
}
// We sort statuses by ID. Something we need to invent some ID for placeholder.
// Not sure if inc()/dec() should be made `operator` or not
/**
* "Increment" string so that during sorting it's bigger than [this].
* "Increment" string so that during sorting it's bigger than [this]. Inverse operation to [dec].
*/
fun String.inc(): String {
// We assume that we will stay in the safe range for now
val builder = this.toCharArray()
builder[lastIndex] = builder[lastIndex].inc()
return String(builder)
var i = builder.lastIndex
while (i >= 0) {
if (builder[i] < 'z') {
builder[i] = builder[i].inc()
return String(builder)
} else {
builder[i] = '0'
}
i--
}
return String(
CharArray(builder.size + 1) { index ->
if (index == 0) '0' else builder[index - 1]
}
)
}
/**
* "Decrement" string so that during sorting it's smaller than [this].
* "Decrement" string so that during sorting it's smaller than [this]. Inverse operation to [inc].
*/
fun String.dec(): String {
if (this.isEmpty()) return this
val builder = this.toCharArray()
var i = builder.lastIndex
while (i > 0) {
while (i >= 0) {
if (builder[i] > '0') {
builder[i] = builder[i].dec()
return String(builder)
@ -46,12 +58,7 @@ fun String.dec(): String {
}
i--
}
return if (builder[0] > '1') {
builder[0] = builder[0].dec()
String(builder)
} else {
String(builder.copyOfRange(1, builder.size))
}
return String(builder.copyOfRange(1, builder.size))
}
/**
@ -71,15 +78,6 @@ fun String.isLessThan(other: String): Boolean {
}
}
fun String.idCompareTo(other: String): Int {
return when {
this === other -> 0
this.length < other.length -> -1
this.length > other.length -> 1
else -> this.compareTo(other)
}
}
fun Spanned.trimTrailingWhitespace(): Spanned {
var i = length
do {