feat: 优化代码

pull/169/head
kunfei 5 years ago
parent afba849286
commit 9d9519d508
  1. 2
      app/src/main/assets/updateLog.md
  2. 14
      app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt
  3. 3
      app/src/main/java/io/legado/app/ui/widget/prefs/EditTextPreference.kt
  4. 9
      app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt
  5. 4
      app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt
  6. 40
      app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt
  7. 12
      app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt

@ -1,7 +1,7 @@
## 更新日志 ## 更新日志
* 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。
* 请关注[开源阅读]()支持我,同时关注合作公众号[小说拾遗](),阅读公众号小编。 * 请关注[开源阅读]()支持我,同时关注合作公众号[小说拾遗](),阅读公众号小编。
* 弄了个企业公众号[开源阅读](),后面弄好后会把原来的开源阅读软件迁移过来 * 弄了个企业公众号[开源阅读](),后面弄好后会把原来的[开源阅读软件]()迁移过来
**2020/03/19** **2020/03/19**
* 美化界面我的 by yangyxd * 美化界面我的 by yangyxd

@ -6,24 +6,14 @@ import android.content.res.TypedArray
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import com.jaredrummler.android.colorpicker.* import com.jaredrummler.android.colorpicker.*
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.accentColor
import org.jetbrains.anko.layoutInflater
import org.jetbrains.anko.px2dip
class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs), class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs),
ColorPickerDialogListener { ColorPickerDialogListener {
@ -121,12 +111,12 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
} }
override fun onBindViewHolder(holder: PreferenceViewHolder) { override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val v = io.legado.app.ui.widget.prefs.Preference.bindView<ColorPanelView>(context, holder, icon, title, summary, widgetLayoutResource, val v = io.legado.app.ui.widget.prefs.Preference.bindView<ColorPanelView>(context, holder, icon, title, summary, widgetLayoutResource,
io.legado.app.R.id.cpv_preference_preview_color_panel, 30, 30) io.legado.app.R.id.cpv_preference_preview_color_panel, 30, 30)
if (v is ColorPanelView) { if (v is ColorPanelView) {
v?.color = color v.color = color
} }
super.onBindViewHolder(holder)
} }
override fun onSetInitialValue(defaultValue: Any?) { override fun onSetInitialValue(defaultValue: Any?) {

@ -14,9 +14,8 @@ class EditTextPreference(context: Context, attrs: AttributeSet) : androidx.prefe
} }
override fun onBindViewHolder(holder: PreferenceViewHolder?) { override fun onBindViewHolder(holder: PreferenceViewHolder?) {
Preference.bindView<TextView>(context, holder, icon, title, summary, null, null)
super.onBindViewHolder(holder) super.onBindViewHolder(holder)
val txt = if (text == null || text.isEmpty()) summary else text
val v = Preference.bindView<TextView>(context, holder, icon, title, txt, null, null)
} }
} }

@ -10,22 +10,17 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.jaredrummler.android.colorpicker.ColorPanelView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.lib.theme.accentColor
import io.legado.app.utils.getCompatDrawable import io.legado.app.utils.getCompatDrawable
import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_icon_preference.view.* import kotlinx.android.synthetic.main.item_icon_preference.view.*
import org.jetbrains.anko.layoutInflater
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
@ -34,7 +29,7 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
private val mEntryDrawables = arrayListOf<Drawable?>() private val mEntryDrawables = arrayListOf<Drawable?>()
init { init {
layoutResource = io.legado.app.R.layout.view_preference layoutResource = R.layout.view_preference
widgetLayoutResource = R.layout.view_icon widgetLayoutResource = R.layout.view_icon
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.IconListPreference, 0, 0) val a = context.theme.obtainStyledAttributes(attrs, R.styleable.IconListPreference, 0, 0)
@ -63,7 +58,7 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
val selectedIndex = findIndexOfValue(value) val selectedIndex = findIndexOfValue(value)
if (selectedIndex >= 0) { if (selectedIndex >= 0) {
val drawable = mEntryDrawables[selectedIndex] val drawable = mEntryDrawables[selectedIndex]
v?.setImageDrawable(drawable) v.setImageDrawable(drawable)
} }
} }
} }

