Skip to content

Commit

Permalink
Replace pending message animation & code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavDevIOS committed Jul 12, 2022
1 parent 34300e4 commit 6c44c3b
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 215 deletions.
20 changes: 8 additions & 12 deletions Adamant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
3AA2D5F7280EADE3000ED971 /* SocketService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA2D5F6280EADE3000ED971 /* SocketService.swift */; };
3AA2D5FA280EAF5D000ED971 /* AdamantSocketService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA2D5F9280EAF5D000ED971 /* AdamantSocketService.swift */; };
3AD95B96284121E100767396 /* HeaderReusableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AD95B95284121E100767396 /* HeaderReusableView.swift */; };
3AE89DD72837F42D0051D3A9 /* UITableView+adamant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE89DD62837F42D0051D3A9 /* UITableView+adamant.swift */; };
3AE89DD92837F5BF0051D3A9 /* ChatRooms.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE89DD82837F5BF0051D3A9 /* ChatRooms.swift */; };
3AE89DDB2837F5D30051D3A9 /* ChatRoomsChats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AE89DDA2837F5D30051D3A9 /* ChatRoomsChats.swift */; };
3C06931576393125C61FB8F6 /* Pods_Adamant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33975C0D891698AA7E74EBCC /* Pods_Adamant.framework */; };
Expand Down Expand Up @@ -617,7 +616,6 @@
3AA2D5F6280EADE3000ED971 /* SocketService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketService.swift; sourceTree = "<group>"; };
3AA2D5F9280EAF5D000ED971 /* AdamantSocketService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantSocketService.swift; sourceTree = "<group>"; };
3AD95B95284121E100767396 /* HeaderReusableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeaderReusableView.swift; sourceTree = "<group>"; };
3AE89DD62837F42D0051D3A9 /* UITableView+adamant.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView+adamant.swift"; sourceTree = "<group>"; };
3AE89DD82837F5BF0051D3A9 /* ChatRooms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRooms.swift; sourceTree = "<group>"; };
3AE89DDA2837F5D30051D3A9 /* ChatRoomsChats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRoomsChats.swift; sourceTree = "<group>"; };
41935847287841E20083363B /* MacOSDeterminer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacOSDeterminer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1364,7 +1362,6 @@
E9256F5E2034C21100DE86E9 /* String+localized.swift */,
E98FC34320F920BD00032D65 /* UIFont+adamant.swift */,
645AE06521E67D3300AD3623 /* UITextField+adamant.swift */,
3AE89DD62837F42D0051D3A9 /* UITableView+adamant.swift */,
41935847287841E20083363B /* MacOSDeterminer.swift */,
);
path = Helpers;
Expand Down Expand Up @@ -2689,7 +2686,6 @@
64EE46B220FE0C8D00194DDA /* LskTransactionsViewController.swift in Sources */,
E94008832114EE4700CD2D67 /* LskWallet.swift in Sources */,
64E1C833222EA0F0006C4DA7 /* DogeWalletViewController.swift in Sources */,
3AE89DD72837F42D0051D3A9 /* UITableView+adamant.swift in Sources */,
E96D64BA2295BED700CA5587 /* TransactionType.swift in Sources */,
E93EB09F20DA3FA4001F9601 /* NodesEditorRoutes.swift in Sources */,
6489794F24CE00C000C33A68 /* SwiftyOnboardOverlay.swift in Sources */,
Expand Down Expand Up @@ -2925,7 +2921,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
INFOPLIST_FILE = MessageNotificationContentExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
Expand Down Expand Up @@ -2957,7 +2953,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
INFOPLIST_FILE = MessageNotificationContentExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
Expand Down Expand Up @@ -3107,7 +3103,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
DISPLAY_NAME = ADM.Dev;
EXCLUDED_SOURCE_FILE_NAMES = "";
Expand Down Expand Up @@ -3139,7 +3135,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
DISPLAY_NAME = Adamant;
EXCLUDED_SOURCE_FILE_NAMES = Debug.xcassets;
Expand Down Expand Up @@ -3170,7 +3166,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
INFOPLIST_FILE = TransferNotificationContentExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
Expand Down Expand Up @@ -3202,7 +3198,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
INFOPLIST_FILE = TransferNotificationContentExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
Expand Down Expand Up @@ -3234,7 +3230,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
INFOPLIST_FILE = NotificationServiceExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
Expand Down Expand Up @@ -3266,7 +3262,7 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 198;
CURRENT_PROJECT_VERSION = 202;
DEVELOPMENT_TEAM = J2L77FMN46;
INFOPLIST_FILE = NotificationServiceExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
Expand Down
62 changes: 30 additions & 32 deletions Adamant/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,44 +345,42 @@ extension AppDelegate: UNUserNotificationCenterDelegate {

//MARK: Open Chat From Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let transactionID = userInfo[AdamantNotificationUserInfoKeys.transactionId] as? String,
let transactionRaw = userInfo[AdamantNotificationUserInfoKeys.transaction] as? String,
let data = transactionRaw.data(using: .utf8),
let trs = try? JSONDecoder().decode(Transaction.self, from: data) {
if application.applicationState != .background && application.applicationState != .inactive {
completionHandler(.noData)
return
}

let senderAddress = trs.senderId

if let tabbar = window?.rootViewController as? UITabBarController,
let chats = tabbar.viewControllers?.first as? UISplitViewController,
let chatList = chats.viewControllers.first as? UINavigationController,
let list = chatList.viewControllers.first as? ChatListViewController {

if case .loggedIn = list.accountService.state {
self.openDialog(chatList: chatList, tabbar: tabbar, list: list, transactionID: transactionID, senderAddress: senderAddress)
}

// if not logged in
list.didLoadedMessages = { [weak self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self?.dialogService.dismissProgress()
self?.openDialog(chatList: chatList, tabbar: tabbar, list: list, transactionID: transactionID, senderAddress: senderAddress)
}
}
}
completionHandler(.newData)
} else {
guard let transactionID = userInfo[AdamantNotificationUserInfoKeys.transactionId] as? String,
let transactionRaw = userInfo[AdamantNotificationUserInfoKeys.transaction] as? String,
let data = transactionRaw.data(using: .utf8),
let trs = try? JSONDecoder().decode(Transaction.self, from: data),
let tabbar = window?.rootViewController as? UITabBarController,
let chats = tabbar.viewControllers?.first as? UISplitViewController,
let chatList = chats.viewControllers.first as? UINavigationController,
let list = chatList.viewControllers.first as? ChatListViewController,
(application.applicationState != .active)
else {
completionHandler(.noData)
return
}

if case .loggedIn = list.accountService.state {
self.openDialog(chatList: chatList, tabbar: tabbar, list: list, transactionID: transactionID, senderAddress: trs.senderId)
}

// if not logged in
list.didLoadedMessages = { [weak self] in
var timeout = 2.0
if #available(iOS 13.0, *) { timeout = 0.5 }
DispatchQueue.main.asyncAfter(deadline: .now() + timeout) {
self?.dialogService.dismissProgress()
self?.openDialog(chatList: chatList, tabbar: tabbar, list: list, transactionID: transactionID, senderAddress: trs.senderId)
}
}

completionHandler(.newData)
}

func openDialog(chatList: UINavigationController, tabbar: UITabBarController, list: ChatListViewController, transactionID: String, senderAddress: String) {
if let chatVCNav = chatList.viewControllers.last as? UINavigationController,
let chatVC = chatVCNav.viewControllers.first as? ChatViewController,
chatVC.chatroom?.partner?.address == senderAddress {
chatVC.forceScrollToBottom = true
chatVC.scrollDown()
return
}
Expand All @@ -404,7 +402,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
if #available(iOS 13.0, *) { timeout = 0 }
DispatchQueue.main.asyncAfter(deadline: .now() + timeout) {
let chat = UINavigationController(rootViewController: vc)
split.showDetailViewController(chat, sender: self)
split.showDetailViewController(chat, sender: list)
}
} else {
chatList.pushViewController(vc, animated: false)
Expand Down Expand Up @@ -540,7 +538,7 @@ extension AppDelegate {
})
}

