Ver código fonte

refactor FileTextCell, create a dedicated FileView for reuse in draft area

cyberta 4 anos atrás
pai
commit
2fa4919df7

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -23,6 +23,7 @@
 		303492952565AABC00A523D0 /* DraftModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 303492942565AABC00A523D0 /* DraftModel.swift */; };
 		3034929F25752FC800A523D0 /* MediaPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3034929E25752FC800A523D0 /* MediaPreview.swift */; };
 		303492A5257546B400A523D0 /* DraftPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = 303492A4257546B400A523D0 /* DraftPreview.swift */; };
+		303492AD2577CAC300A523D0 /* FileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 303492AC2577CAC300A523D0 /* FileView.swift */; };
 		304219D3243F588500516852 /* DcCore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 304219D1243F588500516852 /* DcCore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
 		304219D92440734A00516852 /* DcMsg+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 304219D82440734A00516852 /* DcMsg+Extension.swift */; };
 		304F5E44244F571C00462538 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7A9FB14A1FB061E2001FEA36 /* Assets.xcassets */; };
@@ -221,6 +222,7 @@
 		303492942565AABC00A523D0 /* DraftModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraftModel.swift; sourceTree = "<group>"; };
 		3034929E25752FC800A523D0 /* MediaPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaPreview.swift; sourceTree = "<group>"; };
 		303492A4257546B400A523D0 /* DraftPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraftPreview.swift; sourceTree = "<group>"; };
+		303492AC2577CAC300A523D0 /* FileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileView.swift; sourceTree = "<group>"; };
 		304219D1243F588500516852 /* DcCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = DcCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		304219D82440734A00516852 /* DcMsg+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DcMsg+Extension.swift"; sourceTree = "<group>"; };
 		3052C609253F082E007D13EA /* MessageLabelDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageLabelDelegate.swift; sourceTree = "<group>"; };
@@ -553,6 +555,7 @@
 				3034929E25752FC800A523D0 /* MediaPreview.swift */,
 				30349290256441E200A523D0 /* QuotePreview.swift */,
 				303492A4257546B400A523D0 /* DraftPreview.swift */,
+				303492AC2577CAC300A523D0 /* FileView.swift */,
 			);
 			path = Views;
 			sourceTree = "<group>";
@@ -1187,6 +1190,7 @@
 				AEC67A1C241CE9E4007DDBE1 /* AppStateRestorer.swift in Sources */,
 				3008CB7224F93EB900E6A617 /* AudioMessageCell.swift in Sources */,
 				302E1BB4252B5AB4008F4264 /* PlayButtonView.swift in Sources */,
+				303492AD2577CAC300A523D0 /* FileView.swift in Sources */,
 				7AE0A5491FC42F65005ECB4B /* NewChatViewController.swift in Sources */,
 				AE77838F23E4276D0093EABD /* ContactCellViewModel.swift in Sources */,
 				B20462E62440C99600367A57 /* SettingsAutodelSetController.swift in Sources */,

+ 8 - 90
deltachat-ios/Chat/Views/Cells/FileTextCell.swift

