Quellcode durchsuchen

don't save absolute path to background image file but save image name + file extension and let file manager return the path to the application support directory

cyberta vor 3 Jahren
Ursprung
Commit
2e0bd62818

+ 5 - 5
deltachat-ios/Chat/ChatViewController.swift

@@ -60,10 +60,10 @@ class ChatViewController: UITableViewController {
     public lazy var backgroundContainer: UIImageView = {
         let view = UIImageView()
         view.contentMode = .scaleAspectFill
-        if let path = UserDefaults.standard.string(forKey: Constants.Keys.backgroundImageUrl) {
-            view.sd_setImage(with: URL(fileURLWithPath: path)) { [weak self] (_, error, _, _) in
-                if error != nil {
-                    logger.warning(String(describing: error))
+        if let backgroundImageName = UserDefaults.standard.string(forKey: Constants.Keys.backgroundImageName) {
+            view.sd_setImage(with: Utils.getBackgroundImageURL(name: backgroundImageName), placeholderImage: nil, options: [.retryFailed]) { [weak self] (_, error, _, _) in
+                if let error = error {
+                    logger.error("Error loading background image: \(error.localizedDescription)" )
                     DispatchQueue.main.async {
                         self?.setDefaultBackgroundImage(view: view)
                     }
@@ -904,7 +904,7 @@ class ChatViewController: UITableViewController {
     override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
         messageInputBar.inputTextView.layer.borderColor = DcColors.colorDisabled.cgColor
         if #available(iOS 12.0, *),
-            UserDefaults.standard.string(forKey: Constants.Keys.backgroundImageUrl) == nil {
+            UserDefaults.standard.string(forKey: Constants.Keys.backgroundImageName) == nil {
             backgroundContainer.image = UIImage(named: traitCollection.userInterfaceStyle == .light ? "background_light" : "background_dark")
         }
     }

+ 11 - 11
deltachat-ios/Controller/SettingsBackgroundSelectionController.swift

@@ -62,15 +62,15 @@ class SettingsBackgroundSelectionController: UIViewController, MediaPickerDelega
         view.contentMode = .scaleAspectFill
         view.translatesAutoresizingMaskIntoConstraints = false
         view.clipsToBounds = true
-        if let backgroundImageURL = UserDefaults.standard.string(forKey: Constants.Keys.backgroundImageUrl) {
-            view.sd_setImage(with: URL(fileURLWithPath: backgroundImageURL), placeholderImage: nil, options: .refreshCached) { [weak self] (_, error, _, _) in
-                if error != nil {
-                    logger.warning(String.init(describing: error))
-                    DispatchQueue.main.async {
-                        self?.setDefault(view)
+        if let backgroundImageName = UserDefaults.standard.string(forKey: Constants.Keys.backgroundImageName) {
+            view.sd_setImage(with: Utils.getBackgroundImageURL(name: backgroundImageName), placeholderImage: nil, options: [.retryFailed, .refreshCached]) { [weak self] (_, error, _, _) in
+                    if let error = error {
+                        logger.error("Error loading background image: \(error.localizedDescription)" )
+                        DispatchQueue.main.async {
+                            self?.setDefault(view)
+                        }
                     }
                 }
-            }
         } else {
             setDefault(view)
         }
@@ -138,7 +138,7 @@ class SettingsBackgroundSelectionController: UIViewController, MediaPickerDelega
 
     @objc private func onDefaultSelected() {
         setDefault(backgroundContainer)
-        UserDefaults.standard.set(nil, forKey: Constants.Keys.backgroundImageUrl)
+        UserDefaults.standard.set(nil, forKey: Constants.Keys.backgroundImageName)
         UserDefaults.standard.synchronize()
     }
 
@@ -152,10 +152,10 @@ class SettingsBackgroundSelectionController: UIViewController, MediaPickerDelega
 
     // MARK: MediaPickerDelegate
     func onImageSelected(image: UIImage) {
-        if let pathInDocDir = ImageFormat.saveImage(image: image, name: Constants.backgroundImageName) {
-            UserDefaults.standard.set(pathInDocDir, forKey: Constants.Keys.backgroundImageUrl)
+        if let path = ImageFormat.saveImage(image: image, name: Constants.backgroundImageName) {
+            UserDefaults.standard.set(URL(fileURLWithPath: path).lastPathComponent, forKey: Constants.Keys.backgroundImageName)
             UserDefaults.standard.synchronize()
-            backgroundContainer.sd_setImage(with: URL(fileURLWithPath: pathInDocDir), placeholderImage: nil, options: .refreshCached, completed: nil)
+            backgroundContainer.sd_setImage(with: URL(fileURLWithPath: path), placeholderImage: nil, options: .refreshCached, completed: nil)
         } else {
             logger.error("failed to save background image")
         }

+ 1 - 1
deltachat-ios/Helper/Constants.swift

@@ -10,7 +10,7 @@ struct Constants {
         static let deltachatImapEmailKey = "__DELTACHAT_IMAP_EMAIL_KEY__"
         static let deltachatImapPasswordKey = "__DELTACHAT_IMAP_PASSWORD_KEY__"
         static let lastSelectedAccountKey = "__DELTACHAT_LAST_SELECTED_ACCOUNT_KEY__"
-        static let backgroundImageUrl = "__BACKGROUND_IMAGE_URL__"
+        static let backgroundImageName = "__BACKGROUND_IMAGE_NAME__"
         static let notificationTimestamps = "__NOTIFICATION_TIMESTAMPS__"
     }
 

+ 3 - 1
deltachat-ios/Helper/ImageFormat.swift

@@ -100,7 +100,9 @@ extension ImageFormat {
         }
         var subdirectoryURL = directoryURL.appendingPathComponent(identifier)
         do {
-            try fileManager.createDirectory(at: subdirectoryURL, withIntermediateDirectories: true, attributes: nil)
+            if !fileManager.fileExists(atPath: subdirectoryURL.path) {
+                try fileManager.createDirectory(at: subdirectoryURL, withIntermediateDirectories: true, attributes: nil)
+            }
         } catch {
             print("err: \(error.localizedDescription)")
             return nil

+ 14 - 0
deltachat-ios/Helper/Utils.swift

@@ -56,4 +56,18 @@ struct Utils {
         // TODO: add more file suffixes
         return url.absoluteString.hasSuffix("wav")
     }
+
+    public static func getBackgroundImageURL(name: String) -> URL? {
+        let fileManager = FileManager.default
+        let urls = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask) as [URL]
+        guard let identifier = Bundle.main.bundleIdentifier else {
+            logger.error("backgroundImageURL: Could not find bundle identifier")
+            return nil
+        }
+        guard let directoryURL = urls.last else {
+            logger.error("backgroundImageURL: Could not find directory url for .applicationSupportDirectory in .userDomainMask")
+            return nil
+        }
+        return directoryURL.appendingPathComponent(identifier).appendingPathComponent(name)
+    }
 }