if let welcome = AdamantContacts.adamantBountyWallet.messages["chats.welcome_message"] {
if let welcome = AdamantContacts.adamantWelcomeWallet.messages["chats.welcome_message"] {
chatProvider.fakeReceived(message: welcome.message,
senderId: AdamantContacts.adamantWelcomeWallet.name,
date: Date.adamantNullDate,
Expand Down
57 changes: 0 additions & 57 deletions Adamant/Helpers/UITableView+adamant.swift

This file was deleted.

92 changes: 49 additions & 43 deletions Adamant/Stories/Chats/ChatListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,46 +447,21 @@ extension ChatListViewController {
}
}

if let roomsLoadedCount = chatsProvider.roomsLoadedCount,
let roomsMaxCount = chatsProvider.roomsMaxCount,
roomsLoadedCount < roomsMaxCount,
roomsMaxCount > 0,
!isBusy,
tableView.numberOfRows(inSection: 0) >= roomsLoadedCount,
let lastVisibleIndexPath = tableView.indexPathsForVisibleRows,
lastVisibleIndexPath.contains(IndexPath(row: tableView.numberOfRows(inSection: 0) - 3, section: 0)) {
isBusy = true
lastSystemChatPositionRow = getBottomSystemChatIndex()
DispatchQueue.main.async { [weak self] in
if #available(iOS 11.0, *) {
tableView.performBatchUpdates {
tableView.insertRows(at: [
IndexPath(row: self?.lastSystemChatPositionRow ?? 0, section: 0)
], with: .none)
tableView.reloadRows(at: [IndexPath(row: (self?.lastSystemChatPositionRow ?? 0) - 1, section: 0)], with: .none)
}
} else {
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: self?.lastSystemChatPositionRow ?? 0, section: 0)], with: .none)
tableView.reloadRows(at: [IndexPath(row: (self?.lastSystemChatPositionRow ?? 0) - 1, section: 0)], with: .none)
tableView.endUpdates()
}
}

