From 19963de0556bbdc8158b9e27a92a9ac2c95b4fed Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:15:40 -0800 Subject: [PATCH] Add BlurHash --- .../Mastodon/Entities/Attachment.swift | 2 +- Metatext.xcodeproj/project.pbxproj | 25 +++++++++++++++++++ .../xcshareddata/swiftpm/Package.resolved | 9 +++++++ View Controllers/ImageViewController.swift | 14 ++++++++--- Views/SwiftUI/AcknowledgmentsView.swift | 15 +++++++++++ Views/UIKit/AttachmentView.swift | 13 +++++++++- Views/UIKit/EditThumbnailView.swift | 13 +++++----- Views/ViewConstants.swift | 4 +++ 8 files changed, 83 insertions(+), 12 deletions(-) diff --git a/Mastodon/Sources/Mastodon/Entities/Attachment.swift b/Mastodon/Sources/Mastodon/Entities/Attachment.swift index eb0b2eb..80e3668 100644 --- a/Mastodon/Sources/Mastodon/Entities/Attachment.swift +++ b/Mastodon/Sources/Mastodon/Entities/Attachment.swift @@ -37,9 +37,9 @@ public struct Attachment: Codable, Hashable { public let url: URL public let remoteUrl: URL? public let previewUrl: URL? - public let textUrl: URL? public let meta: Meta? public let description: String? + public let blurhash: String? } public extension Attachment { diff --git a/Metatext.xcodeproj/project.pbxproj b/Metatext.xcodeproj/project.pbxproj index 5f39b92..d5cc6fe 100644 --- a/Metatext.xcodeproj/project.pbxproj +++ b/Metatext.xcodeproj/project.pbxproj @@ -91,6 +91,7 @@ D0625E59250F092900502611 /* StatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0625E58250F092900502611 /* StatusTableViewCell.swift */; }; D0625E5D250F0B5C00502611 /* StatusContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0625E5C250F0B5C00502611 /* StatusContentConfiguration.swift */; }; D06BC5E625202AD90079541D /* ProfileViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06BC5E525202AD90079541D /* ProfileViewController.swift */; }; + D06FA32725F0659600C9639A /* BlurHash in Frameworks */ = {isa = PBXBuildFile; productRef = D06FA32625F0659600C9639A /* BlurHash */; }; D07EC7CF25B13921006DF726 /* PickerEmoji+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07EC7CE25B13921006DF726 /* PickerEmoji+Extensions.swift */; }; D07EC7D025B13921006DF726 /* PickerEmoji+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07EC7CE25B13921006DF726 /* PickerEmoji+Extensions.swift */; }; D07EC7DC25B13DBB006DF726 /* EmojiCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D07EC7DB25B13DBB006DF726 /* EmojiCollectionViewCell.swift */; }; @@ -183,6 +184,7 @@ D0D2AC4D25BCD2A9003D5DF2 /* TagTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D2AC4C25BCD2A9003D5DF2 /* TagTableViewCell.swift */; }; D0D2AC5325BCD2BA003D5DF2 /* TagContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D2AC5225BCD2BA003D5DF2 /* TagContentConfiguration.swift */; }; D0D2AC6725BD0484003D5DF2 /* LineChartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D2AC6625BD0484003D5DF2 /* LineChartView.swift */; }; + D0D4301425F067D600BE5504 /* BlurHash in Frameworks */ = {isa = PBXBuildFile; productRef = D0D4301325F067D600BE5504 /* BlurHash */; }; D0D93EBA25D9C70400C622ED /* AutocompleteItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D93EB925D9C70400C622ED /* AutocompleteItemView.swift */; }; D0D93EC025D9C71D00C622ED /* AutocompleteItemContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D93EBF25D9C71D00C622ED /* AutocompleteItemContentConfiguration.swift */; }; D0D93EC525D9C75E00C622ED /* AutocompleteItemContentConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D93EBF25D9C71D00C622ED /* AutocompleteItemContentConfiguration.swift */; }; @@ -442,6 +444,7 @@ buildActionMask = 2147483647; files = ( D0E2C1D124FD97F000854680 /* ViewModels in Frameworks */, + D06FA32725F0659600C9639A /* BlurHash in Frameworks */, D04F34B625E42ABE00714251 /* SDWebImage in Frameworks */, D0FE7C8025C4C79F00203957 /* PreviewViewModels in Frameworks */, ); @@ -458,6 +461,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + D0D4301425F067D600BE5504 /* BlurHash in Frameworks */, D04F34BC25E42ADC00714251 /* SDWebImage in Frameworks */, D04F9E8E259E9C950081B0C9 /* ViewModels in Frameworks */, ); @@ -862,6 +866,7 @@ D0E2C1D024FD97F000854680 /* ViewModels */, D0FE7C7F25C4C79F00203957 /* PreviewViewModels */, D04F34B525E42ABE00714251 /* SDWebImage */, + D06FA32625F0659600C9639A /* BlurHash */, ); productName = "Metatext (iOS)"; productReference = D047FA8C24C3E21200AF17C5 /* Metatext.app */; @@ -903,6 +908,7 @@ packageProductDependencies = ( D04F9E8D259E9C950081B0C9 /* ViewModels */, D04F34BB25E42ADC00714251 /* SDWebImage */, + D0D4301325F067D600BE5504 /* BlurHash */, ); productName = "Share Extension"; productReference = D08E526C257C36CA00FA2C5F /* Share Extension.appex */; @@ -966,6 +972,7 @@ mainGroup = D047FA7F24C3E21000AF17C5; packageReferences = ( D04F34B425E42ABE00714251 /* XCRemoteSwiftPackageReference "SDWebImage" */, + D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */, ); productRefGroup = D047FA8D24C3E21200AF17C5 /* Products */; projectDirPath = ""; @@ -1664,6 +1671,14 @@ minimumVersion = 5.10.4; }; }; + D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/metabolist/blurhash.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 1.0.0; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -1690,6 +1705,16 @@ isa = XCSwiftPackageProductDependency; productName = ViewModels; }; + D06FA32625F0659600C9639A /* BlurHash */ = { + isa = XCSwiftPackageProductDependency; + package = D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */; + productName = BlurHash; + }; + D0D4301325F067D600BE5504 /* BlurHash */ = { + isa = XCSwiftPackageProductDependency; + package = D06FA32525F0659600C9639A /* XCRemoteSwiftPackageReference "blurhash" */; + productName = BlurHash; + }; D0E2C1D024FD97F000854680 /* ViewModels */ = { isa = XCSwiftPackageProductDependency; productName = ViewModels; diff --git a/Metatext.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Metatext.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index da39d70..0a1aa33 100644 --- a/Metatext.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Metatext.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -10,6 +10,15 @@ "version": "1.0.0" } }, + { + "package": "BlurHash", + "repositoryURL": "https://github.com/metabolist/blurhash.git", + "state": { + "branch": null, + "revision": "4d86c470d866b2d8989adc5a65b121221e40df54", + "version": "1.0.0" + } + }, { "package": "CodableBloomFilter", "repositoryURL": "https://github.com/metabolist/codable-bloom-filter.git", diff --git a/View Controllers/ImageViewController.swift b/View Controllers/ImageViewController.swift index 20cbeda..829875a 100644 --- a/View Controllers/ImageViewController.swift +++ b/View Controllers/ImageViewController.swift @@ -1,6 +1,7 @@ // Copyright © 2020 Metabolist. All rights reserved. import AVFoundation +import BlurHash import Mastodon import SDWebImage import UIKit @@ -111,11 +112,16 @@ final class ImageViewController: UIViewController { imageView.tag = viewModel.tag playerView.isHidden = true - let placeholderKey = viewModel.attachment.previewUrl?.absoluteString - let placeholderImage = SDImageCache.shared.imageFromCache(forKey: placeholderKey) + let placeholderImage: UIImage? + let cachedImageKey = viewModel.attachment.previewUrl?.absoluteString + let cachedImage = SDImageCache.shared.imageFromCache(forKey: cachedImageKey) - if placeholderImage != nil { - imageView.sd_imageIndicator = nil + if cachedImage != nil { + placeholderImage = cachedImage + } else if let blurHash = viewModel.attachment.blurhash { + placeholderImage = UIImage(blurHash: blurHash, size: .blurHashSize) + } else { + placeholderImage = nil } imageView.sd_setImage(with: viewModel.attachment.url, placeholderImage: placeholderImage) diff --git a/Views/SwiftUI/AcknowledgmentsView.swift b/Views/SwiftUI/AcknowledgmentsView.swift index 294582a..8c53527 100644 --- a/Views/SwiftUI/AcknowledgmentsView.swift +++ b/Views/SwiftUI/AcknowledgmentsView.swift @@ -22,6 +22,7 @@ struct AcknowledgmentsView: View { private extension AcknowledgmentsView { enum Acknowledgment: CaseIterable { + case blurHash case combineExpectations case grdb case sdwebimage @@ -32,6 +33,8 @@ private extension AcknowledgmentsView { extension AcknowledgmentsView.Acknowledgment { var name: String { switch self { + case .blurHash: + return "BlurHash" case .combineExpectations: return "CombineExpectations" case .grdb: @@ -45,6 +48,8 @@ extension AcknowledgmentsView.Acknowledgment { var url: URL { switch self { + case .blurHash: + return URL(string: "https://github.com/woltapp/blurhash")! case .combineExpectations: return URL(string: "https://github.com/groue/CombineExpectations")! case .grdb: @@ -59,6 +64,16 @@ extension AcknowledgmentsView.Acknowledgment { // swiftlint:disable line_length var license: String { switch self { + case .blurHash: + return """ +Copyright (c) 2018 Wolt Enterprises + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +""" case .combineExpectations: return """ Copyright (C) 2019 Gwendal Roué diff --git a/Views/UIKit/AttachmentView.swift b/Views/UIKit/AttachmentView.swift index c805819..8248dc4 100644 --- a/Views/UIKit/AttachmentView.swift +++ b/Views/UIKit/AttachmentView.swift @@ -1,6 +1,7 @@ // Copyright © 2020 Metabolist. All rights reserved. import AVKit +import BlurHash import Combine import SDWebImage import UIKit @@ -169,7 +170,17 @@ private extension AttachmentView { switch viewModel.attachment.type { case .image, .video, .gifv: - imageView.sd_setImage(with: viewModel.attachment.previewUrl) { [weak self] _, _, _, _ in + let placeholderImage: UIImage? + + if let blurHash = viewModel.attachment.blurhash { + placeholderImage = UIImage(blurHash: blurHash, size: .blurHashSize) + } else { + placeholderImage = nil + } + + imageView.sd_setImage( + with: viewModel.attachment.previewUrl, + placeholderImage: placeholderImage) { [weak self] _, _, _, _ in self?.layoutSubviews() } case .audio: diff --git a/Views/UIKit/EditThumbnailView.swift b/Views/UIKit/EditThumbnailView.swift index cd8c043..080294f 100644 --- a/Views/UIKit/EditThumbnailView.swift +++ b/Views/UIKit/EditThumbnailView.swift @@ -1,5 +1,6 @@ // Copyright © 2021 Metabolist. All rights reserved. +import BlurHash import Combine import SDWebImage import UIKit @@ -142,15 +143,15 @@ private extension EditThumbnailView { switch viewModel.attachment.type { case .image: playerView.isHidden = true + let placeholderImage: UIImage? - let placeholderKey = viewModel.attachment.previewUrl?.absoluteString - let placeholderImage = SDImageCache.shared.imageFromCache(forKey: placeholderKey) - - if placeholderImage != nil { - imageView.sd_imageIndicator = nil + if let blurHash = viewModel.attachment.blurhash { + placeholderImage = UIImage(blurHash: blurHash, size: .blurHashSize) + } else { + placeholderImage = nil } - imageView.sd_setImage(with: viewModel.attachment.url, placeholderImage: placeholderImage) + imageView.sd_setImage(with: viewModel.attachment.previewUrl, placeholderImage: placeholderImage) case .gifv: imageView.isHidden = true let player = PlayerCache.shared.player(url: viewModel.attachment.url) diff --git a/Views/ViewConstants.swift b/Views/ViewConstants.swift index ca2c138..cdc8c20 100644 --- a/Views/ViewConstants.swift +++ b/Views/ViewConstants.swift @@ -21,6 +21,10 @@ extension Float { static let defaultShadowOpacity: Self = 0.25 } +extension CGSize { + static let blurHashSize = Self(width: 32, height: 32) +} + extension CGRect { static let defaultContentsRect = Self(origin: .zero, size: .init(width: 1, height: 1)) }