refactored base fragments and activities
fixed some memory leaks
This commit is contained in:
parent
dd815fef84
commit
92a8609491
|
@ -37,7 +37,7 @@ import org.mariotaku.twidere.constant.IntentConstants.EXTRA_LONGITUDE
|
|||
import org.mariotaku.twidere.fragment.iface.IBaseFragment
|
||||
import org.mariotaku.twidere.fragment.iface.IMapFragment
|
||||
|
||||
class GoogleMapFragment : SupportMapFragment(), Constants, IMapFragment, IBaseFragment {
|
||||
class GoogleMapFragment : SupportMapFragment(), Constants, IMapFragment, IBaseFragment<GoogleMapFragment> {
|
||||
|
||||
private val actionHelper = IBaseFragment.ActionHelper(this)
|
||||
|
||||
|
@ -108,7 +108,7 @@ class GoogleMapFragment : SupportMapFragment(), Constants, IMapFragment, IBaseFr
|
|||
}
|
||||
}
|
||||
|
||||
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (IBaseFragment) -> Unit) {
|
||||
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (GoogleMapFragment) -> Unit) {
|
||||
actionHelper.executeAfterFragmentResumed(useHandler, action)
|
||||
}
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ import java.util.*
|
|||
import javax.inject.Inject
|
||||
|
||||
@SuppressLint("Registered")
|
||||
open class BaseActivity : ChameleonActivity(), IExtendedActivity, IThemedActivity,
|
||||
open class BaseActivity : ChameleonActivity(), IExtendedActivity<BaseActivity>, IThemedActivity,
|
||||
IControlBarActivity, OnFitSystemWindowsListener, SystemWindowsInsetsCallback,
|
||||
KeyboardShortcutCallback, OnPreferenceDisplayDialogCallback {
|
||||
|
||||
|
@ -273,7 +273,7 @@ open class BaseActivity : ChameleonActivity(), IExtendedActivity, IThemedActivit
|
|||
actionHelper.dispatchOnResumeFragments()
|
||||
}
|
||||
|
||||
override fun executeAfterFragmentResumed(action: (IExtendedActivity) -> Unit) {
|
||||
override fun executeAfterFragmentResumed(action: (BaseActivity) -> Unit) {
|
||||
actionHelper.executeAfterFragmentResumed(action)
|
||||
}
|
||||
|
||||
|
|
|
@ -765,8 +765,11 @@ class HomeActivity : BaseActivity(), OnClickListener, OnPageChangeListener, Supp
|
|||
}
|
||||
|
||||
private fun showAutoRefreshConfirm() {
|
||||
val df = AutoRefreshConfirmDialogFragment()
|
||||
df.show(supportFragmentManager, "auto_refresh_confirm")
|
||||
if (isFinishing) return
|
||||
executeAfterFragmentResumed { activity ->
|
||||
val df = AutoRefreshConfirmDialogFragment()
|
||||
df.show(activity.supportFragmentManager, "auto_refresh_confirm")
|
||||
}
|
||||
}
|
||||
|
||||
private fun setTabPosition(initialTab: Int) {
|
||||
|
|
|
@ -85,7 +85,7 @@ class LinkHandlerActivity : BaseActivity(), SystemWindowsInsetsCallback, IContro
|
|||
override fun onFitSystemWindows(insets: Rect) {
|
||||
super.onFitSystemWindows(insets)
|
||||
val fragment = currentVisibleFragment
|
||||
if (fragment is IBaseFragment) {
|
||||
if (fragment is IBaseFragment<*>) {
|
||||
fragment.requestFitSystemWindows()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ import android.os.Bundle
|
|||
import android.os.Parcelable
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v4.app.Fragment
|
||||
import android.support.v4.app.FragmentActivity
|
||||
import android.support.v4.app.hasRunningLoadersSafe
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.support.v4.view.ViewPager
|
||||
|
@ -57,7 +56,7 @@ import java.io.File
|
|||
import javax.inject.Inject
|
||||
import android.Manifest.permission as AndroidPermissions
|
||||
|
||||
class MediaViewerActivity : BaseActivity(), IExtendedActivity, IMediaViewerActivity {
|
||||
class MediaViewerActivity : BaseActivity(), IMediaViewerActivity {
|
||||
|
||||
@Inject
|
||||
lateinit var mFileCache: FileCache
|
||||
|
@ -342,20 +341,20 @@ class MediaViewerActivity : BaseActivity(), IExtendedActivity, IMediaViewerActiv
|
|||
private val PROGRESS_FRAGMENT_TAG = "progress"
|
||||
|
||||
override fun dismissProgress() {
|
||||
val activity = context as IExtendedActivity
|
||||
val activity = context as IExtendedActivity<*>
|
||||
activity.executeAfterFragmentResumed { activity ->
|
||||
val fm = (activity as FragmentActivity).supportFragmentManager
|
||||
val fm = activity.supportFragmentManager
|
||||
val fragment = fm.findFragmentByTag(PROGRESS_FRAGMENT_TAG) as? DialogFragment
|
||||
fragment?.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
override fun showProgress() {
|
||||
val activity = context as IExtendedActivity
|
||||
val activity = context as IExtendedActivity<*>
|
||||
activity.executeAfterFragmentResumed { activity ->
|
||||
val fragment = ProgressDialogFragment()
|
||||
fragment.isCancelable = false
|
||||
fragment.show((activity as FragmentActivity).supportFragmentManager, PROGRESS_FRAGMENT_TAG)
|
||||
fragment.show(activity.supportFragmentManager, PROGRESS_FRAGMENT_TAG)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,19 +19,20 @@
|
|||
|
||||
package org.mariotaku.twidere.activity.iface
|
||||
|
||||
import android.support.v4.app.FragmentActivity
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by mariotaku on 15/12/28.
|
||||
*/
|
||||
interface IExtendedActivity {
|
||||
interface IExtendedActivity<out A : FragmentActivity> {
|
||||
|
||||
fun executeAfterFragmentResumed(action: (IExtendedActivity) -> Unit)
|
||||
fun executeAfterFragmentResumed(action: (A) -> Unit)
|
||||
|
||||
class ActionHelper(private val activity: IExtendedActivity) {
|
||||
class ActionHelper<out A: FragmentActivity>(private val activity: A) {
|
||||
|
||||
private var fragmentResumed: Boolean = false
|
||||
private val actionQueue = LinkedList<(IExtendedActivity) -> Unit>()
|
||||
private val actionQueue = LinkedList<(A) -> Unit>()
|
||||
|
||||
fun dispatchOnPause() {
|
||||
fragmentResumed = false
|
||||
|
@ -45,14 +46,14 @@ interface IExtendedActivity {
|
|||
|
||||
private fun executePending() {
|
||||
if (!fragmentResumed) return
|
||||
var action: ((IExtendedActivity) -> Unit)?
|
||||
var action: ((A) -> Unit)?
|
||||
do {
|
||||
action = actionQueue.poll()
|
||||
action?.invoke(activity)
|
||||
} while (action != null)
|
||||
}
|
||||
|
||||
fun executeAfterFragmentResumed(action: (IExtendedActivity) -> Unit) {
|
||||
fun executeAfterFragmentResumed(action: (A) -> Unit) {
|
||||
actionQueue.add(action)
|
||||
executePending()
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ abstract class AbsToolbarTabPagesFragment : BaseSupportFragment(), RefreshScroll
|
|||
for (i in 0 until count) {
|
||||
if (i > currentItem - pageLimit - 1 || i < currentItem + pageLimit) {
|
||||
val obj = pagerAdapter!!.instantiateItem(viewPager, i)
|
||||
if (obj is IBaseFragment) {
|
||||
if (obj is IBaseFragment<*>) {
|
||||
obj.requestFitSystemWindows()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
|
|||
import org.mariotaku.twidere.util.sync.SyncController
|
||||
import javax.inject.Inject
|
||||
|
||||
abstract class BasePreferenceFragment : PreferenceFragmentCompat(), IBaseFragment {
|
||||
abstract class BasePreferenceFragment : PreferenceFragmentCompat(), IBaseFragment<BasePreferenceFragment> {
|
||||
private var ringtonePreferenceKey: String? = null
|
||||
|
||||
@Inject
|
||||
|
@ -122,7 +122,7 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat(), IBaseFragmen
|
|||
return super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
|
||||
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (IBaseFragment) -> Unit) {
|
||||
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BasePreferenceFragment) -> Unit) {
|
||||
actionHelper.executeAfterFragmentResumed(useHandler, action)
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.mariotaku.twidere.util.*
|
|||
import org.mariotaku.twidere.util.dagger.GeneralComponentHelper
|
||||
import javax.inject.Inject
|
||||
|
||||
open class BaseSupportFragment : Fragment(), IBaseFragment {
|
||||
open class BaseSupportFragment : Fragment(), IBaseFragment<BaseSupportFragment> {
|
||||
|
||||
// Utility classes
|
||||
@Inject
|
||||
|
@ -69,7 +69,7 @@ open class BaseSupportFragment : Fragment(), IBaseFragment {
|
|||
GeneralComponentHelper.build(context!!).inject(this)
|
||||
}
|
||||
|
||||
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (IBaseFragment) -> Unit) {
|
||||
override fun executeAfterFragmentResumed(useHandler: Boolean, action: (BaseSupportFragment) -> Unit) {
|
||||
actionHelper.executeAfterFragmentResumed(useHandler, action)
|
||||
}
|
||||
|
||||
|
|
|
@ -300,8 +300,7 @@ class DraftsFragment : BaseSupportFragment(), LoaderCallbacks<Cursor?>, OnItemCl
|
|||
|
||||
override fun onPreExecute() {
|
||||
super.onPreExecute()
|
||||
(activity as IExtendedActivity).executeAfterFragmentResumed {
|
||||
val activity = it as FragmentActivity
|
||||
(activity as IExtendedActivity<*>).executeAfterFragmentResumed { activity ->
|
||||
val f = ProgressDialogFragment.show(activity.supportFragmentManager, FRAGMENT_TAG_DELETING_DRAFTS)
|
||||
f.isCancelable = false
|
||||
}
|
||||
|
@ -309,8 +308,7 @@ class DraftsFragment : BaseSupportFragment(), LoaderCallbacks<Cursor?>, OnItemCl
|
|||
|
||||
override fun onPostExecute(result: Unit) {
|
||||
super.onPostExecute(result)
|
||||
(activity as IExtendedActivity).executeAfterFragmentResumed {
|
||||
val activity = it as FragmentActivity
|
||||
(activity as IExtendedActivity<*>).executeAfterFragmentResumed { activity ->
|
||||
val fm = activity.supportFragmentManager
|
||||
val f = fm.findFragmentByTag(FRAGMENT_TAG_DELETING_DRAFTS)
|
||||
if (f is DialogFragment) {
|
||||
|
|
|
@ -130,6 +130,7 @@ import org.mariotaku.twidere.util.support.WindowSupport
|
|||
import org.mariotaku.twidere.view.HeaderDrawerLayout.DrawerCallback
|
||||
import org.mariotaku.twidere.view.TabPagerIndicator
|
||||
import org.mariotaku.twidere.view.iface.IExtendedView.OnSizeChangedListener
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.*
|
||||
|
||||
class UserFragment : BaseSupportFragment(), OnClickListener, OnLinkClickListener,
|
||||
|
@ -931,14 +932,14 @@ class UserFragment : BaseSupportFragment(), OnClickListener, OnLinkClickListener
|
|||
df?.dismiss()
|
||||
}
|
||||
}.successUi { result ->
|
||||
executeAfterFragmentResumed {
|
||||
executeAfterFragmentResumed { fragment ->
|
||||
val df = AddRemoveUserListDialogFragment()
|
||||
df.arguments = Bundle {
|
||||
this[EXTRA_ACCOUNT_KEY] = user.account_key
|
||||
this[EXTRA_USER_KEY] = user.key
|
||||
this[EXTRA_USER_LISTS] = result
|
||||
}
|
||||
df.show(fragmentManager, "add_remove_list")
|
||||
df.show(fragment.fragmentManager, "add_remove_list")
|
||||
}
|
||||
}.failUi {
|
||||
Utils.showErrorMessage(context, R.string.action_getting_user_lists, it, false)
|
||||
|
@ -1594,14 +1595,15 @@ class UserFragment : BaseSupportFragment(), OnClickListener, OnLinkClickListener
|
|||
dialog.setOnShowListener {
|
||||
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
|
||||
val checkedPositions = dialog.listView.checkedItemPositions
|
||||
|
||||
val weakActivity = WeakReference(activity)
|
||||
promiseOnUi {
|
||||
val activity = activity as IExtendedActivity
|
||||
activity.executeAfterFragmentResumed {
|
||||
ProgressDialogFragment.show(fragmentManager, "update_lists_progress")
|
||||
val activity = weakActivity.get() as? IExtendedActivity<*> ?: return@promiseOnUi
|
||||
activity.executeAfterFragmentResumed { activity ->
|
||||
ProgressDialogFragment.show(activity.supportFragmentManager, "update_lists_progress")
|
||||
}
|
||||
}.then {
|
||||
val twitter = MicroBlogAPIFactory.getInstance(context, accountKey)
|
||||
val activity = weakActivity.get() ?: throw IllegalStateException()
|
||||
val twitter = MicroBlogAPIFactory.getInstance(activity, accountKey)
|
||||
val successfulStates = SparseBooleanArray()
|
||||
try {
|
||||
for (i in 0 until checkedPositions.size()) {
|
||||
|
@ -1620,9 +1622,10 @@ class UserFragment : BaseSupportFragment(), OnClickListener, OnLinkClickListener
|
|||
throw UpdateListsException(successfulStates)
|
||||
}
|
||||
}.alwaysUi {
|
||||
val activity = activity as IExtendedActivity
|
||||
activity.executeAfterFragmentResumed {
|
||||
val df = fragmentManager.findFragmentByTag("update_lists_progress") as? DialogFragment
|
||||
val activity = weakActivity.get() as? IExtendedActivity<*> ?: return@alwaysUi
|
||||
activity.executeAfterFragmentResumed { activity ->
|
||||
val manager = activity.supportFragmentManager
|
||||
val df = manager.findFragmentByTag("update_lists_progress") as? DialogFragment
|
||||
df?.dismiss()
|
||||
}
|
||||
}.successUi {
|
||||
|
|
|
@ -27,7 +27,7 @@ import android.support.v4.app.Fragment
|
|||
import org.mariotaku.twidere.constant.IntentConstants
|
||||
import java.util.*
|
||||
|
||||
interface IBaseFragment {
|
||||
interface IBaseFragment<out F : Fragment> {
|
||||
val extraConfiguration: Bundle?
|
||||
get() = null
|
||||
|
||||
|
@ -64,18 +64,14 @@ interface IBaseFragment {
|
|||
fun getSystemWindowsInsets(insets: Rect): Boolean
|
||||
}
|
||||
|
||||
fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (IBaseFragment) -> Unit)
|
||||
fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (F) -> Unit)
|
||||
|
||||
class ActionHelper(private val fragment: IBaseFragment) {
|
||||
class ActionHelper<out F : Fragment>(private val fragment: F) {
|
||||
|
||||
private val handler: Handler
|
||||
|
||||
init {
|
||||
handler = Handler(Looper.getMainLooper())
|
||||
}
|
||||
private val handler: Handler = Handler(Looper.getMainLooper())
|
||||
|
||||
private var fragmentResumed: Boolean = false
|
||||
private val actionQueue = LinkedList<ExecuteInfo>()
|
||||
private val actionQueue = LinkedList<ExecuteInfo<F>>()
|
||||
|
||||
fun dispatchOnPause() {
|
||||
fragmentResumed = false
|
||||
|
@ -86,10 +82,9 @@ interface IBaseFragment {
|
|||
executePending()
|
||||
}
|
||||
|
||||
|
||||
private fun executePending() {
|
||||
if (!fragmentResumed) return
|
||||
var info: ExecuteInfo
|
||||
var info: ExecuteInfo<F>
|
||||
while (true) {
|
||||
info = actionQueue.poll() ?: break
|
||||
if (info.useHandler) {
|
||||
|
@ -100,11 +95,11 @@ interface IBaseFragment {
|
|||
}
|
||||
}
|
||||
|
||||
fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (IBaseFragment) -> Unit) {
|
||||
fun executeAfterFragmentResumed(useHandler: Boolean = false, action: (F) -> Unit) {
|
||||
actionQueue.add(ExecuteInfo(action, useHandler))
|
||||
executePending()
|
||||
}
|
||||
|
||||
private data class ExecuteInfo(val action: (IBaseFragment) -> Unit, val useHandler: Boolean)
|
||||
private data class ExecuteInfo<in F : Fragment>(val action: (F) -> Unit, val useHandler: Boolean)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,6 @@ package org.mariotaku.twidere.task
|
|||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v4.app.FragmentActivity
|
||||
import org.mariotaku.twidere.activity.iface.IExtendedActivity
|
||||
import org.mariotaku.twidere.fragment.ProgressDialogFragment
|
||||
import java.io.File
|
||||
|
@ -38,16 +37,16 @@ abstract class ProgressSaveFileTask(
|
|||
) : SaveFileTask(context, source, destination, getMimeType) {
|
||||
|
||||
override fun showProgress() {
|
||||
(context as IExtendedActivity).executeAfterFragmentResumed { activity ->
|
||||
(context as IExtendedActivity<*>).executeAfterFragmentResumed { activity ->
|
||||
val fragment = ProgressDialogFragment()
|
||||
fragment.isCancelable = false
|
||||
fragment.show((activity as FragmentActivity).supportFragmentManager, PROGRESS_FRAGMENT_TAG)
|
||||
fragment.show(activity.supportFragmentManager, PROGRESS_FRAGMENT_TAG)
|
||||
}
|
||||
}
|
||||
|
||||
override fun dismissProgress() {
|
||||
(context as IExtendedActivity).executeAfterFragmentResumed { activity ->
|
||||
val fm = (activity as FragmentActivity).supportFragmentManager
|
||||
(context as IExtendedActivity<*>).executeAfterFragmentResumed { activity ->
|
||||
val fm = activity.supportFragmentManager
|
||||
val fragment = fm.findFragmentByTag(PROGRESS_FRAGMENT_TAG) as? DialogFragment
|
||||
fragment?.dismiss()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue