Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AccountsDashboardFragment.kt

772 lines
33 KiB
Kotlin
Raw Normal View History

2016-07-04 03:31:17 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.fragment
2016-12-04 04:58:03 +01:00
import android.accounts.AccountManager
import android.accounts.OnAccountsUpdateListener
2016-07-04 03:31:17 +02:00
import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
2017-08-27 18:37:40 +02:00
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
2016-07-04 03:31:17 +02:00
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
2016-07-21 08:46:24 +02:00
import android.database.ContentObserver
2016-07-04 03:31:17 +02:00
import android.graphics.Matrix
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.ColorDrawable
2016-07-04 03:31:17 +02:00
import android.graphics.drawable.Drawable
2016-07-21 08:46:24 +02:00
import android.net.Uri
2016-07-04 03:31:17 +02:00
import android.os.Bundle
import android.view.*
import android.view.View.OnClickListener
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
2020-05-18 07:04:53 +02:00
import androidx.appcompat.view.SupportMenuInflater
import androidx.appcompat.widget.ActionMenuView.OnMenuItemClickListener
import androidx.core.content.ContextCompat
import androidx.core.view.MenuItemCompat
import androidx.loader.app.LoaderManager.LoaderCallbacks
import androidx.loader.content.FixedAsyncTaskLoader
import androidx.loader.content.Loader
import androidx.viewpager.widget.ViewPager
import com.google.android.material.navigation.NavigationView
2016-07-21 08:46:24 +02:00
import kotlinx.android.synthetic.main.header_drawer_account_selector.view.*
import org.mariotaku.chameleon.Chameleon
2017-01-07 15:45:33 +01:00
import org.mariotaku.kpreferences.get
2017-02-01 07:04:14 +01:00
import org.mariotaku.kpreferences.set
2017-05-13 08:19:23 +02:00
import org.mariotaku.ktextension.*
2017-02-01 07:04:14 +01:00
import org.mariotaku.twidere.Constants.EXTRA_FEATURES_NOTICE_VERSION
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.*
2017-03-25 14:44:07 +01:00
import org.mariotaku.twidere.activity.ComposeActivity
import org.mariotaku.twidere.activity.HomeActivity
import org.mariotaku.twidere.activity.PremiumDashboardActivity
import org.mariotaku.twidere.activity.QuickSearchBarActivity
import org.mariotaku.twidere.adapter.AccountSelectorAdapter
import org.mariotaku.twidere.adapter.RecyclerPagerAdapter
2016-12-03 06:48:40 +01:00
import org.mariotaku.twidere.annotation.AccountType
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.annotation.CustomTabType
2017-03-06 08:12:18 +01:00
import org.mariotaku.twidere.annotation.ProfileImageSize
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.constant.KeyboardShortcutConstants.*
2017-02-01 07:04:14 +01:00
import org.mariotaku.twidere.constant.extraFeaturesNoticeVersionKey
2017-04-17 15:10:14 +02:00
import org.mariotaku.twidere.constant.iWantMyStarsBackKey
2017-04-07 13:05:02 +02:00
import org.mariotaku.twidere.constant.newDocumentApiKey
2017-01-07 15:45:33 +01:00
import org.mariotaku.twidere.constant.profileImageStyleKey
import org.mariotaku.twidere.extension.loadProfileBanner
2017-03-02 02:08:54 +01:00
import org.mariotaku.twidere.extension.loadProfileImage
2016-12-29 06:50:18 +01:00
import org.mariotaku.twidere.extension.model.setActivated
2017-08-27 18:37:40 +02:00
import org.mariotaku.twidere.extension.queryCount
2016-07-21 08:11:00 +02:00
import org.mariotaku.twidere.fragment.AccountsDashboardFragment.AccountsInfo
2017-02-01 07:04:14 +01:00
import org.mariotaku.twidere.graphic.BadgeDrawable
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.menu.AccountToggleProvider
2016-12-04 04:58:03 +01:00
import org.mariotaku.twidere.model.AccountDetails
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.model.SupportTabSpec
import org.mariotaku.twidere.model.UserKey
2016-12-04 04:58:03 +01:00
import org.mariotaku.twidere.model.util.AccountUtils
2016-07-21 08:11:00 +02:00
import org.mariotaku.twidere.provider.TwidereDataStore.Drafts
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback
2017-03-25 14:44:07 +01:00
import org.mariotaku.twidere.view.holder.AccountProfileImageViewHolder
import org.mariotaku.twidere.view.transformer.AccountsSelectorTransformer
import java.lang.ref.WeakReference
2016-07-04 03:31:17 +02:00
2017-01-12 17:26:44 +01:00
class AccountsDashboardFragment : BaseFragment(), LoaderCallbacks<AccountsInfo>,
2017-01-09 06:16:23 +01:00
OnSharedPreferenceChangeListener, OnClickListener, KeyboardShortcutCallback,
2017-03-25 14:44:07 +01:00
NavigationView.OnNavigationItemSelectedListener, AccountSelectorAdapter.Listener {
2016-07-04 03:31:17 +02:00
2016-12-14 09:02:50 +01:00
private val systemWindowsInsets = Rect()
2016-07-04 03:31:17 +02:00
2016-12-14 09:02:50 +01:00
private lateinit var accountsAdapter: AccountSelectorAdapter
2016-07-21 08:46:24 +02:00
private val navigationView by lazy { view as NavigationView }
private val accountsHeader by lazy { navigationView.getHeaderView(0) }
2016-12-05 08:20:57 +01:00
private val hasNextAccountIndicator by lazy { accountsHeader.hasNextAccountIndicator }
private val hasPrevAccountIndicator by lazy { accountsHeader.hasPrevAccountIndicator }
2016-12-05 04:39:22 +01:00
private val accountsSelector by lazy { accountsHeader.otherAccountsList }
2016-07-21 08:46:24 +02:00
private val accountProfileBanner by lazy { accountsHeader.accountProfileBanner }
private val floatingProfileImageSnapshot by lazy { accountsHeader.floatingProfileImageSnapshot }
private val accountProfileImageView by lazy { accountsHeader.profileImage }
private val accountProfileNameView by lazy { accountsHeader.name }
private val accountProfileScreenNameView by lazy { accountsHeader.screenName }
private val accountDashboardMenu by lazy { accountsHeader.accountDashboardMenu }
private val profileContainer by lazy { accountsHeader.profileContainer }
private val noAccountContainer by lazy { accountsHeader.noAccountContainer }
2016-07-04 03:31:17 +02:00
private lateinit var accountActionProvider: AccountToggleProvider
2016-07-21 08:46:24 +02:00
2016-12-05 04:39:22 +01:00
private var switchAccountAnimationPlaying: Boolean = false
private var useStarsForLikes: Boolean = false
private var loaderInitialized: Boolean = false
2016-07-04 03:31:17 +02:00
2017-08-27 18:37:40 +02:00
@SuppressLint("RestrictedApi")
2016-12-05 04:39:22 +01:00
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
accountsAdapter = AccountSelectorAdapter(layoutInflater, preferences, requestManager).also {
2017-03-25 14:44:07 +01:00
it.listener = this
}
2016-12-05 04:39:22 +01:00
accountsSelector.adapter = accountsAdapter
2016-12-05 08:20:57 +01:00
accountsSelector.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
2016-12-14 09:02:50 +01:00
val adapter = accountsAdapter
2016-12-05 08:20:57 +01:00
val pagePosition = position + positionOffset
2016-12-06 02:26:53 +01:00
val pageCount = adapter.count
val visiblePages = 1 / adapter.getPageWidth(position)
if (pageCount < visiblePages) {
hasPrevAccountIndicator.alpha = 0f
hasNextAccountIndicator.alpha = 0f
} else {
2017-02-04 11:42:14 +01:00
hasPrevAccountIndicator.alpha = pagePosition.coerceIn(0f, 1f)
hasNextAccountIndicator.alpha = (pageCount - (pagePosition + visiblePages))
.coerceIn(0f, 1f)
2016-12-06 02:26:53 +01:00
}
2016-12-05 08:20:57 +01:00
}
})
2016-12-05 04:39:22 +01:00
accountsSelector.setPageTransformer(false, AccountsSelectorTransformer)
2016-12-06 02:26:53 +01:00
hasPrevAccountIndicator.alpha = 0f
hasNextAccountIndicator.alpha = 0f
2017-03-02 10:23:34 +01:00
val profileImageStyle = preferences[profileImageStyleKey]
floatingProfileImageSnapshot.style = profileImageStyle
accountProfileImageView.style = profileImageStyle
SupportMenuInflater(context).inflate(R.menu.action_dashboard_timeline_toggle,
accountDashboardMenu.menu)
accountActionProvider = MenuItemCompat.getActionProvider(
accountDashboardMenu.menu.findItem(R.id.select_account)) as AccountToggleProvider
2016-12-05 04:39:22 +01:00
accountDashboardMenu.setOnMenuItemClickListener(OnMenuItemClickListener { item ->
if (item.groupId == AccountToggleProvider.MENU_GROUP) {
val accounts = accountActionProvider.accounts
val account = accounts[item.order]
val newActivated = !account.activated
accountActionProvider.setAccountActivated(account.key, newActivated)
account.account.setActivated(AccountManager.get(context), newActivated)
return@OnMenuItemClickListener true
} else {
2016-12-05 04:39:22 +01:00
when (item.itemId) {
R.id.compose -> {
2016-12-14 09:02:50 +01:00
val account = accountsAdapter.selectedAccount ?: return@OnMenuItemClickListener true
2016-12-05 04:39:22 +01:00
val composeIntent = Intent(INTENT_ACTION_COMPOSE)
2020-01-26 08:35:15 +01:00
activity?.let { composeIntent.setClass(it, ComposeActivity::class.java) }
2016-12-05 04:39:22 +01:00
composeIntent.putExtra(EXTRA_ACCOUNT_KEY, account.key)
startActivity(composeIntent)
return@OnMenuItemClickListener true
}
}
}
return@OnMenuItemClickListener false
2016-12-05 04:39:22 +01:00
})
profileContainer.setOnClickListener(this)
accountProfileBanner.setInAnimation(context, android.R.anim.fade_in)
accountProfileBanner.setOutAnimation(context, android.R.anim.fade_out)
accountProfileBanner.setFactory {
2017-06-25 17:01:33 +02:00
layoutInflater.inflate(R.layout.layout_account_dashboard_profile_banner,
2016-12-05 04:39:22 +01:00
accountProfileBanner, false)
2016-07-04 03:31:17 +02:00
}
2016-12-05 04:39:22 +01:00
navigationView.setNavigationItemSelectedListener(this)
preferences.registerOnSharedPreferenceChangeListener(this)
updateSystemWindowsInsets()
2016-07-04 03:31:17 +02:00
}
2016-12-05 04:39:22 +01:00
override fun onStart() {
super.onStart()
2017-02-05 15:15:03 +01:00
loadAccounts()
2016-07-04 03:31:17 +02:00
}
2016-12-05 04:39:22 +01:00
override fun onResume() {
super.onResume()
updateDefaultAccountState()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
2016-12-05 04:39:22 +01:00
return inflater.inflate(R.layout.fragment_accounts_dashboard, container, false)
2016-07-04 03:31:17 +02:00
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
REQUEST_SETTINGS -> {
if (data == null) return
2016-07-26 09:12:25 +02:00
if (data.getBooleanExtra(EXTRA_SHOULD_RESTART, false)) {
Utils.restartActivity(activity)
} else if (data.getBooleanExtra(EXTRA_SHOULD_RECREATE, false)) {
2020-01-26 08:35:15 +01:00
activity?.recreate()
2016-07-04 03:31:17 +02:00
}
return
}
}
super.onActivityResult(requestCode, resultCode, data)
}
2016-12-05 04:39:22 +01:00
override fun handleKeyboardShortcutSingle(handler: KeyboardShortcutsHandler,
2017-03-01 15:12:25 +01:00
keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
2016-12-05 04:39:22 +01:00
return false
}
override fun isKeyboardShortcutHandled(handler: KeyboardShortcutsHandler, keyCode: Int, event: KeyEvent, metaState: Int): Boolean {
val action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState)
return ACTION_NAVIGATION_PREVIOUS == action || ACTION_NAVIGATION_NEXT == action
}
override fun handleKeyboardShortcutRepeat(handler: KeyboardShortcutsHandler, keyCode: Int,
2017-03-01 15:12:25 +01:00
repeatCount: Int, event: KeyEvent, metaState: Int): Boolean {
2016-12-05 04:39:22 +01:00
return true
2016-07-04 03:31:17 +02:00
}
override fun onClick(v: View) {
when (v.id) {
R.id.profileContainer -> {
2016-12-14 09:02:50 +01:00
val account = accountsAdapter.selectedAccount ?: return
2016-07-04 03:31:17 +02:00
val activity = activity
2016-12-04 04:58:03 +01:00
if (account.user != null) {
2020-01-26 08:35:15 +01:00
activity?.let {
IntentUtils.openUserProfile(it, account.user!!,
preferences[newDocumentApiKey], null)
}
2016-07-04 03:31:17 +02:00
} else {
2020-01-26 08:35:15 +01:00
activity?.let {
IntentUtils.openUserProfile(it, account.key, account.key,
account.user.screen_name, null, preferences[newDocumentApiKey],
null)
}
2016-07-04 03:31:17 +02:00
}
}
}
}
2017-02-05 15:15:03 +01:00
override fun onCreateLoader(id: Int, args: Bundle?): Loader<AccountsInfo> {
return AccountsInfoLoader(requireActivity(), accountsAdapter.accounts == null)
2016-07-04 03:31:17 +02:00
}
2016-12-05 04:39:22 +01:00
2016-07-21 08:11:00 +02:00
override fun onLoadFinished(loader: Loader<AccountsInfo>, data: AccountsInfo) {
2019-10-24 17:52:11 +02:00
if (context == null || isDetached || (activity?.isFinishing != false)) return
2016-07-04 03:31:17 +02:00
updateAccountProviderData(data)
}
override fun onLoaderReset(loader: Loader<AccountsInfo>) {
}
override fun onSharedPreferenceChanged(preferences: SharedPreferences, key: String) {
if (KEY_DEFAULT_ACCOUNT_KEY == key) {
updateDefaultAccountState()
}
}
2017-06-25 17:01:33 +02:00
override fun onApplySystemWindowInsets(insets: Rect) {
2020-01-26 08:35:15 +01:00
view?.findViewById<View?>(com.google.android.material.R.id.design_navigation_view)?.
2017-10-02 16:03:50 +02:00
setPadding(0, 0, 0, insets.bottom)
systemWindowsInsets.set(insets)
updateSystemWindowsInsets()
}
2017-02-05 15:15:03 +01:00
fun loadAccounts() {
if (!loaderInitialized) {
loaderInitialized = true
2017-02-05 15:15:03 +01:00
loaderManager.initLoader(0, null, this)
} else {
2017-02-05 15:15:03 +01:00
loaderManager.restartLoader(0, null, this)
}
}
2016-07-21 08:11:00 +02:00
private fun updateAccountProviderData(data: AccountsInfo) {
val accounts = data.accounts
2016-12-04 04:58:03 +01:00
if (accounts.isNotEmpty()) {
2016-07-21 08:46:24 +02:00
noAccountContainer.visibility = View.GONE
profileContainer.visibility = View.VISIBLE
2016-07-04 03:31:17 +02:00
} else {
2016-07-21 08:46:24 +02:00
noAccountContainer.visibility = View.VISIBLE
profileContainer.visibility = View.INVISIBLE
2016-07-04 03:31:17 +02:00
}
2017-04-17 15:10:14 +02:00
useStarsForLikes = preferences[iWantMyStarsBackKey]
2016-12-14 09:02:50 +01:00
accountsAdapter.accounts = accounts
2017-02-17 11:42:39 +01:00
val defaultKey = preferences.getString(KEY_DEFAULT_ACCOUNT_KEY, null)?.let(UserKey::valueOf)
2016-12-08 09:49:58 +01:00
?: accounts.firstOrNull { it.activated }?.key
val defaultAccount = accounts.firstOrNull { it.key.maybeEquals(defaultKey) }
2016-12-14 09:02:50 +01:00
accountsAdapter.selectedAccount = defaultAccount
2016-07-04 03:31:17 +02:00
accountActionProvider.isExclusive = false
accountActionProvider.accounts = accounts
2016-07-04 03:31:17 +02:00
updateAccountActions()
2016-12-14 09:02:50 +01:00
val currentAccount = accountsAdapter.selectedAccount
2016-07-04 03:31:17 +02:00
if (currentAccount != null) {
displayAccountBanner(currentAccount)
displayCurrentAccount(null)
}
updateDefaultAccountState()
2016-07-21 08:11:00 +02:00
if (data.draftsCount > 0) {
2016-12-26 17:26:09 +01:00
navigationView.menu.findItem(R.id.drafts).title = "${getString(R.string.title_drafts)} (${data.draftsCount})"
2016-07-21 08:11:00 +02:00
} else {
2016-12-26 17:26:09 +01:00
navigationView.menu.findItem(R.id.drafts).title = getString(R.string.title_drafts)
2016-07-21 08:11:00 +02:00
}
2016-07-04 03:31:17 +02:00
}
private fun updateSystemWindowsInsets() {
2017-06-25 17:01:33 +02:00
profileContainer?.setPadding(0, systemWindowsInsets.top, 0, 0)
2016-07-04 03:31:17 +02:00
}
internal fun updateAccountActions() {
2017-04-14 16:17:20 +02:00
val activity = activity as? HomeActivity ?: return
2016-07-04 03:31:17 +02:00
val tabs = activity.tabs
2016-12-14 09:02:50 +01:00
val account = accountsAdapter.selectedAccount ?: return
2016-07-04 03:31:17 +02:00
var hasDmTab = false
var hasInteractionsTab = false
2017-04-13 09:53:22 +02:00
var hasPublicTimelineTab = false
var hasNetworkPublicTimelineTab = false
2016-07-04 03:31:17 +02:00
for (tab in tabs) {
when (tab.type) {
CustomTabType.DIRECT_MESSAGES -> {
if (!hasDmTab) {
2016-12-04 04:58:03 +01:00
hasDmTab = hasAccountInTab(tab, account.key, account.activated)
2016-07-04 03:31:17 +02:00
}
}
CustomTabType.NOTIFICATIONS_TIMELINE -> {
if (!hasInteractionsTab) {
2016-12-04 04:58:03 +01:00
hasInteractionsTab = hasAccountInTab(tab, account.key, account.activated)
2016-07-04 03:31:17 +02:00
}
}
2017-04-13 09:53:22 +02:00
CustomTabType.PUBLIC_TIMELINE -> {
if (!hasPublicTimelineTab) {
hasPublicTimelineTab = hasAccountInTab(tab, account.key, true)
}
}
CustomTabType.NETWORK_PUBLIC_TIMELINE -> {
if (!hasNetworkPublicTimelineTab) {
hasNetworkPublicTimelineTab = hasAccountInTab(tab, account.key, true)
}
}
2016-07-04 03:31:17 +02:00
}
}
2016-07-21 08:11:00 +02:00
val menu = navigationView.menu
2016-07-04 03:31:17 +02:00
menu.setItemAvailability(R.id.interactions, !hasInteractionsTab)
2016-12-14 09:02:50 +01:00
menu.setItemAvailability(R.id.favorites, useStarsForLikes)
menu.setItemAvailability(R.id.likes, !useStarsForLikes)
2017-01-09 06:16:23 +01:00
menu.setItemAvailability(R.id.premium_features, extraFeaturesService.isSupported())
2017-02-01 07:04:14 +01:00
if (preferences[extraFeaturesNoticeVersionKey] < EXTRA_FEATURES_NOTICE_VERSION) {
2020-01-26 08:35:15 +01:00
val icon = context?.let { ContextCompat.getDrawable(it, R.drawable.ic_action_infinity) }
val color = context?.let { ContextCompat.getColor(it, R.color.material_red) }
2017-02-01 07:04:14 +01:00
val size = resources.getDimensionPixelSize(R.dimen.element_spacing_msmall)
2020-01-26 08:35:15 +01:00
if (icon != null && color != null) {
menu.setItemIcon(R.id.premium_features, BadgeDrawable(icon, color, size))
}
} else {
menu.setItemIcon(R.id.premium_features, R.drawable.ic_action_infinity)
2017-02-01 07:04:14 +01:00
}
2016-07-04 03:31:17 +02:00
var hasLists = false
var hasGroups = false
var hasPublicTimeline = false
var hasNetworkPublicTimeline = false
var hasDirectMessages = false
2016-12-04 06:45:57 +01:00
when (account.type) {
2016-12-03 06:48:40 +01:00
AccountType.TWITTER -> {
hasDirectMessages = !hasDmTab
2016-07-04 03:31:17 +02:00
hasLists = true
}
2016-12-03 06:48:40 +01:00
AccountType.STATUSNET -> {
hasDirectMessages = !hasDmTab
2016-07-04 03:31:17 +02:00
hasGroups = true
2017-04-13 09:53:22 +02:00
hasPublicTimeline = !hasPublicTimelineTab
hasNetworkPublicTimeline = !hasNetworkPublicTimelineTab
2016-07-04 03:31:17 +02:00
}
2016-12-03 06:48:40 +01:00
AccountType.FANFOU -> {
hasDirectMessages = !hasDmTab
2017-04-13 09:53:22 +02:00
hasPublicTimeline = !hasPublicTimelineTab
2016-07-04 03:31:17 +02:00
}
2017-04-21 19:06:07 +02:00
AccountType.MASTODON -> {
hasPublicTimeline = !hasPublicTimelineTab
hasNetworkPublicTimeline = !hasNetworkPublicTimelineTab
2017-04-21 19:06:07 +02:00
}
2016-07-04 03:31:17 +02:00
}
menu.setItemAvailability(R.id.messages, hasDirectMessages)
2017-04-12 10:17:20 +02:00
menu.setItemAvailability(R.id.groups, hasGroups)
menu.setItemAvailability(R.id.lists, hasLists)
menu.setItemAvailability(R.id.public_timeline, hasPublicTimeline)
menu.setItemAvailability(R.id.network_public_timeline, hasNetworkPublicTimeline)
2016-07-04 03:31:17 +02:00
}
2017-04-12 14:58:08 +02:00
private fun hasAccountInTab(tab: SupportTabSpec, accountKey: UserKey, isActivated: Boolean): Boolean {
2016-07-04 03:31:17 +02:00
if (tab.args == null) return false
2020-01-26 08:35:15 +01:00
val accountKeys = context?.let { Utils.getAccountKeys(it, tab.args) } ?: return isActivated
2017-04-12 14:58:08 +02:00
return accountKey in accountKeys
2016-07-04 03:31:17 +02:00
}
private fun closeAccountsDrawer() {
2016-12-05 04:39:22 +01:00
val activity = activity as? HomeActivity ?: return
activity.closeAccountsDrawer()
2016-07-04 03:31:17 +02:00
}
private fun getLocationOnScreen(view: View, rectF: RectF) {
val location = IntArray(2)
view.getLocationOnScreen(location)
rectF.set(location[0].toFloat(), location[1].toFloat(), (location[0] + view.width).toFloat(), (location[1] + view.height).toFloat())
}
2017-03-25 14:44:07 +01:00
override fun onAccountSelected(holder: AccountProfileImageViewHolder, details: AccountDetails) {
2016-12-05 04:39:22 +01:00
if (switchAccountAnimationPlaying) return
2016-07-21 08:46:24 +02:00
val snapshotView = floatingProfileImageSnapshot
val profileImageView = accountProfileImageView
2016-07-04 03:31:17 +02:00
val clickedImageView = holder.iconView
// Reset snapshot view position
snapshotView.pivotX = 0f
snapshotView.pivotY = 0f
snapshotView.translationX = 0f
snapshotView.translationY = 0f
val matrix = Matrix()
val sourceBounds = RectF()
val destBounds = RectF()
val snapshotBounds = RectF()
getLocationOnScreen(clickedImageView, sourceBounds)
getLocationOnScreen(profileImageView, destBounds)
getLocationOnScreen(snapshotView, snapshotBounds)
val finalScale = destBounds.width() / sourceBounds.width()
val snapshotBitmap = TransitionUtils.createViewBitmap(clickedImageView, matrix,
RectF(0f, 0f, sourceBounds.width(), sourceBounds.height()))
val lp = snapshotView.layoutParams
lp.width = clickedImageView.width
lp.height = clickedImageView.height
snapshotView.layoutParams = lp
// Copied from MaterialNavigationDrawer: https://github.com/madcyph3r/AdvancedMaterialDrawer/
val set = AnimatorSet()
set.play(ObjectAnimator.ofFloat(snapshotView, View.TRANSLATION_X, sourceBounds.left - snapshotBounds.left, destBounds.left - snapshotBounds.left))
.with(ObjectAnimator.ofFloat(snapshotView, View.TRANSLATION_Y, sourceBounds.top - snapshotBounds.top, destBounds.top - snapshotBounds.top))
.with(ObjectAnimator.ofFloat<View>(snapshotView, View.SCALE_X, 1f, finalScale))
.with(ObjectAnimator.ofFloat<View>(snapshotView, View.SCALE_Y, 1f, finalScale))
.with(ObjectAnimator.ofFloat<View>(profileImageView, View.ALPHA, 1f, 0f))
.with(ObjectAnimator.ofFloat<View>(clickedImageView, View.SCALE_X, 0f, 1f))
.with(ObjectAnimator.ofFloat<View>(clickedImageView, View.SCALE_Y, 0f, 1f))
val animationTransition: Long = 400
set.duration = animationTransition
set.interpolator = DecelerateInterpolator()
set.addListener(object : AnimatorListener {
private var clickedDrawable: Drawable? = null
private var clickedColors: IntArray? = null
override fun onAnimationStart(animation: Animator) {
2019-10-24 17:52:11 +02:00
if (context == null || isDetached || (activity?.isFinishing != false)) return
2016-07-04 03:31:17 +02:00
snapshotView.visibility = View.VISIBLE
snapshotView.setImageBitmap(snapshotBitmap)
2016-07-21 08:46:24 +02:00
val profileDrawable = profileImageView.drawable
2016-07-04 03:31:17 +02:00
clickedDrawable = clickedImageView.drawable
2017-03-02 02:08:54 +01:00
//TODO complete border color
2017-03-02 07:59:19 +01:00
clickedColors = clickedImageView.borderColors
2016-12-14 09:02:50 +01:00
val oldSelectedAccount = accountsAdapter.selectedAccount ?: return
val profileImageStyle = preferences[profileImageStyleKey]
2020-01-26 08:35:15 +01:00
requestManager.loadProfileImage(context!!, oldSelectedAccount,
profileImageStyle, clickedImageView.cornerRadius, clickedImageView.cornerRadiusRatio)
2017-03-06 08:12:18 +01:00
.into(clickedImageView).onLoadStarted(profileDrawable)
2017-03-02 02:08:54 +01:00
//TODO complete border color
2017-03-02 07:59:19 +01:00
clickedImageView.setBorderColors(*profileImageView.borderColors)
2016-07-04 03:31:17 +02:00
2017-03-25 14:44:07 +01:00
displayAccountBanner(details)
2016-07-04 03:31:17 +02:00
2016-12-05 04:39:22 +01:00
switchAccountAnimationPlaying = true
2016-07-04 03:31:17 +02:00
}
override fun onAnimationEnd(animation: Animator) {
finishAnimation()
}
override fun onAnimationCancel(animation: Animator) {
finishAnimation()
}
override fun onAnimationRepeat(animation: Animator) {
}
private fun finishAnimation() {
2016-12-05 04:39:22 +01:00
preferences.edit()
2017-03-25 14:44:07 +01:00
.putString(KEY_DEFAULT_ACCOUNT_KEY, details.key.toString())
2016-12-05 04:39:22 +01:00
.apply()
2017-03-25 14:44:07 +01:00
accountsAdapter.selectedAccount = details
2016-07-04 03:31:17 +02:00
updateAccountActions()
displayCurrentAccount(clickedDrawable)
snapshotView.visibility = View.INVISIBLE
snapshotView.setImageDrawable(null)
2016-07-21 08:46:24 +02:00
profileImageView.setImageDrawable(clickedDrawable)
2017-03-02 02:08:54 +01:00
//TODO complete border color
//profileImageView.setBorderColors(*clickedColors!!)
2016-07-04 03:31:17 +02:00
profileImageView.alpha = 1f
clickedImageView.scaleX = 1f
clickedImageView.scaleY = 1f
clickedImageView.alpha = 1f
2016-12-05 04:39:22 +01:00
switchAccountAnimationPlaying = false
2016-07-04 03:31:17 +02:00
}
})
set.start()
}
2016-12-06 06:15:22 +01:00
private fun displayAccountBanner(account: AccountDetails) {
2019-10-24 17:52:11 +02:00
if (context == null || isDetached || (activity?.isFinishing != false)) return
2016-07-21 08:46:24 +02:00
val bannerWidth = accountProfileBanner.width
2016-07-04 03:31:17 +02:00
val res = resources
val defWidth = res.displayMetrics.widthPixels
val width = if (bannerWidth > 0) bannerWidth else defWidth
2016-07-21 08:46:24 +02:00
val bannerView = accountProfileBanner.nextView as ImageView
val user = account.user
2020-06-09 02:21:48 +02:00
val fallbackBanner = when {
user.link_color != 0 -> {
ColorDrawable(user.link_color)
}
user.account_color != 0 -> {
ColorDrawable(user.account_color)
}
else -> {
ColorDrawable(Chameleon.getOverrideTheme(requireActivity(), activity).colorPrimary)
}
}
requestManager.loadProfileBanner(requireContext(), account.user, width).fallback(fallbackBanner)
.into(bannerView)
2016-07-04 03:31:17 +02:00
}
private fun displayCurrentAccount(profileImageSnapshot: Drawable?) {
2019-10-24 17:52:11 +02:00
if (context == null || isDetached || (activity?.isFinishing != false)) return
2016-12-14 09:02:50 +01:00
val account = accountsAdapter.selectedAccount ?: return
2017-05-13 08:19:23 +02:00
accountProfileNameView.spannable = account.user.name
2020-05-18 07:04:53 +02:00
val showType = accountsAdapter.accounts?.groupBy { it.type }?.count()?.let {
2020-05-15 10:16:02 +02:00
it > 1
2020-05-18 07:04:53 +02:00
} ?: false
2020-06-08 07:42:37 +02:00
accountProfileScreenNameView.spannable = if (account.type == AccountType.MASTODON || account.type == AccountType.STATUSNET) {
2020-05-18 07:04:53 +02:00
account.account.name
} else {
"${if (showType) account.type else ""}@${account.user.screen_name}"
2020-05-15 10:16:02 +02:00
}
requestManager.loadProfileImage(requireContext(), account, preferences[profileImageStyleKey],
accountProfileImageView.cornerRadius, accountProfileImageView.cornerRadiusRatio,
ProfileImageSize.REASONABLY_SMALL).placeholder(profileImageSnapshot).into(accountProfileImageView)
2017-03-02 02:08:54 +01:00
//TODO complete border color
2017-03-02 07:59:19 +01:00
accountProfileImageView.setBorderColors(account.color)
2016-07-21 08:46:24 +02:00
accountProfileBanner.showNext()
2016-07-04 03:31:17 +02:00
}
private fun updateDefaultAccountState() {
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
2016-12-14 09:02:50 +01:00
val account = accountsAdapter.selectedAccount ?: return false
2020-01-26 08:35:15 +01:00
val currentActivity = activity ?: return false
2016-07-04 03:31:17 +02:00
when (item.itemId) {
R.id.search -> {
2020-01-26 08:35:15 +01:00
val intent = Intent(currentActivity, QuickSearchBarActivity::class.java)
2016-12-04 04:58:03 +01:00
intent.putExtra(EXTRA_ACCOUNT_KEY, account.key)
2016-07-04 03:31:17 +02:00
startActivity(intent)
closeAccountsDrawer()
}
R.id.compose -> {
val composeIntent = Intent(INTENT_ACTION_COMPOSE)
2020-01-26 08:35:15 +01:00
composeIntent.setClass(currentActivity, ComposeActivity::class.java)
2016-12-04 04:58:03 +01:00
composeIntent.putExtra(EXTRA_ACCOUNT_KEY, account.key)
2016-07-04 03:31:17 +02:00
startActivity(composeIntent)
}
2016-12-14 09:02:50 +01:00
R.id.likes, R.id.favorites -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openUserFavorites(currentActivity, account.key, account.key,
2016-12-14 09:02:50 +01:00
account.user.screen_name)
2016-07-04 03:31:17 +02:00
}
R.id.lists -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openUserLists(currentActivity, account.key,
2016-12-04 04:58:03 +01:00
account.key, account.user.screen_name)
2016-07-04 03:31:17 +02:00
}
R.id.groups -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openUserGroups(currentActivity, account.key,
2016-12-04 04:58:03 +01:00
account.key, account.user.screen_name)
2016-07-04 03:31:17 +02:00
}
R.id.public_timeline -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openPublicTimeline(currentActivity, account.key)
2016-07-04 03:31:17 +02:00
}
R.id.network_public_timeline -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openNetworkPublicTimeline(currentActivity, account.key)
}
2016-07-04 03:31:17 +02:00
R.id.messages -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openDirectMessages(currentActivity, account.key)
2016-07-04 03:31:17 +02:00
}
R.id.interactions -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openInteractions(currentActivity, account.key)
2016-07-04 03:31:17 +02:00
}
R.id.edit -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openProfileEditor(currentActivity, account.key)
2016-07-04 03:31:17 +02:00
}
R.id.accounts -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openAccountsManager(currentActivity)
2016-07-04 03:31:17 +02:00
closeAccountsDrawer()
}
R.id.drafts -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openDrafts(currentActivity)
2016-07-04 03:31:17 +02:00
closeAccountsDrawer()
}
R.id.filters -> {
2020-01-26 08:35:15 +01:00
IntentUtils.openFilters(currentActivity)
2016-07-04 03:31:17 +02:00
closeAccountsDrawer()
}
2016-12-26 04:25:55 +01:00
R.id.premium_features -> {
2020-01-26 08:35:15 +01:00
val intent = Intent(currentActivity, PremiumDashboardActivity::class.java)
2016-07-04 03:31:17 +02:00
startActivity(intent)
2017-02-01 07:04:14 +01:00
preferences[extraFeaturesNoticeVersionKey] = EXTRA_FEATURES_NOTICE_VERSION
2016-07-04 03:31:17 +02:00
closeAccountsDrawer()
}
R.id.settings -> {
2017-03-13 06:35:11 +01:00
val intent = IntentUtils.settings()
2016-07-04 03:31:17 +02:00
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
startActivityForResult(intent, REQUEST_SETTINGS)
closeAccountsDrawer()
}
}
return false
}
fun setStatusBarHeight(height: Int) {
2020-01-26 08:35:15 +01:00
val top = activity?.let { Utils.getInsetsTopWithoutActionBarHeight(it, height) }
if (top != null) {
profileContainer.setPadding(0, top, 0, 0)
}
2016-07-04 03:31:17 +02:00
}
2016-12-05 04:39:22 +01:00
fun shouldDisableDrawerSlide(e: MotionEvent): Boolean {
if (accountsSelector == null) return false
2017-04-04 06:37:44 +02:00
return TwidereViewUtils.hitView(e, accountsSelector)
2016-12-05 04:39:22 +01:00
}
internal class AccountSpaceViewHolder(itemView: View) : RecyclerPagerAdapter.ViewHolder(itemView)
2016-07-21 08:11:00 +02:00
data class AccountsInfo(
2016-12-04 04:58:03 +01:00
val accounts: Array<AccountDetails>,
2016-07-21 08:11:00 +02:00
val draftsCount: Int
)
2016-07-04 03:31:17 +02:00
2017-02-07 16:30:37 +01:00
class AccountsInfoLoader(
context: Context,
val firsSyncLoad: Boolean
) : FixedAsyncTaskLoader<AccountsInfo>(context) {
2017-04-03 16:55:41 +02:00
2016-07-21 08:46:24 +02:00
private var contentObserver: ContentObserver? = null
2017-04-03 16:55:41 +02:00
set(value) {
field?.let {
context.contentResolver.unregisterContentObserver(it)
}
if (value != null) {
context.contentResolver.registerContentObserver(Drafts.CONTENT_URI, true, value)
}
}
2016-12-16 01:38:37 +01:00
private var accountListener: OnAccountsUpdateListener? = null
2017-04-03 16:55:41 +02:00
set(value) {
2017-04-04 05:42:45 +02:00
val am = AccountManager.get(context)
2017-04-03 16:55:41 +02:00
field?.let {
am.removeOnAccountsUpdatedListenerSafe(it)
}
if (value != null) {
am.addOnAccountsUpdatedListenerSafe(value, updateImmediately = true)
}
}
2016-12-05 04:39:22 +01:00
2016-07-21 08:46:24 +02:00
private var firstLoad: Boolean
init {
firstLoad = true
}
override fun loadInBackground(): AccountsInfo {
2017-04-10 11:35:35 +02:00
return loadAccountsInfo(true)
}
2017-01-31 16:24:33 +01:00
override fun onForceLoad() {
if (firsSyncLoad && firstLoad) {
2017-04-10 11:35:35 +02:00
deliverResult(loadAccountsInfo(false))
return
}
super.onForceLoad()
2016-07-21 08:11:00 +02:00
}
2016-07-04 03:31:17 +02:00
2016-07-21 08:46:24 +02:00
/**
* Handles a request to completely reset the Loader.
*/
override fun onReset() {
super.onReset()
// Ensure the loader is stopped
onStopLoading()
// Stop monitoring for changes.
2017-04-03 16:55:41 +02:00
contentObserver = null
accountListener = null
2016-07-21 08:46:24 +02:00
}
/**
* Handles a request to start the Loader.
*/
2016-07-21 08:11:00 +02:00
override fun onStartLoading() {
val weakLoader = WeakReference(this)
2016-07-21 08:46:24 +02:00
// Start watching for changes in the app data.
if (contentObserver == null) {
contentObserver = object : ContentObserver(null) {
2016-07-21 08:46:24 +02:00
override fun onChange(selfChange: Boolean) {
weakLoader.get()?.onContentChanged()
2016-07-21 08:46:24 +02:00
}
override fun onChange(selfChange: Boolean, uri: Uri?) {
weakLoader.get()?.onContentChanged()
2016-07-21 08:46:24 +02:00
}
}
}
2016-12-16 01:38:37 +01:00
if (accountListener == null) {
accountListener = OnAccountsUpdateListener {
weakLoader.get()?.onContentChanged()
2016-12-16 01:38:37 +01:00
}
}
2016-07-21 08:46:24 +02:00
if (takeContentChanged() || firstLoad) {
// If the data has changed since the last time it was loaded
// or is not currently available, start a load.
forceLoad()
firstLoad = false
2016-07-21 08:46:24 +02:00
}
}
/**
* Handles a request to stop the Loader.
*/
override fun onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad()
2016-07-21 08:11:00 +02:00
}
2016-12-05 04:39:22 +01:00
2017-04-10 11:35:35 +02:00
private fun loadAccountsInfo(loadFromDb: Boolean): AccountsInfo {
val accounts = AccountUtils.getAllAccountDetails(AccountManager.get(context), true)
2017-04-10 11:35:35 +02:00
val draftsCount = if (loadFromDb) {
2017-08-27 18:37:40 +02:00
context.contentResolver.queryCount(Drafts.CONTENT_URI_UNSENT, null,
2017-04-10 11:35:35 +02:00
null)
} else {
-1
}
return AccountsInfo(accounts, draftsCount)
}
2016-12-05 04:39:22 +01:00
}
2016-12-05 08:20:57 +01:00
}