添加源地址订阅功能

pull/517/head
gedoor 4 years ago
parent ecc4f2f2df
commit cb4be31602
  1. 8
      app/src/main/java/io/legado/app/data/AppDatabase.kt
  2. 9
      app/src/main/java/io/legado/app/data/dao/SourceSubDao.kt
  3. 3
      app/src/main/java/io/legado/app/data/entities/SourceSub.kt
  4. 5
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  5. 22
      app/src/main/java/io/legado/app/ui/rss/subscription/SourceSubActivity.kt
  6. 37
      app/src/main/java/io/legado/app/ui/rss/subscription/SourceSubAdapter.kt

@ -206,7 +206,13 @@ abstract class AppDatabase : RoomDatabase() {
private val migration_24_25 = object : Migration(24, 25) { private val migration_24_25 = object : Migration(24, 25) {
override fun migrate(database: SupportSQLiteDatabase) { override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS `sourceSubs` (`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, `type` INTEGER NOT NULL, PRIMARY KEY(`id`))") database.execSQL(
"""
CREATE TABLE IF NOT EXISTS `sourceSubs`
(`id` INTEGER NOT NULL, `name` TEXT NOT NULL, `url` TEXT NOT NULL, `type` INTEGER NOT NULL, `customOrder` INTEGER NOT NULL,
PRIMARY KEY(`id`))
"""
)
} }
} }
} }

@ -7,16 +7,21 @@ import io.legado.app.data.entities.SourceSub
@Dao @Dao
interface SourceSubDao { interface SourceSubDao {
@get:Query("select * from sourceSubs") @get:Query("select * from sourceSubs order by customOrder")
val all: List<SourceSub> val all: List<SourceSub>
@Query("select * from sourceSubs") @Query("select * from sourceSubs order by customOrder")
fun observeAll(): LiveData<List<SourceSub>> fun observeAll(): LiveData<List<SourceSub>>
@get:Query("select customOrder from sourceSubs order by customOrder limit 0,1")
val maxOrder: Int
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg sourceSub: SourceSub) fun insert(vararg sourceSub: SourceSub)
@Delete @Delete
fun delete(vararg sourceSub: SourceSub) fun delete(vararg sourceSub: SourceSub)
@Update
fun update(vararg sourceSub: SourceSub)
} }

@ -9,7 +9,8 @@ data class SourceSub(
val id: Long = System.currentTimeMillis(), val id: Long = System.currentTimeMillis(),
var name: String = "", var name: String = "",
var url: String = "", var url: String = "",
var type: Int = 0 var type: Int = 0,
var customOrder: Int = 0
) { ) {
fun setType(type: Type) { fun setType(type: Type) {

@ -118,8 +118,7 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
binding.recyclerView.addItemDecoration(VerticalDivider(this)) binding.recyclerView.addItemDecoration(VerticalDivider(this))
adapter = RssSourceAdapter(this, this) adapter = RssSourceAdapter(this, this)
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
val itemTouchCallback = ItemTouchCallback(adapter)
itemTouchCallback.isCanDrag = true
val dragSelectTouchHelper: DragSelectTouchHelper = val dragSelectTouchHelper: DragSelectTouchHelper =
DragSelectTouchHelper(adapter.initDragSelectTouchHelperCallback()).setSlideArea(16, 50) DragSelectTouchHelper(adapter.initDragSelectTouchHelperCallback()).setSlideArea(16, 50)
dragSelectTouchHelper.attachToRecyclerView(binding.recyclerView) dragSelectTouchHelper.attachToRecyclerView(binding.recyclerView)
@ -127,6 +126,8 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
dragSelectTouchHelper.activeSlideSelect() dragSelectTouchHelper.activeSlideSelect()
// Note: need judge selection first, so add ItemTouchHelper after it. // Note: need judge selection first, so add ItemTouchHelper after it.
val itemTouchCallback = ItemTouchCallback(adapter)
itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView)
} }

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.ItemTouchHelper
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
@ -13,6 +14,7 @@ import io.legado.app.databinding.DialogSourceSubEditBinding
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.ui.association.ImportBookSourceActivity import io.legado.app.ui.association.ImportBookSourceActivity
import io.legado.app.ui.association.ImportRssSourceActivity import io.legado.app.ui.association.ImportRssSourceActivity
import io.legado.app.ui.widget.recycler.ItemTouchCallback
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
@ -47,6 +49,9 @@ class SourceSubActivity : BaseActivity<ActivitySourceSubBinding>(),
private fun initView() { private fun initView() {
adapter = SourceSubAdapter(this, this) adapter = SourceSubAdapter(this, this)
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
val itemTouchCallback = ItemTouchCallback(adapter)
itemTouchCallback.isCanDrag = true
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView)
} }
private fun initData() { private fun initData() {
@ -99,4 +104,21 @@ class SourceSubActivity : BaseActivity<ActivitySourceSubBinding>(),
App.db.sourceSubDao().delete(sourceSub) App.db.sourceSubDao().delete(sourceSub)
} }
} }
override fun updateSourceSub(vararg sourceSub: SourceSub) {
launch(IO) {
App.db.sourceSubDao().update(*sourceSub)
}
}
override fun upOrder() {
launch(IO) {
val sourceSubs = App.db.sourceSubDao().all
for ((index: Int, sourceSub: SourceSub) in sourceSubs.withIndex()) {
sourceSub.customOrder = index + 1
}
App.db.sourceSubDao().update(*sourceSubs.toTypedArray())
}
}
} }

@ -4,16 +4,19 @@ import android.content.Context
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.PopupMenu import android.widget.PopupMenu
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.SourceSub import io.legado.app.data.entities.SourceSub
import io.legado.app.databinding.ItemSourceSubBinding import io.legado.app.databinding.ItemSourceSubBinding
import io.legado.app.ui.widget.recycler.ItemTouchCallback
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.*
class SourceSubAdapter(context: Context, val callBack: Callback) : class SourceSubAdapter(context: Context, val callBack: Callback) :
SimpleRecyclerAdapter<SourceSub, ItemSourceSubBinding>(context) { SimpleRecyclerAdapter<SourceSub, ItemSourceSubBinding>(context),
ItemTouchCallback.Callback {
override fun convert( override fun convert(
holder: ItemViewHolder, holder: ItemViewHolder,
@ -55,10 +58,40 @@ class SourceSubAdapter(context: Context, val callBack: Callback) :
return ItemSourceSubBinding.inflate(inflater, parent, false) return ItemSourceSubBinding.inflate(inflater, parent, false)
} }
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) {
if (srcItem.customOrder == targetItem.customOrder) {
callBack.upOrder()
} else {
val srcOrder = srcItem.customOrder
srcItem.customOrder = targetItem.customOrder
targetItem.customOrder = srcOrder
movedItems.add(srcItem)
movedItems.add(targetItem)
}
}
Collections.swap(getItems(), srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
return true
}
private val movedItems = hashSetOf<SourceSub>()
override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
if (movedItems.isNotEmpty()) {
callBack.updateSourceSub(*movedItems.toTypedArray())
movedItems.clear()
}
}
interface Callback { interface Callback {
fun openSubscription(sourceSub: SourceSub) fun openSubscription(sourceSub: SourceSub)
fun editSubscription(sourceSub: SourceSub) fun editSubscription(sourceSub: SourceSub)
fun delSubscription(sourceSub: SourceSub) fun delSubscription(sourceSub: SourceSub)
fun updateSourceSub(vararg sourceSub: SourceSub)
fun upOrder()
} }
} }
Loading…
Cancel
Save