From 945241e730fef8967eb98ad664a1b3417b5eb057 Mon Sep 17 00:00:00 2001 From: CMK Date: Wed, 11 May 2022 18:39:34 +0800 Subject: [PATCH] fix: NewsView image layout not fixed issue --- Mastodon.xcodeproj/project.pbxproj | 12 ------- .../Content/MediaView+Configuration.swift | 28 +-------------- Mastodon/State/AppContext.swift | 3 +- .../Service/BlurhashImageCacheService.swift | 3 ++ .../MastodonUI/Vendor}/BlurHashDecode.swift | 0 .../MastodonUI/Vendor}/BlurHashEncode.swift | 0 .../Content/MediaView+Configuration.swift | 35 +++++++++++++++++++ .../View/Content/NewsView+Configuration.swift | 1 + .../MastodonUI/View/Content/NewsView.swift | 16 +++++++++ 9 files changed, 58 insertions(+), 40 deletions(-) rename {Mastodon => MastodonSDK/Sources/MastodonUI}/Service/BlurhashImageCacheService.swift (96%) rename {Mastodon/Vender => MastodonSDK/Sources/MastodonUI/Vendor}/BlurHashDecode.swift (100%) rename {Mastodon/Vender => MastodonSDK/Sources/MastodonUI/Vendor}/BlurHashEncode.swift (100%) diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index d5f5cbd79..1a185f86c 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -286,8 +286,6 @@ DB4F097F26A03DA600D62E92 /* SearchHistoryFetchedResultController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB4F097E26A03DA600D62E92 /* SearchHistoryFetchedResultController.swift */; }; DB4FFC2B269EC39600D62E92 /* SearchToSearchDetailViewControllerAnimatedTransitioning.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB4FFC29269EC39600D62E92 /* SearchToSearchDetailViewControllerAnimatedTransitioning.swift */; }; DB4FFC2C269EC39600D62E92 /* SearchTransitionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB4FFC2A269EC39600D62E92 /* SearchTransitionController.swift */; }; - DB51D172262832380062B7A1 /* BlurHashDecode.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB51D170262832380062B7A1 /* BlurHashDecode.swift */; }; - DB51D173262832380062B7A1 /* BlurHashEncode.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB51D171262832380062B7A1 /* BlurHashEncode.swift */; }; DB552D4F26BBD10C00E481F6 /* OrderedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = DB552D4E26BBD10C00E481F6 /* OrderedCollections */; }; DB564BD3269F3B35001E39A7 /* StatusFilterService.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB564BD2269F3B35001E39A7 /* StatusFilterService.swift */; }; DB59F10E25EF724F001F1DAB /* APIService+Poll.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB59F10D25EF724F001F1DAB /* APIService+Poll.swift */; }; @@ -415,7 +413,6 @@ DB852D1F26FB037800FC9D81 /* SidebarViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB852D1E26FB037800FC9D81 /* SidebarViewModel.swift */; }; DB87D4452609BE0500D12C0D /* ComposeStatusPollOptionCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB87D4442609BE0500D12C0D /* ComposeStatusPollOptionCollectionViewCell.swift */; }; DB87D4512609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB87D4502609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift */; }; - DB894CC427A5490600684B74 /* BlurhashImageCacheService.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB894CC327A5490600684B74 /* BlurhashImageCacheService.swift */; }; DB8AF52E25C13561002E6C99 /* ViewStateStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF52B25C13561002E6C99 /* ViewStateStore.swift */; }; DB8AF52F25C13561002E6C99 /* DocumentStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF52C25C13561002E6C99 /* DocumentStore.swift */; }; DB8AF53025C13561002E6C99 /* AppContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB8AF52D25C13561002E6C99 /* AppContext.swift */; }; @@ -1041,8 +1038,6 @@ DB519B15281BCC2F00F0C99D /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/Intents.strings; sourceTree = ""; }; DB519B16281BCC2F00F0C99D /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/InfoPlist.strings; sourceTree = ""; }; DB519B17281BCC2F00F0C99D /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = tr; path = tr.lproj/Intents.stringsdict; sourceTree = ""; }; - DB51D170262832380062B7A1 /* BlurHashDecode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashDecode.swift; sourceTree = ""; }; - DB51D171262832380062B7A1 /* BlurHashEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashEncode.swift; sourceTree = ""; }; DB564BD2269F3B35001E39A7 /* StatusFilterService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusFilterService.swift; sourceTree = ""; }; DB59F10D25EF724F001F1DAB /* APIService+Poll.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Poll.swift"; sourceTree = ""; }; DB5B7294273112B100081888 /* FollowingListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowingListViewController.swift; sourceTree = ""; }; @@ -1167,7 +1162,6 @@ DB852D1E26FB037800FC9D81 /* SidebarViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarViewModel.swift; sourceTree = ""; }; DB87D4442609BE0500D12C0D /* ComposeStatusPollOptionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeStatusPollOptionCollectionViewCell.swift; sourceTree = ""; }; DB87D4502609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeStatusPollOptionAppendEntryCollectionViewCell.swift; sourceTree = ""; }; - DB894CC327A5490600684B74 /* BlurhashImageCacheService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurhashImageCacheService.swift; sourceTree = ""; }; DB89BA1025C10FF5008580ED /* Mastodon.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Mastodon.entitlements; sourceTree = ""; }; DB8AF52B25C13561002E6C99 /* ViewStateStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewStateStore.swift; sourceTree = ""; }; DB8AF52C25C13561002E6C99 /* DocumentStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DocumentStore.swift; sourceTree = ""; }; @@ -1665,8 +1659,6 @@ isa = PBXGroup; children = ( 2D5A3D0225CF8742002347D6 /* ControlContainableScrollViews.swift */, - DB51D170262832380062B7A1 /* BlurHashDecode.swift */, - DB51D171262832380062B7A1 /* BlurHashEncode.swift */, DB6180EC26391C6C0018D199 /* TransitioningMath.swift */, DB75BF1D263C1C1B00EDBF1F /* CustomScheduler.swift */, DBF156E32702DB3F00EC00B7 /* HandleTapAction.swift */, @@ -1692,7 +1684,6 @@ DB297B1A2679FAE200704C90 /* PlaceholderImageCacheService.swift */, DB564BD2269F3B35001E39A7 /* StatusFilterService.swift */, DB73BF42271192BB00781945 /* InstanceService.swift */, - DB894CC327A5490600684B74 /* BlurhashImageCacheService.swift */, ); path = Service; sourceTree = ""; @@ -4203,7 +4194,6 @@ 2D5A3D0325CF8742002347D6 /* ControlContainableScrollViews.swift in Sources */, DB36679D268AB91B0027D07F /* ComposeStatusAttachmentTableViewCell.swift in Sources */, DB98336B25C9420100AD9700 /* APIService+App.swift in Sources */, - DB894CC427A5490600684B74 /* BlurhashImageCacheService.swift in Sources */, DBFEF07B26A6BCE8006D7ED1 /* APIService+Status+Publish.swift in Sources */, DBA0A11325FB3FC10079C110 /* ComposeToolbarView.swift in Sources */, 5B90C48526259BF10002E742 /* APIService+Subscriptions.swift in Sources */, @@ -4254,7 +4244,6 @@ DB3E6FE42806A5B800B035AE /* DiscoverySection.swift in Sources */, DB8190C62601FF0400020C08 /* AttachmentContainerView.swift in Sources */, DB697DDB278F4DE3004EF2F7 /* DataSourceProvider+StatusTableViewCellDelegate.swift in Sources */, - DB51D173262832380062B7A1 /* BlurHashEncode.swift in Sources */, 2D32EAAC25CB96DC00C9ED86 /* TimelineMiddleLoaderTableViewCell.swift in Sources */, DB87D4512609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift in Sources */, DBB45B5627B39FC9002DC5A7 /* MediaPreviewVideoViewController.swift in Sources */, @@ -4312,7 +4301,6 @@ DB6180ED26391C6C0018D199 /* TransitioningMath.swift in Sources */, DB63F771279A858500455B82 /* Persistence+Notification.swift in Sources */, 2D6DE40026141DF600A63F6A /* SearchViewModel.swift in Sources */, - DB51D172262832380062B7A1 /* BlurHashDecode.swift in Sources */, DBCCC71E25F73297007E1AB6 /* APIService+Reblog.swift in Sources */, DB0617FD27855BFE0030EE79 /* ServerRuleItem.swift in Sources */, 5BB04FD5262E7AFF0043BFF6 /* ReportViewController.swift in Sources */, diff --git a/Mastodon/Scene/Share/View/Content/MediaView+Configuration.swift b/Mastodon/Scene/Share/View/Content/MediaView+Configuration.swift index 02f9ad5a4..b782f8e8a 100644 --- a/Mastodon/Scene/Share/View/Content/MediaView+Configuration.swift +++ b/Mastodon/Scene/Share/View/Content/MediaView+Configuration.swift @@ -57,33 +57,7 @@ extension MediaView { } // end switch }() - if let previewURL = configuration.previewURL, - let url = URL(string: previewURL) - { - let placeholder = UIImage.placeholder(color: .systemGray6) - let request = URLRequest(url: url) - ImageDownloader.default.download(request, completion: { response in - switch response.result { - case .success(let image): - configuration.previewImage = image - case .failure: - configuration.previewImage = placeholder - } - }) - } - - if let assetURL = configuration.assetURL, - let blurhash = configuration.blurhash - { - AppContext.shared.blurhashImageCacheService.image( - blurhash: blurhash, - size: configuration.aspectRadio, - url: assetURL - ) - .assign(to: \.blurhashImage, on: configuration) - .store(in: &configuration.blurhashImageDisposeBag) - } - + configuration.load() configuration.isReveal = status.isMediaSensitive ? status.isSensitiveToggled : true return configuration diff --git a/Mastodon/State/AppContext.swift b/Mastodon/State/AppContext.swift index 9de19c44f..683c81f33 100644 --- a/Mastodon/State/AppContext.swift +++ b/Mastodon/State/AppContext.swift @@ -11,6 +11,7 @@ import Combine import CoreData import CoreDataStack import AlamofireImage +import MastodonUI class AppContext: ObservableObject { @@ -35,7 +36,7 @@ class AppContext: ObservableObject { let photoLibraryService = PhotoLibraryService() let placeholderImageCacheService = PlaceholderImageCacheService() - let blurhashImageCacheService = BlurhashImageCacheService() + let blurhashImageCacheService = BlurhashImageCacheService.shared let documentStore: DocumentStore private var documentStoreSubscription: AnyCancellable! diff --git a/Mastodon/Service/BlurhashImageCacheService.swift b/MastodonSDK/Sources/MastodonUI/Service/BlurhashImageCacheService.swift similarity index 96% rename from Mastodon/Service/BlurhashImageCacheService.swift rename to MastodonSDK/Sources/MastodonUI/Service/BlurhashImageCacheService.swift index b15a9750b..cc9459b4e 100644 --- a/Mastodon/Service/BlurhashImageCacheService.swift +++ b/MastodonSDK/Sources/MastodonUI/Service/BlurhashImageCacheService.swift @@ -10,6 +10,9 @@ import Combine public final class BlurhashImageCacheService { + // MARK: - Singleton + public static let shared = BlurhashImageCacheService() + static let edgeMaxLength: CGFloat = 20 let cache = NSCache() diff --git a/Mastodon/Vender/BlurHashDecode.swift b/MastodonSDK/Sources/MastodonUI/Vendor/BlurHashDecode.swift similarity index 100% rename from Mastodon/Vender/BlurHashDecode.swift rename to MastodonSDK/Sources/MastodonUI/Vendor/BlurHashDecode.swift diff --git a/Mastodon/Vender/BlurHashEncode.swift b/MastodonSDK/Sources/MastodonUI/Vendor/BlurHashEncode.swift similarity index 100% rename from Mastodon/Vender/BlurHashEncode.swift rename to MastodonSDK/Sources/MastodonUI/Vendor/BlurHashEncode.swift diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/MediaView+Configuration.swift b/MastodonSDK/Sources/MastodonUI/View/Content/MediaView+Configuration.swift index 6026e668f..cfe9e73ce 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/MediaView+Configuration.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/MediaView+Configuration.swift @@ -10,6 +10,7 @@ import UIKit import Combine import CoreData import Photos +import AlamofireImage extension MediaView { public class Configuration: Hashable { @@ -142,3 +143,37 @@ extension MediaView.Configuration { } } + +extension MediaView.Configuration { + + public func load() { + if let previewURL = previewURL, + let url = URL(string: previewURL) + { + let placeholder = UIImage.placeholder(color: .systemGray6) + let request = URLRequest(url: url) + ImageDownloader.default.download(request, completion: { [weak self] response in + guard let self = self else { return } + switch response.result { + case .success(let image): + self.previewImage = image + case .failure: + self.previewImage = placeholder + } + }) + } + + if let assetURL = assetURL, + let blurhash = blurhash + { + BlurhashImageCacheService.shared.image( + blurhash: blurhash, + size: aspectRadio, + url: assetURL + ) + .assign(to: \.blurhashImage, on: self) + .store(in: &blurhashImageDisposeBag) + } + } + +} diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift index 397982aaf..7f44232aa 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift @@ -6,6 +6,7 @@ // import UIKit +import Combine import MastodonSDK import MastodonLocalization import AlamofireImage diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift index 6d4cf3fd7..0d4298035 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift @@ -6,9 +6,14 @@ // import UIKit +import Combine import MastodonAsset public final class NewsView: UIView { + + static let imageViewWidth: CGFloat = 132 + + var disposeBag = Set() let container = UIStackView() @@ -44,10 +49,14 @@ public final class NewsView: UIView { }() let imageView = MediaView() + +// let imageView = UIImageView() +// var imageViewMediaConfiguration: MediaView.Configuration? public func prepareForReuse() { providerFaviconImageView.tag = (0..