-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCallRatingModalViewController.swift
183 lines (149 loc) · 6.78 KB
/
CallRatingModalViewController.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// CallRatingModalViewController.swift
// Telegram
//
// Created by keepcoder on 12/05/2017.
// Copyright © 2017 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import TelegramCore
import SwiftSignalKit
import Postbox
private enum CallRatingState {
case stars
}
private class CallRatingModalView: View {
let rating:View = View()
let textView = TextView()
var starsChangeHandler:((Int32?)->Void)? = nil
private(set) var stars:Int32? = nil
var state:CallRatingState = .stars {
didSet {
if oldValue != state {
updateState(state, animated: true)
}
}
}
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
var x:CGFloat = 0
for i in 0 ..< 5 {
let star = ImageButton()
star.set(image: #imageLiteral(resourceName: "Icon_CallStar").precomposed(theme.colors.accent), for: .Normal)
star.sizeToFit()
star.setFrameOrigin(x, 0)
rating.addSubview(star)
x += floorToScreenPixels(backingScaleFactor, star.frame.width) + 10
star.set(handler: { [weak self] current in
for j in 0 ... i {
(self?.rating.subviews[j] as? ImageButton)?.set(image: #imageLiteral(resourceName: "Icon_CallStar_Highlighted").precomposed(theme.colors.accent), for: .Normal)
}
for j in i + 1 ..< 5 {
(self?.rating.subviews[j] as? ImageButton)?.set(image: #imageLiteral(resourceName: "Icon_CallStar").precomposed(theme.colors.accent), for: .Normal)
}
self?.state = .stars
delay(0.15, closure: {
self?.starsChangeHandler?( Int32(i + 1) )
})
}, for: .Click)
}
rating.setFrameSize(x - 10, floorToScreenPixels(backingScaleFactor, rating.subviews[0].frame.height))
addSubview(rating)
addSubview(textView)
textView.isSelectable = false
textView.userInteractionEnabled = false
updateState(.stars)
let layout = TextViewLayout(.initialize(string: strings().callRatingModalText, color: theme.colors.text, font: .medium(.text)), alignment: .center)
layout.measure(width: frame.width - 60)
textView.update(layout)
needsLayout = true
}
override func layout() {
super.layout()
rating.centerX(y: frame.midY + 5)
textView.centerX(y: frame.midY - textView.frame.height - 5)
}
private func updateState(_ state:CallRatingState, animated: Bool = false) {
switch state {
case .stars:
rating.change(pos: focus(rating.frame.size).origin, animated: animated)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func makeRatingView(_ rating:Int) {
}
}
class CallRatingModalViewController: ModalViewController {
private let context:AccountContext
private let callId:CallId
private var starsCount:Int32? = nil
private let isVideo: Bool
private let userInitiated: Bool
init(_ context: AccountContext, callId:CallId, userInitiated: Bool, isVideo: Bool) {
self.context = context
self.callId = callId
self.isVideo = isVideo
self.userInitiated = userInitiated
super.init(frame: NSMakeRect(0, 0, 300, 100))
bar = .init(height: 0)
}
private var genericView:CallRatingModalView {
return view as! CallRatingModalView
}
override func viewClass() -> AnyClass {
return CallRatingModalView.self
}
override var modalInteractions: ModalInteractions? {
return ModalInteractions(acceptTitle: strings().callRatingModalNotNow, singleButton: true)
}
override func becomeFirstResponder() -> Bool? {
return true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
genericView.starsChangeHandler = { [weak self] stars in
if let stars = stars {
self?.saveRating(Int(stars))
}
}
readyOnce()
}
private func saveRating(_ starsCount: Int) {
self.close()
if starsCount < 4, let window = self.window {
showModal(with: CallFeedbackController(context: context, callId: callId, starsCount: starsCount, userInitiated: userInitiated, isVideo: isVideo), for: window)
} else {
let _ = rateCallAndSendLogs(context: context, callId: self.callId, starsCount: starsCount, comment: "", userInitiated: userInitiated, includeLogs: false).start()
}
}
}
func rateCallAndSendLogs(context: AccountContext, callId: CallId, starsCount: Int, comment: String, userInitiated: Bool, includeLogs: Bool) -> Signal<Void, NoError> {
let peerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt64Value(4244000))
let rate = context.engine.calls.rateCall(callId: callId, starsCount: Int32(starsCount), comment: comment, userInitiated: userInitiated)
if includeLogs {
let id = arc4random64()
let name = "\(callId.id)_\(callId.accessHash).log.json"
let path = callLogsPath(account: context.account) + "/" + name
let file = TelegramMediaFile(fileId: MediaId(namespace: Namespaces.Media.LocalFile, id: id), partialReference: nil, resource: LocalFileReferenceMediaResource(localFilePath: path, randomId: id), previewRepresentations: [], videoThumbnails: [], immediateThumbnailData: nil, mimeType: "application/text", size: nil, attributes: [.FileName(fileName: name)])
let message = EnqueueMessage.message(text: comment, attributes: [], inlineStickers: [:], mediaReference: .standalone(media: file), threadId: nil, replyToMessageId: nil, replyToStoryId: nil, localGroupingKey: nil, correlationId: nil, bubbleUpEmojiOrStickersets: [])
return rate
|> then(enqueueMessages(account: context.account, peerId: peerId, messages: [message])
|> mapToSignal({ _ -> Signal<Void, NoError> in
return .single(Void())
}))
} else if !comment.isEmpty {
return rate
|> then(enqueueMessages(account: context.account, peerId: peerId, messages: [.message(text: comment, attributes: [], inlineStickers: [:], mediaReference: nil, threadId: nil, replyToMessageId: nil, replyToStoryId: nil, localGroupingKey: nil, correlationId: nil, bubbleUpEmojiOrStickersets: [])])
|> mapToSignal({ _ -> Signal<Void, NoError> in
return .single(Void())
}))
} else {
return rate
}
}