@ -16,10 +16,10 @@ class NameListPreference(context: Context, attrs: AttributeSet) : ListPreference
} }
override fun onBindViewHolder(holder: PreferenceViewHolder?) { override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
val v = Preference.bindView<TextView>(context, holder, icon, title, summary, widgetLayoutResource, R.id.text_view) val v = Preference.bindView<TextView>(context, holder, icon, title, summary, widgetLayoutResource, R.id.text_view)
if (v is TextView) { if (v is TextView) {
v?.text = entry v.text = entry
} }
super.onBindViewHolder(holder)
} }
} }

@ -1,33 +1,17 @@
package io.legado.app.ui.widget.prefs package io.legado.app.ui.widget.prefs
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.media.Image
import android.os.Bundle
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.FragmentActivity
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder import androidx.preference.PreferenceViewHolder
import com.jaredrummler.android.colorpicker.*
import io.legado.app.lib.theme.ATH
import io.legado.app.R import io.legado.app.R
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import org.jetbrains.anko.imageBitmap
import org.jetbrains.anko.layoutInflater import org.jetbrains.anko.layoutInflater
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -44,21 +28,21 @@ class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Pr
weightWidth: Int = 0, weightHeight: Int = 0): T? { weightWidth: Int = 0, weightHeight: Int = 0): T? {
if (it == null) return null if (it == null) return null
val view = it.findViewById(R.id.preference_title) val view = it.findViewById(R.id.preference_title)
if (view is TextView) { // && !view.isInEditMode if (view is TextView) {
view.text = title view.text = title
view.isVisible = title != null && title.isNotEmpty() view.isVisible = title != null && title.isNotEmpty()
val tv_summary = it.findViewById(R.id.preference_desc) val tvSummary = it.findViewById(R.id.preference_desc)
if(tv_summary is TextView) { if (tvSummary is TextView) {
tv_summary.text = summary tvSummary.text = summary
tv_summary.isVisible = summary != null && summary.isNotEmpty() tvSummary.isVisible = summary != null && summary.isNotEmpty()
} }
val _icon = it.findViewById(R.id.preference_icon) val iconView = it.findViewById(R.id.preference_icon)
if (_icon is ImageView) { if (iconView is ImageView) {
_icon.isVisible = icon != null && icon.isVisible iconView.isVisible = icon != null && icon.isVisible
_icon.setImageDrawable(icon) iconView.setImageDrawable(icon)
_icon.setColorFilter(context.accentColor) iconView.setColorFilter(context.accentColor)
} }
} }
@ -72,7 +56,7 @@ class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Pr
lay.removeAllViews() lay.removeAllViews()
lay.addView(childView) lay.addView(childView)
lay.isVisible = true lay.isVisible = true
v = lay.findViewById<T>(viewId) v = lay.findViewById(viewId)
} }
if (weightWidth > 0 || weightHeight > 0) { if (weightWidth > 0 || weightHeight > 0) {
@ -94,8 +78,8 @@ class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Pr
} }
override fun onBindViewHolder(holder: PreferenceViewHolder?) { override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder)
bindView<View>(context, holder, icon, title, summary, null, null) bindView<View>(context, holder, icon, title, summary, null, null)
super.onBindViewHolder(holder)
} }
} }

@ -17,11 +17,19 @@ class SwitchPreference(context: Context, attrs: AttributeSet) :
} }
override fun onBindViewHolder(holder: PreferenceViewHolder?) { override fun onBindViewHolder(holder: PreferenceViewHolder?) {
super.onBindViewHolder(holder) val v = Preference.bindView<SwitchCompat>(
val v = Preference.bindView<SwitchCompat>(context, holder, icon, title, summary, widgetLayoutResource, R.id.switchWidget) context,
holder,
icon,
title,
summary,
widgetLayoutResource,
R.id.switchWidget
)
if (v is SwitchCompat) { if (v is SwitchCompat) {
ATH.setTint(v, context.accentColor) ATH.setTint(v, context.accentColor)
} }
super.onBindViewHolder(holder)
} }
} }

Loading…
Cancel
Save