Skip to content

Commit

Permalink
move tab config out of json
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Sep 23, 2024
1 parent f77cf4d commit 099de7a
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ open class RoutingTabBarController: UITabBarController, ParsingProtocol {

private var actionPath: String?

@IBInspectable open var routingMap: String? {
didSet {
if routingMap != oldValue {
if let destinations = parser.asArray(JsonLoader.load(bundles: Bundle.particles, fileName: routingMap)) {
parse(array: destinations)
}
}
}
}

var previousController: UIViewController?

override open func viewDidLoad() {
Expand Down Expand Up @@ -104,18 +94,6 @@ open class RoutingTabBarController: UITabBarController, ParsingProtocol {
}
}

public func parse(array: [Any]) {
if let data = array as? [[String: Any]] {
var maps = [TabbarItemInfo]()
for dictionary in data {
let routing = TabbarItemInfo()
routing.parse(dictionary: dictionary)
maps.append(routing)
}
self.maps = maps
}
}

open func diff(current: [TabbarItemInfo], old: [TabbarItemInfo]) -> Diff {
return old.diff(current) { (object1, object2) -> Bool in
object1.path == object2.path
Expand Down Expand Up @@ -160,8 +138,8 @@ open class RoutingTabBarController: UITabBarController, ParsingProtocol {
}

private func path(info: TabbarItemInfo) -> String? {
if let router = Router.shared as? MappedUIKitRouter, let path = info.path {
let request = RoutingRequest(path: path)
if let router = Router.shared as? MappedUIKitRouter {
let request = RoutingRequest(path: info.path)
return router.transform(request: request).path
} else {
return nil
Expand Down Expand Up @@ -225,13 +203,11 @@ open class RoutingTabBarController: UITabBarController, ParsingProtocol {

private func setup(viewController: UIViewController, info: TabbarItemInfo) {
let tabbarItem = UITabBarItem()
tabbarItem.title = info.title?.localized ?? " "
if let image = info.image {
tabbarItem.image = UIImage.named(image, bundles: Bundle.particles)
}
if let selected = info.selected {
tabbarItem.selectedImage = UIImage.named(selected, bundles: Bundle.particles)
}
tabbarItem.title = info.title
tabbarItem.image = UIImage.named(info.image, bundles: Bundle.particles)
// if let selected = info.selected {
// tabbarItem.selectedImage = UIImage.named(selected, bundles: Bundle.particles)
// }
viewController.tabBarItem = tabbarItem
}

Expand All @@ -241,11 +217,7 @@ open class RoutingTabBarController: UITabBarController, ParsingProtocol {
let viewController = viewControllers[i]
if let tabbarItem = viewController.tabBarItem {
let map = maps[i]
if let path = map.path {
tabbarItem.badgeValue = badging?.badge(for: path)
} else {
tabbarItem.badgeValue = nil
}
tabbarItem.badgeValue = badging?.badge(for: map.path)
}
}
}
Expand Down Expand Up @@ -276,24 +248,21 @@ open class RoutingTabBarController: UITabBarController, ParsingProtocol {
}
}

public class TabbarItemInfo: NSObject, ParsingProtocol {
override open var parser: Parser {
return RoutingTabBarController.parserOverwrite ?? super.parser
public struct TabbarItemInfo: Equatable {
public init(path: String,
title: String?,
image: String,
split: Bool) {
self.path = path
self.title = title
self.image = image
self.split = split
}

public var path: String?
public var path: String
public var title: String?
public var image: String?
public var selected: String?
public var split: Bool?

public func parse(dictionary: [String: Any]) {
path = parser.asString(dictionary["path"])
title = parser.asString(dictionary["title"])
image = parser.asString(dictionary["image"])
selected = parser.asString(dictionary["selected"])
split = parser.asBoolean(dictionary["split"])?.boolValue
}
public var image: String
public var split: Bool
}

extension RoutingTabBarController: UITabBarControllerDelegate {
Expand Down
2 changes: 1 addition & 1 deletion dydx/dydxPresenters/dydxPresenters/_Features/tabs_v4.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"path":"/portfolio",
"title":"",
"image":"icon_wallet",
"image":"icon_portfolio",
"split":true
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"path":"/portfolio",
"title":"",
"image":"icon_wallet",
"image":"icon_portfolio",
"split":true
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,11 @@ public class dydxV4TabBarBuilder: NSObject, ObjectBuilderProtocol {
}
}

override public var selectedIndex: Int {
didSet {
if selectedIndex != oldValue {
update(index: oldValue, selected: false)
update(index: selectedIndex, selected: true)
}
}
}

public override func viewDidLoad() {
super.viewDidLoad()

createCenterButton()
routingMap = dydxBoolFeatureFlag.isVaultEnabled.isEnabled ? "tabs_v4_vault.json" : "tabs_v4.json"
maps = dydxBoolFeatureFlag.isVaultEnabled.isEnabled ? Self.tabBarItemInfosV2 : Self.tabBarItemInfos
}

override public func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -87,28 +78,53 @@ public class dydxV4TabBarBuilder: NSObject, ObjectBuilderProtocol {
private func updateCenterButton() {
centerButton?.buttonImage = UIImage.named("icon_trade", bundles: Bundle.particles)
}
}

private func update(index: Int, selected: Bool) {
if index != NSNotFound {
if let item = tabBar.items?[index] {
update(item: item, index: index, selected: selected)
}
}
}

private func update(item: UITabBarItem, index: Int, selected: Bool) {
item.title = selected ? "" : " "
// if selected, [0, 3, 4].contains(index), wallet === nil {
// promptLogin()
// }
}

override public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let items = tabBar.items {
for index in 0 ..< items.count {
let tab = items[index]
update(item: tab, index: index, selected: tab === item)
}
}
}
private extension dydxV4TabBarController {
static let tabBarItemInfos: [TabbarItemInfo] = [
.init(path: "/portfolio",
title: DataLocalizer.localize(path: "APP.PORTFOLIO.PORTFOLIO"),
image: "icon_portfolio",
split: true),
.init(path: "/markets",
title: DataLocalizer.localize(path: "APP.GENERAL.MARKETS"),
image: "icon_market",
split: true),
.init(path: "/trade",
title: DataLocalizer.localize(path: "APP.GENERAL.TRADE"),
image: "icon_trade",
split: true),
.init(path: "/alerts",
title: DataLocalizer.localize(path: "APP.GENERAL.ALERTS"),
image: "icon_alerts",
split: true),
.init(path: "/my-profile",
title: DataLocalizer.localize(path: "APP.GENERAL.PROFILE"),
image: "icon_profile",
split: true)
]

static let tabBarItemInfosV2: [TabbarItemInfo] = [
.init(path: "/portfolio",
title: DataLocalizer.localize(path: "APP.PORTFOLIO.PORTFOLIO"),
image: "icon_portfolio",
split: true),
.init(path: "/markets",
title: DataLocalizer.localize(path: "APP.GENERAL.MARKETS"),
image: "icon_market",
split: true),
.init(path: "/trade",
title: DataLocalizer.localize(path: "APP.GENERAL.TRADE"),
image: "icon_trade",
split: true),
.init(path: "/vault",
title: DataLocalizer.localize(path: "APP.VAULTS.EARN"),
image: "icon_earn",
split: true),
.init(path: "/my-profile",
title: DataLocalizer.localize(path: "APP.GENERAL.PROFILE"),
image: "icon_profile",
split: true)
]
}

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "Vector-23.pdf",
"filename" : "icon_market.pdf",
"idiom" : "universal"
}
],
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "icon_portfolio.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "Vector-25.pdf",
"filename" : "icon_profile.pdf",
"idiom" : "universal"
}
],
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 099de7a

Please sign in to comment.