2019-06-05 23:06:37 +02:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.SharedPreferences
|
2020-01-28 02:12:08 +01:00
|
|
|
import android.os.SystemClock
|
2021-05-17 16:13:04 +02:00
|
|
|
import jp.juggler.subwaytooter.api.ApiPath
|
2020-01-28 02:12:08 +01:00
|
|
|
import jp.juggler.subwaytooter.api.TootApiClient
|
2019-06-05 23:06:37 +02:00
|
|
|
import jp.juggler.subwaytooter.api.TootApiResult
|
|
|
|
import jp.juggler.subwaytooter.api.TootParser
|
2021-05-22 00:03:16 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.TimelineItem
|
|
|
|
import jp.juggler.subwaytooter.api.entity.TootAnnouncement
|
|
|
|
import jp.juggler.subwaytooter.api.entity.TootInstance
|
|
|
|
import jp.juggler.subwaytooter.api.entity.parseList
|
2019-06-05 23:06:37 +02:00
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
2021-05-22 00:03:16 +02:00
|
|
|
import jp.juggler.util.*
|
2020-04-07 06:52:54 +02:00
|
|
|
import kotlinx.coroutines.*
|
2019-06-05 23:06:37 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean
|
|
|
|
|
2020-09-18 23:40:15 +02:00
|
|
|
enum class ColumnTaskType(val marker:Char) {
|
|
|
|
LOADING('L'),
|
|
|
|
REFRESH_TOP('T'),
|
|
|
|
REFRESH_BOTTOM('B'),
|
|
|
|
GAP('G')
|
2019-06-05 23:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ColumnTask(
|
|
|
|
val column : Column,
|
|
|
|
val ctType : ColumnTaskType
|
2020-09-12 19:47:00 +02:00
|
|
|
) {
|
2019-06-06 01:31:33 +02:00
|
|
|
|
2019-06-05 23:06:37 +02:00
|
|
|
val ctStarted = AtomicBoolean(false)
|
|
|
|
val ctClosed = AtomicBoolean(false)
|
|
|
|
|
2020-09-09 21:46:50 +02:00
|
|
|
private var job : Job? = null
|
2020-04-07 06:52:54 +02:00
|
|
|
|
2020-09-12 19:47:00 +02:00
|
|
|
val isCancelled : Boolean
|
|
|
|
get() = job?.isCancelled ?: false
|
2020-04-07 06:52:54 +02:00
|
|
|
|
2019-06-05 23:06:37 +02:00
|
|
|
var parser = TootParser(context, access_info, highlightTrie = highlight_trie)
|
|
|
|
|
|
|
|
var list_tmp : ArrayList<TimelineItem>? = null
|
|
|
|
|
|
|
|
val context : Context
|
|
|
|
get() = column.context
|
|
|
|
|
|
|
|
val access_info : SavedAccount
|
|
|
|
get() = column.access_info
|
|
|
|
|
|
|
|
val highlight_trie : WordTrieTree?
|
|
|
|
get() = column.highlight_trie
|
|
|
|
|
2020-01-28 02:12:08 +01:00
|
|
|
val isPseudo : Boolean
|
2020-01-27 06:45:16 +01:00
|
|
|
get() = access_info.isPseudo
|
|
|
|
|
2020-09-09 21:46:50 +02:00
|
|
|
private val isMastodon : Boolean
|
2020-01-27 06:45:16 +01:00
|
|
|
get() = access_info.isMastodon
|
|
|
|
|
2019-06-05 23:06:37 +02:00
|
|
|
val isMisskey : Boolean
|
|
|
|
get() = access_info.isMisskey
|
|
|
|
|
|
|
|
val misskeyVersion : Int
|
|
|
|
get() = access_info.misskeyVersion
|
|
|
|
|
|
|
|
val pref : SharedPreferences
|
|
|
|
get() = column.app_state.pref
|
|
|
|
|
2020-01-07 09:03:32 +01:00
|
|
|
internal fun JsonObject.addMisskeyNotificationFilter() = addMisskeyNotificationFilter(column)
|
|
|
|
internal fun JsonObject.addRangeMisskey(bBottom : Boolean) = addRangeMisskey(column, bBottom)
|
2019-06-05 23:06:37 +02:00
|
|
|
|
2020-01-28 02:12:08 +01:00
|
|
|
internal val profileDirectoryPath : String
|
|
|
|
get() {
|
2019-08-29 18:16:21 +02:00
|
|
|
val order = when(val q = column.search_query) {
|
|
|
|
"new" -> q
|
|
|
|
else -> "active"
|
|
|
|
}
|
|
|
|
val local = ! column.search_resolve
|
2021-05-17 16:13:04 +02:00
|
|
|
return "${ApiPath.PATH_PROFILE_DIRECTORY}&order=$order&local=$local"
|
2019-08-29 18:16:21 +02:00
|
|
|
}
|
2020-01-28 02:12:08 +01:00
|
|
|
|
2020-12-08 21:24:42 +01:00
|
|
|
internal suspend fun getAnnouncements(
|
2020-01-28 02:12:08 +01:00
|
|
|
client : TootApiClient,
|
|
|
|
force : Boolean = false
|
|
|
|
) : TootApiResult? {
|
|
|
|
// announcements is shown only mastodon home timeline, not pseudo.
|
|
|
|
if(isMastodon && ! isPseudo) {
|
|
|
|
// force (initial loading) always reload announcements
|
|
|
|
// other (refresh,gap) may reload announcements if last load is too old
|
|
|
|
if(force || SystemClock.elapsedRealtime() - column.announcementUpdated >= 15 * 60000L) {
|
|
|
|
if(force) {
|
|
|
|
column.announcements = null
|
|
|
|
column.announcementUpdated = SystemClock.elapsedRealtime()
|
|
|
|
client.publishApiProgress("announcements reset")
|
|
|
|
}
|
|
|
|
val (instance, _) = TootInstance.get(client)
|
|
|
|
if(instance?.versionGE(TootInstance.VERSION_3_1_0_rc1) == true) {
|
|
|
|
val result = client.request("/api/v1/announcements")
|
|
|
|
?: return null // cancelled.
|
2020-09-09 21:46:50 +02:00
|
|
|
when(result.response?.code ?: 0) {
|
2020-01-28 02:12:08 +01:00
|
|
|
// just skip load announcements for 4xx error if server does not support announcements.
|
|
|
|
in 400 until 500 -> {
|
|
|
|
}
|
2020-09-12 19:47:00 +02:00
|
|
|
|
2020-01-28 02:12:08 +01:00
|
|
|
else -> {
|
|
|
|
column.announcements =
|
|
|
|
parseList(::TootAnnouncement, parser, result.jsonArray)
|
|
|
|
.notEmpty()
|
|
|
|
column.announcementUpdated = SystemClock.elapsedRealtime()
|
|
|
|
client.publishApiProgress("announcements loaded")
|
|
|
|
// other errors such as network or server fails will stop column loading.
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TootApiResult()
|
|
|
|
}
|
2020-04-07 06:52:54 +02:00
|
|
|
|
2020-12-11 22:46:49 +01:00
|
|
|
|
|
|
|
abstract suspend fun background() : TootApiResult?
|
|
|
|
abstract suspend fun handleResult(result : TootApiResult?)
|
|
|
|
|
2020-04-07 06:52:54 +02:00
|
|
|
fun cancel() {
|
|
|
|
job?.cancel()
|
|
|
|
}
|
2020-12-11 22:46:49 +01:00
|
|
|
|
2020-04-07 06:52:54 +02:00
|
|
|
fun start() {
|
2021-05-27 04:15:59 +02:00
|
|
|
job = launchMain {
|
|
|
|
val result = try {
|
|
|
|
withContext(Dispatchers.IO) { background() }
|
|
|
|
} catch(ex : CancellationException) {
|
|
|
|
null // キャンセルされたらresult==nullとする
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
// その他のエラー
|
|
|
|
TootApiResult(ex.withCaption("error"))
|
|
|
|
}
|
|
|
|
handleResult( result )
|
2020-04-07 06:52:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-05 23:06:37 +02:00
|
|
|
}
|