Skip to content

Commit

Permalink
#34 Refactor PlaceholderAvatarImage to accept a text only
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailgulek committed Jun 23, 2022
1 parent 25fb6ce commit f742baa
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ElementX/Sources/Screens/HomeScreen/View/HomeScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct RoomCell: View {
.frame(width: 40, height: 40)
.mask(Circle())
} else {
PlaceholderAvatarImage(text: room.displayName ?? "")
PlaceholderAvatarImage(text: room.displayName ?? room.id)
.clipShape(Circle())
.frame(width: 40, height: 40)
}
Expand Down
4 changes: 2 additions & 2 deletions ElementX/Sources/Screens/RoomScreen/View/RoomHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct RoomHeaderView: View {
.scaledToFill()
.accessibilityIdentifier("roomAvatarImage")
} else {
PlaceholderAvatarImage(firstCharacter: String(context.viewState.roomTitle.first ?? Character("")))
PlaceholderAvatarImage(text: context.viewState.roomTitle)
.accessibilityIdentifier("roomAvatarPlaceholderImage")
}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ struct RoomHeaderView_Previews: PreviewProvider {
let viewModel = RoomScreenViewModel(timelineController: MockRoomTimelineController(),
timelineViewFactory: RoomTimelineViewFactory(),
roomName: "Some Room name",
roomAvatar: Asset.Images.appLogo.image,
roomAvatar: nil,
roomEncryptionBadge: Asset.Images.encryptionTrusted.image
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct EventBasedTimelineSenderView: View {
.scaledToFill()
.overlay(Circle().stroke(Color.element.accent))
} else {
PlaceholderAvatarImage(firstCharacter: String(firstLetter))
PlaceholderAvatarImage(text: timelineItem.senderDisplayName ?? timelineItem.senderId)
}
}
.clipShape(Circle())
Expand All @@ -50,14 +50,6 @@ struct EventBasedTimelineSenderView: View {

.animation(.default, value: timelineItem.senderAvatar)
}

private var firstLetter: String {
if let senderDisplayName = timelineItem.senderDisplayName {
return senderDisplayName.prefix(1).uppercased()
} else {
return timelineItem.senderId.prefix(2).suffix(1).uppercased()
}
}
}

struct EventBasedTimelineSenderView_Previews: PreviewProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import SwiftUI

struct PlaceholderAvatarImage: View {

let firstCharacter: String
private let textForImage: String

var body: some View {
ZStack {
Color.element.accent
Text(firstCharacter)
Text(textForImage)
.padding(4)
.foregroundColor(.white)
// Make the text resizable (i.e. Make it large and then allow it to scale down)
Expand All @@ -33,16 +33,8 @@ struct PlaceholderAvatarImage: View {
.aspectRatio(1, contentMode: .fill)
}

init(firstCharacter: String) {
self.firstCharacter = firstCharacter
}

init(text: String) {
if let first = text.first {
firstCharacter = String(first)
} else {
firstCharacter = ""
}
textForImage = text.first?.uppercased() ?? ""
}
}

Expand All @@ -54,7 +46,7 @@ struct PlaceholderAvatarImage_Previews: PreviewProvider {

@ViewBuilder
static var body: some View {
PlaceholderAvatarImage(firstCharacter: "X")
PlaceholderAvatarImage(text: "X")
.clipShape(Circle())
.frame(width: 150, height: 100)
}
Expand Down

0 comments on commit f742baa

Please sign in to comment.