Skip to content

Commit 3326518

Browse files
Merge pull request #289 from Adamant-im/dev/trello.com/c/GipQRMWc
[trello.com/c/GipQRMWc] Save database only in memory
2 parents 4fd8da1 + c2a729b commit 3326518

33 files changed

+114
-341
lines changed

Adamant.xcodeproj/project.pbxproj

Lines changed: 8 additions & 159 deletions
Large diffs are not rendered by default.

Adamant/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
263263
forName: Notification.Name.AdamantAccountService.userLoggedOut,
264264
object: nil,
265265
queue: OperationQueue.main
266-
) { [weak container] _ in
266+
) { _ in
267267
resetScreensAction()
268-
container?.resolve(CoreDataStack.self)?.deleteAccounts()
269268
}
270269

271270
// MARK: 8. Welcome messages

Adamant/CoreData/Chatroom+CoreDataClass.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ public class Chatroom: NSManagedObject {
3030
return nil
3131
}
3232

33+
@MainActor func getName(addressBookService: AddressBookService) -> String? {
34+
guard let partner = partner else { return nil }
35+
let result: String?
36+
if let title = title {
37+
result = title
38+
} else if let name = partner.name {
39+
result = name
40+
} else if
41+
let address = partner.address,
42+
let name = addressBookService.getName(for: address)
43+
{
44+
result = name
45+
} else {
46+
result = partner.address
47+
}
48+
49+
return result?.checkAndReplaceSystemWallets()
50+
}
51+
3352
private var semaphore: DispatchSemaphore?
3453

