2018-03-27 19:47:00 +02:00
|
|
|
/* Copyright 2018 charlag
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.network
|
|
|
|
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
import com.keylesspalace.tusky.appstore.*
|
2018-03-27 19:47:00 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Relationship
|
|
|
|
import com.keylesspalace.tusky.entity.Status
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
import io.reactivex.Single
|
2018-10-03 21:27:52 +02:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxkotlin.addTo
|
2018-03-27 19:47:00 +02:00
|
|
|
import okhttp3.ResponseBody
|
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
|
|
|
import retrofit2.Response
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 3/24/18.
|
|
|
|
*/
|
|
|
|
|
|
|
|
interface TimelineCases {
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
fun reblog(status: Status, reblog: Boolean): Single<Status>
|
|
|
|
fun favourite(status: Status, favourite: Boolean): Single<Status>
|
2018-03-27 19:47:00 +02:00
|
|
|
fun mute(id: String)
|
|
|
|
fun block(id: String)
|
|
|
|
fun delete(id: String)
|
2018-10-03 21:27:52 +02:00
|
|
|
fun pin(status: Status, pin: Boolean)
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TimelineCasesImpl(
|
|
|
|
private val mastodonApi: MastodonApi,
|
2018-05-27 10:22:12 +02:00
|
|
|
private val eventHub: EventHub
|
2018-03-27 19:47:00 +02:00
|
|
|
) : TimelineCases {
|
2018-10-03 21:27:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unused yet but can be use for cancellation later. It's always a good idea to save
|
|
|
|
* Disposables.
|
|
|
|
*/
|
|
|
|
private val cancelDisposable = CompositeDisposable()
|
|
|
|
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
override fun reblog(status: Status, reblog: Boolean): Single<Status> {
|
2018-03-27 19:47:00 +02:00
|
|
|
val id = status.actionableId
|
|
|
|
|
|
|
|
val call = if (reblog) {
|
|
|
|
mastodonApi.reblogStatus(id)
|
|
|
|
} else {
|
|
|
|
mastodonApi.unreblogStatus(id)
|
|
|
|
}
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
return call.doAfterSuccess {
|
|
|
|
eventHub.dispatch(ReblogEvent(status.id, reblog))
|
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
override fun favourite(status: Status, favourite: Boolean): Single<Status> {
|
2018-03-27 19:47:00 +02:00
|
|
|
val id = status.actionableId
|
|
|
|
|
|
|
|
val call = if (favourite) {
|
|
|
|
mastodonApi.favouriteStatus(id)
|
|
|
|
} else {
|
|
|
|
mastodonApi.unfavouriteStatus(id)
|
|
|
|
}
|
Caching toots (#809)
* Initial timeline cache implementation
* Fix build/DI errors for caching
* Rename timeline entities tables. Add migration. Add DB scheme file.
* Fix uniqueness problem, change offline strategy, improve mapping
* Try to merge in new statuses, fix bottom loading, fix saving spans.
* Fix reblogs IDs, fix inserting elements from top
* Send one more request to get latest timeline statuses
* Give Timeline placeholders string id. Rewrite Either in Kotlin
* Initial placeholder implementation for caching
* Fix crash on removing overlap statuses
* Migrate counters to long
* Remove unused counters. Add minimal TimelineDAOTest
* Fix bug with placeholder ID
* Update cache in response to events. Refactor TimelineCases
* Fix crash, reduce number of placeholders
* Fix crash, fix filtering, improve placeholder handling
* Fix migration, add 8-9 migration test
* Fix initial timeline update, remove more placeholders
* Add cleanup for old statuses
* Fix cleanup
* Delete ExampleInstrumentedTest
* Improve timeline UX regarding caching
* Fix typos
* Fix initial timeline update
* Cleanup/fix initial timeline update
* Workaround for weird behavior of first post on initial tl update.
* Change counter types back to int
* Clear timeline cache on logout
* Fix loading when timeline is completely empty
* Fix androidx migration issues
* Fix tests
* Apply caching feedback
* Save account emojis to cache
* Fix warnings and bugs
2019-01-14 22:05:08 +01:00
|
|
|
return call.doAfterSuccess {
|
|
|
|
eventHub.dispatch(FavoriteEvent(status.id, favourite))
|
|
|
|
}
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun mute(id: String) {
|
|
|
|
val call = mastodonApi.muteAccount(id)
|
|
|
|
call.enqueue(object : Callback<Relationship> {
|
|
|
|
override fun onResponse(call: Call<Relationship>, response: Response<Relationship>) {}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<Relationship>, t: Throwable) {}
|
|
|
|
})
|
2018-05-27 10:22:12 +02:00
|
|
|
eventHub.dispatch(MuteEvent(id))
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun block(id: String) {
|
|
|
|
val call = mastodonApi.blockAccount(id)
|
|
|
|
call.enqueue(object : Callback<Relationship> {
|
|
|
|
override fun onResponse(call: Call<Relationship>, response: retrofit2.Response<Relationship>) {}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<Relationship>, t: Throwable) {}
|
|
|
|
})
|
2018-05-27 10:22:12 +02:00
|
|
|
eventHub.dispatch(BlockEvent(id))
|
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun delete(id: String) {
|
|
|
|
val call = mastodonApi.deleteStatus(id)
|
|
|
|
call.enqueue(object : Callback<ResponseBody> {
|
|
|
|
override fun onResponse(call: Call<ResponseBody>, response: retrofit2.Response<ResponseBody>) {}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {}
|
|
|
|
})
|
2018-05-27 10:22:12 +02:00
|
|
|
eventHub.dispatch(StatusDeletedEvent(id))
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 21:27:52 +02:00
|
|
|
override fun pin(status: Status, pin: Boolean) {
|
|
|
|
// Replace with extension method if we use RxKotlin
|
|
|
|
(if (pin) mastodonApi.pinStatus(status.id) else mastodonApi.unpinStatus(status.id))
|
|
|
|
.subscribe({ updatedStatus ->
|
|
|
|
status.pinned = updatedStatus.pinned
|
|
|
|
}, {})
|
|
|
|
.addTo(this.cancelDisposable)
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:47:00 +02:00
|
|
|
}
|