feat: 优化代码

pull/103/head
kunfei 5 years ago
parent d8e2e36c23
commit 25c9285524
  1. 4
      app/src/main/java/io/legado/app/lib/theme/prefs/ATESwitchPreference.kt
  2. 6
      app/src/main/java/io/legado/app/ui/widget/DetailSeekBar.kt
  3. 8
      app/src/main/java/io/legado/app/ui/widget/SelectActionBar.kt
  4. 135
      app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt
  5. 9
      app/src/main/res/layout/view_select_action_bar.xml
  6. 2
      app/src/main/res/values/arrays.xml
  7. 4
      app/src/main/res/values/pref_key_value.xml
  8. 9
      app/src/main/res/xml/pref_config_theme.xml

@ -7,7 +7,7 @@ import androidx.preference.PreferenceViewHolder
import androidx.preference.SwitchPreferenceCompat
import io.legado.app.R
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ThemeStore
import io.legado.app.lib.theme.accentColor
class ATESwitchPreference(context: Context, attrs: AttributeSet) :
SwitchPreferenceCompat(context, attrs) {
@ -17,7 +17,7 @@ class ATESwitchPreference(context: Context, attrs: AttributeSet) :
holder?.let {
val view = it.findViewById(R.id.switchWidget)
if (view is SwitchCompat) {
ATH.setTint(view, ThemeStore.accentColor(view.getContext()))
ATH.setTint(view, context.accentColor)
}
}
}

@ -2,16 +2,15 @@ package io.legado.app.ui.widget
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.SeekBar
import androidx.appcompat.widget.LinearLayoutCompat
import io.legado.app.R
import io.legado.app.utils.progressAdd
import kotlinx.android.synthetic.main.view_detail_seek_bar.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class DetailSeekBar(context: Context, attrs: AttributeSet?) : LinearLayoutCompat(context, attrs),
class DetailSeekBar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs),
SeekBar.OnSeekBarChangeListener {
var valueFormat: ((progress: Int) -> String)? = null
@ -28,7 +27,6 @@ class DetailSeekBar(context: Context, attrs: AttributeSet?) : LinearLayoutCompat
}
init {
gravity = Gravity.CENTER_VERTICAL
View.inflate(context, R.layout.view_detail_seek_bar, this)
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.DetailSeekBar)

