Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/loader/users/UserListRelatedUsersLoader.kt

85 lines
3.7 KiB
Kotlin
Raw Normal View History

2016-08-13 16:04:31 +02:00
/*
2017-04-21 11:23:55 +02:00
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
2016-08-13 16:04:31 +02:00
* 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.
2017-04-21 11:23:55 +02:00
*
2016-08-13 16:04:31 +02:00
* This program 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.
2017-04-21 11:23:55 +02:00
*
2016-08-13 16:04:31 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-04-21 11:23:55 +02:00
package org.mariotaku.twidere.loader.users
2016-08-13 16:04:31 +02:00
import android.content.Context
import org.mariotaku.microblog.library.MicroBlog
import org.mariotaku.microblog.library.MicroBlogException
2017-04-21 11:23:55 +02:00
import org.mariotaku.microblog.library.twitter.model.PageableResponseList
2016-08-13 16:04:31 +02:00
import org.mariotaku.microblog.library.twitter.model.Paging
import org.mariotaku.microblog.library.twitter.model.User
2017-04-20 19:23:11 +02:00
import org.mariotaku.twidere.annotation.AccountType
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.exception.APINotSupportedException
import org.mariotaku.twidere.extension.model.api.microblog.mapToPaginated
2017-04-20 19:23:11 +02:00
import org.mariotaku.twidere.extension.model.api.toParcelable
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
2016-12-03 17:09:27 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-08-13 16:04:31 +02:00
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.UserKey
2017-04-21 11:23:55 +02:00
import org.mariotaku.twidere.model.pagination.PaginatedList
2016-08-13 16:04:31 +02:00
2017-04-21 11:23:55 +02:00
abstract class UserListRelatedUsersLoader(
2016-08-13 16:04:31 +02:00
context: Context,
2017-04-12 14:58:08 +02:00
accountKey: UserKey?,
2016-12-03 17:09:27 +01:00
private val listId: String?,
private val userKey: UserKey?,
private val screenName: String?,
2016-12-16 16:48:16 +01:00
private val listName: String?,
2016-12-04 04:58:03 +01:00
data: List<ParcelableUser>?,
2016-08-13 16:04:31 +02:00
fromUser: Boolean
2017-04-21 11:23:55 +02:00
) : AbsRequestUsersLoader(context, accountKey, data, fromUser) {
2016-08-13 16:04:31 +02:00
@Throws(MicroBlogException::class)
2019-10-24 17:52:11 +02:00
final override fun getUsers(details: AccountDetails, paging: Paging): PaginatedList<ParcelableUser> {
2017-04-20 19:23:11 +02:00
when (details.type) {
2017-04-21 11:23:55 +02:00
AccountType.TWITTER -> return getTwitterUsers(details, paging).mapToPaginated {
it.toParcelable(details, profileImageSize = profileImageSize)
2017-04-20 19:23:11 +02:00
}
2017-04-21 11:23:55 +02:00
else -> {
throw APINotSupportedException(details.type)
}
2017-04-20 19:23:11 +02:00
}
}
2017-04-21 11:23:55 +02:00
protected abstract fun getByListId(microBlog: MicroBlog, listId: String, paging: Paging): PageableResponseList<User>
protected abstract fun getByUserKey(microBlog: MicroBlog, listName: String, userKey: UserKey, paging: Paging): PageableResponseList<User>
protected abstract fun getByScreenName(microBlog: MicroBlog, listName: String, screenName: String, paging: Paging): PageableResponseList<User>
2017-04-20 19:23:11 +02:00
@Throws(MicroBlogException::class)
2017-04-21 11:23:55 +02:00
private fun getTwitterUsers(details: AccountDetails, paging: Paging): PageableResponseList<User> {
2017-04-20 19:23:11 +02:00
val microBlog = details.newMicroBlogInstance(context, MicroBlog::class.java)
2017-04-21 11:23:55 +02:00
when {
listId != null -> {
return getByListId(microBlog, listId, paging)
}
listName != null && userKey != null -> {
return getByUserKey(microBlog, listName.replace(' ', '-'), userKey, paging)
}
listName != null && screenName != null -> {
return getByScreenName(microBlog, listName.replace(' ', '-'), screenName, paging)
2016-12-16 16:48:16 +01:00
}
}
2016-08-13 16:04:31 +02:00
throw MicroBlogException("list_id or list_name and user_id (or screen_name) required")
}
}