202 lines
7.9 KiB
Swift
202 lines
7.9 KiB
Swift
//
|
|
// MainControllerContent.swift
|
|
// List
|
|
//
|
|
// Created by Igor Danich on 25.06.2020.
|
|
// Copyright © 2020 Igor Danich. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import class Resolver.Resolver
|
|
import struct RealmSwift.Results
|
|
import class RealmSwift.NotificationToken
|
|
import Combine
|
|
import WalletKit
|
|
|
|
final class MainControllerContent: UITabBarController {
|
|
|
|
var update: (() -> Void)?
|
|
|
|
deinit { bag.flush() }
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
private var deals: Results<P2PDeal> = Resolver.resolve()
|
|
private var notificationTokens: [NotificationToken] = []
|
|
|
|
private func setupP2PBadges() {
|
|
// viewControllers?.first(where: { (($0 as? UINavigationController)?.viewControllers.first as? P2PControllerDashboard)?.service.kind == .buy })?.tabBarItem.badgeValue = "\(P2PMyDealsService.shared.allBuyDeals.count)"
|
|
|
|
let activeDeals = deals.filter("didWithdraw == false || status IN {'proceed', 'dispute'}")
|
|
let allBuyDealsCount = activeDeals.filter("buyer == '\(Accounts().current?.name ?? "")'").count
|
|
let allSellDealsCount = activeDeals.filter("seller == '\(Accounts().current?.name ?? "")'").count
|
|
|
|
if let viewControllers = self.viewControllers,
|
|
let viewController = viewControllers[safe: 2] {
|
|
viewController.tabBarItem.badgeValue = allSellDealsCount > 0 ? "\(allSellDealsCount)" : nil
|
|
}
|
|
|
|
if let viewControllers = self.viewControllers,
|
|
let viewController = viewControllers[safe: 3] {
|
|
viewController.tabBarItem.badgeValue = allBuyDealsCount > 0 ? "\(allBuyDealsCount)" : nil
|
|
}
|
|
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
self.setupTabBarOrder()
|
|
|
|
Notification.subscribe(bag: bag, name: .didChangeInterface) { [weak self]_ in
|
|
self?.setupTabBarOrder()
|
|
}
|
|
|
|
Accounts().activePublisher
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] wallet in
|
|
|
|
guard let self else { return }
|
|
|
|
self.notificationTokens.removeAll()
|
|
self.deals = Resolver.resolve()
|
|
self.notificationTokens.append(contentsOf: [
|
|
self.deals.observe { [weak self] _ in self?.setupP2PBadges() }
|
|
].compactMap { $0 })
|
|
|
|
// Cryprocash exchange requests
|
|
self.viewControllers?[4].tabBarItem.badgeValue = nil
|
|
|
|
self.checkForAccountAvailability(wallet: wallet)
|
|
}
|
|
.store(in: &self.cancellables)
|
|
|
|
Notification.subscribe(name: .didUpdateHistory) {
|
|
guard Accounts().current?.name == $0.userInfo?["username"] as? String else { return }
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
if let currentVisibleNavController = self.viewControllers?[self.selectedIndex],
|
|
let topVC = currentVisibleNavController.children.last {
|
|
self.setupMainControllerPresentation(on: topVC)
|
|
}
|
|
}
|
|
|
|
func push(_ viewController: UIViewController, animated: Bool) {
|
|
guard let navigationController = selectedViewController as? UINavigationController,
|
|
!(navigationController.topViewController?.isKind(of: type(of: viewController)) ?? true) else { return }
|
|
|
|
(selectedViewController as? UINavigationController)?.pushViewController(viewController, animated: animated)
|
|
}
|
|
|
|
func pop(animated: Bool) {
|
|
(selectedViewController as? UINavigationController)?.popViewController(animated: animated)
|
|
}
|
|
|
|
func popToRoot(animated: Bool) {
|
|
(selectedViewController as? UINavigationController)?.popToRootViewController(animated: animated)
|
|
}
|
|
|
|
func setupTabBarOrder() {
|
|
let viewControllers: [UIViewController]
|
|
if UserDefaults.standard.bool(forKey: "chatsFirst") {
|
|
viewControllers = [
|
|
StoryboardScene.CryptoChat.initialScene.instantiate(),
|
|
StoryboardScene.Wallet.initialScene.instantiate(),
|
|
// StoryboardScene.Swap.swapNavigationMain.instantiate(),
|
|
p2p(isSell: true),
|
|
p2p(isSell: false),
|
|
StoryboardScene.DeFi.deFiNavigationControllerMain.instantiate()
|
|
]
|
|
self.selectedIndex = 0
|
|
} else {
|
|
viewControllers = [
|
|
StoryboardScene.Wallet.initialScene.instantiate(),
|
|
StoryboardScene.CryptoChat.initialScene.instantiate(),
|
|
// StoryboardScene.Swap.swapNavigationMain.instantiate(),
|
|
p2p(isSell: true),
|
|
p2p(isSell: false),
|
|
StoryboardScene.DeFi.deFiNavigationControllerMain.instantiate()
|
|
]
|
|
}
|
|
// viewControllers?
|
|
// .compactMap({ $0 as? UINavigationController })
|
|
// .compactMap({ $0.viewControllers.first })
|
|
// .filter({ !($0 is CryptoChatControllerChats) })
|
|
// .forEach({ print($0) })
|
|
// .forEach({ _ = $0.view })
|
|
viewControllers
|
|
.compactMap { $0 as? UINavigationController }
|
|
.forEach { $0.delegate = self }
|
|
|
|
self.viewControllers = viewControllers
|
|
// selectedIndex = 2
|
|
}
|
|
|
|
private func p2p(isSell: Bool) -> UIViewController {
|
|
let ctrl = StoryboardScene.P2P.initial.instantiate()
|
|
ctrl.service = P2P.Service.dashboard(kind: isSell ? .sell : .buy)
|
|
let navCtrl = UINavigationController(rootViewController: ctrl)
|
|
navCtrl.tabBarItem = .init(
|
|
title: isSell ? L10n.TabBar.p2pSell : L10n.TabBar.p2pBuy,
|
|
image: isSell ? Asset.mainP2pSell.image : Asset.mainP2pBuy.image,
|
|
tag: 0
|
|
)
|
|
return navCtrl
|
|
}
|
|
|
|
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
|
|
update?()
|
|
}
|
|
|
|
private func checkForAccountAvailability(wallet: WalletKit.Wallet?) {
|
|
if !wallet.isExist {
|
|
self.selectedIndex = UserDefaults.standard.bool(forKey: "chatsFirst") ? 1 : 0
|
|
}
|
|
|
|
self.tabBar.items?.forEach { $0.isEnabled = wallet.isExist }
|
|
}
|
|
|
|
func selectChatViewController() {
|
|
self.selectedIndex = UserDefaults.standard.bool(forKey: "chatsFirst") ? 0 : 1
|
|
}
|
|
|
|
func setTabBarHidden(hidden: Bool, viewController: UIViewController? = nil) {
|
|
self.tabBar.isHidden = hidden
|
|
viewController?.view.setNeedsLayout()
|
|
}
|
|
|
|
private func setupMainControllerPresentation(on controller: UIViewController) {
|
|
switch controller {
|
|
case let vc where vc is WalletController,
|
|
let vc where vc is CryptoChatControllerChats:
|
|
self.changeMainControllerPresentation(visibility: true, isTabbarHidden: false)
|
|
case let vc where vc is P2PControllerDashboard,
|
|
let vc where vc is DeFiControllerMain:
|
|
self.changeMainControllerPresentation(visibility: false, isTabbarHidden: false)
|
|
default:
|
|
self.changeMainControllerPresentation(visibility: false,
|
|
isTabbarHidden: true,
|
|
on: controller)
|
|
}
|
|
}
|
|
|
|
private func changeMainControllerPresentation(visibility: Bool,
|
|
isTabbarHidden: Bool,
|
|
on controller: UIViewController? = nil) {
|
|
self.mainController.inviteView.setupVisability(visibleOnController: visibility)
|
|
self.mainController.view.layoutIfNeeded()
|
|
self.setTabBarHidden(hidden: isTabbarHidden, viewController: controller)
|
|
}
|
|
}
|
|
|
|
extension MainControllerContent: UINavigationControllerDelegate {
|
|
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
|
|
self.update?()
|
|
self.setupMainControllerPresentation(on: viewController)
|
|
}
|
|
}
|