This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
AvatarImageView.swift
126 lines (107 loc) · 4.49 KB
/
AvatarImageView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// Seaglass, a native macOS Matrix client
// Copyright © 2018, Neil Alexander
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
import Cocoa
import SwiftMatrixSDK
class AvatarImageView: ContextImageView {
var mxcUrl: String?
var url: String?
required init?(coder: NSCoder) {
super.init(coder: coder)
if layer == nil {
layer = CALayer()
}
wantsLayer = true
canDrawSubviewsIntoLayer = true
layer?.masksToBounds = true
layer?.contentsGravity = kCAGravityResizeAspectFill
layer?.cornerRadius = (frame.width)/2
}
override func layout() {
layer?.cornerRadius = (frame.width)/2
super.layout()
}
override var image: NSImage? {
set {
layer?.contents = newValue
super.image = newValue
}
get {
return super.image
}
}
func setAvatar(forMxcUrl: String?, defaultImage: NSImage, useCached: Bool = true) {
image = defaultImage
mxcUrl = forMxcUrl
guard mxcUrl != nil else { return }
if mxcUrl!.hasPrefix("mxc://") {
url = MatrixServices.inst.client.url(ofContentThumbnail: forMxcUrl, toFitViewSize: CGSize(width: 96, height: 96), with: MXThumbnailingMethodScale)
guard url != nil else { return }
if url!.hasPrefix("http://") || url!.hasPrefix("https://") {
guard let path = MXMediaManager.cachePathForMedia(withURL: url, andType: nil, inFolder: kMXMediaManagerAvatarThumbnailFolder) else { return }
if FileManager.default.fileExists(atPath: path) && useCached {
{ [weak self] in
if let image = MXMediaManager.loadThroughCache(withFilePath: path) {
self?.image = image
self?.layout()
}
}()
} else {
DispatchQueue.main.async {
let previousPath = path
MXMediaManager.downloadMedia(fromURL: self.url!, andSaveAtFilePath: path, success: { [weak self] in
if let image = MXMediaManager.loadThroughCache(withFilePath: path) {
guard previousPath == path else { return }
self?.image = image
self?.layout()
}
}) { [weak self] (error) in
guard previousPath == path else { return }
self?.image = defaultImage
self?.layout()
}
}
}
}
}
}
func setAvatar(forUserId userId: String, useCached: Bool = true) {
guard let user = MatrixServices.inst.session.user(withUserId: userId) else {
setAvatar(forText: "?")
return
}
if user.avatarUrl != "" {
setAvatar(forMxcUrl: user.avatarUrl, defaultImage: NSImage.create(withLetterString: user.displayname ?? "?"), useCached: useCached)
} else {
setAvatar(forText: user.displayname)
}
}
func setAvatar(forRoomId roomId: String, useCached: Bool = true) {
guard let room = MatrixServices.inst.session.room(withRoomId: roomId) else {
setAvatar(forText: "?")
return
}
if room.summary.avatar != "" {
setAvatar(forMxcUrl: room.summary.avatar, defaultImage: NSImage.create(withLetterString: room.summary.displayname ?? "?"), useCached: useCached)
} else {
setAvatar(forText: room.summary.displayname)
}
}
func setAvatar(forText: String) {
image = NSImage.create(withLetterString: forText)
}
}