<commit_before>// 
// 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: Theme
    @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 {
            if let image = viewModel.viewState.avatarImage {
                Image(uiImage: image)
                    .resizable()
                    .frame(width: CGFloat(size.rawValue), height: CGFloat(size.rawValue))
                    .clipShape(Circle())
            } else {
                Text(firstCharacterCapitalized(displayName))
                    .padding(4)
                    .frame(width: CGFloat(size.rawValue), height: CGFloat(size.rawValue))
                    .foregroundColor(.white)
                    .background(stableColor)
                    .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)
            }
        }
        .onAppear {
            viewModel.inject(dependencies: dependencies)
            MXLog.debug("injected dependencies, \(dependencies)")
            viewModel.loadAvatar(
                mxContentUri: mxContentUri,
                avatarSize: size
            )
        }
    }
    
    private func firstCharacterCapitalized(_ string: String?) -> String {
        guard let character = string?.first else {
            return ""
        }
        return String(character).capitalized
    }
    
    /**
     Provides the same color each time for a specified matrixId.
     Same algorithm as in AvatarGenerator.
     */
    private var stableColor: Color {
        // Sum all characters
        let sum = matrixItemId.utf8
            .map({ UInt($0) })<commit_msg>Remove log<commit_after>// 
// 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: Theme
    @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 {
            if let image = viewModel.viewState.avatarImage {
                Image(uiImage: image)
                    .resizable()
                    .frame(width: CGFloat(size.rawValue), height: CGFloat(size.rawValue))
                    .clipShape(Circle())
            } else {
                Text(firstCharacterCapitalized(displayName))
                    .padding(4)
                    .frame(width: CGFloat(size.rawValue), height: CGFloat(size.rawValue))
                    .foregroundColor(.white)
                    .background(stableColor)
                    .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)
            }
        }
        .onAppear {
            viewModel.inject(dependencies: dependencies)
            viewModel.loadAvatar(
                mxContentUri: mxContentUri,
                avatarSize: size
            )
        }
    }
    
    private func firstCharacterCapitalized(_ string: String?) -> String {
        guard let character = string?.first else {
            return ""
        }
        return String(character).capitalized
    }
    
    /**
     Provides the same color each time for a specified matrixId.
     Same algorithm as in AvatarGenerator.
     */
    private var stableColor: Color {
        // Sum all characters
        let sum = matrixItemId.utf8
            .map({ UInt($0) })
{'fraction_non_alphanumeric': 0.08777368726990893, 'fraction_numerical': 0.008137957760124007, 'mean_word_length': 1.8886401790710687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4549991', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1292, 'n_words': 426}