Fix crash in AccountListFragment (#1502)

This commit is contained in:
Konrad Pozniak 2019-09-28 12:52:10 +02:00 committed by GitHub
parent f1f34dfd1e
commit 1fd6a7cd72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -57,7 +57,8 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
lateinit var api: MastodonApi lateinit var api: MastodonApi
private lateinit var type: Type private lateinit var type: Type
private lateinit var id: String private var id: String? = null
private lateinit var scrollListener: EndlessOnScrollListener private lateinit var scrollListener: EndlessOnScrollListener
private lateinit var adapter: AccountAdapter private lateinit var adapter: AccountAdapter
private var fetching = false private var fetching = false
@ -66,7 +67,7 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
type = arguments?.getSerializable(ARG_TYPE) as Type type = arguments?.getSerializable(ARG_TYPE) as Type
id = arguments?.getString(ARG_ID)!! id = arguments?.getString(ARG_ID)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
@ -251,36 +252,52 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable {
Log.e(TAG, "Failed to $verb account id $accountId.") Log.e(TAG, "Failed to $verb account id $accountId.")
} }
private fun getFetchCallByListType(type: Type, fromId: String?): Single<Response<List<Account>>> { private fun getFetchCallByListType(fromId: String?): Single<Response<List<Account>>> {
return when (type) { return when (type) {
Type.FOLLOWS -> api.accountFollowing(id, fromId) Type.FOLLOWS -> {
Type.FOLLOWERS -> api.accountFollowers(id, fromId) val accountId = requireId(type, id)
api.accountFollowing(accountId, fromId)
}
Type.FOLLOWERS -> {
val accountId = requireId(type, id)
api.accountFollowers(accountId, fromId)
}
Type.BLOCKS -> api.blocks(fromId) Type.BLOCKS -> api.blocks(fromId)
Type.MUTES -> api.mutes(fromId) Type.MUTES -> api.mutes(fromId)
Type.FOLLOW_REQUESTS -> api.followRequests(fromId) Type.FOLLOW_REQUESTS -> api.followRequests(fromId)
Type.REBLOGGED -> api.statusRebloggedBy(id, fromId) Type.REBLOGGED -> {
Type.FAVOURITED -> api.statusFavouritedBy(id, fromId) val statusId = requireId(type, id)
api.statusRebloggedBy(statusId, fromId)
}
Type.FAVOURITED -> {
val statusId = requireId(type, id)
api.statusFavouritedBy(statusId, fromId)
}
} }
} }
private fun fetchAccounts(id: String? = null) { private fun requireId(type: Type, id: String?): String {
return requireNotNull(id) { "id must not be null for type "+type.name }
}
private fun fetchAccounts(fromId: String? = null) {
if (fetching) { if (fetching) {
return return
} }
fetching = true fetching = true
if (id != null) { if (fromId != null) {
recyclerView.post { adapter.setBottomLoading(true) } recyclerView.post { adapter.setBottomLoading(true) }
} }
getFetchCallByListType(type, id) getFetchCallByListType(fromId)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.autoDispose(from(this, Lifecycle.Event.ON_DESTROY)) .autoDispose(from(this, Lifecycle.Event.ON_DESTROY))
.subscribe({ response -> .subscribe({ response ->
val accountList = response.body() val accountList = response.body()
if (response.isSuccessful && accountList != null) { if (response.isSuccessful && accountList != null) {
val linkHeader = response.headers().get("Link") val linkHeader = response.headers()["Link"]
onFetchAccountsSuccess(accountList, linkHeader) onFetchAccountsSuccess(accountList, linkHeader)
} else { } else {
onFetchAccountsFailure(Exception(response.message())) onFetchAccountsFailure(Exception(response.message()))