3554
func updateLastTransaction() {

Adamant/Debug.entitlements

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
</array>
1111
<key>com.apple.security.app-sandbox</key>
1212
<true/>
13-
<key>com.apple.security.application-groups</key>
14-
<array>
15-
<string>group.adamant.adamant-messenger-dev</string>
16-
</array>
1713
<key>com.apple.security.device.camera</key>
1814
<true/>
1915
<key>com.apple.security.network.client</key>

Adamant/Release.entitlements

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
</array>
1111
<key>com.apple.security.app-sandbox</key>
1212
<true/>
13-
<key>com.apple.security.application-groups</key>
14-
<array>
15-
<string>group.adamant.adamant-messenger</string>
16-
</array>
1713
<key>com.apple.security.device.camera</key>
1814
<true/>
1915
<key>com.apple.security.network.client</key>

Adamant/ServiceProtocols/AddressBookService.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ extension AddressBookServiceError: RichError {
8484
protocol AddressBookService: AnyObject {
8585
// MARK: Work with Address book
8686
func set(name: String, for: String) async
87-
@MainActor func getName(key: String) -> String?
88-
@MainActor func getName(chatroom: Chatroom) -> String?
87+
@MainActor func getName(for key: String) -> String?
8988

9089
// MARK: Updating & saving
9190
func update() async -> AddressBookServiceResult?

AdamantShared/ServiceProtocols/CoreDataStack.swift renamed to Adamant/ServiceProtocols/DataProviders/CoreDataStack.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ import CoreData
1111

1212
protocol CoreDataStack {
1313
var container: NSPersistentContainer { get }
14-
15-
@MainActor func deleteAccounts()
1614
}

Adamant/Services/AdamantAddressBookService.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,10 @@ final class AdamantAddressBookService: AddressBookService {
115115

116116
// MARK: - Setting
117117

118-
@MainActor func getName(key: String) -> String? {
118+
@MainActor func getName(for key: String) -> String? {
119119
return addressBook[key]?.checkAndReplaceSystemWallets()
120120
}
121121

122-
@MainActor func getName(chatroom: Chatroom) -> String? {
123-
guard let partner = chatroom.partner else { return nil }
124-
let result: String?
125-
if let title = chatroom.title {
126-
result = title
127-
} else if let name = partner.name {
128-
result = name
129-
} else if
130-
let address = partner.address,
131-
let name = getName(key: address)
132-
{
133-
result = name
134-
} else {
135-
result = partner.address
136-
}
137-
138-
return result?.checkAndReplaceSystemWallets()
139-
}
140-
141122
func set(name: String, for address: String) async {
142123
guard addressBook[address] == nil || addressBook[address] != name else {
143124
return

Adamant/Services/DataProviders/AdamantAccountsProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ extension AdamantAccountsProvider {
512512
}
513513

514514
if let address = coreAccount.address,
515-
let name = addressBookService.getName(key: address) {
515+
let name = addressBookService.getName(for: address) {
516516
coreAccount.name = name
517517
chatroom.title = name.checkAndReplaceSystemWallets()
518518
}

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,14 +1636,6 @@ extension AdamantChatsProvider {
16361636
for chatroom in viewContextChatrooms {
16371637
await chatroom.updateLastTransaction()
16381638
}
1639-
1640-
if stack.container.viewContext.hasChanges {
1641-
do {
1642-
try stack.container.viewContext.save()
1643-
} catch {
1644-
print(error)
1645-
}
1646-
}
16471639
}
16481640
}
16491641

Adamant/Services/DataProviders/AdamantTransfersProvider.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,20 +1070,12 @@ extension AdamantTransfersProvider {
10701070
}
10711071

10721072
@MainActor func updateContext(rooms: [Chatroom]) async {
1073-
let viewContextChatrooms = Set<Chatroom>(rooms).compactMap {
1074-
self.stack.container.viewContext.object(with: $0.objectID) as? Chatroom
1075-
}
1076-
1077-
for chatroom in viewContextChatrooms {
1078-
await chatroom.updateLastTransaction()
1079-
}
1080-
1081-
if stack.container.viewContext.hasChanges {
1082-
do {
1083-
try stack.container.viewContext.save()
1084-
} catch {
1085-
print(error)
1086-
}
1087-
}
1088-
}
1073+
let viewContextChatrooms = Set<Chatroom>(rooms).compactMap {
1074+
self.stack.container.viewContext.object(with: $0.objectID) as? Chatroom
1075+
}
1076+
1077+
for chatroom in viewContextChatrooms {
1078+
await chatroom.updateLastTransaction()
1079+
}
1080+
}
10891081
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// InMemoryCoreDataStack.swift
3+
// Adamant
4+
//
5+
// Created by Anokhov Pavel on 27.01.2018.
6+
// Copyright © 2018 Adamant. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import CoreData
11+
12+
class InMemoryCoreDataStack: CoreDataStack {
13+
let container: NSPersistentContainer
14+
15+
init(modelUrl url: URL) throws {
16+
guard let model = NSManagedObjectModel(contentsOf: url) else {
17+
throw AdamantError(message: "Can't load ManagedObjectModel")
18+
}
19+
20+
let description = NSPersistentStoreDescription()
21+
description.type = NSInMemoryStoreType
22+
23+
container = NSPersistentContainer(name: "Adamant", managedObjectModel: model)
24+
container.persistentStoreDescriptions = [description]
25+
container.loadPersistentStores { (_, _) in }
26+
container.viewContext.mergePolicy = NSMergePolicy(merge: NSMergePolicyType.mergeByPropertyObjectTrumpMergePolicyType)
27+
28+
NotificationCenter.default.addObserver(forName: Notification.Name.AdamantAccountService.userLoggedOut, object: nil, queue: OperationQueue.main) { [weak self] _ in
29+
guard let context = self?.container.viewContext else {
30+
return
31+
}
32+
33+
let fetch = NSFetchRequest<NSManagedObject>(entityName: "BaseAccount")
34+
35+
do {
36+
let result = try context.fetch(fetch)
37+
for account in result {
38+
context.delete(account)
39+
}
40+
41+
try context.save()
42+
} catch {
43+
print("Got error saving context after reset")
44+
}
45+
}
46+
}
47+
}

Adamant/Stories/Chat/View/ChatViewController.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ final class ChatViewController: MessagesViewController {
3434
private var messagesLoaded = false
3535
private var isScrollPositionNearlyTheBottom = true
3636
private var viewAppeared = false
37-
private var isViewVisible = false
3837

3938
private lazy var inputBar = ChatInputBar()
4039
private lazy var loadingView = LoadingView()
@@ -117,11 +116,9 @@ final class ChatViewController: MessagesViewController {
117116

118117
override func viewDidAppear(_ animated: Bool) {
119118
super.viewDidAppear(animated)
120-
isViewVisible = true
121119
defer { viewAppeared = true }
122120
inputBar.isUserInteractionEnabled = true
123121
chatMessagesCollectionView.fixedBottomOffset = nil
124-
checkIsChatWasRead()
125122

126123
guard isMacOS, !viewAppeared else { return }
127124
focusInputBarWithoutAnimation()
@@ -134,7 +131,6 @@ final class ChatViewController: MessagesViewController {
134131

135132
override func viewDidDisappear(_ animated: Bool) {
136133
super.viewDidDisappear(animated)
137-
isViewVisible = false
138134
viewModel.preserveMessage(inputBar.text)
139135
viewModel.saveChatOffset(
140136
isScrollPositionNearlyTheBottom
@@ -158,12 +154,12 @@ final class ChatViewController: MessagesViewController {
158154

159155
super.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath)
160156

161-
let isItemVisible = collectionView.indexPathsForVisibleItems.contains {
157+
let isVisible = collectionView.indexPathsForVisibleItems.contains {
162158
$0.section == viewModel.minIndexForStartLoadNewMessages
163159
}
164160

165161
guard indexPath.section < viewModel.minIndexForStartLoadNewMessages,
166-
isItemVisible
162+
isVisible
167163
else { return }
168164

169165
viewModel.loadMoreMessagesIfNeeded()
@@ -498,7 +494,7 @@ private extension ChatViewController {
498494
}
499495

500496
func checkIsChatWasRead() {
501-
guard isScrollPositionNearlyTheBottom, messagesLoaded, isViewVisible else { return }
497+
guard isScrollPositionNearlyTheBottom, messagesLoaded else { return }
502498
viewModel.entireChatWasRead()
503499
}
504500

@@ -713,11 +709,11 @@ extension ChatViewController {
713709

714710
guard let index = viewModel.needToAnimateCellIndex else { return }
715711

716-
let isItemVisible = messagesCollectionView.indexPathsForVisibleItems.contains {
712+
let isVisible = messagesCollectionView.indexPathsForVisibleItems.contains {
717713
$0.section == index
718714
}
719715

720-
guard isItemVisible else { return }
716+
guard isVisible else { return }
721717

722718
// TODO: refactor for architecture
723719
let cell = messagesCollectionView.cellForItem(at: .init(item: .zero, section: index))

Adamant/Stories/Chat/ViewModel/ChatViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ final class ChatViewModel: NSObject {
261261
}
262262

263263
func getKvsName(for address: String) -> String? {
264-
return addressBookService.getName(key: address)
264+
return addressBookService.getName(for: address)
265265
}
266266

267267
func setNewName(_ newName: String) {
@@ -634,7 +634,7 @@ private extension ChatViewModel {
634634
}
635635

636636
func updateTitle() {
637-
partnerName = chatroom.map { addressBookService.getName(chatroom: $0) } ?? nil
637+
partnerName = chatroom?.getName(addressBookService: addressBookService)
638638
}
639639

640640
func updateAttachmentButtonAvailability() {
@@ -687,7 +687,7 @@ private extension ChatViewModel {
687687
let account = account,
688688
let address = account.address,
689689
account.name == nil,
690-
addressBookService.getName(key: address) == nil
690+
addressBookService.getName(for: address) == nil
691691
else {
692692
return
693693
}

Adamant/Stories/ChatsList/ChatListViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ extension ChatListViewController {
579579
}
580580
}
581581

582-
cell.accountLabel.text = addressBook.getName(chatroom: chatroom)
582+
cell.accountLabel.text = chatroom.getName(addressBookService: addressBook)
583583
cell.hasUnreadMessages = chatroom.hasUnreadMessages
584584

585585
if let lastTransaction = chatroom.lastTransaction {
@@ -705,7 +705,7 @@ extension ChatListViewController: NewChatViewControllerDelegate {
705705

706706
if let name = name,
707707
let address = account.address,
708-
addressBook.getName(key: address) == nil {
708+
addressBook.getName(for: address) == nil {
709709
account.name = name
710710
chatroom.title = name
711711
Task {
@@ -1129,7 +1129,7 @@ extension ChatListViewController {
11291129
alert.addTextField { [weak self] textField in
11301130
textField.placeholder = .adamantLocalized.chat.name
11311131
textField.autocapitalizationType = .words
1132-
textField.text = self?.addressBook.getName(key: address)
1132+
textField.text = self?.addressBook.getName(for: address)
11331133
}
11341134

11351135
let renameAction = UIAlertAction(
@@ -1259,7 +1259,7 @@ extension ChatListViewController: UISearchBarDelegate, UISearchResultsUpdating,
12591259
}
12601260

12611261
if let address = partner.address {
1262-
if let name = self.addressBook.getName(key: address) {
1262+
if let name = self.addressBook.getName(for: address) {
12631263
return name.localizedCaseInsensitiveContains(searchString) || address.localizedCaseInsensitiveContains(searchString)
12641264
}
12651265
return address.localizedCaseInsensitiveContains(searchString)

Adamant/Stories/ChatsList/ComplexTransferViewController.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class ComplexTransferViewController: UIViewController {
2929
var services: [WalletServiceWithSend] = []
3030
var partner: CoreDataAccount? {
3131
didSet {
32-
navigationItem.title = partner?.chatroom.map {
33-
addressBookService.getName(chatroom: $0)
34-
} ?? nil
32+
navigationItem.title = partner?.chatroom?.getName(addressBookService: addressBookService)
3533
}
3634
}
3735
var replyToMessageId: String?
@@ -105,7 +103,7 @@ extension ComplexTransferViewController: PagingViewControllerDataSource {
105103

106104
guard let address = partner?.address else { return vc }
107105

108-
let name = partner?.chatroom.map { addressBookService.getName(chatroom: $0) } ?? nil
106+
let name = partner?.chatroom?.getName(addressBookService: addressBookService)
109107

110108
v.replyToMessageId = replyToMessageId
111109
v.admReportRecipient = address

Adamant/Stories/ChatsList/SearchResultsViewController.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ class SearchResultsViewController: UITableViewController {
157157
cell.lastMessageLabel.text = title
158158
}
159159

160-
cell.accountLabel.text = addressBookService.getName(chatroom: chatroom)
160+
cell.accountLabel.text = chatroom.getName(
161+
addressBookService: addressBookService
162+
)
163+
161164
cell.hasUnreadMessages = false
162165
cell.dateLabel.text = nil
163166
}
@@ -181,9 +184,9 @@ class SearchResultsViewController: UITableViewController {
181184
}
182185
}
183186

184-
cell.accountLabel.text = message.chatroom.map {
185-
addressBookService.getName(chatroom: $0)
186-
} ?? nil
187+
cell.accountLabel.text = message.chatroom?.getName(
188+
addressBookService: addressBookService
189+
)
187190

188191
cell.hasUnreadMessages = false
189192

Adamant/Wallets/Adamant/AdmTransactionsViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class AdmTransactionsViewController: TransactionsListViewControllerBase {
219219
if let address = accountService.account?.address,
220220
let partenerAddress = transaction.partner?.address {
221221

222-
let partnerName = addressBookService.getName(key: partenerAddress)
222+
let partnerName = addressBookService.getName(for: partenerAddress)
223223

224224
if address == transaction.senderId {
225225
controller.senderName = String.adamantLocalized.transactionDetails.yourAddress
@@ -245,7 +245,7 @@ class AdmTransactionsViewController: TransactionsListViewControllerBase {
245245

246246
let amount: Decimal = transaction.amount as Decimal? ?? 0
247247

248-
var partnerName = transaction.partner?.name?.checkAndReplaceSystemWallets() ?? addressBookService.getName(key: partnerId)
248+
var partnerName = transaction.partner?.name?.checkAndReplaceSystemWallets() ?? addressBookService.getName(for: partnerId)
249249

250250
if let address = accountService.account?.address, partnerId == address {
251251
partnerName = String.adamantLocalized.transactionDetails.yourAddress

0 commit comments

Comments
 (0)