-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from RakuyoKit/fix/update-ButtonRow-image
Adjust the image setting method of `ImageRow` and `ButtonRow`
- Loading branch information
Showing
9 changed files
with
322 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// AnyImageProviding.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/5/27. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
// MARK: - AnyImageProviding | ||
|
||
public protocol AnyImageProviding: Equatable { | ||
associatedtype Value | ||
|
||
/// Storing the original object. | ||
var value: Value? { get } | ||
|
||
/// Used to implement `Equatable`. | ||
/// | ||
/// When using this type, you do not need to care about the specifics of the value. | ||
var equals: (Value?) -> Bool { get } | ||
} | ||
|
||
// MARK: - Equatable | ||
|
||
extension AnyImageProviding { | ||
public static func == (lhs: Self, rhs: Self) -> Bool { | ||
lhs.equals(rhs.value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// AnyButtonImageContent.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/5/27. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
import RAKCore | ||
|
||
// MARK: - AnyButtonImageContent | ||
|
||
public struct AnyButtonImageContent<View>: AnyImageProviding { | ||
public typealias Value = ButtonImageContentProviding | ||
|
||
public typealias Input = Equatable & Value | ||
|
||
public let value: (any Value)? | ||
|
||
public let equals: ((any Value)?) -> Bool | ||
|
||
public let setForViewAction: (View?, UIControl.State) -> Void | ||
|
||
public init<T: Input>(_ value: T?) { | ||
self.value = value | ||
equals = { ($0 as? T == value) } | ||
setForViewAction = { value?.setForView($0, state: $1) } | ||
} | ||
} | ||
|
||
// MARK: ButtonImageContentProviding | ||
|
||
extension AnyButtonImageContent: ButtonImageContentProviding { | ||
public func setForView<V>(_ view: V?, state: UIControl.State) { | ||
setForViewAction(view as? View, state) | ||
} | ||
} | ||
|
||
// MARK: FastImageContentProviding | ||
|
||
extension AnyButtonImageContent: FastImageContentProviding { | ||
public static func asset(name: String, bundle: Bundle = .main, with configuration: UIImage.Configuration? = nil) -> Self { | ||
.init(UIImage(named: name, in: bundle, with: configuration)) | ||
} | ||
|
||
public static func data(_ data: Data) -> Self { | ||
.init(UIImage(data: data)) | ||
} | ||
|
||
public static func file(path: String) -> Self { | ||
.init(UIImage(contentsOfFile: path)) | ||
} | ||
|
||
public static func sfSymbols(name: String, configuration: UIImage.SymbolConfiguration? = nil) -> Self { | ||
.init(UIImage(systemName: name, withConfiguration: configuration)) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Sources/Epoxy/Row/ButtonRow/ButtonImageContentProviding.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ButtonImageContentProviding.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/5/27. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
// MARK: - ButtonImageContentProviding | ||
|
||
/// Provider that can provide image for ``ButtonRow`` | ||
public protocol ButtonImageContentProviding: Equatable { | ||
func setForView<V>(_ view: V?, state: UIControl.State) | ||
} | ||
|
||
// MARK: - UIImage + ButtonImageContentProviding | ||
|
||
extension UIImage: ButtonImageContentProviding { | ||
public func setForView<V>(_ view: V?, state: UIControl.State) { | ||
guard let view else { return } | ||
|
||
if let button = view as? UIButton { | ||
button.setImage(self, for: state) | ||
} else { | ||
assertionFailure("UIImage.setForView(_:state:) has no implementation for the \(type(of: view)) type") | ||
} | ||
} | ||
} | ||
|
||
// MARK: - String + ButtonImageContentProviding | ||
|
||
extension String: ButtonImageContentProviding { | ||
public func setForView<V>(_ view: V?, state: UIControl.State) { | ||
guard let view else { return } | ||
|
||
if let button = view as? UIButton { | ||
button.setImage(.init(named: self), for: state) | ||
} else { | ||
assertionFailure("String.setForView(_:state:) has no implementation for the \(type(of: view)) type") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// AnyImageContent.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/5/27. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
import RAKCore | ||
|
||
// MARK: - AnyImageContent | ||
|
||
public struct AnyImageContent<View>: AnyImageProviding { | ||
public typealias Value = ImageContentProviding | ||
|
||
public typealias Input = Equatable & Value | ||
|
||
public let value: (any Value)? | ||
|
||
public let equals: ((any Value)?) -> Bool | ||
|
||
public let setForViewAction: (View?) -> Void | ||
|
||
public init<T: Input>(_ value: T?) { | ||
self.value = value | ||
equals = { ($0 as? T == value) } | ||
setForViewAction = { value?.setForView($0) } | ||
} | ||
} | ||
|
||
// MARK: ImageContentProviding | ||
|
||
extension AnyImageContent: ImageContentProviding { | ||
public func setForView<V>(_ view: V?) { | ||
setForViewAction(view as? View) | ||
} | ||
} | ||
|
||
// MARK: FastImageContentProviding | ||
|
||
extension AnyImageContent: FastImageContentProviding { | ||
public static func asset(name: String, bundle: Bundle = .main, with configuration: UIImage.Configuration? = nil) -> Self { | ||
.init(UIImage(named: name, in: bundle, with: configuration)) | ||
} | ||
|
||
public static func data(_ data: Data) -> Self { | ||
.init(UIImage(data: data)) | ||
} | ||
|
||
public static func file(path: String) -> Self { | ||
.init(UIImage(contentsOfFile: path)) | ||
} | ||
|
||
public static func sfSymbols(name: String, configuration: UIImage.SymbolConfiguration? = nil) -> Self { | ||
.init(UIImage(systemName: name, withConfiguration: configuration)) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Sources/Epoxy/Row/ImageRow/FastImageContentProviding.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// ImageContentProviding.swift | ||
// RakuyoKit | ||
// | ||
// Created by Rakuyo on 2024/5/27. | ||
// Copyright © 2024 RakuyoKit. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
public protocol FastImageContentProviding { | ||
static func asset(name: String, bundle: Bundle, with configuration: UIImage.Configuration?) -> Self | ||
static func data(_ data: Data) -> Self | ||
static func file(path: String) -> Self | ||
static func sfSymbols(name: String, configuration: UIImage.SymbolConfiguration?) -> Self | ||
} |
Oops, something went wrong.