From c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 23 Feb 2020 16:06:45 -0800 Subject: Move prefs to service locator --- .../com/pitchedapps/frost/views/AccountItem.kt | 13 +++++++---- .../com/pitchedapps/frost/views/BadgedIcon.kt | 14 +++++++----- .../pitchedapps/frost/views/FrostContentView.kt | 13 ++++++----- .../pitchedapps/frost/views/FrostRecyclerView.kt | 9 ++++++-- .../pitchedapps/frost/views/FrostVideoViewer.kt | 12 +++++++---- .../com/pitchedapps/frost/views/FrostViewPager.kt | 10 ++++++--- .../com/pitchedapps/frost/views/FrostWebView.kt | 11 +++++++--- .../kotlin/com/pitchedapps/frost/views/Keywords.kt | 25 +++++++++++++--------- 8 files changed, 71 insertions(+), 36 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps/frost/views') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt index 1f4e62ab..effa8ad4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/AccountItem.kt @@ -38,18 +38,23 @@ import com.pitchedapps.frost.facebook.profilePictureUrl import com.pitchedapps.frost.glide.FrostGlide import com.pitchedapps.frost.glide.GlideApp import com.pitchedapps.frost.utils.Prefs +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-06-05. */ -class AccountItem(val cookie: CookieEntity?) : KauIItem - (R.layout.view_account, { ViewHolder(it) }, R.id.item_account) { +class AccountItem(val cookie: CookieEntity?) : + KauIItem(R.layout.view_account, { ViewHolder(it) }, R.id.item_account) , +KoinComponent { + + private val prefs: Prefs by inject() override fun bindView(holder: ViewHolder, payloads: MutableList) { super.bindView(holder, payloads) with(holder) { text.invisible() - text.setTextColor(Prefs.textColor) + text.setTextColor(prefs.textColor) if (cookie != null) { text.text = cookie.name GlideApp.with(itemView).load(profilePictureUrl(cookie.id)) @@ -81,7 +86,7 @@ class AccountItem(val cookie: CookieEntity?) : KauIItem GoogleMaterial.Icon.gmd_add_circle_outline.toDrawable( itemView.context, 100, - Prefs.textColor + prefs.textColor ) ) text.text = itemView.context.getString(R.string.kau_add_account) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt index 097aea32..bc5c6bcc 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/BadgedIcon.kt @@ -30,6 +30,8 @@ import ca.allanwang.kau.utils.withAlpha import com.mikepenz.iconics.typeface.IIcon import com.pitchedapps.frost.databinding.ViewBadgedIconBinding import com.pitchedapps.frost.utils.Prefs +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-06-19. @@ -38,8 +40,10 @@ class BadgedIcon @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : ConstraintLayout(context, attrs, defStyleAttr) { +) : ConstraintLayout(context, attrs, defStyleAttr), KoinComponent { + + private val prefs: Prefs by inject() private val binding: ViewBadgedIconBinding = ViewBadgedIconBinding.inflate(LayoutInflater.from(context), this, true) @@ -49,7 +53,7 @@ class BadgedIcon @JvmOverloads constructor( fun ViewBadgedIconBinding.init() { val badgeColor = - Prefs.mainActivityLayout.backgroundColor().withAlpha(255).colorToForeground(0.2f) + prefs.mainActivityLayout.backgroundColor(prefs).withAlpha(255).colorToForeground(0.2f) val badgeBackground = GradientDrawable( GradientDrawable.Orientation.BOTTOM_TOP, @@ -57,7 +61,7 @@ class BadgedIcon @JvmOverloads constructor( ) badgeBackground.cornerRadius = 13.dpToPx.toFloat() badgeText.background = badgeBackground - badgeText.setTextColor(Prefs.mainActivityLayout.iconColor()) + badgeText.setTextColor(prefs.mainActivityLayout.iconColor(prefs)) } var iicon: IIcon? = null @@ -67,13 +71,13 @@ class BadgedIcon @JvmOverloads constructor( value?.toDrawable( context, sizeDp = 20, - color = Prefs.mainActivityLayout.iconColor() + color = prefs.mainActivityLayout.iconColor(prefs) ) ) } fun setAllAlpha(alpha: Float) { - // badgeTextView.setTextColor(Prefs.textColor.withAlpha(alpha.toInt())) + // badgeTextView.setTextColor(prefs.textColor.withAlpha(alpha.toInt())) binding.badgeImage.drawable.alpha = alpha.toInt() } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt index baf9421f..4fbf1482 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostContentView.kt @@ -47,6 +47,8 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.ConflatedBroadcastChannel import kotlinx.coroutines.channels.ReceiveChannel +import org.koin.core.KoinComponent +import org.koin.core.inject class FrostContentWeb @JvmOverloads constructor( context: Context, @@ -75,8 +77,9 @@ abstract class FrostContentView @JvmOverloads constructor( defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr, defStyleRes), - FrostContentParent where T : View, T : FrostContentCore { + FrostContentParent, KoinComponent where T : View, T : FrostContentCore { + private val prefs: Prefs by inject() private val refresh: SwipeRefreshLayout by bindView(R.id.content_refresh) private val progress: ProgressBar by bindView(R.id.content_progress) val coreView: T by bindView(R.id.content_core) @@ -153,9 +156,9 @@ abstract class FrostContentView @JvmOverloads constructor( } override fun reloadThemeSelf() { - progress.tint(Prefs.textColor.withAlpha(180)) - refresh.setColorSchemeColors(Prefs.iconColor) - refresh.setProgressBackgroundColorSchemeColor(Prefs.headerColor.withAlpha(255)) + progress.tint(prefs.textColor.withAlpha(180)) + refresh.setColorSchemeColors(prefs.iconColor) + refresh.setProgressBackgroundColorSchemeColor(prefs.headerColor.withAlpha(255)) } override fun reloadTextSizeSelf() { @@ -192,7 +195,7 @@ abstract class FrostContentView @JvmOverloads constructor( if (isVisible) fadeOut(duration = 200L) } else if (loading) { - if (animate && Prefs.animate) circularReveal(offset = WEB_LOAD_DELAY) + if (animate && prefs.animate) circularReveal(offset = WEB_LOAD_DELAY) else fadeIn(duration = 200L, offset = WEB_LOAD_DELAY) L.v { "Transition loaded in ${System.currentTimeMillis() - transitionStart} ms" } receiver.cancel() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt index 5e82f0e9..6b156cc4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostRecyclerView.kt @@ -30,6 +30,8 @@ import com.pitchedapps.frost.fragments.RecyclerContentContract import com.pitchedapps.frost.utils.Prefs import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.launch +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-05-29. @@ -41,8 +43,11 @@ class FrostRecyclerView @JvmOverloads constructor( attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : RecyclerView(context, attrs, defStyleAttr), + KoinComponent, FrostContentCore { + private val prefs: Prefs by inject() + override fun reload(animate: Boolean) = reloadBase(animate) override lateinit var parent: FrostContentParent @@ -71,13 +76,13 @@ class FrostRecyclerView @JvmOverloads constructor( var onReloadClear: () -> Unit = {} override fun reloadBase(animate: Boolean) { - if (Prefs.animate) fadeOut(onFinish = onReloadClear) + if (prefs.animate) fadeOut(onFinish = onReloadClear) scope.launch { parent.refreshChannel.offer(true) recyclerContract.reload { parent.progressChannel.offer(it) } parent.progressChannel.offer(100) parent.refreshChannel.offer(false) - if (Prefs.animate) circularReveal() + if (prefs.animate) circularReveal() } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt index bc565527..7b4bd096 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoViewer.kt @@ -47,6 +47,8 @@ import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.utils.ctxCoroutine import com.pitchedapps.frost.utils.frostDownload +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-10-13. @@ -55,7 +57,7 @@ class FrostVideoViewer @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : FrameLayout(context, attrs, defStyleAttr), FrostVideoViewerContract { +) : FrameLayout(context, attrs, defStyleAttr), FrostVideoViewerContract , KoinComponent { companion object { /** @@ -85,6 +87,8 @@ class FrostVideoViewer @JvmOverloads constructor( } } + private val prefs: Prefs by inject() + private val binding: ViewVideoBinding = ViewVideoBinding.inflate(LayoutInflater.from(context), this, true) @@ -95,8 +99,8 @@ class FrostVideoViewer @JvmOverloads constructor( fun ViewVideoBinding.init() { alpha = 0f videoBackground.setBackgroundColor( - if (!Prefs.blackMediaBg && Prefs.bgColor.isColorDark) - Prefs.bgColor.withMinAlpha(200) + if (!prefs.blackMediaBg && prefs.bgColor.isColorDark) + prefs.bgColor.withMinAlpha(200) else Color.BLACK ) @@ -104,7 +108,7 @@ class FrostVideoViewer @JvmOverloads constructor( video.pause() videoToolbar.inflateMenu(R.menu.menu_video) context.setMenuIcons( - videoToolbar.menu, Prefs.iconColor, + videoToolbar.menu, prefs.iconColor, R.id.action_pip to GoogleMaterial.Icon.gmd_picture_in_picture_alt, R.id.action_download to GoogleMaterial.Icon.gmd_file_download ) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostViewPager.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostViewPager.kt index bf2f771d..819ec68a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostViewPager.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostViewPager.kt @@ -22,6 +22,8 @@ import android.util.AttributeSet import android.view.MotionEvent import androidx.viewpager.widget.ViewPager import com.pitchedapps.frost.utils.Prefs +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-07-07. @@ -29,12 +31,14 @@ import com.pitchedapps.frost.utils.Prefs * Basic override to allow us to control swiping */ class FrostViewPager @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : - ViewPager(context, attrs) { + ViewPager(context, attrs), KoinComponent { + + private val prefs: Prefs by inject() var enableSwipe = true override fun onInterceptTouchEvent(ev: MotionEvent?) = try { - Prefs.viewpagerSwipe && enableSwipe && super.onInterceptTouchEvent(ev) + prefs.viewpagerSwipe && enableSwipe && super.onInterceptTouchEvent(ev) } catch (e: IllegalArgumentException) { false } @@ -42,7 +46,7 @@ class FrostViewPager @JvmOverloads constructor(context: Context, attrs: Attribut @SuppressLint("ClickableViewAccessibility") override fun onTouchEvent(ev: MotionEvent?): Boolean = try { - Prefs.viewpagerSwipe && enableSwipe && super.onTouchEvent(ev) + prefs.viewpagerSwipe && enableSwipe && super.onTouchEvent(ev) } catch (e: IllegalArgumentException) { false } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt index 8cba5e11..f275aad8 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/FrostWebView.kt @@ -41,6 +41,8 @@ import com.pitchedapps.frost.web.FrostChromeClient import com.pitchedapps.frost.web.FrostJSI import com.pitchedapps.frost.web.FrostWebViewClient import com.pitchedapps.frost.web.NestedWebView +import org.koin.core.KoinComponent +import org.koin.core.inject import kotlin.math.abs import kotlin.math.max import kotlin.math.min @@ -54,7 +56,10 @@ class FrostWebView @JvmOverloads constructor( attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : NestedWebView(context, attrs, defStyleAttr), - FrostContentCore { + FrostContentCore, + KoinComponent { + + val prefs: Prefs by inject() override fun reload(animate: Boolean) { if (parent.registerTransition(false, animate)) @@ -75,7 +80,7 @@ class FrostWebView @JvmOverloads constructor( javaScriptEnabled = true mediaPlaybackRequiresUserGesture = false // TODO check if we need this allowFileAccess = true - textZoom = Prefs.webTextScaling + textZoom = prefs.webTextScaling domStorageEnabled = true } setLayerType(LAYER_TYPE_HARDWARE, null) @@ -208,7 +213,7 @@ class FrostWebView @JvmOverloads constructor( } override fun reloadTextSizeSelf() { - settings.textZoom = Prefs.webTextScaling + settings.textZoom = prefs.webTextScaling } override fun destroy() { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/Keywords.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/Keywords.kt index 8f0309cb..ff8a5bb7 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/views/Keywords.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/views/Keywords.kt @@ -38,6 +38,8 @@ import com.mikepenz.iconics.typeface.IIcon import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial import com.pitchedapps.frost.R import com.pitchedapps.frost.utils.Prefs +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-06-19. @@ -46,8 +48,9 @@ class Keywords @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : ConstraintLayout(context, attrs, defStyleAttr) { +) : ConstraintLayout(context, attrs, defStyleAttr), KoinComponent { + private val prefs: Prefs by inject() val editText: AppCompatEditText by bindView(R.id.edit_text) val addIcon: ImageView by bindView(R.id.add_icon) val recycler: RecyclerView by bindView(R.id.recycler) @@ -55,8 +58,8 @@ class Keywords @JvmOverloads constructor( init { inflate(context, R.layout.view_keywords, this) - editText.tint(Prefs.textColor) - addIcon.setImageDrawable(GoogleMaterial.Icon.gmd_add.keywordDrawable(context)) + editText.tint(prefs.textColor) + addIcon.setImageDrawable(GoogleMaterial.Icon.gmd_add.keywordDrawable(context, prefs)) addIcon.setOnClickListener { if (editText.text.isNullOrEmpty()) editText.error = context.string(R.string.empty_keyword) @@ -65,7 +68,7 @@ class Keywords @JvmOverloads constructor( editText.text?.clear() } } - adapter.add(Prefs.notificationKeywords.map { KeywordItem(it) }) + adapter.add(prefs.notificationKeywords.map { KeywordItem(it) }) recycler.layoutManager = LinearLayoutManager(context) recycler.adapter = adapter adapter.addEventHook(object : ClickEventHook() { @@ -84,12 +87,12 @@ class Keywords @JvmOverloads constructor( } fun save() { - Prefs.notificationKeywords = adapter.adapterItems.mapTo(mutableSetOf()) { it.keyword } + prefs.notificationKeywords = adapter.adapterItems.mapTo(mutableSetOf()) { it.keyword } } } -private fun IIcon.keywordDrawable(context: Context): Drawable = - toDrawable(context, 20, Prefs.textColor) +private fun IIcon.keywordDrawable(context: Context, prefs: Prefs): Drawable = + toDrawable(context, 20, prefs.textColor) class KeywordItem(val keyword: String) : AbstractItem() { @@ -111,13 +114,15 @@ class KeywordItem(val keyword: String) : AbstractItem() holder.text.text = null } - class ViewHolder(v: View) : RecyclerView.ViewHolder(v) { + class ViewHolder(v: View) : RecyclerView.ViewHolder(v), KoinComponent { + + private val prefs: Prefs by inject() val text: AppCompatTextView by bindView(R.id.keyword_text) val delete: ImageView by bindView(R.id.keyword_delete) init { - text.setTextColor(Prefs.textColor) - delete.setImageDrawable(GoogleMaterial.Icon.gmd_delete.keywordDrawable(itemView.context)) + text.setTextColor(prefs.textColor) + delete.setImageDrawable(GoogleMaterial.Icon.gmd_delete.keywordDrawable(itemView.context, prefs)) } } } -- cgit v1.2.3