diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index 53a28233d..c8b54ffe1 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -206,7 +206,13 @@ abstract class AppDatabase : RoomDatabase() { private val migration_24_25 = object : Migration(24, 25) { 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`)) + """ + ) } } } diff --git a/app/src/main/java/io/legado/app/data/dao/SourceSubDao.kt b/app/src/main/java/io/legado/app/data/dao/SourceSubDao.kt index 5a62b1475..753b714f3 100644 --- a/app/src/main/java/io/legado/app/data/dao/SourceSubDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SourceSubDao.kt @@ -7,16 +7,21 @@ import io.legado.app.data.entities.SourceSub @Dao interface SourceSubDao { - @get:Query("select * from sourceSubs") + @get:Query("select * from sourceSubs order by customOrder") val all: List - @Query("select * from sourceSubs") + @Query("select * from sourceSubs order by customOrder") fun observeAll(): LiveData> + @get:Query("select customOrder from sourceSubs order by customOrder limit 0,1") + val maxOrder: Int + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg sourceSub: SourceSub) @Delete fun delete(vararg sourceSub: SourceSub) + @Update + fun update(vararg sourceSub: SourceSub) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/SourceSub.kt b/app/src/main/java/io/legado/app/data/entities/SourceSub.kt index 401d9a515..ac2926eb6 100644 --- a/app/src/main/java/io/legado/app/data/entities/SourceSub.kt +++ b/app/src/main/java/io/legado/app/data/entities/SourceSub.kt @@ -9,7 +9,8 @@ data class SourceSub( val id: Long = System.currentTimeMillis(), var name: String = "", var url: String = "", - var type: Int = 0 + var type: Int = 0, + var customOrder: Int = 0 ) { fun setType(type: Type) { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index b811de086..41a15225f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -118,8 +118,7 @@ class RssSourceActivity : VMBaseActivity(), private fun initView() { adapter = SourceSubAdapter(this, this) binding.recyclerView.adapter = adapter + val itemTouchCallback = ItemTouchCallback(adapter) + itemTouchCallback.isCanDrag = true + ItemTouchHelper(itemTouchCallback).attachToRecyclerView(binding.recyclerView) } private fun initData() { @@ -99,4 +104,21 @@ class SourceSubActivity : BaseActivity(), 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()) + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/subscription/SourceSubAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/subscription/SourceSubAdapter.kt index c5b85ebee..830601cac 100644 --- a/app/src/main/java/io/legado/app/ui/rss/subscription/SourceSubAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/subscription/SourceSubAdapter.kt @@ -4,16 +4,19 @@ import android.content.Context import android.view.View import android.view.ViewGroup import android.widget.PopupMenu +import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.data.entities.SourceSub import io.legado.app.databinding.ItemSourceSubBinding +import io.legado.app.ui.widget.recycler.ItemTouchCallback import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class SourceSubAdapter(context: Context, val callBack: Callback) : - SimpleRecyclerAdapter(context) { - + SimpleRecyclerAdapter(context), + ItemTouchCallback.Callback { override fun convert( holder: ItemViewHolder, @@ -55,10 +58,40 @@ class SourceSubAdapter(context: Context, val callBack: Callback) : 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() + + override fun onClearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (movedItems.isNotEmpty()) { + callBack.updateSourceSub(*movedItems.toTypedArray()) + movedItems.clear() + } + } + interface Callback { fun openSubscription(sourceSub: SourceSub) fun editSubscription(sourceSub: SourceSub) fun delSubscription(sourceSub: SourceSub) + fun updateSourceSub(vararg sourceSub: SourceSub) + fun upOrder() } } \ No newline at end of file