@@ -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
0 commit comments