diff --git a/app/src/main/java/io/legado/app/ui/readbook/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/readbook/config/BgTextConfigDialog.kt index be505737d..73da06e09 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/config/BgTextConfigDialog.kt @@ -1,6 +1,9 @@ package io.legado.app.ui.readbook.config +import android.annotation.SuppressLint +import android.content.Context import android.content.DialogInterface +import android.content.Intent import android.graphics.Color import android.os.Bundle import android.util.DisplayMetrics @@ -9,11 +12,20 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.DialogFragment +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.jaredrummler.android.colorpicker.ColorPickerDialog import io.legado.app.R +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.help.ImageLoader import io.legado.app.help.ReadBookConfig +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat import io.legado.app.ui.readbook.Help +import io.legado.app.utils.getCompatColor import kotlinx.android.synthetic.main.dialog_read_bg_text.* +import kotlinx.android.synthetic.main.item_bg_image.view.* import org.jetbrains.anko.sdk27.listeners.onClick class BgTextConfigDialog : DialogFragment() { @@ -23,6 +35,9 @@ class BgTextConfigDialog : DialogFragment() { const val BG_COLOR = 122 } + private val ResultSelectBg = 123 + private lateinit var adapter: BgAdapter + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -58,8 +73,23 @@ class BgTextConfigDialog : DialogFragment() { ReadBookConfig.save() } + @SuppressLint("InflateParams") private fun initData() = with(ReadBookConfig.getConfig()) { sw_dark_status_icon.isChecked = statusIconDark() + adapter = BgAdapter(requireContext()) + recycler_view.layoutManager = + LinearLayoutManager(requireContext(), RecyclerView.HORIZONTAL, false) + recycler_view.adapter = adapter + val headerView = LayoutInflater.from(requireContext()) + .inflate(R.layout.item_bg_image, recycler_view, false) + adapter.addHeaderView(headerView) + headerView.tv_name.text = getString(R.string.select_image) + headerView.iv_bg.setImageResource(R.drawable.ic_image) + headerView.iv_bg.setColorFilter(getCompatColor(R.color.tv_text_default)) + headerView.onClick { selectImage() } + requireContext().assets.list("bg/")?.let { + adapter.setItems(it.toList()) + } } private fun initView() = with(ReadBookConfig.getConfig()) { @@ -87,5 +117,32 @@ class BgTextConfigDialog : DialogFragment() { } } + private fun selectImage() { + PermissionsCompat.Builder(this) + .addPermissions(Permissions.READ_EXTERNAL_STORAGE, Permissions.WRITE_EXTERNAL_STORAGE) + .rationale(R.string.bg_image_per) + .onGranted { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.type = "image/*" + startActivityForResult(intent, ResultSelectBg) + Unit + } + .request() + } + + class BgAdapter(context: Context) : + SimpleRecyclerAdapter(context, R.layout.item_bg_image) { + + override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { + with(holder.itemView) { + ImageLoader.load(context, context.assets.open("bg/$item").readBytes()) + .centerCrop() + .setAsFile(iv_bg) + tv_name.text = item + } + } + + } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_image.xml b/app/src/main/res/drawable/ic_image.xml new file mode 100644 index 000000000..bfd8e5c65 --- /dev/null +++ b/app/src/main/res/drawable/ic_image.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_read_bg_text.xml b/app/src/main/res/layout/dialog_read_bg_text.xml index b1d5df162..e5618b955 100644 --- a/app/src/main/res/layout/dialog_read_bg_text.xml +++ b/app/src/main/res/layout/dialog_read_bg_text.xml @@ -77,12 +77,10 @@ diff --git a/app/src/main/res/layout/item_bg_image.xml b/app/src/main/res/layout/item_bg_image.xml new file mode 100644 index 000000000..c3e4a81e0 --- /dev/null +++ b/app/src/main/res/layout/item_bg_image.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7e93d73f8..95a6d72ef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -489,5 +489,6 @@ 背景图片 背景颜色 文字颜色 + 选择图片