Skip to content

Commit

Permalink
Nuke adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
testableapple committed Oct 19, 2023
1 parent 3f37012 commit fd32fad
Show file tree
Hide file tree
Showing 36 changed files with 110 additions and 108 deletions.
25 changes: 25 additions & 0 deletions Scripts/removePublicDeclarations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,38 @@ do
if [[ $directory == *"Nuke"* ]]; then
replaceDeclaration 'var log' 'var nukeLog' $f
replaceDeclaration 'log =' 'nukeLog =' $f
replaceDeclaration 'log: log' 'log: nukeLog' $f
replaceDeclaration 'signpost(log' 'signpost(nukeLog' $f
replaceDeclaration ' Cache(' ' NukeCache(' $f
replaceDeclaration ' Cache<' ' NukeCache<' $f
replaceDeclaration ' Image?' ' NukeImage?' $f
replaceDeclaration ' Image(' ' NukeImage(' $f
replaceDeclaration 'struct Image:' 'struct NukeImage:' $f
replaceDeclaration 'extension Image {' 'extension NukeImage {' $f
replaceDeclaration 'Content == Image' 'Content == NukeImage' $f
replaceDeclaration ' VideoPlayerView' ' NukeVideoPlayerView' $f
replaceDeclaration 'typealias Color' 'typealias NukeColor' $f
replaceDeclaration 'extension Color' 'extension NukeColor' $f
replaceDeclaration 'AssetType' 'NukeAssetType' $f
replaceDeclaration 'typealias ImageRequest = Nuke.ImageRequest' '' $f
replaceDeclaration 'typealias ImageResponse = Nuke.ImageResponse' '' $f
replaceDeclaration 'typealias ImagePipeline = Nuke.ImagePipeline' '' $f
replaceDeclaration 'typealias ImageContainer = Nuke.ImageContainer' '' $f
replaceDeclaration 'open class ' '' $f
replaceDeclaration 'import Nuke' '' $f

# Remove Cancellable interface duplicate
if [[ $f == *"DataLoader"* && `head -10 $f` == *"protocol Cancellable"* ]]; then
`sed -i '' -e '7,11d' $f`
fi

# Rename files
if [[ $f == *"Cache.swift"* ]]; then
new_f="${f/Cache.swift/NukeCache.swift}"
mv $f $new_f
elif [[ $f == *"VideoPlayerView.swift"* ]]; then
new_f="${f/VideoPlayerView.swift/NukeVideoPlayerView.swift}"
mv $f $new_f
fi
fi
done
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

import Combine
import Nuke
import StreamChat
import SwiftUI

Expand Down Expand Up @@ -163,7 +162,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {

@objc
private func didReceiveMemoryWarning() {
Nuke.ImageCache.shared.removeAll()
ImageCache.shared.removeAll()
messageCachingUtils.clearCache()
}

Expand Down Expand Up @@ -511,7 +510,7 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
messageCachingUtils.clearCache()
if messageController == nil {
utils.channelControllerFactory.clearCurrentController()
Nuke.ImageCache.shared.trim(toCost: utils.messageListConfig.cacheSizeOnChatDismiss)
ImageCache.shared.trim(toCost: utils.messageListConfig.cacheSizeOnChatDismiss)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ private struct ZoomableScrollViewImpl<Content: View>: UIViewControllerRepresenta
updateConstraintsCancellable = scrollView.publisher(for: \.bounds).map(\.size).removeDuplicates()
.sink { [unowned self] _ in
view.setNeedsUpdateConstraints()
}
doubleTapCancellable = doubleTap.sink { [unowned self] in handleDoubleTap() }
} as! any Cancellable // FIXME
doubleTapCancellable = doubleTap.sink { [unowned self] in handleDoubleTap() } as! any Cancellable // FIXME
}

func update(content: Content, doubleTap: AnyPublisher<Void, Never>) {
coordinator.hostingController.rootView = content
scrollView.setNeedsUpdateConstraints()
doubleTapCancellable = doubleTap.sink { [unowned self] in handleDoubleTap() }
doubleTapCancellable = doubleTap.sink { [unowned self] in handleDoubleTap() } as! any Cancellable // FIXME
}