chatsProvider.getChatRooms(offset: roomsLoadedCount, completion: { [weak self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
self?.isBusy = false
if #available(iOS 13.0, *) {
tableView.reloadData()
} else {
let oldTableViewHeight = tableView.contentSize.height;
tableView.reloadData()
let newTableViewHeight = tableView.contentSize.height;
tableView.contentOffset = CGPoint(x: 0, y: newTableViewHeight - oldTableViewHeight);
}
})
})
guard let roomsLoadedCount = chatsProvider.roomsLoadedCount,
let roomsMaxCount = chatsProvider.roomsMaxCount,
roomsLoadedCount < roomsMaxCount,
roomsMaxCount > 0,
!isBusy,
tableView.numberOfRows(inSection: 0) >= roomsLoadedCount,
let lastVisibleIndexPath = tableView.indexPathsForVisibleRows,
lastVisibleIndexPath.contains(IndexPath(row: tableView.numberOfRows(inSection: 0) - 3, section: 0))
else {
return
}

isBusy = true
insertReloadRow()
loadNewChats(offset: roomsLoadedCount)
}

private func configureCell(_ cell: LoadingTableViewCell) {
Expand Down Expand Up @@ -543,6 +518,34 @@ extension ChatListViewController {
cell.dateLabel.text = nil
}
}

private func insertReloadRow() {
lastSystemChatPositionRow = getBottomSystemChatIndex()
DispatchQueue.main.async { [weak self] in
if #available(iOS 11.0, *) {
self?.tableView.performBatchUpdates {
self?.tableView.insertRows(at: [
IndexPath(row: self?.lastSystemChatPositionRow ?? 0, section: 0)
], with: .none)
self?.tableView.reloadRows(at: [IndexPath(row: (self?.lastSystemChatPositionRow ?? 0) - 1, section: 0)], with: .none)
}
} else {
self?.tableView.beginUpdates()
self?.tableView.insertRows(at: [IndexPath(row: self?.lastSystemChatPositionRow ?? 0, section: 0)], with: .none)
self?.tableView.reloadRows(at: [IndexPath(row: (self?.lastSystemChatPositionRow ?? 0) - 1, section: 0)], with: .none)
self?.tableView.endUpdates()
}
}
}

private func loadNewChats(offset: Int) {
chatsProvider.getChatRooms(offset: offset, completion: { [weak self] in
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
self?.isBusy = false
self?.tableView.reloadData()
})
})
}
}


Expand Down Expand Up @@ -984,12 +987,15 @@ extension ChatListViewController {
var index = 0
try? chatsController?.performFetch()
chatsController?.fetchedObjects?.enumerated().forEach({ (i, room) in
if index == 0,
let date = room.updatedAt as? Date,
date == Date.adamantNullDate {
index = i
guard index == 0,
let date = room.updatedAt as? Date,
date == Date.adamantNullDate
else {
return
}
index = i
})

return index
}
}
Expand Down
Loading

0 comments on commit 6c44c3b

Please sign in to comment.