-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/manual-backup-key-list' into feature/backup-cus…
…tom-key-flow-adaptation # Conflicts: # novawallet.xcodeproj/project.pbxproj
- Loading branch information
Showing
7 changed files
with
136 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
novawallet/Modules/ManualBackupKeyList/ManualBackupKeyListViewModelFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import Foundation | ||
import SoraFoundation | ||
|
||
class ManualBackupKeyListViewModelFactory { | ||
private let localizationManager: LocalizationManagerProtocol | ||
private let networkViewModelFactory: NetworkViewModelFactoryProtocol | ||
|
||
init( | ||
localizationManager: LocalizationManagerProtocol, | ||
networkViewModelFactory: NetworkViewModelFactoryProtocol | ||
) { | ||
self.localizationManager = localizationManager | ||
self.networkViewModelFactory = networkViewModelFactory | ||
} | ||
|
||
func createViewModel( | ||
from defaultChains: [ChainModel], | ||
_ customChains: [ChainModel] | ||
) -> ManualBackupKeyListViewLayout.Model { | ||
let listHeaderText = R.string.localizable.chainAccountsListHeader( | ||
preferredLanguages: localizationManager.selectedLocale.rLanguages | ||
) | ||
|
||
var sections: [ManualBackupKeyListViewLayout.Sections] = [] | ||
|
||
if !defaultChains.isEmpty { | ||
sections.append(createDefaultChainsSection(for: defaultChains)) | ||
} | ||
|
||
if !customChains.isEmpty { | ||
sections.append(createCustomChainsSection(for: customChains)) | ||
} | ||
|
||
return .init( | ||
listHeaderText: listHeaderText, | ||
accountsSections: sections | ||
) | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private extension ManualBackupKeyListViewModelFactory { | ||
func createDefaultChainsSection(for chains: [ChainModel]) -> ManualBackupKeyListViewLayout.Sections { | ||
let defaultChainsHeaderText = R.string.localizable.chainAccountsListDefaultHeader( | ||
preferredLanguages: localizationManager.selectedLocale.rLanguages | ||
) | ||
|
||
let defaultChainsTitleText = R.string.localizable.chainAccountsListDefaultTitle( | ||
preferredLanguages: localizationManager.selectedLocale.rLanguages | ||
) | ||
|
||
return .defaultKeys( | ||
.init( | ||
headerText: defaultChainsHeaderText.uppercased(), | ||
accounts: [ | ||
.init( | ||
title: defaultChainsTitleText, | ||
subtitle: formattedString(for: chains) | ||
) | ||
] | ||
) | ||
) | ||
} | ||
|
||
func createCustomChainsSection(for chains: [ChainModel]) -> ManualBackupKeyListViewLayout.Sections { | ||
let customChainsViewModels = chains | ||
.compactMap { [weak self] chain -> ManualBackupKeyListViewLayout.CustomAccount? in | ||
guard let self else { return .none } | ||
|
||
return ManualBackupKeyListViewLayout.CustomAccount( | ||
network: networkViewModelFactory.createViewModel(from: chain), | ||
chainId: chain.chainId | ||
) | ||
} | ||
|
||
let customChainsHeaderText = R.string.localizable.chainAccountsListCustomHeader( | ||
preferredLanguages: localizationManager.selectedLocale.rLanguages | ||
) | ||
|
||
return .customKeys( | ||
.init( | ||
headerText: customChainsHeaderText.uppercased(), | ||
accounts: customChainsViewModels | ||
) | ||
) | ||
} | ||
|
||
func formattedString(for defaultChains: [ChainModel]) -> String { | ||
let chainsToMention = defaultChains.count > 1 | ||
? defaultChains.prefix(2) | ||
: defaultChains.prefix(1) | ||
let restCount = defaultChains.count - chainsToMention.count | ||
let othersString = R.string.localizable.chainAccountsListDefaultSubtitle( | ||
restCount, | ||
preferredLanguages: localizationManager.selectedLocale.rLanguages | ||
) | ||
|
||
var joinedChains = chainsToMention | ||
.map(\.name) | ||
.joined(with: String.CompoundSeparator.commaSpace) | ||
|
||
return restCount > 0 | ||
? [joinedChains, othersString].joined(with: .commaSpace) | ||
: joinedChains | ||
} | ||
} |