fix: NewsView image layout not fixed issue

This commit is contained in:
CMK 2022-05-11 18:39:34 +08:00
parent 35d572660b
commit 945241e730
9 changed files with 58 additions and 40 deletions

View File

@ -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 = "<group>"; };
DB519B16281BCC2F00F0C99D /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = tr; path = tr.lproj/InfoPlist.strings; sourceTree = "<group>"; };
DB519B17281BCC2F00F0C99D /* tr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = tr; path = tr.lproj/Intents.stringsdict; sourceTree = "<group>"; };
DB51D170262832380062B7A1 /* BlurHashDecode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashDecode.swift; sourceTree = "<group>"; };
DB51D171262832380062B7A1 /* BlurHashEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashEncode.swift; sourceTree = "<group>"; };
DB564BD2269F3B35001E39A7 /* StatusFilterService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusFilterService.swift; sourceTree = "<group>"; };
DB59F10D25EF724F001F1DAB /* APIService+Poll.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIService+Poll.swift"; sourceTree = "<group>"; };
DB5B7294273112B100081888 /* FollowingListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowingListViewController.swift; sourceTree = "<group>"; };
@ -1167,7 +1162,6 @@
DB852D1E26FB037800FC9D81 /* SidebarViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarViewModel.swift; sourceTree = "<group>"; };
DB87D4442609BE0500D12C0D /* ComposeStatusPollOptionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeStatusPollOptionCollectionViewCell.swift; sourceTree = "<group>"; };
DB87D4502609CF1E00D12C0D /* ComposeStatusPollOptionAppendEntryCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeStatusPollOptionAppendEntryCollectionViewCell.swift; sourceTree = "<group>"; };
DB894CC327A5490600684B74 /* BlurhashImageCacheService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurhashImageCacheService.swift; sourceTree = "<group>"; };
DB89BA1025C10FF5008580ED /* Mastodon.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Mastodon.entitlements; sourceTree = "<group>"; };
DB8AF52B25C13561002E6C99 /* ViewStateStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewStateStore.swift; sourceTree = "<group>"; };
DB8AF52C25C13561002E6C99 /* DocumentStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DocumentStore.swift; sourceTree = "<group>"; };
@ -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 = "<group>";
@ -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 */,

View File

@ -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

View File

@ -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!

View File

@ -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<Key, UIImage>()

View File

@ -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)
}
}
}

View File

@ -6,6 +6,7 @@
//
import UIKit
import Combine
import MastodonSDK
import MastodonLocalization
import AlamofireImage

View File

@ -6,9 +6,14 @@
//
import UIKit
import Combine
import MastodonAsset
public final class NewsView: UIView {
static let imageViewWidth: CGFloat = 132
var disposeBag = Set<AnyCancellable>()
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..<Int.max).randomElement() ?? -1
imageView.prepareForReuse()
disposeBag.removeAll()
}
override init(frame: CGRect) {
@ -65,6 +74,7 @@ public final class NewsView: UIView {
extension NewsView {
private func _init() {
// container: H - [ textContainer | imageView ]
container.distribution = .fill
container.axis = .horizontal
container.spacing = 8
container.translatesAutoresizingMaskIntoConstraints = false
@ -121,6 +131,12 @@ extension NewsView {
NSLayoutConstraint.activate([
imageView.widthAnchor.constraint(equalToConstant: 132).priority(.required - 1),
])
imageView.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
imageView.setContentCompressionResistancePriority(.defaultLow - 100, for: .vertical)
imageView.imageView.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
imageView.imageView.setContentCompressionResistancePriority(.defaultLow - 100, for: .vertical)
imageView.blurhashImageView.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
imageView.blurhashImageView.setContentCompressionResistancePriority(.defaultLow - 100, for: .vertical)
imageView.isUserInteractionEnabled = false
}
}