From 67f882f2bc95208e5712d82b024cb14d8ac93eb9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 11 Feb 2020 13:43:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B9=A6=E6=BA=90=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/source/manage/BookSourceActivity.kt | 74 +++++-------- .../legado/app/ui/widget/SelectActionBar.kt | 101 ++++++++++++++++++ .../io/legado/app/utils/ContextExtensions.kt | 3 + .../main/res/layout/activity_book_source.xml | 55 +--------- .../res/layout/view_select_action_bar.xml | 50 +++++++++ 5 files changed, 181 insertions(+), 102 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/SelectActionBar.kt create mode 100644 app/src/main/res/layout/view_select_action_bar.xml diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index b5575c869..702973880 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -7,7 +7,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.SubMenu -import android.widget.PopupMenu +import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.lifecycle.Observer @@ -33,11 +33,11 @@ import io.legado.app.service.help.CheckSource import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.qrcode.QrCodeActivity +import io.legado.app.ui.widget.SelectActionBar import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_book_source.* import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_search.* -import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast @@ -57,7 +57,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity private var bookSourceLiveDate: LiveData>? = null private var groups = linkedSetOf() private var groupMenu: SubMenu? = null - private lateinit var selMenu: PopupMenu override fun onActivityCreated(savedInstanceState: Bundle?) { initUriScheme() @@ -65,7 +64,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity initSearchView() initLiveDataBookSource() initLiveDataGroup() - initViewEvent() + initSelectActionBar() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -156,23 +155,28 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity }) } - private fun initViewEvent() { - selMenu = PopupMenu(this, iv_menu_more) - selMenu.inflate(R.menu.book_source_sel) - selMenu.setOnMenuItemClickListener(this) - cb_selected_all.onClick { - if (adapter.getSelection().size == adapter.getActualItemCount()) { + private fun initSelectActionBar() { + select_action_bar.setMainActionText(R.string.delete) + select_action_bar.inflateMenu(R.menu.book_source_sel) + select_action_bar.setOnMenuItemClickListener(this) + select_action_bar.setCallBack(object : SelectActionBar.CallBack { + override fun selectAll(selectAll: Boolean) { + if (selectAll) { + adapter.selectAll() + } else { + adapter.revertSelection() + } + } + + override fun revertSelection() { adapter.revertSelection() - } else { - adapter.selectAll() } - } - btn_revert_selection.onClick { - adapter.revertSelection() - } - iv_menu_more.onClick { - selMenu.show() - } + + override fun onClickMainAction() { + super.onClickMainAction() + } + }) + } override fun onMenuItemClick(item: MenuItem?): Boolean { @@ -259,37 +263,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity } override fun upCountView() { - val selectCount = adapter.getSelection().size - if (selectCount == 0) { - cb_selected_all.isChecked = false - } else { - cb_selected_all.isChecked = selectCount >= adapter.getActualItemCount() - } - - //重置全选的文字 - if (cb_selected_all.isChecked) { - cb_selected_all.text = getString( - R.string.select_cancel_count, - selectCount, - adapter.getActualItemCount() - ) - } else { - cb_selected_all.text = getString( - R.string.select_all_count, - selectCount, - adapter.getActualItemCount() - ) - } - setMenuClickable(selectCount > 0) - } - - private fun setMenuClickable(isClickable: Boolean) { - //设置是否可删除 - btn_delete.isEnabled = isClickable - btn_delete.isClickable = isClickable - //设置是否可添加书籍 - btn_revert_selection.isEnabled = isClickable - btn_revert_selection.isClickable = isClickable + select_action_bar.upCountView(adapter.getSelection().size, adapter.getActualItemCount()) } override fun onFilePicked(requestCode: Int, currentPath: String) { diff --git a/app/src/main/java/io/legado/app/ui/widget/SelectActionBar.kt b/app/src/main/java/io/legado/app/ui/widget/SelectActionBar.kt new file mode 100644 index 000000000..4597abd3b --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/SelectActionBar.kt @@ -0,0 +1,101 @@ +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 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) { + 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) { + callBack?.selectAll(isChecked) + } + } + btn_revert_selection.onClick { callBack?.revertSelection() } + btn_select_action_main.onClick { callBack?.onClickMainAction() } + iv_menu_more.onClick { selMenu?.show() } + } + + fun setMainActionText(text: String) { + btn_select_action_main.text = text + btn_select_action_main.visible() + } + + fun setMainActionText(@StringRes id: Int) { + btn_select_action_main.setText(id) + btn_select_action_main.visible() + } + + fun inflateMenu(@MenuRes resId: Int): Menu? { + selMenu = PopupMenu(context, iv_menu_more) + selMenu?.inflate(resId) + iv_menu_more.visible() + return selMenu?.menu + } + + fun setCallBack(callBack: CallBack) { + this.callBack = callBack + } + + fun setOnMenuItemClickListener(listener: PopupMenu.OnMenuItemClickListener) { + selMenu?.setOnMenuItemClickListener(listener) + } + + fun upCountView(selectCount: Int, allCount: Int) { + if (selectCount == 0) { + cb_selected_all.isChecked = false + } else { + cb_selected_all.isChecked = selectCount >= allCount + } + + //重置全选的文字 + if (cb_selected_all.isChecked) { + cb_selected_all.text = context.getString( + R.string.select_cancel_count, + selectCount, + allCount + ) + } else { + cb_selected_all.text = context.getString( + R.string.select_all_count, + selectCount, + allCount + ) + } + setMenuClickable(selectCount > 0) + } + + private fun setMenuClickable(isClickable: Boolean) { + btn_revert_selection.isEnabled = isClickable + btn_revert_selection.isClickable = isClickable + btn_select_action_main.isEnabled = isClickable + btn_select_action_main.isClickable = isClickable + } + + interface CallBack { + + fun selectAll(selectAll: Boolean) + + fun revertSelection() + + fun onClickMainAction() {} + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index d5fc85fdf..de1eedaa8 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -65,6 +65,9 @@ fun Context.getCompatDrawable(@DrawableRes id: Int): Drawable? = ContextCompat.g fun Context.getCompatColorStateList(@ColorRes id: Int): ColorStateList? = ContextCompat.getColorStateList(this, id) +/** + * 系统息屏时间 + */ fun Context.getScreenOffTime(): Int { var screenOffTime = 0 try { diff --git a/app/src/main/res/layout/activity_book_source.xml b/app/src/main/res/layout/activity_book_source.xml index 3d07dd15b..090be7b00 100644 --- a/app/src/main/res/layout/activity_book_source.xml +++ b/app/src/main/res/layout/activity_book_source.xml @@ -3,7 +3,6 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" - xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"> - - - - - - - - - - - - + android:layout_height="wrap_content" /> \ No newline at end of file diff --git a/app/src/main/res/layout/view_select_action_bar.xml b/app/src/main/res/layout/view_select_action_bar.xml new file mode 100644 index 000000000..6a2e67e6e --- /dev/null +++ b/app/src/main/res/layout/view_select_action_bar.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + \ No newline at end of file