You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

115 lines
3.4 KiB

//
// CustomTabBar.swift
// GME Remit
//
// Created by Dibya Malla Thakuri on 05/03/2023.
// Copyright © 2023 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import RxSwift
import RxCocoa
import RxGesture
final class CustomTabBar: UIStackView {
var itemTapped: Observable<Int> { itemTappedSubject.asObservable() }
private lazy var customItemViews: [CustomItemTabView] = [homeItem, sendItem, profileItem]
private let homeItem = CustomItemTabView(with: .home, index: 0)
private let sendItem = CustomItemTabView(with: .sendMoney, index: 1)
private let profileItem = CustomItemTabView(with: .profile, index: 2)
var itemTappedSubject = PublishSubject<Int>()
private let disposeBag = DisposeBag()
var selectedItem: Int = 0
init() {
super.init(frame: .zero)
setupHierarchy()
setupProperties()
bind()
setNeedsLayout()
layoutIfNeeded()
selectItem(index: selectedItem)
NotificationCenter.default.addObserver(
self,
selector: #selector(updateTabar),
name: NSNotification.Name(getBeneficeriesNavigateName().rawValue),
object: nil
)
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupHierarchy() {
addArrangedSubviews([homeItem, sendItem, profileItem])
}
private func setupProperties() {
distribution = .fillEqually
alignment = .top
backgroundColor = .themeWhite
setupCornerRadius(20)
customItemViews.forEach {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.clipsToBounds = true
}
}
@objc func updateTabar(_ notification: NSNotification) {
if let dict = notification.userInfo as NSDictionary? {
if let id = dict["index"] as? Int{
selectItem(index: id)
}
}
}
private func selectItem(index: Int) {
customItemViews.forEach { $0.isSelected = $0.index == index }
itemTappedSubject.onNext(index)
}
func getBeneficeriesNavigateName() -> Notification.Name {
return Notification.Name.init(rawValue: AppConstants.tabbarItemChangeNotification)
}
//MARK: - Bindings
private func bind() {
homeItem.rx.tapGesture()
.when(.recognized)
.bind { [weak self] _ in
guard let self = self else { return }
self.homeItem.animateClick {
self.selectItem(index: self.homeItem.index)
}
}
.disposed(by: disposeBag)
sendItem.rx.tapGesture()
.when(.recognized)
.bind { [weak self] _ in
guard let self = self else { return }
self.sendItem.animateClick {
self.selectItem(index: self.sendItem.index)
}
}
.disposed(by: disposeBag)
profileItem.rx.tapGesture()
.when(.recognized)
.bind { [weak self] _ in
guard let self = self else { return }
self.profileItem.animateClick {
self.selectItem(index: self.profileItem.index)
}
}
.disposed(by: disposeBag)
}
}