Skip to content

Commit 0848cc6

Browse files
committed
下载列表添加排序功能
1 parent 6f284aa commit 0848cc6

10 files changed

Lines changed: 1042 additions & 534 deletions

File tree

app/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ dependencies {
115115
implementation 'androidx.cardview:cardview:1.0.0'
116116
implementation "androidx.fragment:fragment:1.8.6"
117117
implementation "androidx.preference:preference-ktx:1.2.1"
118-
implementation 'androidx.recyclerview:recyclerview:1.2.0'
118+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
119119
implementation 'org.conscrypt:conscrypt-android:2.5.1'
120120
// https://mvnrepository.com/artifact/androidx.core/core-ktx
121121
implementation 'androidx.core:core-ktx:1.15.0'
@@ -145,6 +145,10 @@ dependencies {
145145
implementation 'com.github.seven332:refreshlayout:0.1.0'
146146
implementation 'com.github.seven332:ripple:0.1.2'
147147
implementation 'com.github.seven332:streampipe:0.1.0'
148+
// 列表组件
149+
// implementation "androidx.paging:paging-runtime:3.2.1"
150+
// implementation "androidx.paging:paging-rxjava3:3.2.1" // 如果需要 RxJava
151+
// implementation "androidx.paging:paging-guava:3.2.1" // 如果需要 Guava
148152

149153

150154
implementation 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:1.0.0'

app/src/main/java/com/hippo/ehviewer/EhDB.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ private static void upgradeDB(SQLiteDatabase db, int oldVersion) {
116116
"\"ENABLE\" INTEGER);");
117117
db.execSQL("INSERT INTO \"FILTER2\" (" +
118118
"_id, MODE, TEXT, ENABLE)" +
119-
"SELECT _id, MODE, TEXT, 1 FROM FILTER;");
120-
db.execSQL("DROP TABLE FILTER");
121-
db.execSQL("ALTER TABLE FILTER2 RENAME TO FILTER");
119+
"SELECT _id, MODE, TEXT, 1 FROM [FILTER];");
120+
db.execSQL("DROP TABLE [FILTER]");
121+
db.execSQL("ALTER TABLE FILTER2 RENAME TO [FILTER]");
122122
case 3: // 3 to 4, add PAGE_FROM and PAGE_TO column to QUICK_SEARCH
123123
db.execSQL("CREATE TABLE " + "\"QUICK_SEARCH2\" (" +
124124
"\"_id\" INTEGER PRIMARY KEY ," +
@@ -414,6 +414,28 @@ public static synchronized List<DownloadInfo> getAllDownloadInfo() {
414414
return list;
415415
}
416416

417+
public static synchronized void moveDownloadInfo(List<DownloadInfo> infos, int fromPosition, int toPosition){
418+
if (fromPosition == toPosition) {
419+
return;
420+
}
421+
DownloadsDao dao = sDaoSession.getDownloadsDao();
422+
boolean reverse = fromPosition > toPosition;
423+
int offset = reverse ? toPosition : fromPosition;
424+
int limit = reverse ? fromPosition - toPosition + 1 : toPosition - fromPosition + 1;
425+
426+
List<DownloadInfo> list = infos.subList(offset, offset + limit);
427+
428+
int step = reverse ? 1 : -1;
429+
int start = reverse ? limit - 1 : 0;
430+
int end = reverse ? 0 : limit - 1;
431+
long toTime = list.get(end).time;
432+
for(int i = end; reverse ? i < start : i > start; i += step) {
433+
list.get(i).setTime(list.get(i + step).getTime());
434+
}
435+
list.get(start).setTime(toTime);
436+
dao.updateInTx(list);
437+
}
438+
417439
// Insert or update
418440
public static synchronized void putDownloadInfo(DownloadInfo downloadInfo) {
419441
DownloadsDao dao = sDaoSession.getDownloadsDao();
@@ -734,28 +756,46 @@ public static synchronized void deleteQuickSearch(QuickSearch quickSearch) {
734756
dao.delete(quickSearch);
735757
}
736758

759+
/**
760+
* 快速搜索项移动方法
761+
* 用于调整快速搜索项的顺序,通过时间戳来实现位置调整
762+
*
763+
* @param fromPosition 起始位置
764+
* @param toPosition 目标位置
765+
*/
737766
public static synchronized void moveQuickSearch(int fromPosition, int toPosition) {
767+
// 如果起始位置和目标位置相同,则直接返回
738768
if (fromPosition == toPosition) {
739769
return;
740770
}
741771

772+
// 判断是否需要反向移动
742773
boolean reverse = fromPosition > toPosition;
774+
// 计算偏移量,用于查询数据库时的起始位置
743775
int offset = reverse ? toPosition : fromPosition;
776+
// 计算需要移动的项目数量
744777
int limit = reverse ? fromPosition - toPosition + 1 : toPosition - fromPosition + 1;
745778

779+
// 获取QuickSearchDao对象
746780
QuickSearchDao dao = sDaoSession.getQuickSearchDao();
781+
// 查询需要移动的搜索项列表,按时间升序排列
747782
List<QuickSearch> list = dao.queryBuilder().orderAsc(QuickSearchDao.Properties.Time)
748783
.offset(offset).limit(limit).list();
749784

785+
// 设置移动方向和起始、结束位置
750786
int step = reverse ? 1 : -1;
751787
int start = reverse ? limit - 1 : 0;
752788
int end = reverse ? 0 : limit - 1;
789+
// 获取目标位置的时间戳
753790
long toTime = list.get(end).getTime();
791+
// 遍历列表,交换相邻项的时间戳,实现位置移动
754792
for (int i = end; reverse ? i < start : i > start; i += step) {
755793
list.get(i).setTime(list.get(i + step).getTime());
756794
}
795+
// 将起始项的时间设置为目标时间
757796
list.get(start).setTime(toTime);
758797

798+
// 在事务中更新所有更改
759799
dao.updateInTx(list);
760800
}
761801

app/src/main/java/com/hippo/ehviewer/client/EhClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.annotation.SuppressLint;
2020
import android.content.Context;
2121
import android.os.AsyncTask;
22+
import android.util.Log;
2223

2324
import com.google.firebase.crashlytics.FirebaseCrashlytics;
2425
import com.hippo.ehviewer.EhApplication;
@@ -150,6 +151,7 @@ public void stop() {
150151
@Override
151152
protected Object doInBackground(Object... params) {
152153
try {
154+
Log.i(TAG, "doInBackground: "+mMethod);
153155
switch (mMethod) {
154156
case METHOD_SIGN_IN:
155157
return EhEngine.signIn(this, mOkHttpClient, (String) params[0], (String) params[1]);

0 commit comments

Comments
 (0)