Format TimelineRepository.kt

This commit is contained in:
Adolfo Santiago 2022-04-03 10:24:46 +02:00
parent 7047eb67aa
commit 69f27b92e5
No known key found for this signature in database
GPG key ID: 244D6F9A317B4A65

View file

@ -19,7 +19,6 @@ import io.reactivex.schedulers.Schedulers
import java.io.IOException
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.collections.ArrayList
data class Placeholder(val id: String)
@ -30,8 +29,10 @@ enum class TimelineRequestMode {
}
interface TimelineRepository {
fun getStatuses(maxId: String?, sinceId: String?, sincedIdMinusOne: String?, limit: Int,
requestMode: TimelineRequestMode): Single<out List<TimelineStatus>>
fun getStatuses(
maxId: String?, sinceId: String?, sincedIdMinusOne: String?, limit: Int,
requestMode: TimelineRequestMode
): Single<out List<TimelineStatus>>
companion object {
val CLEANUP_INTERVAL = TimeUnit.DAYS.toMillis(14)
@ -49,7 +50,8 @@ class TimelineRepositoryImpl(
this.cleanup()
}
override fun getStatuses(maxId: String?, sinceId: String?, sincedIdMinusOne: String?,
override fun getStatuses(
maxId: String?, sinceId: String?, sincedIdMinusOne: String?,
limit: Int, requestMode: TimelineRequestMode
): Single<out List<TimelineStatus>> {
val acc = accountManager.activeAccount ?: throw IllegalStateException()
@ -62,7 +64,8 @@ class TimelineRepositoryImpl(
}
}
private fun getStatusesFromNetwork(maxId: String?, sinceId: String?,
private fun getStatusesFromNetwork(
maxId: String?, sinceId: String?,
sinceIdMinusOne: String?, limit: Int,
accountId: Long, requestMode: TimelineRequestMode
): Single<out List<TimelineStatus>> {
@ -82,7 +85,8 @@ class TimelineRepositoryImpl(
}
}
private fun addFromDbIfNeeded(accountId: Long, statuses: List<Either<Placeholder, Status>>,
private fun addFromDbIfNeeded(
accountId: Long, statuses: List<Either<Placeholder, Status>>,
maxId: String?, sinceId: String?, limit: Int,
requestMode: TimelineRequestMode
): Single<List<TimelineStatus>>? {
@ -107,8 +111,10 @@ class TimelineRepositoryImpl(
}
}
private fun getStatusesFromDb(accountId: Long, maxId: String?, sinceId: String?,
limit: Int): Single<out List<TimelineStatus>> {
private fun getStatusesFromDb(
accountId: Long, maxId: String?, sinceId: String?,
limit: Int
): Single<out List<TimelineStatus>> {
return timelineDao.getStatusesForAccount(accountId, maxId, sinceId, limit)
.subscribeOn(Schedulers.io())
.map { statuses ->
@ -116,7 +122,8 @@ class TimelineRepositoryImpl(
}
}
private fun saveStatusesToDb(accountId: Long, statuses: List<Status>,
private fun saveStatusesToDb(
accountId: Long, statuses: List<Status>,
maxId: String?, sinceId: String?
): List<Either<Placeholder, Status>> {
var placeholderToInsert: Placeholder? = null
@ -165,13 +172,16 @@ class TimelineRepositoryImpl(
// (for requests on next launches) but not return it.
if(sinceId == null && statuses.isNotEmpty()) {
timelineDao.insertStatusIfNotThere(
Placeholder(statuses.last().id.dec()).toEntity(accountId))
Placeholder(statuses.last().id.dec()).toEntity(accountId)
)
}
// There may be placeholders which we thought could be from our TL but they are not
if(statuses.size > 2) {
timelineDao.removeAllPlaceholdersBetween(accountId, statuses.first().id,
statuses.last().id)
timelineDao.removeAllPlaceholdersBetween(
accountId, statuses.first().id,
statuses.last().id
)
} else if(placeholderToInsert == null && maxId != null && sinceId != null) {
timelineDao.removeAllPlaceholdersBetween(accountId, maxId, sinceId)
}
@ -194,13 +204,19 @@ class TimelineRepositoryImpl(
return Either.Left(Placeholder(this.status.serverId))
}
val attachments: ArrayList<Attachment> = gson.fromJson(status.attachments,
object : TypeToken<List<Attachment>>() {}.type) ?: ArrayList()
val mentions: Array<Status.Mention> = gson.fromJson(status.mentions,
Array<Status.Mention>::class.java) ?: arrayOf()
val attachments: ArrayList<Attachment> = gson.fromJson(
status.attachments,
object : TypeToken<List<Attachment>>() {}.type
) ?: ArrayList()
val mentions: Array<Status.Mention> = gson.fromJson(
status.mentions,
Array<Status.Mention>::class.java
) ?: arrayOf()
val application = gson.fromJson(status.application, Status.Application::class.java)
val emojis: List<Emoji> = gson.fromJson(status.emojis,
object : TypeToken<List<Emoji>>() {}.type) ?: listOf()
val emojis: List<Emoji> = gson.fromJson(
status.emojis,
object : TypeToken<List<Emoji>>() {}.type
) ?: listOf()
val poll: Poll? = gson.fromJson(status.poll, Poll::class.java)
val pleroma = gson.fromJson(status.pleroma, Status.PleromaStatus::class.java)
@ -212,7 +228,8 @@ class TimelineRepositoryImpl(
inReplyToId = status.inReplyToId,
inReplyToAccountId = status.inReplyToAccountId,
reblog = null,
content = status.content?.parseAsHtml()?.trimTrailingWhitespace() ?: SpannedString(""),
content = status.content?.parseAsHtml()?.trimTrailingWhitespace()
?: SpannedString(""),
createdAt = Date(status.createdAt),
emojis = emojis,
reblogsCount = status.reblogsCount,
@ -267,7 +284,8 @@ class TimelineRepositoryImpl(
inReplyToId = status.inReplyToId,
inReplyToAccountId = status.inReplyToAccountId,
reblog = null,
content = status.content?.parseAsHtml()?.trimTrailingWhitespace() ?: SpannedString(""),
content = status.content?.parseAsHtml()?.trimTrailingWhitespace()
?: SpannedString(""),
createdAt = Date(status.createdAt),
emojis = emojis,
reblogsCount = status.reblogsCount,
@ -359,8 +377,10 @@ fun Placeholder.toEntity(timelineUserId: Long): TimelineStatusEntity {
)
}
fun Status.toEntity(timelineUserId: Long,
gson: Gson): TimelineStatusEntity {
fun Status.toEntity(
timelineUserId: Long,
gson: Gson
): TimelineStatusEntity {
val actionable = actionableStatus
return TimelineStatusEntity(
serverId = this.id,