refactoring, code review

This commit is contained in:
Valere 2019-08-29 17:27:49 +02:00
parent 75266f42bb
commit 35817245cb
7 changed files with 37 additions and 25 deletions

View File

@ -16,22 +16,21 @@
package im.vector.riotx

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.core.provider.FontRequest
import androidx.emoji.text.EmojiCompat
import androidx.emoji.text.FontRequestEmojiCompatConfig
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

object EmojiCompatHelper {
@Singleton
class EmojiCompatWrapper @Inject constructor(private val context: Context) {

var initialized = false
private var initialized = false

fun init(fontRequest: FontRequest) {

fun init(context: Context) {
val fontRequest = FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
"Noto Color Emoji Compat",
R.array.com_google_android_gms_fonts_certs
)
//Use emoji compat for the benefit of emoji spans
val config = FontRequestEmojiCompatConfig(context, fontRequest)
// we want to replace all emojis with selected font

View File

@ -19,13 +19,10 @@ package im.vector.riotx
import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.os.Handler
import android.os.HandlerThread
import androidx.core.provider.FontRequest
import androidx.core.provider.FontsContractCompat
import androidx.emoji.text.EmojiCompat
import androidx.emoji.text.FontRequestEmojiCompatConfig
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
@ -69,6 +66,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration.
@Inject lateinit var authenticator: Authenticator
@Inject lateinit var vectorConfiguration: VectorConfiguration
@Inject lateinit var emojiCompatFontProvider: EmojiCompatFontProvider
@Inject lateinit var emojiCompatWrapper: EmojiCompatWrapper
@Inject lateinit var vectorUncaughtExceptionHandler: VectorUncaughtExceptionHandler
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var notificationDrawerManager: NotificationDrawerManager
@ -109,7 +107,7 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration.
FontsContractCompat.requestFont(this, fontRequest, emojiCompatFontProvider, getFontThreadHandler())
vectorConfiguration.initConfiguration()

EmojiCompatHelper.init(this)
emojiCompatWrapper.init(fontRequest)

NotificationUtils.createNotificationChannels(applicationContext)
if (authenticator.hasAuthenticatedSessions() && !activeSessionHolder.hasActiveSession()) {

View File

@ -58,6 +58,7 @@ import im.vector.riotx.features.rageshake.BugReportActivity
import im.vector.riotx.features.rageshake.BugReporter
import im.vector.riotx.features.rageshake.RageShake
import im.vector.riotx.features.reactions.EmojiReactionPickerActivity
import im.vector.riotx.features.reactions.widget.ReactionButton
import im.vector.riotx.features.roomdirectory.PublicRoomsFragment
import im.vector.riotx.features.roomdirectory.RoomDirectoryActivity
import im.vector.riotx.features.roomdirectory.createroom.CreateRoomActivity
@ -181,6 +182,8 @@ interface ScreenComponent {

fun inject(displayReadReceiptsBottomSheet: DisplayReadReceiptsBottomSheet)

fun inject(reactionButton: ReactionButton)

@Component.Factory
interface Factory {
fun create(vectorComponent: VectorComponent,

View File

@ -24,6 +24,7 @@ import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.auth.Authenticator
import im.vector.matrix.android.api.session.Session
import im.vector.riotx.EmojiCompatFontProvider
import im.vector.riotx.EmojiCompatWrapper
import im.vector.riotx.VectorApplication
import im.vector.riotx.core.pushers.PushersManager
import im.vector.riotx.features.configuration.VectorConfiguration
@ -70,6 +71,8 @@ interface VectorComponent {

fun emojiCompatFontProvider(): EmojiCompatFontProvider

fun emojiCompatWrapper() : EmojiCompatWrapper

fun eventHtmlRenderer(): EventHtmlRenderer

fun navigator(): Navigator

View File

@ -46,9 +46,7 @@ class ViewReactionBottomSheet : VectorBaseBottomSheetDialogFragment() {
@BindView(R.id.bottom_sheet_display_reactions_list)
lateinit var epoxyRecyclerView: EpoxyRecyclerView

private val epoxyController by lazy {
ViewReactionsEpoxyController(requireContext())
}
@Inject lateinit var epoxyController: ViewReactionsEpoxyController

override fun injectWith(screenComponent: ScreenComponent) {
screenComponent.inject(this)

View File

@ -17,22 +17,23 @@
package im.vector.riotx.features.home.room.detail.timeline.action

import android.content.Context
import android.graphics.Typeface
import android.text.format.DateUtils
import androidx.emoji.text.EmojiCompat
import com.airbnb.epoxy.TypedEpoxyController
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Incomplete
import com.airbnb.mvrx.Success
import im.vector.riotx.EmojiCompatHelper
import im.vector.riotx.EmojiCompatWrapper
import im.vector.riotx.R
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.ui.list.genericFooterItem
import im.vector.riotx.core.ui.list.genericLoaderItem
import javax.inject.Inject

/**
* Epoxy controller for reaction event list
*/
class ViewReactionsEpoxyController(private val context: Context)
class ViewReactionsEpoxyController @Inject constructor(
private val stringProvider: StringProvider,
private val emojiCompatWrapper: EmojiCompatWrapper )
: TypedEpoxyController<DisplayReactionsViewState>() {

override fun buildModels(state: DisplayReactionsViewState) {
@ -45,7 +46,7 @@ class ViewReactionsEpoxyController(private val context: Context)
is Fail -> {
genericFooterItem {
id("failure")
text(context.getString(R.string.unknown_error))
text(stringProvider.getString(R.string.unknown_error))
}
}
is Success -> {
@ -53,7 +54,7 @@ class ViewReactionsEpoxyController(private val context: Context)
reactionInfoSimpleItem {
id(it.eventId)
timeStamp(it.timestamp)
reactionKey(EmojiCompatHelper.safeEmojiSpanify(it.reactionKey))
reactionKey(emojiCompatWrapper.safeEmojiSpanify(it.reactionKey))
authorDisplayName(it.authorName ?: it.authorId)
}
}

View File

@ -34,9 +34,11 @@ import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import im.vector.riotx.EmojiCompatHelper
import im.vector.riotx.EmojiCompatWrapper
import im.vector.riotx.R
import im.vector.riotx.core.di.HasScreenInjector
import im.vector.riotx.core.utils.TextUtils
import javax.inject.Inject

/**
* An animated reaction button.
@ -46,6 +48,12 @@ class ReactionButton @JvmOverloads constructor(context: Context, attrs: Attribut
defStyleAttr: Int = 0)
: FrameLayout(context, attrs, defStyleAttr), View.OnClickListener, View.OnLongClickListener {

init {
if (context is HasScreenInjector) {
context.injector().inject(this)
}
}

companion object {
private val DECCELERATE_INTERPOLATOR = DecelerateInterpolator()
private val ACCELERATE_DECELERATE_INTERPOLATOR = AccelerateDecelerateInterpolator()
@ -53,6 +61,8 @@ class ReactionButton @JvmOverloads constructor(context: Context, attrs: Attribut

}

@Inject lateinit var emojiCompatWrapper: EmojiCompatWrapper

private var emojiView: TextView? = null
private var countTextView: TextView? = null

@ -77,7 +87,7 @@ class ReactionButton @JvmOverloads constructor(context: Context, attrs: Attribut
set(value) {
field = value
//maybe cache this for performances?
val emojiSpanned = EmojiCompatHelper.safeEmojiSpanify(value)
val emojiSpanned = emojiCompatWrapper.safeEmojiSpanify(value)
emojiView?.text = emojiSpanned
}