Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #8375: Wallet account blockie v2 #8510

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions Sources/BraveWallet/Blockies/Blockies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ private struct XorshiftRandomNumberGenerator: RandomNumberGenerator {
class Blockies {
private var generator: XorshiftRandomNumberGenerator
private let colors: [Int] = [
0x5B5C63, 0x151E9A, 0x2197F9, 0x1FC3DC, 0x086582,
0x67D4B4, 0xAFCE57, 0xF0CB44, 0xF28A29, 0xFC798F,
0xC1226E, 0xFAB5EE, 0x9677EE, 0x5433B0,
0x423EEE, 0xE2E2FC, 0xFE5907, 0xFEDED6, 0x5F5CF1,
0x171553, 0x1C1E26, 0xE1E2E8,
]

init(seed: String) {
Expand All @@ -53,13 +52,17 @@ class Blockies {
let normalized = Double(generator.next()) / Double(Int32.max)
return UIColor(rgb: colors[Int(floor(normalized * 100)) % colors.count])
}

func rand() -> Double {
Double(generator.next()) / Double(Int32.max)
}

func image(length: Int, scale: CGFloat) -> UIImage {
let color = makeColor()
let backgroundColor = makeColor()
let spotColor = makeColor()

func data() -> [[Double]] {
func data() -> [Double] {
let dataLength = Int(ceil(Double(length) / 2.0))
var data: [[Double]] = []
for _ in 0..<length {
Expand All @@ -72,31 +75,43 @@ class Blockies {
let mirrorCopy = row.reversed()
data.append(row + mirrorCopy)
}
return data
return data.flatMap { $0 }
}

let size = CGSize(width: CGFloat(length) * scale, height: CGFloat(length) * scale)
let renderer = UIGraphicsImageRenderer(size: size)
let data = data()
let width = sqrt(Double(data.count))

let image = renderer.image { context in
backgroundColor.setFill()
context.fill(.init(origin: .zero, size: size))
spotColor.setFill()
for (y, row) in data.enumerated() {
for (x, value) in row.enumerated() {
let rect = CGRect(x: x, y: y, width: 1, height: 1)
.applying(CGAffineTransform(scaleX: scale, y: scale))
if value > 0 {
if value == 1 {
color.setFill()
} else {
spotColor.setFill()
}
context.fill(rect)
}
for (i, value) in data.enumerated() where value > 0 {
let row = floor(Double(i) / width)
let col = i % Int(width)
let fillColor = value == 1 ? color : spotColor
let shapeType = floor(rand() * 3)

switch shapeType {
case 0:
let rectSizeMultiplier = rand() * 2
let rect = CGRect(x: Int(col) * Int(scale), y: Int(row) * Int(scale), width: Int(scale * rectSizeMultiplier), height: Int(scale * rectSizeMultiplier))
fillColor.setFill()
context.fill(rect)
case 1:
let rectSizeMultiplier = rand()
let x = Int(col) * Int(scale) + Int(scale) / 2 - Int(scale * rectSizeMultiplier / 2)
let y = Int(row) * Int(scale) + Int(scale) / 2 - Int(scale * rectSizeMultiplier / 2)
let rect = CGRect(x: x, y: y, width: Int(scale * rectSizeMultiplier), height: Int(scale * rectSizeMultiplier))
fillColor.setFill()
context.cgContext.fillEllipse(in: rect)
default:
break
}
}
}

return image
}
}
Expand All @@ -111,9 +126,8 @@ struct Blockie: View {
var shape: Shape = .circle
StephenHeaps marked this conversation as resolved.
Show resolved Hide resolved

private var base: some View {
Image(uiImage: Blockies(seed: address.lowercased()).image(length: 8, scale: 16))
Image(uiImage: Blockies(seed: address.lowercased()).image(length: 4, scale: 25))
.resizable()
.blur(radius: 8, opaque: true)
StephenHeaps marked this conversation as resolved.
Show resolved Hide resolved
}

var body: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private struct AccountListRowView: View {
AddressView(address: account.address) {
Button(action: didSelect) {
HStack {
AccountView(address: account.address, name: account.name)
AccountView(address: account.address, name: account.name, blockieShape: .rectangle)
checkmark
}
.contentShape(Rectangle())
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveWallet/Crypto/Accounts/AccountsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct AccountsView: View {
selectedAccount = account
} label: {
AddressView(address: account.address) {
AccountView(address: account.address, name: account.name)
AccountView(address: account.address, name: account.name, blockieShape: .rectangle)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ private struct AccountActivityHeaderView: View {

var body: some View {
VStack {
Blockie(address: account.address)
Blockie(address: account.address, shape: .rectangle)
.frame(width: 64, height: 64)
.clipShape(RoundedRectangle(cornerRadius: 4))
.accessibilityHidden(true)
VStack(spacing: 4) {
Text(account.name)
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveWallet/Settings/DappsSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private struct SiteConnectionDetailView: View {
List {
Section(header: Text(String.localizedStringWithFormat(Strings.Wallet.manageSiteConnectionsDetailHeader, siteConnection.coin.localizedTitle))) {
ForEach(siteConnection.connectedAddresses, id: \.self) { address in
AccountView(address: address, name: siteConnectionStore.accountInfo(for: address)?.name ?? "")
AccountView(address: address, name: siteConnectionStore.accountInfo(for: address)?.name ?? "", blockieShape: .rectangle)
.swipeActions(edge: .trailing) {
Button(role: .destructive, action: {
withAnimation(.default) {
Expand Down