185 lines
7.4 KiB
Swift
185 lines
7.4 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
|
|
|
|
class MainControllerContent: UITabBarController {
|
|
|
|
var update: (() -> Void)?
|
|
|
|
deinit { bag.flush() }
|
|
|
|
private var requestsService: CCCP.Service.ExchangeRequests { Resolver.resolve() }
|
|
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?.username ?? "")'").count
|
|
let allSellDealsCount = activeDeals.filter("seller == '\(Accounts().current?.username ?? "")'").count
|
|
|
|
if allSellDealsCount > 0 {
|
|
viewControllers?[2].tabBarItem.badgeValue = "\(allSellDealsCount)"
|
|
} else {
|
|
viewControllers?[2].tabBarItem.badgeValue = nil
|
|
}
|
|
|
|
if allBuyDealsCount > 0 {
|
|
viewControllers?[3].tabBarItem.badgeValue = "\(allBuyDealsCount)"
|
|
} else {
|
|
viewControllers?[3].tabBarItem.badgeValue = nil
|
|
}
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
setupTabBarOrder()
|
|
Notification.subscribe(bag: bag, name: .didChangeInterface) { [weak self]_ in
|
|
self?.setupTabBarOrder()
|
|
}
|
|
|
|
Notification.subscribe(name: .didChangeAccount) { [weak self] _ in
|
|
self?.notificationTokens.removeAll()
|
|
self?.deals = Resolver.resolve()
|
|
self?.notificationTokens.append(contentsOf: [
|
|
self?.deals.observe { [weak self] _ in self?.setupP2PBadges() }
|
|
].compactMap({ $0 }))
|
|
|
|
//Cryprocash exchange requuests
|
|
self?.viewControllers?[4].tabBarItem.badgeValue = nil
|
|
self?.requestsService.fetch()
|
|
|
|
self?.checkForAccountAvailability()
|
|
}
|
|
|
|
Notification.subscribe(name: .didUpdateCryptocashExchangeRequests) { [weak self] _ in
|
|
DispatchQueue.main.async {
|
|
if (self?.requestsService.requests.count ?? 0) > 0 {
|
|
self?.viewControllers?[4].tabBarItem.badgeValue = "\(self?.requestsService.requests.count ?? 0)"
|
|
} else {
|
|
self?.viewControllers?[4].tabBarItem.badgeValue = nil
|
|
}
|
|
}
|
|
}
|
|
|
|
Notification.subscribe(name: .didUpdateHistory) { [weak self] in
|
|
guard Accounts().current?.username == $0.userInfo?["username"] as? String else { return }
|
|
self?.requestsService.fetch()
|
|
}
|
|
}
|
|
|
|
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() {
|
|
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()
|
|
]
|
|
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 })
|
|
// 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() {
|
|
if Accounts().current == nil {
|
|
self.selectedIndex = UserDefaults.standard.bool(forKey: "chatsFirst") ? 1 : 0
|
|
}
|
|
|
|
self.tabBar.items?.forEach { $0.isEnabled = Accounts().current != nil }
|
|
}
|
|
|
|
// TODO: Refactor
|
|
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()
|
|
}
|
|
}
|
|
|
|
extension MainControllerContent: UINavigationControllerDelegate {
|
|
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
|
|
update?()
|
|
|
|
switch viewController {
|
|
case let vc where vc is WalletController:
|
|
self.mainController.inviteView.setupVisability(visibleOnController: true)
|
|
self.setTabBarHidden(hidden: false)
|
|
case let vc where vc is CryptoChatControllerChats:
|
|
self.mainController.inviteView.setupVisability(visibleOnController: true)
|
|
self.setTabBarHidden(hidden: false)
|
|
case let vc where vc is P2PControllerDashboard:
|
|
self.mainController.inviteView.setupVisability(visibleOnController: false)
|
|
self.setTabBarHidden(hidden: false)
|
|
case let vc where vc is DeFiControllerMain:
|
|
self.mainController.inviteView.setupVisability(visibleOnController: false)
|
|
self.setTabBarHidden(hidden: false)
|
|
default:
|
|
self.mainController.inviteView.setupVisability(visibleOnController: false)
|
|
self.setTabBarHidden(hidden: true, viewController: viewController)
|
|
}
|
|
}
|
|
}
|