-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ios: SwiftUI wrapper and test previews
- Loading branch information
Showing
3 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
ios/Packages/PolkadotIdenticon/Sources/PolkadotIdenticon/Public/PolkadotIdenticonView.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,59 @@ | ||
// | ||
// PolkadotIdenticonView.swift | ||
// | ||
// | ||
// Created by Krzysztof Rodak on 02/08/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// `PolkadotIdenticonView` is a SwiftUI view that renders a Polkadot Identicon. | ||
/// | ||
/// An Identicon is a visual representation of a public key value, typically used to represent user identities in the | ||
/// context of cryptographic applications. | ||
/// This struct uses `PolkadotIdenticonGenerator` to generate a unique image, based on a provided public key input, | ||
/// which can then be rendered within the SwiftUI view hierarchy. | ||
public struct PolkadotIdenticonView: View { | ||
private let identiconGenerator: PolkadotIdenticonGenerator = PolkadotIdenticonGenerator() | ||
|
||
/// The public key input based on which the Identicon is generated. | ||
/// The public key can be in one of three formats: raw binary data (`Data`), a hexadecimal string, or a base58 | ||
/// string. | ||
let publicKey: PublicKey | ||
|
||
/// The size of the Identicon image to be generated. | ||
let size: CGFloat | ||
|
||
/// Initializes a new instance of `PolkadotIdenticonView`. | ||
/// | ||
/// - Parameters: | ||
/// - publicKey: The public key input based on which the Identicon is generated. This could be any unique public | ||
/// key. | ||
/// - size: The size of the Identicon image to be generated. | ||
public init(publicKey: PublicKey, size: CGFloat) { | ||
self.publicKey = publicKey | ||
self.size = size | ||
} | ||
|
||
/// Defines the content and behavior of this view. | ||
/// | ||
/// The body property returns an `Image` view if an Identicon image can be generated from the public key, | ||
/// or an `EmptyView` if not. | ||
public var body: some View { | ||
if let identiconImage = createIdenticonImage() { | ||
Image(uiImage: identiconImage) | ||
.resizable() | ||
.frame(width: size, height: size) | ||
.aspectRatio(contentMode: .fit) | ||
} else { | ||
EmptyView() | ||
} | ||
} | ||
|
||
/// Helper function to generate an Identicon image. | ||
/// | ||
/// - Returns: A `UIImage` instance representing the generated Identicon, or nil if the image couldn't be generated. | ||
private func createIdenticonImage() -> UIImage? { | ||
identiconGenerator.generateIdenticonImage(from: publicKey, size: size) | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
ios/PolkadotVault/Components/Identicons/PolkadotIdenticonViewPreviews.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,31 @@ | ||
// | ||
// PolkadotIdenticonViewPreviews.swift | ||
// PolkadotVault | ||
// | ||
// Created by Krzysztof Rodak on 02/08/2023. | ||
// | ||
|
||
import PolkadotIdenticon | ||
import SwiftUI | ||
|
||
struct PolkadotIdenticonView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
VStack { | ||
PolkadotIdenticonView( | ||
publicKey: .hex("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"), | ||
size: 100 | ||
) | ||
PolkadotIdenticonView( | ||
publicKey: .base58("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"), | ||
size: 100 | ||
) | ||
PolkadotIdenticonView( | ||
publicKey: .data(Data([ | ||
212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, | ||
133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125 | ||
])), | ||
size: 100 | ||
) | ||
} | ||
} | ||
} |