mirror of
https://github.com/element-hq/element-x-ios.git
synced 2025-03-10 21:39:12 +00:00
Expose avatar background for a given content id from ElementColors (#209)
This commit is contained in:
parent
74cab0fc0b
commit
e9a272c120
@ -57,6 +57,11 @@ public struct ElementColors {
|
||||
public var systemGray4: Color { Color(.systemGray4) }
|
||||
public var systemGray5: Color { Color(.systemGray5) }
|
||||
public var systemGray6: Color { Color(.systemGray6) }
|
||||
|
||||
public func avatarBackground(for contentId: String) -> Color {
|
||||
let colorIndex = Int(contentId.hashCode % Int32(contentAndAvatars.count))
|
||||
return contentAndAvatars[colorIndex % contentAndAvatars.count]
|
||||
}
|
||||
|
||||
// MARK: - Temp
|
||||
|
||||
@ -119,6 +124,11 @@ public extension UIColor {
|
||||
public var systemGray4: UIColor { .systemGray4 }
|
||||
public var systemGray5: UIColor { .systemGray5 }
|
||||
public var systemGray6: UIColor { .systemGray6 }
|
||||
|
||||
public func avatarBackground(for contentId: String) -> UIColor {
|
||||
let colorIndex = Int(contentId.hashCode % Int32(contentAndAvatars.count))
|
||||
return contentAndAvatars[colorIndex % contentAndAvatars.count]
|
||||
}
|
||||
|
||||
// MARK: - Temp
|
||||
|
||||
@ -139,3 +149,17 @@ public extension UIColor {
|
||||
public var tempActionBackgroundTint: UIColor { tempActionBackground.withAlphaComponent(0.2) }
|
||||
public var tempActionForegroundTint: UIColor { tempActionForeground.withAlphaComponent(0.2) }
|
||||
}
|
||||
|
||||
private extension String {
|
||||
/// Calculates a numeric hash same as Element Web
|
||||
/// See original function here https://github.com/matrix-org/matrix-react-sdk/blob/321dd49db4fbe360fc2ff109ac117305c955b061/src/utils/FormattingUtils.js#L47
|
||||
var hashCode: Int32 {
|
||||
var hash: Int32 = 0
|
||||
|
||||
for character in self {
|
||||
let shiftedHash = hash << 5
|
||||
hash = shiftedHash.subtractingReportingOverflow(hash).partialValue + Int32(character.unicodeScalars[character.unicodeScalars.startIndex].value)
|
||||
}
|
||||
return abs(hash)
|
||||
}
|
||||
}
|
||||
|
@ -42,9 +42,7 @@ struct PlaceholderAvatarImage: View {
|
||||
return .element.accent
|
||||
}
|
||||
|
||||
let colors = Color.element.contentAndAvatars
|
||||
let colorIndex = Int(contentId.hashCode % Int32(colors.count))
|
||||
return Color.element.contentAndAvatars[colorIndex % colors.count]
|
||||
return .element.avatarBackground(for: contentId)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user