@@ -5,70 +5,11 @@ import SDWebImage
 
 class FileTextCell: BaseMessageCell {
 
-    private lazy var defaultImage: UIImage = {
-        let image = UIImage(named: "ic_attach_file_36pt")
-        return image!
-    }()
-
-    private var imageWidthConstraint: NSLayoutConstraint?
-    private var imageHeightConstraint: NSLayoutConstraint?
     private var spacer: NSLayoutConstraint?
 
-    private var horizontalLayout: Bool {
-        set {
-            if newValue {
-                fileStackView.axis = .horizontal
-                imageWidthConstraint?.isActive = true
-                imageHeightConstraint?.isActive = true
-                fileStackView.alignment = .center
-            } else {
-                fileStackView.axis = .vertical
-                imageWidthConstraint?.isActive = false
-                imageHeightConstraint?.isActive = false
-                fileStackView.alignment = .leading
-            }
-        }
-        get {
-            return fileStackView.axis == .horizontal
-        }
-    }
-
-    private lazy var fileStackView: UIStackView = {
-        let stackView = UIStackView(arrangedSubviews: [fileImageView, fileMetadataStackView])
-        stackView.axis = .horizontal
-        stackView.spacing = 6
-        return stackView
-    }()
-
-    private lazy var fileImageView: UIImageView = {
-        let imageView = UIImageView()
-        imageView.contentMode = .scaleAspectFit
-        return imageView
-    }()
-
-    private lazy var fileMetadataStackView: UIStackView = {
-        let stackView = UIStackView(arrangedSubviews: [fileTitle, fileSubtitle])
-        stackView.axis = .vertical
-        stackView.translatesAutoresizingMaskIntoConstraints = false
-        stackView.clipsToBounds = true
-        return stackView
-    }()
-
-    private lazy var fileTitle: UILabel = {
-        let title = UILabel()
-        title.font = UIFont.preferredItalicFont(for: .body)
-        title.translatesAutoresizingMaskIntoConstraints = false
-        title.numberOfLines = 3
-        title.lineBreakMode = .byCharWrapping
-        return title
-    }()
-
-    private lazy var fileSubtitle: UILabel = {
-        let subtitle = UILabel()
-        subtitle.font = UIFont.preferredItalicFont(for: .caption2)
-        subtitle.translatesAutoresizingMaskIntoConstraints = false
-        subtitle.numberOfLines = 1
-        return subtitle
+    private lazy var fileView: FileView = {
+        let view = FileView()
+        return view
     }()
 
     override func setupSubviews() {
@@ -76,17 +17,15 @@ class FileTextCell: BaseMessageCell {
         let spacerView = UIView()
         spacer = spacerView.constraintHeightTo(8, priority: .defaultHigh)
         spacer?.isActive = true
-        mainContentView.addArrangedSubview(fileStackView)
+        mainContentView.addArrangedSubview(fileView)
         mainContentView.addArrangedSubview(spacerView)
         mainContentView.addArrangedSubview(messageLabel)
-        imageWidthConstraint = fileImageView.constraintWidthTo(50)
-        imageHeightConstraint = fileImageView.constraintHeightTo(50 * 1.3, priority: .defaultLow)
-        horizontalLayout = true
+        fileView.horizontalLayout = true
         mainContentViewHorizontalPadding = 12
     }
 
     override func prepareForReuse() {
-        fileImageView.image = nil
+        fileView.prepareForReuse()
     }
 
     override func update(msg: DcMsg, messageStyle: UIRectCorner, isAvatarVisible: Bool, isGroup: Bool) {
@@ -96,30 +35,9 @@ class FileTextCell: BaseMessageCell {
         } else {
             spacer?.isActive = false
         }
-        if let url = msg.fileURL {
-            generateThumbnailFor(url: url, placeholder: defaultImage)
-        } else {
-            fileImageView.image = defaultImage
-            horizontalLayout = true
-        }
-        fileTitle.text = msg.filename
-        fileSubtitle.text = msg.getPrettyFileSize()
+        
+        fileView.configure(message: msg)
         super.update(msg: msg, messageStyle: messageStyle, isAvatarVisible: isAvatarVisible, isGroup: isGroup)
     }
-
-    private func generateThumbnailFor(url: URL, placeholder: UIImage?) {
-        if let thumbnail = ThumbnailCache.shared.restoreImage(key: url.absoluteString) {
-            fileImageView.image = thumbnail
-            horizontalLayout = false
-        } else if let pdfThumbnail = DcUtils.thumbnailFromPdf(withUrl: url) {
-            fileImageView.image = pdfThumbnail
-            horizontalLayout = false
-            ThumbnailCache.shared.storeImage(image: pdfThumbnail, key: url.absoluteString)
-        } else {
-            let controller = UIDocumentInteractionController(url: url)
-            fileImageView.image = controller.icons.first ?? placeholder
-            horizontalLayout = true
-        }
-    }
     
 }

+ 0 - 9
deltachat-ios/Chat/Views/DraftArea.swift

@@ -1,9 +0,0 @@
-//
-//  DraftArea.swift
-//  deltachat-ios
-//
-//  Created by Macci on 01.12.20.
-//  Copyright © 2020 Jonas Reinsch. All rights reserved.
-//
-
-import Foundation

+ 130 - 0
deltachat-ios/Chat/Views/FileView.swift

@@ -0,0 +1,130 @@
+import UIKit
+import DcCore
+public class FileView: UIView {
+
+    private var imageWidthConstraint: NSLayoutConstraint?
+    private var imageHeightConstraint: NSLayoutConstraint?
+
+    public var horizontalLayout: Bool {
+        set {
+            if newValue {
+                fileStackView.axis = .horizontal
+                imageWidthConstraint?.isActive = true
+                imageHeightConstraint?.isActive = true
+                fileStackView.alignment = .center
+            } else {
+                fileStackView.axis = .vertical
+                imageWidthConstraint?.isActive = false
+                imageHeightConstraint?.isActive = false
+                fileStackView.alignment = .leading
+            }
+        }
+        get {
+            return fileStackView.axis == .horizontal
+        }
+    }
+
+    // allow to automatically switch between small and large preview of a file,
+    // depending on the file type, if false the view will be configured according to horizontalLayout Bool
+    public var allowLayoutChange: Bool = true
+
+    private lazy var defaultImage: UIImage = {
+        let image = UIImage(named: "ic_attach_file_36pt")
+        return image!
+    }()
+
+    private lazy var fileStackView: UIStackView = {
+        let stackView = UIStackView(arrangedSubviews: [fileImageView, fileMetadataStackView])
+        stackView.axis = .horizontal
+        stackView.translatesAutoresizingMaskIntoConstraints = false
+        stackView.spacing = 6
+        return stackView
+    }()
+
+    private lazy var fileImageView: UIImageView = {
+        let imageView = UIImageView()
+        imageView.contentMode = .scaleAspectFit
+        return imageView
+    }()
+
+    private lazy var fileMetadataStackView: UIStackView = {
+        let stackView = UIStackView(arrangedSubviews: [fileTitle, fileSubtitle])
+        stackView.axis = .vertical
+        stackView.translatesAutoresizingMaskIntoConstraints = false
+        stackView.clipsToBounds = true
+        return stackView
+    }()
+
+    private lazy var fileTitle: UILabel = {
+        let title = UILabel()
+        title.font = UIFont.preferredItalicFont(for: .body)
+        title.translatesAutoresizingMaskIntoConstraints = false
+        title.numberOfLines = 3
+        title.lineBreakMode = .byCharWrapping
+        return title
+    }()
+
+    private lazy var fileSubtitle: UILabel = {
+        let subtitle = UILabel()
+        subtitle.font = UIFont.preferredItalicFont(for: .caption2)
+        subtitle.translatesAutoresizingMaskIntoConstraints = false
+        subtitle.numberOfLines = 1
+        return subtitle
+    }()
+
+    convenience init() {
+        self.init(frame: .zero)
+
+    }
+
+    public override init(frame: CGRect) {
+        super.init(frame: frame)
+        self.setupSubviews()
+    }
+
+    required init(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+
+    func setupSubviews() {
+        addSubview(fileStackView)
+        fileStackView.fillSuperview()
+        imageWidthConstraint = fileImageView.constraintWidthTo(50)
+        imageHeightConstraint = fileImageView.constraintHeightTo(50 * 1.3, priority: .defaultLow)
+        horizontalLayout = true
+    }
+
+    public func configure(message: DcMsg) {
+        if let url = message.fileURL {
+            generateThumbnailFor(url: url, placeholder: defaultImage)
+        } else {
+            fileImageView.image = defaultImage
+            horizontalLayout = true
+        }
+        fileTitle.text = message.filename
+        fileSubtitle.text = message.getPrettyFileSize()
+    }
+
+
+    public func prepareForReuse() {
+        fileImageView.image = nil
+    }
+
+    private func generateThumbnailFor(url: URL, placeholder: UIImage?) {
+        if let thumbnail = ThumbnailCache.shared.restoreImage(key: url.absoluteString) {
+            fileImageView.image = thumbnail
+            horizontalLayout = allowLayoutChange ? false : horizontalLayout
+        } else if let pdfThumbnail = DcUtils.thumbnailFromPdf(withUrl: url) {
+            fileImageView.image = pdfThumbnail
+            horizontalLayout = allowLayoutChange ? false : horizontalLayout
+            ThumbnailCache.shared.storeImage(image: pdfThumbnail, key: url.absoluteString)
+        } else {
+            let controller = UIDocumentInteractionController(url: url)
+            fileImageView.image = controller.icons.first ?? placeholder
+            horizontalLayout = allowLayoutChange ? true : horizontalLayout
+        }
+    }
+
+
+}