Skip to content

Commit

Permalink
Allow making empty and single image MMMPhoto
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh committed Oct 29, 2023
1 parent 1c1db4f commit bf7315a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion MMMCommonUI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMCommonUI"
s.version = "3.10.0"
s.version = "3.11.0"
s.summary = "Small UI-related pieces reused in many components from MMMTemple"
s.description = "#{s.summary}."
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand Down
38 changes: 38 additions & 0 deletions Sources/MMMCommonUI/MMMPhoto.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// MMMCommonUI. Part of MMMTemple.
// Copyright (C) 2016-2023 MediaMonks. All rights reserved.
//

import Foundation

/// A photo that vends the same image regardless of the requested size.
///
/// This can be handy when implementing APIs involving `MMMPhoto` while having only a single image.
/// (It could be useful to proxy it and do downscaling, but that could only help with rendering
/// and would not improve overall memory usage as we would be still holding the original image.)
public final class MMMPhotoFromLoadableImage: NSObject, MMMPhoto {

public let image: MMMLoadableImage

public init(_ image: MMMLoadableImage) {
self.image = image
}

public func image(forTargetSize targetSize: CGSize, contentMode: MMMPhotoContentMode) -> MMMLoadableImage {
self.image
}
}

/// A photo that is always failing to load.
///
/// Sometimes we need to implement a non-optional `MMMPhoto` without having an image. Returning an always failing
/// to load image is better in this case as it would allow to fall back to a placeholder of the corresponding image view.
public final class MMMEmptyPhoto: NSObject, MMMPhoto {

// Using the property of `MMMPublicLoadableImage` to quickly fail for `nil` images.
private lazy var image = MMMPublicLoadableImage(url: nil)

public func image(forTargetSize targetSize: CGSize, contentMode: MMMPhotoContentMode) -> MMMLoadableImage {
self.image
}
}

0 comments on commit bf7315a

Please sign in to comment.