From aa3279c5b2bcc5afb05109e7f4b9be91e6dac9fd Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Thu, 21 Jul 2016 14:46:24 +0800 Subject: [PATCH] improved drafts count --- .../fragment/AccountsDashboardFragment.kt | 184 ++++++++++-------- 1 file changed, 105 insertions(+), 79 deletions(-) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt index bd851dcd5..1a606226d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt @@ -25,31 +25,31 @@ import android.animation.AnimatorSet import android.animation.ObjectAnimator import android.content.* import android.content.SharedPreferences.OnSharedPreferenceChangeListener +import android.database.ContentObserver import android.graphics.Matrix import android.graphics.Rect import android.graphics.RectF import android.graphics.drawable.Drawable +import android.net.Uri import android.os.Bundle +import android.os.Handler +import android.os.Looper import android.support.design.widget.NavigationView import android.support.v4.app.LoaderManager.LoaderCallbacks import android.support.v4.content.AsyncTaskLoader import android.support.v4.content.Loader import android.support.v4.view.MenuItemCompat import android.support.v7.view.SupportMenuInflater -import android.support.v7.widget.ActionMenuView import android.support.v7.widget.ActionMenuView.OnMenuItemClickListener import android.support.v7.widget.FixedLinearLayoutManager import android.support.v7.widget.LinearLayoutManager -import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView.Adapter import android.support.v7.widget.RecyclerView.ViewHolder import android.view.* import android.view.View.OnClickListener import android.view.animation.DecelerateInterpolator import android.widget.ImageView -import android.widget.TextView -import android.widget.ViewSwitcher -import kotlinx.android.synthetic.main.fragment_accounts_dashboard.* +import kotlinx.android.synthetic.main.header_drawer_account_selector.view.* import org.apache.commons.lang3.ArrayUtils import org.mariotaku.ktextension.setItemAvailability import org.mariotaku.ktextension.setMenuItemIcon @@ -81,32 +81,22 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks 0) { - mNoAccountContainer!!.visibility = View.GONE - mAccountProfileContainer!!.visibility = View.VISIBLE + noAccountContainer.visibility = View.GONE + profileContainer.visibility = View.VISIBLE } else { - mNoAccountContainer!!.visibility = View.VISIBLE - mAccountProfileContainer!!.visibility = View.INVISIBLE + noAccountContainer.visibility = View.VISIBLE + profileContainer.visibility = View.INVISIBLE } var defaultId: UserKey? = null for (account in accounts) { @@ -281,26 +271,22 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks + menuInflater.inflate(R.menu.action_dashboard_timeline_toggle, accountDashboardMenu.menu) + accountDashboardMenu.setOnMenuItemClickListener(OnMenuItemClickListener { item -> if (item.groupId != AccountToggleProvider.MENU_GROUP) { when (item.itemId) { R.id.compose -> { @@ -326,13 +312,13 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks 0) bannerWidth else defWidth - val bannerView = mAccountProfileBannerView!!.nextView as ImageView + val bannerView = accountProfileBanner.nextView as ImageView if (bannerView.drawable == null || !CompareUtils.objectEquals(account, bannerView.tag)) { mediaLoader.displayProfileBanner(bannerView, account, width) bannerView.tag = account @@ -562,12 +530,12 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks(context) { + + private var contentObserver: ContentObserver? = null + private var firstLoad: Boolean + + init { + firstLoad = true + } + override fun loadInBackground(): AccountsInfo { val accounts = ParcelableAccountUtils.getAccounts(context) val draftsCount = DataStoreUtils.queryCount(context, Drafts.CONTENT_URI, null, null) return AccountsInfo(accounts, draftsCount) } + /** + * Handles a request to completely reset the Loader. + */ + override fun onReset() { + super.onReset() + + // Ensure the loader is stopped + onStopLoading() + + // Stop monitoring for changes. + if (contentObserver != null) { + val cr = context.contentResolver + cr.unregisterContentObserver(contentObserver) + contentObserver = null + } + } + + /** + * Handles a request to start the Loader. + */ override fun onStartLoading() { - forceLoad() + + // Start watching for changes in the app data. + if (contentObserver == null) { + contentObserver = object : ContentObserver(Handler(Looper.getMainLooper())) { + override fun onChange(selfChange: Boolean) { + onContentChanged() + } + + override fun onChange(selfChange: Boolean, uri: Uri?) { + onContentChanged() + } + } + val cr = context.contentResolver + cr.registerContentObserver(Accounts.CONTENT_URI, true, contentObserver) + cr.registerContentObserver(Drafts.CONTENT_URI, true, contentObserver) + } + + if (takeContentChanged() || firstLoad) { + firstLoad = false + // If the data has changed since the last time it was loaded + // or is not currently available, start a load. + forceLoad() + } + } + + /** + * Handles a request to stop the Loader. + */ + override fun onStopLoading() { + // Attempt to cancel the current load task if possible. + cancelLoad() } } } \ No newline at end of file