@ -2,6 +2,7 @@ package io.legado.app.ui.book.source.manage
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.os.Bundle
import android.view.Menu
@ -9,18 +10,23 @@ import android.view.MenuItem
import android.view.SubMenu
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.SearchView
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
import io.legado.app.BuildConfig
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.AppPattern
import io.legado.app.data.entities.BookSource
import io.legado.app.help.IntentDataHelp
import io.legado.app.lib.dialogs.*
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.okButton
import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.service.help.CheckSource
@ -41,6 +47,7 @@ import kotlinx.android.synthetic.main.view_search.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast
import java.io.File
import java.text.Collator
@ -60,6 +67,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
private var groups = linkedSetOf < String > ( )
private var groupMenu : SubMenu ? = null
private var sort = 0
private var sortAscending = 0
override fun onActivityCreated ( savedInstanceState : Bundle ? ) {
initRecyclerView ( )
@ -86,6 +94,21 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
when ( item . itemId ) {
R . id . menu _add _book _source -> startActivity < BookSourceEditActivity > ( )
R . id . menu _import _source _qr -> startActivityForResult < QrCodeActivity > ( qrRequestCode )
R . id . menu _share _source -> {
try {
val json = GSON . toJson ( adapter . getSelection ( ) )
val intent = Intent ( Intent . ACTION _SEND )
val file = FileUtils . createFileWithReplace ( " $filesDir /shareBookSource.json " )
file . writeText ( json )
val fileUri = FileProvider . getUriForFile ( this , BuildConfig . APPLICATION _ID + " .fileProvider " , file )
intent . type = " text/* "
intent . putExtra ( Intent . EXTRA _STREAM , fileUri )
intent . flags = Intent . FLAG _GRANT _READ _URI _PERMISSION
startActivity ( Intent . createChooser ( intent , getString ( R . string . share _selected _source ) ) )
} catch ( e : ActivityNotFoundException ) {
e . printStackTrace ( )
}
}
R . id . menu _group _manage ->
GroupManageDialog ( ) . show ( supportFragmentManager , " groupManage " )
R . id . menu _import _source _local -> FilePicker
@ -93,22 +116,27 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
R . id . menu _import _source _onLine -> showImportDialog ( )
R . id . menu _sort _manual -> {
item . isChecked = true
sort = 0
sortCheck ( 0 )
initLiveDataBookSource ( search _view . query ?. toString ( ) )
}
R . id . menu _sort _auto -> {
item . isChecked = true
sort = 2
sortCheck ( 1 )
initLiveDataBookSource ( search _view . query ?. toString ( ) )
}
R . id . menu _sort _pin _yin -> {
item . isChecked = true
sort = 3
sortCheck ( 2 )
initLiveDataBookSource ( search _view . query ?. toString ( ) )
}
R . id . menu _sort _url -> {
item . isChecked = true
sort = 4
sortCheck ( 3 )
initLiveDataBookSource ( search _view . query ?. toString ( ) )
}
R . id . menu _sort _time -> {
item . isChecked = true
sortCheck ( 4 )
initLiveDataBookSource ( search _view . query ?. toString ( ) )
}
R . id . menu _enabled _group -> {
@ -168,18 +196,38 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
}
}
bookSourceLiveDate ?. observe ( this , { data ->
val sourceList = when ( sort ) {
1 -> data . sortedBy { it . weight }
2 -> data . sortedBy { it . bookSourceName }
3 -> data . sortedBy { it . bookSourceUrl }
else -> data
val sourceList = when ( sortAscending % 2 ) {
0 -> when ( sort ) {
1 -> data . sortedBy { it . weight }
2 -> data . sortedBy { it . bookSourceName }
3 -> data . sortedBy { it . bookSourceUrl }
4 -> data . sortedByDescending { it . lastUpdateTime }
else -> data
}
else -> when ( sort ) {
1 -> data . sortedByDescending { it . weight }
2 -> data . sortedByDescending { it . bookSourceName }
3 -> data . sortedByDescending { it . bookSourceUrl }
4 -> data . sortedBy { it . lastUpdateTime }
else -> data . reversed ( )
}
}
recycler _view . scrollToPosition ( 0 )
val diffResult = DiffUtil
. calculateDiff ( DiffCallBack ( ArrayList ( adapter . getItems ( ) ) , sourceList ) )
adapter . setItems ( sourceList , diffResult )
upCountView ( )
} )
}
private fun sortCheck ( sortId : Int ) {
if ( sort == sortId ) {
sortAscending += 1
}
else {
sortAscending = 0
sort = sortId
}
}
private fun initLiveDataGroup ( ) {
App . db . bookSourceDao ( ) . liveGroup ( ) . observe ( this , {
@ -226,12 +274,12 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
R . id . menu _disable _selection -> viewModel . disableSelection ( adapter . getSelection ( ) )
R . id . menu _enable _explore -> viewModel . enableSelectExplore ( adapter . getSelection ( ) )
R . id . menu _disable _explore -> viewModel . disableSelectExplore ( adapter . getSelection ( ) )
R . id . menu _export _selection -> FilePicker . selectFolder ( this , exportRequestCode )
R . id . menu _check _source -> checkSource ( )
R . id . menu _top _sel -> viewModel . topSource ( * adapter . getSelection ( ) . toTypedArray ( ) )
R . id . menu _bottom _sel -> viewModel . bottomSource ( * adapter . getSelection ( ) . toTypedArray ( ) )
R . id . menu _add _group -> selectionAddToGroups ( )
R . id . menu _remove _group -> selectionRemoveFromGroups ( )
R . id . menu _export _selection -> FilePicker . selectFolder ( this , exportRequestCode )
}
return true
}