From f417cd6808ed1d44d56e5d8e29cc5a4822f23a41 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 31 Aug 2019 19:22:42 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/BookSourceDao.kt | 4 +++ .../app/model/analyzeRule/AnalyzeByJSoup.kt | 2 +- .../app/ui/booksource/BookSourceActivity.kt | 27 ++++++++++++++++++- .../ui/changesource/ChangeSourceViewModel.kt | 2 +- .../ui/readbook/config/BgTextConfigDialog.kt | 2 +- app/src/main/res/menu/book_source.xml | 2 +- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index f39599888..bbacd2c9a 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -1,5 +1,6 @@ package io.legado.app.data.dao +import androidx.lifecycle.LiveData import androidx.paging.DataSource import androidx.room.* import io.legado.app.data.entities.BookSource @@ -13,6 +14,9 @@ interface BookSourceDao { @Query("select * from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey order by customOrder asc") fun observeSearch(searchKey: String = ""): DataSource.Factory + @Query("select bookSourceGroup from book_sources") + fun observeGroup(): LiveData> + @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey") fun searchIsEnable(searchKey: String = ""): List diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt index b937abcb6..570262f17 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt @@ -114,7 +114,7 @@ class AnalyzeByJSoup { } if (results.size > 0) { if ("%" == elementsType) { - for (i in 0 until results[0].size) { + for (i in results[0].indices) { for (temp in results) { if (i < temp.size) { textS.add(temp[i]) diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt index c1a0c6e36..d2329cf27 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt @@ -4,6 +4,7 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.view.SubMenu import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData import androidx.lifecycle.Observer @@ -22,6 +23,7 @@ import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.sourceedit.SourceEditActivity import io.legado.app.utils.getViewModel +import io.legado.app.utils.splitNotBlank import kotlinx.android.synthetic.main.activity_book_source.* import kotlinx.android.synthetic.main.view_search.* import kotlinx.android.synthetic.main.view_title_bar.* @@ -36,9 +38,11 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity override val viewModel: BookSourceViewModel get() = getViewModel(BookSourceViewModel::class.java) + private val qrRequestCode = 101 private lateinit var adapter: BookSourceAdapter private var bookSourceLiveDate: LiveData>? = null - private val qrRequestCode = 101 + private var groups = hashSetOf() + private var groupMenu: SubMenu? = null override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) @@ -52,6 +56,12 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity return super.onCompatCreateOptionsMenu(menu) } + override fun onPrepareOptionsMenu(menu: Menu?): Boolean { + groupMenu = menu?.findItem(R.id.menu_group)?.subMenu + upGroupMenu() + return super.onPrepareOptionsMenu(menu) + } + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_add_book_source -> { @@ -106,6 +116,21 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity val dataFactory = App.db.bookSourceDao().observeSearch("%$searchKey%") bookSourceLiveDate = LivePagedListBuilder(dataFactory, 2000).build() bookSourceLiveDate?.observe(this, Observer { adapter.submitList(it) }) + + App.db.bookSourceDao().observeGroup().observe(this, Observer { + groups.clear() + it.map { group -> + groups.addAll(group.splitNotBlank(",", ";")) + } + upGroupMenu() + }) + } + + private fun upGroupMenu() { + groupMenu?.removeGroup(R.id.source_group) + groups.map { + groupMenu?.add(R.id.source_group, Menu.NONE, Menu.NONE, it) + } } override fun onQueryTextChange(newText: String?): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 938424fe2..df0caee90 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -18,7 +18,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio var callBack: CallBack? = null var name: String = "" var author: String = "" - var screenKey: String = "" + private var screenKey: String = "" private val searchBooks = linkedSetOf() fun initData() { 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 92b8108d0..bd67d8352 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 @@ -143,7 +143,7 @@ class BgTextConfigDialog : DialogFragment() { ImageLoader.load(context, context.assets.open("bg/$item").readBytes()) .centerCrop() .setAsBitmap(iv_bg) - tv_name.text = item.substring(0, item.lastIndexOf(".")) + tv_name.text = item.substringBeforeLast(".") this.onClick { ReadBookConfig.getConfig().setBg(1, item) ReadBookConfig.upBg() diff --git a/app/src/main/res/menu/book_source.xml b/app/src/main/res/menu/book_source.xml index ec7a62b18..6660208ae 100644 --- a/app/src/main/res/menu/book_source.xml +++ b/app/src/main/res/menu/book_source.xml @@ -11,7 +11,7 @@ tools:ignore="AlwaysShowAction" />