Skip to content

Commit a717424

Browse files
committed
Add MainActivity.setupNavigationStack()
1 parent 62473ca commit a717424

File tree

1 file changed

+106
-2
lines changed

1 file changed

+106
-2
lines changed

Demo/app/src/main/swift/MainActivity.swift

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ open class MainActivity: AndroidApp.Activity {
2222
lazy var listView = ListView(self)
2323

2424
lazy var recyclerView = RecyclerView(self)
25+
26+
lazy var button = AndroidWidget.Button(self)
27+
28+
lazy var rootViewID: Int32 = try! JavaClass<AndroidView.View>().generateViewId()
2529
}
2630

2731
@JavaImplementation("com.pureswift.swiftandroid.MainActivity")
@@ -70,11 +74,17 @@ private extension MainActivity {
7074
}
7175

7276
func setRootView() {
73-
setTabBar()
77+
setupNavigationStack()
7478
}
7579

7680
func setTextView() {
77-
setRootView(textView)
81+
let linearLayout = LinearLayout(self)
82+
linearLayout.orientation = .vertical
83+
linearLayout.gravity = .center
84+
linearLayout.addView(textView)
85+
configureButton()
86+
linearLayout.addView(button)
87+
setRootView(linearLayout)
7888
// update view on timer
7989
Task { [weak self] in
8090
while let self {
@@ -84,6 +94,100 @@ private extension MainActivity {
8494
}
8595
}
8696

97+
func setupNavigationStack() {
98+
99+
let fragmentContainer = FrameLayout(self)
100+
fragmentContainer.setId(rootViewID)
101+
let matchParent = try! JavaClass<AndroidView.ViewGroup.LayoutParams>().MATCH_PARENT
102+
fragmentContainer.setLayoutParams(ViewGroup.LayoutParams(matchParent, matchParent))
103+
104+
let homeFragment = Fragment(callback: .init(onViewCreated: { view, bundle in
105+
let context = self
106+
107+
let linearLayout = LinearLayout(self)
108+
linearLayout.setLayoutParams(ViewGroup.LayoutParams(matchParent, matchParent))
109+
linearLayout.orientation = .vertical
110+
linearLayout.gravity = .center
111+
112+
let label = TextView(context)
113+
label.text = "Home View"
114+
label.gravity = .center
115+
linearLayout.addView(label)
116+
117+
let button = Button(context)
118+
button.text = "Push"
119+
label.gravity = .center
120+
let listener = ViewOnClickListener {
121+
self.didPushButton()
122+
}
123+
button.setOnClickListener(listener.as(View.OnClickListener.self))
124+
linearLayout.addView(button)
125+
126+
view.as(ViewGroup.self)!.addView(linearLayout)
127+
}))
128+
129+
// setup initial fragment
130+
_ = getFragmentManager()
131+
.beginTransaction()
132+
.replace(rootViewID, homeFragment)
133+
.addToBackStack(nil)
134+
.commit()
135+
136+
// Set as the content view
137+
setRootView(fragmentContainer)
138+
}
139+
140+
func configureButton() {
141+
button.text = "Push"
142+
let listener = ViewOnClickListener {
143+
self.didPushButton()
144+
}
145+
button.setOnClickListener(listener.as(View.OnClickListener.self))
146+
}
147+
148+
func didPushButton() {
149+
150+
let counter = getFragmentManager().getBackStackEntryCount() + 1
151+
152+
let detailFragment = Fragment(callback: .init(onViewCreated: { view, bundle in
153+
let context = self
154+
155+
let matchParent = try! JavaClass<AndroidView.ViewGroup.LayoutParams>().MATCH_PARENT
156+
157+
let linearLayout = LinearLayout(self)
158+
linearLayout.setLayoutParams(ViewGroup.LayoutParams(matchParent, matchParent))
159+
linearLayout.orientation = .vertical
160+
linearLayout.gravity = .center
161+
162+
let label = TextView(context)
163+
label.text = "Detail View \(counter)"
164+
label.gravity = .center
165+
linearLayout.addView(label)
166+
167+
let button = Button(context)
168+
button.text = "Push"
169+
button.gravity = .center
170+
let listener = ViewOnClickListener {
171+
self.didPushButton()
172+
}
173+
button.setOnClickListener(listener.as(View.OnClickListener.self))
174+
linearLayout.addView(button)
175+
176+
view.as(ViewGroup.self)!.addView(linearLayout)
177+
}))
178+
179+
push(detailFragment, name: "Detail \(counter)")
180+
}
181+
182+
func push(_ fragment: AndroidApp.Fragment, name: String) {
183+
log("\(self).\(#function) \(name)")
184+
_ = getFragmentManager()
185+
.beginTransaction()
186+
.replace(rootViewID, fragment)
187+
.addToBackStack(name)
188+
.commit()
189+
}
190+
87191
func setListView() {
88192
let items = [
89193
"Row 1",

0 commit comments

Comments
 (0)