From 227e2192416b3715ea11f1b2d1d9b5e60a8e503d Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Wed, 25 Nov 2020 19:41:57 +0100 Subject: [PATCH] load drawer avatar earlier to avoid flicker on app start (#1993) * load drawer avatar earlier to avoid flicker on app start * use placeholder when loading drawer avatar for the first time --- .../com/keylesspalace/tusky/MainActivity.kt | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt index 43b38889a..dc66945d0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt @@ -119,7 +119,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - if (accountManager.activeAccount == null) { + + val activeAccount = accountManager.activeAccount + if (activeAccount == null) { // will be redirected to LoginActivity by BaseActivity return } @@ -183,8 +185,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje val hideTopToolbar = preferences.getBoolean(PrefKeys.HIDE_TOP_TOOLBAR, false) mainToolbar.visible(!hideTopToolbar) - val navIconSize = resources.getDimensionPixelSize(R.dimen.avatar_toolbar_nav_icon_size) - mainToolbar.navigationIcon = FixedSizeDrawable(getDrawable(R.drawable.avatar_default), navIconSize, navIconSize) + loadDrawerAvatar(activeAccount.profilePictureUrl, true) mainToolbar.menu.add(R.string.action_search).apply { setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM) @@ -641,23 +642,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje .load(me.header) .into(header.accountHeaderBackground) - val navIconSize = resources.getDimensionPixelSize(R.dimen.avatar_toolbar_nav_icon_size) - - glide.asDrawable() - .override(navIconSize) - .load(me.avatar) - .transform( - RoundedCorners(resources.getDimensionPixelSize(R.dimen.avatar_radius_36dp)) - ) - .into(object : CustomTarget() { - override fun onResourceReady(resource: Drawable, transition: Transition?) { - mainToolbar.navigationIcon = resource - } - - override fun onLoadCleared(placeholder: Drawable?) { - mainToolbar.navigationIcon = placeholder - } - }) + loadDrawerAvatar(me.avatar, false) accountManager.updateActiveAccount(me) NotificationHelper.createNotificationChannelsForAccount(accountManager.activeAccount!!, this) @@ -682,6 +667,36 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje updateShortcut(this, accountManager.activeAccount!!) } + private fun loadDrawerAvatar(avatarUrl: String, showPlaceholder: Boolean) { + val navIconSize = resources.getDimensionPixelSize(R.dimen.avatar_toolbar_nav_icon_size) + + glide.asDrawable() + .load(avatarUrl) + .transform( + RoundedCorners(resources.getDimensionPixelSize(R.dimen.avatar_radius_36dp)) + ) + .apply { + if (showPlaceholder) { + placeholder(R.drawable.avatar_default) + } + } + .into(object : CustomTarget(navIconSize, navIconSize) { + + override fun onLoadStarted(placeholder: Drawable?) { + if(placeholder != null) { + mainToolbar.navigationIcon = FixedSizeDrawable(placeholder, navIconSize, navIconSize) + } + } + override fun onResourceReady(resource: Drawable, transition: Transition?) { + mainToolbar.navigationIcon = resource + } + + override fun onLoadCleared(placeholder: Drawable?) { + mainToolbar.navigationIcon = placeholder + } + }) + } + private fun fetchAnnouncements() { mastodonApi.listAnnouncements(false) .observeOn(AndroidSchedulers.mainThread())