Explorar el Código

1.修复历史浏览记录异步重用,导致的错位问题
2.将 kf的缓存迁移

100Years hace 2 semanas
padre
commit
6cb7ab1bde

+ 10 - 27
TSLiveWallpaper/AppDelegate.swift

@@ -130,27 +130,16 @@ extension AppDelegate {
 extension AppDelegate {
     
     func handleKingfisher(){
-    
-//        let documentURL = FileManager.default.urls(
-//            for: .documentDirectory,
-//            in: .userDomainMask
-//        ).first!
-//        
-//        let cache = try! ImageCache(
-//            name: "permanent_image_cache",
-//            cacheDirectoryURL: libraryURL
-//        )
-        
-        
         moveCacheSmoothly()
-        
-        let cache = ImageCache(
-            name: "permanent_image_cache"
+//        let cache = ImageCache( name: "permanent_image_cache")
+
+        let cache = try! ImageCache(
+            name: "permanent_image_cache",
+            cacheDirectoryURL: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask ).first!
         )
         cache.diskStorage.config.expiration = .never    // 永不过期
         cache.diskStorage.config.sizeLimit = 0          // 无大小限制
         KingfisherManager.shared.cache = cache
-        
     }
     
     
@@ -161,12 +150,13 @@ extension AppDelegate {
             dePrint("Cache Kingfisher 没有缓存,不用迁移")
             return
         }
-        let srcURL = cachesURL.appendingPathComponent("com.onevcat.Kingfisher.ImageCache.permanent_image_cache")
+        let permanent_image_cache = "com.onevcat.Kingfisher.ImageCache.permanent_image_cache"
+        let srcURL = cachesURL.appendingPathComponent(permanent_image_cache)
         guard fileManager.fileExists(atPath: srcURL.path) else { return }
         
         //把srcURL 移动到新的 documentURL
         guard let documentURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
-        let dstURL = documentURL.appendingPathComponent("KingfisherCache")
+        let dstURL = documentURL.appendingPathComponent(permanent_image_cache)
         
         // 4. 处理目标已存在的情况
         if fileManager.fileExists(atPath: dstURL.path) {
@@ -179,22 +169,15 @@ extension AppDelegate {
                 return
             }
         }
-        
-        do {
-              try fileManager.createDirectory(at: dstURL, withIntermediateDirectories: true, attributes: nil)
-          } catch {
-              dePrint("创建目录失败: \(error.localizedDescription)")
-              return
-          }
-        
+
         do {
             dePrint("srcURL=\(srcURL)")
             dePrint("dstURL=\(dstURL)")
-//            try fileManager.copyItem(at: srcURL, to: dstURL)
             try fileManager.moveItem(at: srcURL, to: dstURL)// 移动文件
         } catch {
             debugPrint("尝试移动文件失败: \(error.localizedDescription)")
         }
+   
         
         dePrint("移动 Cache Kingfisher 到 library 流程结束")
     }

+ 20 - 7
TSLiveWallpaper/Business/TSAIListVC/TSAIPhotoDetailsVC/TSAIPhotoDetailsBrowserCell.swift

@@ -5,9 +5,17 @@
 //  Created by 100Years on 2025/6/17.
 //
 
-
+import Kingfisher
 class TSAIPhotoDetailsBaseBrowserCell: TSBaseCollectionCell {
-    var model:TSActionInfoModel = TSActionInfoModel()
+    var model:TSActionInfoModel = TSActionInfoModel(){
+        didSet{
+            model.isCurrent = true
+        }
+    }
+    override func prepareForReuse() {
+        model.isCurrent = false
+    }
+    
 }
 
 class TSAIPhotoDetailsPanComparisonViewCell: TSAIPhotoDetailsImageViewCell {
@@ -28,34 +36,39 @@ class TSAIPhotoDetailsPanComparisonViewCell: TSAIPhotoDetailsImageViewCell {
         }
     }
     
-    
+
     func uploadPanComparisonView(){
-        let infoModel = model
+
         DispatchQueue.global(qos: .userInitiated).async {
+            let infoModel = self.model
             var oldImage:UIImage?
             var newImage:UIImage?
             let grounp = DispatchGroup()
             grounp.enter()
-            TSImageStoreTool.downloadImageWithProgress(urlString: infoModel.request.imageUrl) { image in
+            _ = TSImageStoreTool.downloadImageWithProgress(urlString: infoModel.request.imageUrl) { image in
                 grounp.leave()
                 oldImage = image
                 
             }
             
             grounp.enter()
-            TSImageStoreTool.downloadImageWithProgress(urlString: infoModel.response.resultUrl) { image in
+            _ = TSImageStoreTool.downloadImageWithProgress(urlString: infoModel.response.resultUrl) { image in
                 grounp.leave()
                 newImage = image
             }
             
             grounp.notify(queue: .main) {
+                dePrint("infoModel.request.imageUrl=\(infoModel.request.imageUrl),\(infoModel.isCurrent)")
+                if infoModel.isCurrent == false { return }
                 if let oldImage = oldImage,let newImage = newImage {
+                    self.panComparisonView.isHidden = false
                     let size = oldImage.size.height > newImage.size.height ? oldImage.size : newImage.size
                     self.panComparisonView.snp.updateConstraints { make in
                         make.height.equalTo(kGetUIWdith(designSize: size, currentW: k_ScreenWidth))
                     }
                     self.panComparisonView.configure(oldImage: oldImage, newImage: newImage)
-                }else if let newImage = newImage{
+                }else{
+              
                     self.panComparisonView.isHidden = true
                     self.netWorkImageView.image = newImage
                 }

+ 14 - 13
TSLiveWallpaper/Business/TSAIListVC/TSAIPhotoDetailsVC/TSAIPhotoDetailsBrowserVC.swift

@@ -8,7 +8,20 @@
 class TSAIPhotoDetailsBrowserVC: TSBaseVC {
     
     var deleteBlock:((TSActionInfoModel)->Void)?
-    var dataModelArray:[TSActionInfoModel] = []
+    var dataModelArray:[TSActionInfoModel] = []{
+        didSet{
+            DispatchQueue.main.async {
+                self.collectionView.isHidden = false
+                self.collectionView.reloadData()
+                self.collectionView.setContentOffset(CGPoint(x:0 , y:CGFloat(self.currentIndex) * self.collectionView.frame.size.height), animated: false)
+            
+//                kMainShort {
+//                    self.collectionView.reloadData()
+                    self.reloadUI()
+//                }
+            }
+        }
+    }
 
     var currentModel:TSActionInfoModel?{
         if let model = dataModelArray.safeObj(At: currentIndex){
@@ -121,18 +134,6 @@ class TSAIPhotoDetailsBrowserVC: TSBaseVC {
             make.height.equalTo(48)
             make.bottom.equalTo(bottomBtnTop)
         }
-        
-
-        DispatchQueue.main.async {
-            self.collectionView.isHidden = false
-            self.collectionView.reloadData()
-            self.collectionView.setContentOffset(CGPoint(x:0 , y:CGFloat(self.currentIndex) * self.collectionView.frame.size.height), animated: false)
-
-//            if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
-//                flowLayout.itemSize = self.collectionView.bounds.size
-//            }
-            self.reloadUI()
-        }
     }
     
     //保存功能

+ 2 - 0
TSLiveWallpaper/Data/Model/TSActionInfoModel.swift

@@ -54,6 +54,8 @@ class TSActionInfoModel: TSBaseModel {
 
         uuid      <- map["uuid"]
     }
+    
+    var isCurrent:Bool = false //UI刷新层业务逻辑,特殊地方用到的
 }
 
 extension TSActionInfoModel {