@ -2,27 +2,23 @@ package io.legado.app.ui.widget
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.Menu
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.MenuRes
import androidx.annotation.StringRes
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.appcompat.widget.PopupMenu
import io.legado.app.R
import io.legado.app.utils.dp
import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.view_select_action_bar.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class SelectActionBar(context: Context, attrs: AttributeSet?) : LinearLayoutCompat(context, attrs) {
class SelectActionBar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
private var callBack: CallBack? = null
private var selMenu: PopupMenu? = null
init {
setBackgroundResource(R.color.background_menu)
setPadding(16.dp, 6.dp, 16.dp, 6.dp)
gravity = Gravity.CENTER_VERTICAL
View.inflate(context, R.layout.view_select_action_bar, this)
cb_selected_all.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) {

@ -1,17 +1,31 @@
package io.legado.app.ui.widget.prefs
import android.content.Context
import android.content.ContextWrapper
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.preference.ListPreference
import androidx.preference.PreferenceViewHolder
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.utils.getCompatDrawable
import kotlinx.android.synthetic.main.dialog_recycler_view.*
import kotlinx.android.synthetic.main.item_icon_preference.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference(context, attrs) {
private var iconNames: Array<CharSequence>
private val mEntryDrawables = arrayListOf<Drawable>()
init {
@ -19,19 +33,17 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.IconListPreference, 0, 0)
val drawables: Array<CharSequence>
drawables = try {
iconNames = try {
a.getTextArray(R.styleable.IconListPreference_icons)
} finally {
a.recycle()
}
for (drawable in drawables) {
for (iconName in iconNames) {
val resId = context.resources
.getIdentifier(drawable.toString(), "mipmap", context.packageName)
val d = context.resources.getDrawable(resId)
mEntryDrawables.add(d)
.getIdentifier(iconName.toString(), "mipmap", context.packageName)
val d = context.getCompatDrawable(resId)
mEntryDrawables.add(d!!)
}
}
@ -39,17 +51,118 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
super.onBindViewHolder(holder)
holder?.itemView?.findViewById<ImageView>(R.id.preview)?.let {
val selectedIndex = findIndexOfValue(value)
val drawable = mEntryDrawables[selectedIndex]
it.setImageDrawable(drawable)
if (selectedIndex > 0) {
val drawable = mEntryDrawables[selectedIndex]
it.setImageDrawable(drawable)
}
}
}
override fun onClick() {
getActivity()?.let {
IconDialog().apply {
val args = Bundle()
args.putCharSequenceArray("entries", entries)
args.putCharSequenceArray("entryValues", entryValues)
args.putCharSequenceArray("iconNames", iconNames)
arguments = args
onChanged = { value ->
this@IconListPreference.value = value
}
}.show(it.supportFragmentManager, "iconDialog")
}
}
private fun getActivity(): FragmentActivity? {
val context = context
if (context is FragmentActivity) {
return context
} else if (context is ContextWrapper) {
val baseContext = context.baseContext
if (baseContext is FragmentActivity) {
return baseContext
}
}
return null
}
}
inner class IconDialog : DialogFragment() {
class IconDialog : DialogFragment() {
var onChanged: ((value: String) -> Unit)? = null
var entries: Array<CharSequence>? = null
var entryValues: Array<CharSequence>? = null
var iconNames: Array<CharSequence>? = null
override fun onStart() {
super.onStart()
val dm = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(dm)
dialog?.window?.setLayout(
(dm.widthPixels * 0.9).toInt(),
(dm.heightPixels * 0.9).toInt()
)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_recycler_view, container)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
tool_bar.setTitle(R.string.change_icon)
recycler_view.layoutManager = LinearLayoutManager(requireContext())
val adapter = Adapter(requireContext())
recycler_view.adapter = adapter
arguments?.let {
entries = it.getCharSequenceArray("entries")!!
entryValues = it.getCharSequenceArray("entryValues")!!
iconNames = it.getCharSequenceArray("iconNames")!!
entryValues?.let { values ->
adapter.setItems(values.toList())
}
}
}
inner class Adapter(context: Context) :
SimpleRecyclerAdapter<CharSequence>(context, R.layout.item_icon_preference) {
override fun convert(
holder: ItemViewHolder,
item: CharSequence,
payloads: MutableList<Any>
) {
with(holder.itemView) {
val index = findIndexOfValue(item.toString())
entries?.let {
label.text = it[index]
}
iconNames?.let {
val resId = context.resources
.getIdentifier(it[index].toString(), "mipmap", context.packageName)
val d = context.getCompatDrawable(resId)
icon.setImageDrawable(d)
}
onClick {
onChanged?.invoke(item.toString())
}
}
}
private fun findIndexOfValue(value: String?): Int {
entryValues?.let { values ->
for (i in values.indices.reversed()) {
if (values[i] == value) {
return i
}
}
}
return -1
}
}
}

@ -1,9 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:gravity="center_vertical"
android:elevation="2dp">
<io.legado.app.lib.theme.view.ATECheckBox
@ -47,4 +52,4 @@
android:visibility="gone"
tools:ignore="RtlHardcoded" />
</merge>
</LinearLayout>

@ -139,7 +139,7 @@
<string-array name="icons">
<item>@string/icon_main</item>
<item>@string/icon_book</item>
<item>@string/icon_1</item>
</string-array>
<string-array name="chinese_mode">

@ -13,8 +13,8 @@
<string name="pk_download_path">downloadPath</string>
<string name="pk_check_update">checkUpdate</string>
<string name="icon_main">ic_launcher_round</string>
<string name="icon_book">book_launcher_round</string>
<string name="icon_main">ic_launcher</string>
<string name="icon_1">ic_launcher_1</string>
<string name="pv_user_agent">Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.2357.134 Safari/537.36</string>

@ -2,6 +2,15 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<io.legado.app.ui.widget.prefs.IconListPreference
android:defaultValue="icon_main"
android:key="launcherIcon"
android:title="@string/change_icon"
android:entryValues="@array/icons"
android:entries="@array/icon_names"
app:icons="@array/icons"
app:iconSpaceReserved="false" />
<io.legado.app.lib.theme.prefs.ATESwitchPreference
android:defaultValue="true"
android:key="transparentStatusBar"

Loading…
Cancel
Save