|
| 1 | +// |
| 2 | +// AndroidTabView.swift |
| 3 | +// AndroidSwiftUI |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 7/21/26. |
| 6 | +// |
| 7 | + |
| 8 | +import AndroidKit |
| 9 | + |
| 10 | +extension TabView: AndroidPrimitive { |
| 11 | + |
| 12 | + var renderedBody: AnyView { |
| 13 | + AnyView(AndroidTabViewContainer(proxy: _TabViewProxy(self))) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +/// Native container for `TabView`. A vertical `LinearLayout` that hosts the selected tab's content |
| 18 | +/// above a horizontal row of tab bar buttons. |
| 19 | +struct AndroidTabViewContainer<SelectionValue: Hashable, Content: View> { |
| 20 | + |
| 21 | + let proxy: _TabViewProxy<SelectionValue, Content> |
| 22 | +} |
| 23 | + |
| 24 | +extension AndroidTabViewContainer: ParentView { |
| 25 | + |
| 26 | + var children: [AnyView] { |
| 27 | + let items = proxy.items |
| 28 | + let selectedIndex = proxy.selectedIndex |
| 29 | + // only the selected tab's content is mounted, hidden tabs are never rendered |
| 30 | + let content = AndroidTabContent(content: proxy.selectedContent) |
| 31 | + let bar = AndroidTabBar( |
| 32 | + labels: items.enumerated().map { index, item in |
| 33 | + item.label ?? AnyView(Text("Tab \(index + 1)")) |
| 34 | + }, |
| 35 | + selectedIndex: selectedIndex, |
| 36 | + select: { proxy.select($0) } |
| 37 | + ) |
| 38 | + return [AnyView(content), AnyView(bar)] |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +extension AndroidTabViewContainer: AndroidViewRepresentable { |
| 43 | + |
| 44 | + typealias Coordinator = Void |
| 45 | + |
| 46 | + func makeAndroidView(context: Self.Context) -> AndroidWidget.LinearLayout { |
| 47 | + let view = AndroidWidget.LinearLayout(context.androidContext) |
| 48 | + view.orientation = .vertical |
| 49 | + view.setLayoutParams(ViewGroup.LayoutParams(.matchParent, .matchParent)) |
| 50 | + return view |
| 51 | + } |
| 52 | + |
| 53 | + func updateAndroidView(_ view: AndroidWidget.LinearLayout, context: Self.Context) { |
| 54 | + |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// MARK: - Content |
| 59 | + |
| 60 | +/// Hosts the selected tab's content, filling the space left over by the tab bar. |
| 61 | +/// |
| 62 | +/// Deliberately non-generic so that switching tabs updates this container in place (instead of |
| 63 | +/// remounting it), which keeps the content above the tab bar in the parent `LinearLayout`. |
| 64 | +struct AndroidTabContent { |
| 65 | + |
| 66 | + let content: AnyView |
| 67 | +} |
| 68 | + |
| 69 | +extension AndroidTabContent: ParentView { |
| 70 | + |
| 71 | + var children: [AnyView] { |
| 72 | + [content] |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +extension AndroidTabContent: AndroidViewRepresentable { |
| 77 | + |
| 78 | + typealias Coordinator = Void |
| 79 | + |
| 80 | + func makeAndroidView(context: Self.Context) -> AndroidWidget.LinearLayout { |
| 81 | + let view = AndroidWidget.LinearLayout(context.androidContext) |
| 82 | + view.orientation = .vertical |
| 83 | + // fill the remaining vertical space |
| 84 | + view.setLayoutParams(LinearLayout.LayoutParams(.matchParent, 0, 1).as(ViewGroup.LayoutParams.self)) |
| 85 | + return view |
| 86 | + } |
| 87 | + |
| 88 | + func updateAndroidView(_ view: AndroidWidget.LinearLayout, context: Self.Context) { |
| 89 | + |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// MARK: - Tab Bar |
| 94 | + |
| 95 | +/// The row of tab bar buttons displayed at the bottom of a `TabView`. |
| 96 | +struct AndroidTabBar { |
| 97 | + |
| 98 | + let labels: [AnyView] |
| 99 | + |
| 100 | + let selectedIndex: Int |
| 101 | + |
| 102 | + let select: (Int) -> () |
| 103 | +} |
| 104 | + |
| 105 | +extension AndroidTabBar: ParentView { |
| 106 | + |
| 107 | + var children: [AnyView] { |
| 108 | + labels.enumerated().map { index, label in |
| 109 | + AnyView(AndroidTabBarButton( |
| 110 | + label: label, |
| 111 | + isSelected: index == selectedIndex, |
| 112 | + action: { select(index) } |
| 113 | + )) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +extension AndroidTabBar: AndroidViewRepresentable { |
| 119 | + |
| 120 | + typealias Coordinator = Void |
| 121 | + |
| 122 | + func makeAndroidView(context: Self.Context) -> AndroidWidget.LinearLayout { |
| 123 | + let view = AndroidWidget.LinearLayout(context.androidContext) |
| 124 | + view.orientation = .horizontal |
| 125 | + view.gravity = .center |
| 126 | + view.setLayoutParams(LinearLayout.LayoutParams(.matchParent, .wrapContent, 0).as(ViewGroup.LayoutParams.self)) |
| 127 | + return view |
| 128 | + } |
| 129 | + |
| 130 | + func updateAndroidView(_ view: AndroidWidget.LinearLayout, context: Self.Context) { |
| 131 | + |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/// A single tab bar button, hosting its `tabItem(_:)` label as a mounted child. |
| 136 | +struct AndroidTabBarButton { |
| 137 | + |
| 138 | + let label: AnyView |
| 139 | + |
| 140 | + let isSelected: Bool |
| 141 | + |
| 142 | + let action: () -> () |
| 143 | +} |
| 144 | + |
| 145 | +extension AndroidTabBarButton: ParentView { |
| 146 | + |
| 147 | + var children: [AnyView] { |
| 148 | + [label] |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +extension AndroidTabBarButton: AndroidViewRepresentable { |
| 153 | + |
| 154 | + typealias Coordinator = Void |
| 155 | + |
| 156 | + func makeAndroidView(context: Self.Context) -> AndroidWidget.LinearLayout { |
| 157 | + let view = AndroidWidget.LinearLayout(context.androidContext) |
| 158 | + view.orientation = .vertical |
| 159 | + view.gravity = .center |
| 160 | + // every tab occupies an equal share of the tab bar |
| 161 | + view.setLayoutParams(LinearLayout.LayoutParams(0, .wrapContent, 1).as(ViewGroup.LayoutParams.self)) |
| 162 | + view.setClickable(true) |
| 163 | + updateView(view) |
| 164 | + return view |
| 165 | + } |
| 166 | + |
| 167 | + func updateAndroidView(_ view: AndroidWidget.LinearLayout, context: Self.Context) { |
| 168 | + updateView(view) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +private extension AndroidTabBarButton { |
| 173 | + |
| 174 | + func updateView(_ view: AndroidWidget.LinearLayout) { |
| 175 | + // the listener captures the current selection, so it has to be replaced on every update |
| 176 | + let listener = ViewOnClickListener(action: action) |
| 177 | + view.setOnClickListener(listener.as(AndroidView.View.OnClickListener.self)) |
| 178 | + view.setAlpha(isSelected ? 1.0 : 0.5) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// MARK: - Layout Params |
| 183 | + |
| 184 | +internal extension Int32 { |
| 185 | + |
| 186 | + static var matchParent: Int32 { |
| 187 | + try! JavaClass<ViewGroup.LayoutParams>().MATCH_PARENT |
| 188 | + } |
| 189 | + |
| 190 | + static var wrapContent: Int32 { |
| 191 | + try! JavaClass<ViewGroup.LayoutParams>().WRAP_CONTENT |
| 192 | + } |
| 193 | +} |
0 commit comments