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