Fix crash in media tab, fix #1005 (#1007)

This commit is contained in:
Ivan Kupalov 2019-01-28 11:05:13 +01:00 committed by Konrad Pozniak
parent a3ee13d767
commit ce80b0c663
2 changed files with 12 additions and 3 deletions

View File

@ -17,6 +17,7 @@ package com.keylesspalace.tusky.di
import android.app.Activity import android.app.Activity
import android.app.Application import android.app.Application
import android.content.Context
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
@ -68,8 +69,7 @@ object AppInjector {
if (activity is FragmentActivity) { if (activity is FragmentActivity) {
activity.supportFragmentManager.registerFragmentLifecycleCallbacks( activity.supportFragmentManager.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() { object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentCreated(fm: FragmentManager, f: Fragment, override fun onFragmentPreAttached(fm: FragmentManager, f: Fragment, context: Context) {
savedInstanceState: Bundle?) {
if (f is Injectable) { if (f is Injectable) {
AndroidSupportInjection.inject(f) AndroidSupportInjection.inject(f)
} }

View File

@ -15,6 +15,7 @@
package com.keylesspalace.tusky.fragment package com.keylesspalace.tusky.fragment
import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -74,6 +75,7 @@ class AccountMediaFragment : BaseFragment(), Injectable {
private var currentCall: Call<List<Status>>? = null private var currentCall: Call<List<Status>>? = null
private val statuses = mutableListOf<Status>() private val statuses = mutableListOf<Status>()
private var fetchingStatus = FetchingStatus.NOT_FETCHING private var fetchingStatus = FetchingStatus.NOT_FETCHING
private var isVisibleToUser: Boolean = false
private val callback = object : Callback<List<Status>> { private val callback = object : Callback<List<Status>> {
override fun onFailure(call: Call<List<Status>>?, t: Throwable?) { override fun onFailure(call: Call<List<Status>>?, t: Throwable?) {
@ -187,12 +189,19 @@ class AccountMediaFragment : BaseFragment(), Injectable {
} }
} }
}) })
if (isVisibleToUser) doInitialLoadingIfNeeded()
} }
// That's sort of an optimization to only load media once user has opened the tab // That's sort of an optimization to only load media once user has opened the tab
// Attention: can be called before *any* lifecycle method!
override fun setUserVisibleHint(isVisibleToUser: Boolean) { override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser) super.setUserVisibleHint(isVisibleToUser)
if (!isVisibleToUser) return this.isVisibleToUser = isVisibleToUser
if (isVisibleToUser && isAdded) doInitialLoadingIfNeeded()
}
private fun doInitialLoadingIfNeeded() {
val accountId = arguments?.getString(ACCOUNT_ID_ARG) val accountId = arguments?.getString(ACCOUNT_ID_ARG)
if (fetchingStatus == FetchingStatus.NOT_FETCHING && statuses.isEmpty()) { if (fetchingStatus == FetchingStatus.NOT_FETCHING && statuses.isEmpty()) {
fetchingStatus = FetchingStatus.INITIAL_FETCHING fetchingStatus = FetchingStatus.INITIAL_FETCHING