func handleDoubleTap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down Expand Up @@ -106,7 +104,7 @@ struct LazyGiphyView: View {
var body: some View {
LazyImage(imageURL: source) { state in
if let imageContainer = state.imageContainer {
Image(imageContainer)
NukeImage(imageContainer)
} else if state.error != nil {
Color(.secondarySystemBackground)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//

import AVKit
import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Copyright © 2023 Stream.io Inc. All rights reserved.
//

import Nuke
import NukeUI
import StreamChat
import SwiftUI

Expand Down
24 changes: 12 additions & 12 deletions Sources/StreamChatSwiftUI/StreamNuke/Nuke/Decoding/AssetType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

/// A uniform type identifier (UTI).
struct AssetType: ExpressibleByStringLiteral, Hashable, Sendable {
struct NukeAssetType: ExpressibleByStringLiteral, Hashable, Sendable {
let rawValue: String

init(rawValue: String) {
Expand All @@ -16,42 +16,42 @@ struct AssetType: ExpressibleByStringLiteral, Hashable, Sendable {
self.rawValue = value
}

static let png: AssetType = "public.png"
static let jpeg: AssetType = "public.jpeg"
static let gif: AssetType = "com.compuserve.gif"
static let png: NukeAssetType = "public.png"
static let jpeg: NukeAssetType = "public.jpeg"
static let gif: NukeAssetType = "com.compuserve.gif"
/// HEIF (High Efficiency Image Format) by Apple.
static let heic: AssetType = "public.heic"
static let heic: NukeAssetType = "public.heic"

/// WebP
///
/// Native decoding support only available on the following platforms: macOS 11,
/// iOS 14, watchOS 7, tvOS 14.
static let webp: AssetType = "public.webp"
static let webp: NukeAssetType = "public.webp"

static let mp4: AssetType = "public.mpeg4"
static let mp4: NukeAssetType = "public.mpeg4"

/// The M4V file format is a video container format developed by Apple and
/// is very similar to the MP4 format. The primary difference is that M4V
/// files may optionally be protected by DRM copy protection.
static let m4v: AssetType = "public.m4v"
static let m4v: NukeAssetType = "public.m4v"

static let mov: AssetType = "public.mov"
static let mov: NukeAssetType = "public.mov"

var isVideo: Bool {
self == .mp4 || self == .m4v || self == .mov
}
}

extension AssetType {
extension NukeAssetType {
/// Determines a type of the image based on the given data.
init?(_ data: Data) {
guard let type = AssetType.make(data) else {
guard let type = NukeAssetType.make(data) else {
return nil
}
self = type
}

private static func make(_ data: Data) -> AssetType? {
private static func make(_ data: Data) -> NukeAssetType? {
func _match(_ numbers: [UInt8?], offset: Int = 0) -> Bool {
guard data.count >= numbers.count else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension ImageDecoders {
guard let image = makeImage() else {
throw ImageDecodingError.unknown
}
let type = AssetType(data)
let type = NukeAssetType(data)
var container = ImageContainer(image: image)
container.type = type
if type == .gif {
Expand All @@ -78,7 +78,7 @@ extension ImageDecoders {
lock.lock()
defer { lock.unlock() }

let assetType = AssetType(data)
let assetType = NukeAssetType(data)
if assetType == .gif { // Special handling for GIF
if !isPreviewForGIFGenerated, let image = ImageDecoders.Default._decode(data, scale: scale) {
isPreviewForGIFGenerated = true
Expand All @@ -99,7 +99,7 @@ extension ImageDecoders {
}

private func isProgressiveDecodingAllowed(for data: Data) -> Bool {
let assetType = AssetType(data)
let assetType = NukeAssetType(data)

// Determined whether the image supports progressive decoding or not
// (only proressive JPEG is allowed for now, but you can add support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension ImageDecoders {
/// data to the image container.
struct Empty: ImageDecoding, Sendable {
let isProgressive: Bool
private let assetType: AssetType?
private let assetType: NukeAssetType?

var isAsynchronous: Bool { false }

Expand All @@ -20,7 +20,7 @@ extension ImageDecoders {
/// `nil` by default.
/// - isProgressive: If `false`, returns nil for every progressive
/// scan. `false` by default.
init(assetType: AssetType? = nil, isProgressive: Bool = false) {
init(assetType: NukeAssetType? = nil, isProgressive: Bool = false) {
self.assetType = assetType
self.isProgressive = isProgressive
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import AVKit
extension ImageDecoders {
final class Video: ImageDecoding, @unchecked Sendable {
private var didProducePreview = false
private let type: AssetType
private let type: NukeAssetType
var isAsynchronous: Bool { true }

private let lock = NSLock()

init?(context: ImageDecodingContext) {
guard let type = AssetType(context.data), type.isVideo else { return nil }
guard let type = NukeAssetType(context.data), type.isVideo else { return nil }
self.type = type
}

Expand All @@ -28,7 +28,7 @@ extension ImageDecoders {
lock.lock()
defer { lock.unlock() }

guard let type = AssetType(data), type.isVideo else { return nil }
guard let type = NukeAssetType(data), type.isVideo else { return nil }
guard !didProducePreview else {
return nil // We only need one preview
}
Expand All @@ -41,7 +41,7 @@ extension ImageDecoders {
}
}

private func makePreview(for data: Data, type: AssetType) -> PlatformImage? {
private func makePreview(for data: Data, type: NukeAssetType) -> PlatformImage? {
let asset = AVDataAsset(data: data, type: type)
let generator = AVAssetImageGenerator(asset: asset)
guard let cgImage = try? generator.copyCGImage(at: CMTime(value: 0, timescale: 1), actualTime: nil) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension ImageEncoders {
guard let cgImage = image.cgImage else {
return nil
}
let type: AssetType
let type: NukeAssetType
if cgImage.isOpaque {
if isHEIFPreferred && ImageEncoders.ImageIO.isSupported(type: .heic) {
type = .heic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ extension ImageEncoders {
/// write most image file formats. This framework offers high efficiency,
/// color management, and access to image metadata.
struct ImageIO: ImageEncoding {
let type: AssetType
let type: NukeAssetType
let compressionRatio: Float

/// - parameter format: The output format. Make sure that the format is
/// supported on the current hardware.s
/// - parameter compressionRatio: 0.8 by default.
init(type: AssetType, compressionRatio: Float = 0.8) {
init(type: NukeAssetType, compressionRatio: Float = 0.8) {
self.type = type
self.compressionRatio = compressionRatio
}

private static let lock = NSLock()
private static var availability = [AssetType: Bool]()
private static var availability = [NukeAssetType: Bool]()

/// Returns `true` if the encoding is available for the given format on
/// the current hardware. Some of the most recent formats might not be
/// available so its best to check before using them.
static func isSupported(type: AssetType) -> Bool {
static func isSupported(type: NukeAssetType) -> Bool {
lock.lock()
defer { lock.unlock() }
if let isAvailable = availability[type] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension ImageEncoding where Self == ImageEncoders.Default {
}

extension ImageEncoding where Self == ImageEncoders.ImageIO {
static func imageIO(type: AssetType, compressionRatio: Float = 0.8) -> ImageEncoders.ImageIO {
static func imageIO(type: NukeAssetType, compressionRatio: Float = 0.8) -> ImageEncoders.ImageIO {
ImageEncoders.ImageIO(type: type, compressionRatio: compressionRatio)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct ImageContainer: @unchecked Sendable {
#endif

/// An image type.
var type: AssetType?
var type: NukeAssetType?

/// Returns `true` if the image in the container is a preview of the image.
var isPreview: Bool
Expand All @@ -53,7 +53,7 @@ struct ImageContainer: @unchecked Sendable {
var userInfo: [UserInfoKey: Any]

/// Initializes the container with the given image.
init(image: PlatformImage, type: AssetType? = nil, isPreview: Bool = false, data: Data? = nil, userInfo: [UserInfoKey: Any] = [:]) {
init(image: PlatformImage, type: NukeAssetType? = nil, isPreview: Bool = false, data: Data? = nil, userInfo: [UserInfoKey: Any] = [:]) {
self.image = image
self.type = type
self.isPreview = isPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Foundation

#if !os(watchOS)

private extension AssetType {
private extension NukeAssetType {
var avFileType: AVFileType? {
switch self {
case .mp4: return .mp4
Expand All @@ -22,7 +22,7 @@ private extension AssetType {
final class AVDataAsset: AVURLAsset {
private let resourceLoaderDelegate: DataAssetResourceLoader

init(data: Data, type: AssetType?) {
init(data: Data, type: NukeAssetType?) {
self.resourceLoaderDelegate = DataAssetResourceLoader(
data: data,
contentType: type?.avFileType?.rawValue ?? AVFileType.mp4.rawValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ enum Screen {
}

#if os(macOS)
typealias Color = NSColor
typealias NukeColor = NSColor
#else
typealias Color = UIColor
typealias NukeColor = UIColor
#endif

extension Color {
extension NukeColor {
/// Returns a hex representation of the color, e.g. "#FFFFAA".
var hex: String {
var (r, g, b, a) = (CGFloat(0), CGFloat(0), CGFloat(0), CGFloat(0))
Expand Down
Loading

0 comments on commit fd32fad

Please sign in to comment.