Skip to content
This repository was archived by the owner on Jun 7, 2020. It is now read-only.

Commit 5d638ed

Browse files
authored
Merge pull request #2517 from RocketChat/chore/code_adjustments
[CHORE] Remove unused code, resolve some SwiftLint warnings & suppress couple.
2 parents 92cb228 + a7bd599 commit 5d638ed

17 files changed

Lines changed: 218 additions & 439 deletions

Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 119 additions & 263 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rocket.Chat.xcodeproj/xcshareddata/xcschemes/Rocket.Chat.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1000"
3+
LastUpgradeVersion = "1010"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Rocket.Chat/Controllers/Chat/MessagesListViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import UIKit
1010
import RealmSwift
1111

12+
// swiftlint:disable file_length
1213
extension RoomMessagesResource {
1314
func fetchMessagesFromRealm() -> [Message]? {
1415
var messages = [Message]()

Rocket.Chat/Controllers/Chat/MessagesViewController.swift

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extension SizingCell {
3131
}
3232
}
3333

34+
// swiftlint:disable type_body_length file_length
3435
final class MessagesViewController: RocketChatViewController {
3536

3637
@objc override var bottomHeight: CGFloat {
@@ -46,7 +47,6 @@ final class MessagesViewController: RocketChatViewController {
4647
let viewSizingModel = MessagesSizingManager()
4748
let composerViewModel = MessagesComposerViewModel()
4849

49-
// TODO: Move to another view model
5050
let socketHandlerToken = String.random(5)
5151

5252
var chatTitleView: ChatTitleView? {
@@ -137,54 +137,10 @@ final class MessagesViewController: RocketChatViewController {
137137

138138
registerCells()
139139
setupScrollToBottom()
140+
setupKeyboardNotifications()
140141
setupAnnouncementBanner()
141-
NotificationCenter.default.addObserver(
142-
self,
143-
selector: #selector(keyboardWillShow(_:)),
144-
name: UIResponder.keyboardWillShowNotification,
145-
object: nil
146-
)
147-
148-
NotificationCenter.default.addObserver(
149-
self,
150-
selector: #selector(keyboardWillHide(_:)),
151-
name: UIResponder.keyboardWillHideNotification,
152-
object: nil
153-
)
154-
155-
dataUpdateDelegate = self
156-
viewModel.controllerContext = self
157-
viewModel.onDataChanged = { [weak self] in
158-
guard let self = self else { return }
159-
Log.debug("[VIEW MODEL] dataChanged with \(self.viewModel.dataNormalized.count) values.")
160-
161-
if self.viewModel.dataNormalized.first?.model.differenceIdentifier == AnyHashable(HeaderChatItem.globalIdentifier) {
162-
self.isInverted = false
163-
} else {
164-
self.isInverted = true
165-
}
166-
167-
// Update dataset with the new data normalized
168-
self.updateData(with: self.viewModel.dataNormalized)
169-
self.markAsRead()
170-
}
171-
172-
viewSubscriptionModel.onDataChanged = { [weak self] in
173-
guard let self = self else { return }
174-
self.chatTitleView?.subscription = self.viewSubscriptionModel.subscription
175-
self.updateJoinedView()
176-
177-
if self.viewSubscriptionModel.subscription?.managedObject == nil {
178-
self.navigationController?.popToRootViewController(animated: true)
179-
self.subscription = nil
180-
}
181-
}
182-
183-
viewSubscriptionModel.onTypingChanged = { [weak self] usernames in
184-
DispatchQueue.main.async {
185-
self?.chatTitleView?.updateTypingStatus(usernames: usernames)
186-
}
187-
}
142+
setupSubscriptionViewModel()
143+
setupMessagesViewModel()
188144

189145
composerViewModel.getRecentSenders = { [weak self] in
190146
guard let self = self else {
@@ -248,6 +204,68 @@ final class MessagesViewController: RocketChatViewController {
248204
}
249205
}
250206

207+
// MARK: Notifications
208+
209+
private func setupKeyboardNotifications() {
210+
NotificationCenter.default.addObserver(
211+
self,
212+
selector: #selector(keyboardWillShow(_:)),
213+
name: UIResponder.keyboardWillShowNotification,
214+
object: nil
215+
)
216+
217+
NotificationCenter.default.addObserver(
218+
self,
219+
selector: #selector(keyboardWillHide(_:)),
220+
name: UIResponder.keyboardWillHideNotification,
221+
object: nil
222+
)
223+
}
224+
225+
// MARK: View Models
226+
227+
private func setupSubscriptionViewModel() {
228+
viewSubscriptionModel.onTypingChanged = { [weak self] usernames in
229+
DispatchQueue.main.async {
230+
self?.chatTitleView?.updateTypingStatus(usernames: usernames)
231+
}
232+
}
233+
234+
viewSubscriptionModel.onDataChanged = { [weak self] in
235+
guard let self = self else { return }
236+
self.chatTitleView?.subscription = self.viewSubscriptionModel.subscription
237+
self.updateJoinedView()
238+
239+
if self.viewSubscriptionModel.subscription?.managedObject == nil {
240+
self.navigationController?.popToRootViewController(animated: true)
241+
self.subscription = nil
242+
}
243+
}
244+
}
245+
246+
private func setupMessagesViewModel() {
247+
dataUpdateDelegate = self
248+
viewModel.controllerContext = self
249+
viewModel.onDataChanged = { [weak self] in
250+
guard let self = self else { return }
251+
Log.debug("[VIEW MODEL] dataChanged with \(self.viewModel.dataNormalized.count) values.")
252+
253+
// If first item is the header (list is empty) then we don't use the inverted mode
254+
// because we want the header to appear in the bottom.
255+
let first = self.viewModel.dataNormalized.first
256+
if first?.model.differenceIdentifier == AnyHashable(HeaderChatItem.globalIdentifier) {
257+
self.isInverted = false
258+
} else {
259+
self.isInverted = true
260+
}
261+
262+
// Update dataset with the new data normalized
263+
self.updateData(with: self.viewModel.dataNormalized)
264+
self.markAsRead()
265+
}
266+
267+
}
268+
251269
// MARK: Cells
252270

253271
private func registerCells() {

Rocket.Chat/Controllers/Chat/MessagesViewControllerMessageCellProtocol.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import FLAnimatedImage
1414
import SimpleImageViewer
1515
import RealmSwift
1616

17+
// swiftlint:disable file_length
1718
extension MessagesViewController: ChatMessageCellProtocol {
1819
func handleReviewRequest() {
1920
allowResignFirstResponder = false
@@ -129,11 +130,7 @@ extension MessagesViewController: ChatMessageCellProtocol {
129130
AppManager.openDirectMessage(username: username, replyMessageIdentifier: message.identifier, completion: nil)
130131
}
131132

132-
// TODO: This one can be removed once we remove from the protocol
133133
func openImageFromCell(attachment: UnmanagedAttachment, thumbnail: FLAnimatedImageView) {
134-
// TODO: Adjust for our composer
135-
// textView.resignFirstResponder()
136-
137134
if thumbnail.animatedImage != nil || thumbnail.image != nil {
138135
let configuration = ImageViewerConfiguration { config in
139136
config.image = thumbnail.image
@@ -142,8 +139,6 @@ extension MessagesViewController: ChatMessageCellProtocol {
142139
config.allowSharing = true
143140
}
144141
present(ImageViewerController(configuration: configuration), animated: true)
145-
} else {
146-
// openImage(attachment: attachment)
147142
}
148143
}
149144

@@ -157,8 +152,6 @@ extension MessagesViewController: ChatMessageCellProtocol {
157152
}
158153

159154
present(ImageViewerController(configuration: configuration), animated: true)
160-
} else {
161-
// openImage(attachment: attachment)
162155
}
163156
}
164157

@@ -252,7 +245,6 @@ extension MessagesViewController {
252245
// swiftlint:disable function_body_length
253246
func actionsForMessage(_ message: Message, view: UIView) -> [UIAlertAction] {
254247
guard
255-
let messageUser = message.user,
256248
let auth = AuthManager.isAuthenticated(),
257249
let client = API.current()?.client(MessagesClient.self)
258250
else {

Rocket.Chat/Controllers/Chat/MessagesViewControllerUploading.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extension MessagesViewController {
104104
name: "\(filename.components(separatedBy: ".").first ?? "image").jpeg",
105105
mimeType: "image/jpeg"
106106
)
107+
107108
upload(file)
108109
}
109110

Rocket.Chat/Controllers/Chat/MessagesViewModel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import RealmSwift
1111
import DifferenceKit
1212
import RocketChatViewController
1313

14+
// swiftlint:disable type_body_length file_length
1415
final class MessagesViewModel {
1516

1617
/**
@@ -422,6 +423,7 @@ final class MessagesViewModel {
422423
- oldestMessage: This is the parameter that will be sent to the server in
423424
order to get the correct page of data.
424425
*/
426+
// swiftlint:disable function_body_length
425427
func fetchMessages(from oldestMessage: Date?, prepareAnotherPage: Bool = true) {
426428
guard
427429
requestingData == .none,

Rocket.Chat/Controllers/Preferences/PreferencesViewController.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,7 @@ final class PreferencesViewController: BaseTableViewController {
272272

273273
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
274274
if indexPath.section == kSectionSettings {
275-
switch indexPath.row {
276-
case 0:
277-
cellContactDidPressed()
278-
case 1:
279-
cellLanguageDidPressed()
280-
case 4:
281-
cellReviewDidPressed()
282-
case 5:
283-
shareAppCell = tableView.cellForRow(at: indexPath)
284-
cellShareDidPressed()
285-
case 6:
286-
cellAppIconDidPressed()
287-
default:
288-
break
289-
}
275+
didSelectSettingsCell(indexPath)
290276
} else if indexPath.section == kSectionInformation {
291277
if indexPath.row == 0 {
292278
cellTermsOfServiceDidPressed()
@@ -338,6 +324,24 @@ final class PreferencesViewController: BaseTableViewController {
338324

339325
// MARK: IBAction
340326

327+
fileprivate func didSelectSettingsCell(_ indexPath: IndexPath) {
328+
switch indexPath.row {
329+
case 0:
330+
cellContactDidPressed()
331+
case 1:
332+
cellLanguageDidPressed()
333+
case 4:
334+
cellReviewDidPressed()
335+
case 5:
336+
shareAppCell = tableView.cellForRow(at: indexPath)
337+
cellShareDidPressed()
338+
case 6:
339+
cellAppIconDidPressed()
340+
default:
341+
break
342+
}
343+
}
344+
341345
@IBAction func crashReportSwitchDidChange(sender: Any) {
342346
AnalyticsCoordinator.toggleCrashReporting(disabled: !switchTracking.isOn)
343347
}

Rocket.Chat/Managers/Uploader/UploadManager.swift

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ class UploadManager {
2323

2424
static let shared = UploadManager()
2525

26-
fileprivate func sendFileMessage(params: [Any], completion: @escaping UploadCompletionBlock) {
27-
let request = [
28-
"msg": "method",
29-
"method": "sendFileMessage",
30-
"params": params
31-
] as [String: Any]
32-
33-
SocketManager.send(request) { (response) in
34-
completion(response, response.isError())
35-
}
36-
}
37-
3826
fileprivate func requestUpload(_ url: URL, file: FileUpload, formData: JSON?) -> URLRequest {
3927
var request = URLRequest(url: url)
4028
request.httpMethod = "POST"
@@ -62,84 +50,4 @@ class UploadManager {
6250
return request
6351
}
6452

65-
func upload(file: FileUpload, fileName: String, subscription: Subscription, progress: UploadProgressBlock, completion: @escaping UploadCompletionBlock) {
66-
guard let settings = AuthSettingsManager.settings else { return }
67-
guard let store = settings.uploadStorageType else { return }
68-
uploadToUFSFile(store: store, file: file, fileName: fileName, subscription: subscription, progress: progress, completion: completion)
69-
}
70-
71-
// swiftlint:disable function_body_length function_parameter_count
72-
func uploadToUFSFile(store: String, file: FileUpload, fileName: String, subscription: Subscription, progress: UploadProgressBlock, completion: @escaping UploadCompletionBlock) {
73-
// Normalize the store name, cause setting is not the same value
74-
// In the future, we do plan to change it and return the correct value
75-
let normalizedStore = store == "FileSystem" ? "fileSystem" : "rocketchat_uploads"
76-
let request = [
77-
"msg": "method",
78-
"method": "ufsCreate",
79-
"params": [[
80-
"name": fileName,
81-
"size": file.size,
82-
"type": file.type,
83-
"rid": subscription.rid,
84-
"description": "",
85-
"store": normalizedStore
86-
]]
87-
] as [String: Any]
88-
89-
SocketManager.send(request) { [unowned self] (response) in
90-
guard !response.isError() else {
91-
completion(response, true)
92-
return
93-
}
94-
95-
let result = response.result
96-
97-
guard let auth = AuthManager.isAuthenticated() else { return }
98-
guard let uploadURL = URL(string: result["result"]["url"].stringValue) else { return }
99-
guard let fileToken = result["result"]["token"].string else { return }
100-
guard let fileIdentifier = result["result"]["fileId"].string else { return }
101-
102-
let headers = [[
103-
"name": "Cookie",
104-
"value": "rc_uid=\(auth.userId ?? ""); rc_token=\(auth.token ?? "")"
105-
]]
106-
107-
let request = self.requestUpload(uploadURL, file: file, formData: JSON(headers))
108-
let config = URLSessionConfiguration.default
109-
let session = URLSession(configuration: config)
110-
let task = session.uploadTask(with: request, from: file.data, completionHandler: { (_, _, error) in
111-
if error != nil {
112-
completion(nil, true)
113-
} else {
114-
let request = [
115-
"msg": "method",
116-
"method": "ufsComplete",
117-
"params": [fileIdentifier, normalizedStore, fileToken]
118-
] as [String: Any]
119-
120-
SocketManager.send(request) { [unowned self] (response) in
121-
guard !response.isError() else {
122-
completion(response, true)
123-
return
124-
}
125-
126-
DispatchQueue.main.async {
127-
self.sendFileMessage(params: [
128-
subscription.rid,
129-
NSNull(), [
130-
"type": file.type,
131-
"size": file.size,
132-
"name": fileName,
133-
"_id": fileIdentifier,
134-
"url": response.result["result"]["path"].stringValue
135-
]
136-
], completion: completion)
137-
}
138-
}
139-
}
140-
})
141-
142-
task.resume()
143-
}
144-
}
14553
}

Rocket.Chat/Theme/ThemeableViews.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import UIKit
1010
import RocketChatViewController
1111

12+
// swiftlint:disable file_length
1213
extension UIView: Themeable {
1314

1415
/**

0 commit comments

Comments
 (0)