pull/83/head
kunfei 5 years ago
parent d940849dec
commit e9d8cbf87b
  1. 34
      app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt
  2. 47
      app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt

@ -32,7 +32,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
private val requestCodeSelectFolder = 342
private var rootDoc: DocumentFile? = null
private val subDocs = arrayListOf<DocumentFile>()
private lateinit var importBookAdapter: ImportBookAdapter
private lateinit var adapter: ImportBookAdapter
private var localUriLiveData: LiveData<List<String>>? = null
override val viewModel: ImportBookViewModel
@ -59,8 +59,8 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
private fun initView() {
recycler_view.layoutManager = LinearLayoutManager(this)
importBookAdapter = ImportBookAdapter(this, this)
recycler_view.adapter = importBookAdapter
adapter = ImportBookAdapter(this, this)
recycler_view.adapter = adapter
rotate_loading.loadingColor = accentColor
}
@ -74,13 +74,16 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
btn_delete.onClick {
}
cb_selected_all.onClick {
adapter.selectAll(cb_selected_all.isChecked)
}
}
private fun initData() {
localUriLiveData?.removeObservers(this)
localUriLiveData = App.db.bookDao().observeLocalUri()
localUriLiveData?.observe(this, Observer {
importBookAdapter.upBookHas(it)
adapter.upBookHas(it)
})
}
@ -104,8 +107,8 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
path = path + doc.name + File.separator
}
tv_path.text = path
importBookAdapter.selectedUris.clear()
importBookAdapter.clearItems()
adapter.selectedUris.clear()
adapter.clearItems()
rotate_loading.show()
launch(IO) {
val docList = DocumentUtils.listFiles(
@ -123,7 +126,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
docList.sortWith(compareBy({ !it.isDir }, { it.name }))
withContext(Main) {
rotate_loading.hide()
importBookAdapter.setItems(docList)
adapter.setData(docList)
}
}
}
@ -179,16 +182,29 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
}
override fun upCountView() {
if (importBookAdapter.selectedUris.isEmpty()) {
if (adapter.selectedUris.isEmpty()) {
btn_add_book.setText(R.string.nb_file_add_shelf)
//设置某些按钮的是否可点击
setMenuClickable(false)
} else {
btn_add_book.text =
getString(R.string.nb_file_add_shelves, importBookAdapter.selectedUris.size)
getString(R.string.nb_file_add_shelves, adapter.selectedUris.size)
//设置某些按钮的是否可点击
setMenuClickable(true)
}
if (adapter.checkableCount == 0) {
cb_selected_all.isChecked = false
} else {
cb_selected_all.isChecked = adapter.selectedUris.size >= adapter.checkableCount
}
//重置全选的文字
if (cb_selected_all.isChecked) {
cb_selected_all.setText(R.string.cancel)
} else {
cb_selected_all.setText(R.string.select_all)
}
}
private fun setMenuClickable(isClickable: Boolean) {

@ -14,12 +14,45 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ImportBookAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<DocItem>(context, R.layout.item_import_book) {
var selectedUris = linkedSetOf<String>()
private var localUri = arrayListOf<String>()
var checkableCount = 0
private var bookshelf = arrayListOf<String>()
fun upBookHas(uriList: List<String>) {
localUri.clear()
localUri.addAll(uriList)
bookshelf.clear()
bookshelf.addAll(uriList)
notifyDataSetChanged()
upCheckableCount()
}
fun setData(data: List<DocItem>) {
setItems(data)
upCheckableCount()
}
private fun upCheckableCount() {
checkableCount = 0
getItems().forEach {
if (!it.isDir && !bookshelf.contains(it.uri.toString())) {
checkableCount++
}
}
callBack.upCountView()
}
fun selectAll(selectAll: Boolean) {
if (selectAll) {
getItems().forEach {
if (!it.isDir && !bookshelf.contains(it.uri.toString())) {
selectedUris.add(it.uri.toString())
}
}
notifyDataSetChanged()
callBack.upCountView()
} else {
selectedUris.clear()
notifyDataSetChanged()
callBack.upCountView()
}
}
override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) {
@ -29,8 +62,9 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
iv_icon.visible()
cb_select.invisible()
ll_brief.gone()
cb_select.isChecked = false
} else {
if (localUri.contains(item.uri.toString())) {
if (bookshelf.contains(item.uri.toString())) {
iv_icon.setImageResource(R.drawable.ic_book_has)
iv_icon.visible()
cb_select.invisible()
@ -42,19 +76,20 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
tv_tag.text = item.name.substringAfterLast(".")
tv_size.text = StringUtils.toSize(item.size)
tv_date.text = AppConst.DATE_FORMAT.format(item.date)
cb_select.isChecked = selectedUris.contains(item.uri.toString())
}
tv_name.text = item.name
cb_select.isChecked = selectedUris.contains(item.uri.toString())
onClick {
if (item.isDir) {
callBack.nextDoc(DocumentFile.fromSingleUri(context, item.uri)!!)
} else {
} else if (!bookshelf.contains(item.uri.toString())) {
cb_select.isChecked = !cb_select.isChecked
if (cb_select.isChecked) {
selectedUris.add(item.uri.toString())
} else {
selectedUris.remove(item.uri.toString())
}
callBack.upCountView()
}
}
}

Loading…
Cancel
Save