Files
bundesmessenger-ios/RiotSwiftUI/Modules/Common/Avatar/View/AvatarImage.swift
T
David Langley ada377dcf2 Finish extraction
- Moves SwiftUI code out of Riot and into RiotSwiftUI which has no dependency on Matrix SDK.
- Git wasn't smart enough to see the file moves. Most feature function has remain unchanged. 1 change I did make was remove NotificationSettingsViewModel's dependence on MxPushRule, so that the view model could be moved into RiotSwiftUI.
- Add LocaleProvider to abstract VectorL10n's use of Matrix SDK language so it can be used in RiotSwiftUI.
- Split Theme into UKit/SwiftUI version to remove RiotSwiftUI's dependence on ThemeService and ThemeV1.
- Migrated from ThemeObserver to ThemePublisher. We push updates to ThemePublisher so that we can remove ThemeService as dependency.
- Add .DS_Store to .gitignore
2021-09-01 12:34:38 +01:00

101 lines
3.6 KiB
Swift

//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
import DesignKit
@available(iOS 14.0, *)
struct AvatarImage: View {
@Environment(\.theme) var theme: ThemeSwiftUI
@Environment(\.dependencies) var dependencies: DependencyContainer
@StateObject var viewModel = AvatarViewModel()
var mxContentUri: String?
var matrixItemId: String
var displayName: String?
var size: AvatarSize
var body: some View {
Group {
switch viewModel.viewState {
case .empty:
ProgressView()
case .placeholder(let firstCharacter, let colorIndex):
Text(firstCharacter)
.padding(4)
.frame(width: CGFloat(size.rawValue), height: CGFloat(size.rawValue))
.foregroundColor(.white)
.background(theme.colors.namesAndAvatars[colorIndex])
.clipShape(Circle())
// Make the text resizable (i.e. Make it large and then allow it to scale down)
.font(.system(size: 200))
.minimumScaleFactor(0.001)
case .avatar(let image):
Image(uiImage: image)
.resizable()
.frame(width: CGFloat(size.rawValue), height: CGFloat(size.rawValue))
.clipShape(Circle())
}
}
.onAppear {
viewModel.inject(dependencies: dependencies)
viewModel.loadAvatar(
mxContentUri: mxContentUri,
matrixItemId: matrixItemId,
displayName: displayName,
colorCount: theme.colors.namesAndAvatars.count,
avatarSize: size
)
}
}
}
@available(iOS 14.0, *)
extension AvatarImage {
init(avatarData: AvatarInputType, size: AvatarSize) {
self.init(
mxContentUri: avatarData.mxContentUri,
matrixItemId: avatarData.matrixItemId,
displayName: avatarData.displayName,
size: size
)
}
}
@available(iOS 14.0, *)
struct AvatarImage_Previews: PreviewProvider {
static let mxContentUri = "fakeUri"
static let name = "Alice"
static var previews: some View {
Group {
HStack {
VStack(alignment: .center, spacing: 20) {
AvatarImage(avatarData: MockAvatarInput.example, size: .xSmall)
AvatarImage(avatarData: MockAvatarInput.example, size: .medium)
AvatarImage(avatarData: MockAvatarInput.example, size: .xLarge)
}
VStack(alignment: .center, spacing: 20) {
AvatarImage(mxContentUri: nil, matrixItemId: name, displayName: name, size: .xSmall)
AvatarImage(mxContentUri: nil, matrixItemId: name, displayName: name, size: .medium)
AvatarImage(mxContentUri: nil, matrixItemId: name, displayName: name, size: .xLarge)
}
}
.addDependency(MockAvatarService.example)
}
}
}