mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Add a rounded rect border around space avatars
.. and also refactor the LoadableAvatarImage to stop having knowledge of spaces and instead focus on generic shapes.
This commit is contained in:
committed by
Stefan Ceriu
parent
20a84dd896
commit
591ce3975b
@@ -9,10 +9,15 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct LoadableAvatarImage: View {
|
struct LoadableAvatarImage: View {
|
||||||
|
enum Shape {
|
||||||
|
case circle
|
||||||
|
case roundedRect
|
||||||
|
}
|
||||||
|
|
||||||
private let url: URL?
|
private let url: URL?
|
||||||
private let name: String?
|
private let name: String?
|
||||||
private let contentID: String
|
private let contentID: String
|
||||||
private let isSpace: Bool
|
private let shape: Shape
|
||||||
private let avatarSize: Avatars.Size
|
private let avatarSize: Avatars.Size
|
||||||
private let mediaProvider: MediaProviderProtocol?
|
private let mediaProvider: MediaProviderProtocol?
|
||||||
private let onTap: ((URL) -> Void)?
|
private let onTap: ((URL) -> Void)?
|
||||||
@@ -22,14 +27,14 @@ struct LoadableAvatarImage: View {
|
|||||||
init(url: URL?,
|
init(url: URL?,
|
||||||
name: String?,
|
name: String?,
|
||||||
contentID: String,
|
contentID: String,
|
||||||
isSpace: Bool = false,
|
shape: LoadableAvatarImage.Shape = .circle,
|
||||||
avatarSize: Avatars.Size,
|
avatarSize: Avatars.Size,
|
||||||
mediaProvider: MediaProviderProtocol?,
|
mediaProvider: MediaProviderProtocol?,
|
||||||
onTap: ((URL) -> Void)? = nil) {
|
onTap: ((URL) -> Void)? = nil) {
|
||||||
self.url = url
|
self.url = url
|
||||||
self.name = name
|
self.name = name
|
||||||
self.contentID = contentID
|
self.contentID = contentID
|
||||||
self.isSpace = isSpace
|
self.shape = shape
|
||||||
self.avatarSize = avatarSize
|
self.avatarSize = avatarSize
|
||||||
self.mediaProvider = mediaProvider
|
self.mediaProvider = mediaProvider
|
||||||
self.onTap = onTap
|
self.onTap = onTap
|
||||||
@@ -54,7 +59,7 @@ struct LoadableAvatarImage: View {
|
|||||||
avatar
|
avatar
|
||||||
.frame(width: frameSize, height: frameSize)
|
.frame(width: frameSize, height: frameSize)
|
||||||
.background(Color.compound.bgCanvasDefault)
|
.background(Color.compound.bgCanvasDefault)
|
||||||
.clipAvatar(isSpace: isSpace, scaledSize: _frameSize)
|
.avatarShape(shape, scaledSize: _frameSize)
|
||||||
.environment(\.shouldAutomaticallyLoadImages, true) // We always load avatars.
|
.environment(\.shouldAutomaticallyLoadImages, true) // We always load avatars.
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,35 +82,38 @@ struct LoadableAvatarImage: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension View {
|
extension View {
|
||||||
func clipAvatar(isSpace: Bool, size: CGFloat) -> some View {
|
func avatarShape(_ shape: LoadableAvatarImage.Shape, size: CGFloat) -> some View {
|
||||||
modifier(ClipAvatarModifier(isSpace: isSpace, size: size))
|
modifier(AvatarShapeModifier(shape: shape, size: size))
|
||||||
}
|
}
|
||||||
|
|
||||||
func clipAvatar(isSpace: Bool, scaledSize: ScaledMetric<CGFloat>) -> some View {
|
func avatarShape(_ shape: LoadableAvatarImage.Shape, scaledSize: ScaledMetric<CGFloat>) -> some View {
|
||||||
modifier(ClipAvatarModifier(isSpace: isSpace, scaledSize: scaledSize))
|
modifier(AvatarShapeModifier(shape: shape, scaledSize: scaledSize))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ClipAvatarModifier: ViewModifier {
|
private struct AvatarShapeModifier: ViewModifier {
|
||||||
private let isSpace: Bool
|
private let shape: LoadableAvatarImage.Shape
|
||||||
@ScaledMetric private var scaledSize: CGFloat
|
@ScaledMetric private var scaledSize: CGFloat
|
||||||
|
|
||||||
init(isSpace: Bool, size: CGFloat) {
|
init(shape: LoadableAvatarImage.Shape, size: CGFloat) {
|
||||||
self.isSpace = isSpace
|
self.shape = shape
|
||||||
_scaledSize = ScaledMetric(wrappedValue: size)
|
_scaledSize = ScaledMetric(wrappedValue: size)
|
||||||
}
|
}
|
||||||
|
|
||||||
init(isSpace: Bool, scaledSize: ScaledMetric<CGFloat>) {
|
init(shape: LoadableAvatarImage.Shape, scaledSize: ScaledMetric<CGFloat>) {
|
||||||
self.isSpace = isSpace
|
self.shape = shape
|
||||||
_scaledSize = scaledSize
|
_scaledSize = scaledSize
|
||||||
}
|
}
|
||||||
|
|
||||||
func body(content: Content) -> some View {
|
func body(content: Content) -> some View {
|
||||||
content
|
switch shape {
|
||||||
.clipShape(avatarShape)
|
case .circle:
|
||||||
}
|
content.clipShape(Circle())
|
||||||
|
case .roundedRect:
|
||||||
private var avatarShape: some Shape {
|
let shape = RoundedRectangle(cornerRadius: scaledSize / 4)
|
||||||
isSpace ? AnyShape(RoundedRectangle(cornerRadius: scaledSize / 4)) : AnyShape(Circle())
|
content
|
||||||
|
.clipShape(shape)
|
||||||
|
.overlay(shape.stroke(.compound.iconQuaternaryAlpha, lineWidth: 1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ struct OverridableAvatarImage: View {
|
|||||||
let url: URL?
|
let url: URL?
|
||||||
let name: String?
|
let name: String?
|
||||||
let contentID: String
|
let contentID: String
|
||||||
let isSpace: Bool
|
let shape: LoadableAvatarImage.Shape
|
||||||
let avatarSize: Avatars.Size
|
let avatarSize: Avatars.Size
|
||||||
let mediaProvider: MediaProviderProtocol?
|
let mediaProvider: MediaProviderProtocol?
|
||||||
|
|
||||||
@@ -28,12 +28,12 @@ struct OverridableAvatarImage: View {
|
|||||||
ProgressView()
|
ProgressView()
|
||||||
}
|
}
|
||||||
.scaledFrame(size: avatarSize.value)
|
.scaledFrame(size: avatarSize.value)
|
||||||
.clipAvatar(isSpace: isSpace, size: avatarSize.value)
|
.avatarShape(shape, size: avatarSize.value)
|
||||||
} else {
|
} else {
|
||||||
LoadableAvatarImage(url: url,
|
LoadableAvatarImage(url: url,
|
||||||
name: name,
|
name: name,
|
||||||
contentID: contentID,
|
contentID: contentID,
|
||||||
isSpace: isSpace,
|
shape: shape,
|
||||||
avatarSize: avatarSize,
|
avatarSize: avatarSize,
|
||||||
mediaProvider: mediaProvider)
|
mediaProvider: mediaProvider)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ struct RoomAvatarImage: View {
|
|||||||
LoadableAvatarImage(url: avatarURL,
|
LoadableAvatarImage(url: avatarURL,
|
||||||
name: name,
|
name: name,
|
||||||
contentID: id,
|
contentID: id,
|
||||||
isSpace: true,
|
shape: .roundedRect,
|
||||||
avatarSize: avatarSize,
|
avatarSize: avatarSize,
|
||||||
mediaProvider: mediaProvider,
|
mediaProvider: mediaProvider,
|
||||||
onTap: onAvatarTap)
|
onTap: onAvatarTap)
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ struct RoomDetailsEditScreen: View {
|
|||||||
url: context.viewState.avatarURL,
|
url: context.viewState.avatarURL,
|
||||||
name: context.viewState.initialName,
|
name: context.viewState.initialName,
|
||||||
contentID: context.viewState.roomID,
|
contentID: context.viewState.roomID,
|
||||||
isSpace: context.viewState.isSpace,
|
shape: context.viewState.isSpace ? .roundedRect : .circle,
|
||||||
avatarSize: .user(on: .memberDetails),
|
avatarSize: .user(on: .memberDetails),
|
||||||
mediaProvider: context.mediaProvider)
|
mediaProvider: context.mediaProvider)
|
||||||
.accessibilityLabel(L10n.a11yEditAvatar)
|
.accessibilityLabel(L10n.a11yEditAvatar)
|
||||||
|
|||||||
+1
-1
@@ -64,7 +64,7 @@ struct UserDetailsEditScreen: View {
|
|||||||
url: context.viewState.selectedAvatarURL,
|
url: context.viewState.selectedAvatarURL,
|
||||||
name: context.viewState.currentDisplayName,
|
name: context.viewState.currentDisplayName,
|
||||||
contentID: context.viewState.userID,
|
contentID: context.viewState.userID,
|
||||||
isSpace: false,
|
shape: .circle,
|
||||||
avatarSize: .user(on: .editUserDetails),
|
avatarSize: .user(on: .editUserDetails),
|
||||||
mediaProvider: context.mediaProvider)
|
mediaProvider: context.mediaProvider)
|
||||||
.overlay(alignment: .bottomTrailing) {
|
.overlay(alignment: .bottomTrailing) {
|
||||||
|
|||||||
Reference in New Issue
Block a user