Room detail : lazy load EmojiPopup and AutocompleteMemberPresenter

This commit is contained in:
ganfra 2021-09-22 11:07:36 +02:00
parent 5bf1761f27
commit fb10e9f113
3 changed files with 93 additions and 12 deletions

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.platform
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
fun <T> LifecycleOwner.lifecycleAwareLazy(initializer: () -> T): Lazy<T> = LifecycleAwareLazy(this, initializer)
private object UninitializedValue
class LifecycleAwareLazy<out T>(
private val owner: LifecycleOwner,
initializer: () -> T
) : Lazy<T>, LifecycleObserver {
private var initializer: (() -> T)? = initializer
private var _value: Any? = UninitializedValue
@Suppress("UNCHECKED_CAST")
override val value: T
@MainThread
get() {
if (_value === UninitializedValue) {
_value = initializer!!()
attachToLifecycle()
}
return _value as T
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun resetValue() {
_value = UninitializedValue
detachFromLifecycle()
}
private fun attachToLifecycle() {
if (getLifecycleOwner().lifecycle.currentState == Lifecycle.State.DESTROYED) {
throw IllegalStateException("Initialization failed because lifecycle has been destroyed!")
}
getLifecycleOwner().lifecycle.addObserver(this)
}
private fun detachFromLifecycle() {
getLifecycleOwner().lifecycle.removeObserver(this)
}
private fun getLifecycleOwner() = when (owner) {
is Fragment -> owner.viewLifecycleOwner
else -> owner
}
override fun isInitialized(): Boolean = _value !== UninitializedValue
override fun toString(): String = if (isInitialized()) value.toString() else "Lazy value not initialized yet."
}

View File

@ -19,8 +19,8 @@ package im.vector.app.features.autocomplete.member
import android.content.Context import android.content.Context
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.assisted.AssistedFactory import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.features.autocomplete.AutocompleteClickListener import im.vector.app.features.autocomplete.AutocompleteClickListener
import im.vector.app.features.autocomplete.RecyclerViewPresenter import im.vector.app.features.autocomplete.RecyclerViewPresenter
import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.query.QueryStringValue
@ -35,7 +35,7 @@ class AutocompleteMemberPresenter @AssistedInject constructor(context: Context,
private val controller: AutocompleteMemberController private val controller: AutocompleteMemberController
) : RecyclerViewPresenter<RoomMemberSummary>(context), AutocompleteClickListener<RoomMemberSummary> { ) : RecyclerViewPresenter<RoomMemberSummary>(context), AutocompleteClickListener<RoomMemberSummary> {
private val room = session.getRoom(roomId)!! private val room by lazy { session.getRoom(roomId)!! }
init { init {
controller.listener = this controller.listener = this

View File

@ -87,6 +87,7 @@ import im.vector.app.core.hardware.vibrate
import im.vector.app.core.intent.getFilenameFromUri import im.vector.app.core.intent.getFilenameFromUri
import im.vector.app.core.intent.getMimeTypeFromUri import im.vector.app.core.intent.getMimeTypeFromUri
import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.core.platform.lifecycleAwareLazy
import im.vector.app.core.platform.showOptimizedSnackbar import im.vector.app.core.platform.showOptimizedSnackbar
import im.vector.app.core.resources.ColorProvider import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.ui.views.CurrentCallsView import im.vector.app.core.ui.views.CurrentCallsView
@ -314,7 +315,9 @@ class RoomDetailFragment @Inject constructor(
private val currentCallsViewPresenter = CurrentCallsViewPresenter() private val currentCallsViewPresenter = CurrentCallsViewPresenter()
private var inviteView: VectorInviteView? = null private var inviteView: VectorInviteView? = null
private lateinit var emojiPopup: EmojiPopup private val emojiPopup: EmojiPopup by lifecycleAwareLazy {
createEmojiPopup()
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -323,6 +326,9 @@ class RoomDetailFragment @Inject constructor(
roomDetailViewModel.handle(RoomDetailAction.RoomUpgradeSuccess(replacementRoomId)) roomDetailViewModel.handle(RoomDetailAction.RoomUpgradeSuccess(replacementRoomId))
} }
} }
lifecycleScope.launchWhenResumed {
}
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -349,7 +355,7 @@ class RoomDetailFragment @Inject constructor(
setupJumpToReadMarkerView() setupJumpToReadMarkerView()
setupActiveCallView() setupActiveCallView()
setupJumpToBottomView() setupJumpToBottomView()
setupEmojiPopup() setupEmojiButton()
setupFailedMessagesWarningView() setupFailedMessagesWarningView()
setupRemoveJitsiWidgetView() setupRemoveJitsiWidgetView()
setupVoiceMessageView() setupVoiceMessageView()
@ -585,8 +591,14 @@ class RoomDetailFragment @Inject constructor(
) )
} }
private fun setupEmojiPopup() { private fun setupEmojiButton() {
emojiPopup = EmojiPopup views.composerLayout.views.composerEmojiButton.debouncedClicks {
emojiPopup.toggle()
}
}
private fun createEmojiPopup(): EmojiPopup {
return EmojiPopup
.Builder .Builder
.fromRootView(views.rootConstraintLayout) .fromRootView(views.rootConstraintLayout)
.setKeyboardAnimationStyle(R.style.emoji_fade_animation_style) .setKeyboardAnimationStyle(R.style.emoji_fade_animation_style)
@ -603,10 +615,6 @@ class RoomDetailFragment @Inject constructor(
} }
} }
.build(views.composerLayout.views.composerEditText) .build(views.composerLayout.views.composerEditText)
views.composerLayout.views.composerEmojiButton.debouncedClicks {
emojiPopup.toggle()
}
} }
private fun setupFailedMessagesWarningView() { private fun setupFailedMessagesWarningView() {
@ -775,8 +783,6 @@ class RoomDetailFragment @Inject constructor(
autoCompleter.clear() autoCompleter.clear()
debouncer.cancelAll() debouncer.cancelAll()
views.timelineRecyclerView.cleanup() views.timelineRecyclerView.cleanup()
emojiPopup.dismiss()
super.onDestroyView() super.onDestroyView()
} }