-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple navigation to test app (#1487)
* Add simple navigation to test app For some reason iOS is broken and never receives the click within the JS. Will look into separately. * Update test-app/presenter/src/commonMain/kotlin/com/example/redwood/testing/presenter/TestApp.kt Co-authored-by: Veyndan Stuart <[email protected]> --------- Co-authored-by: Veyndan Stuart <[email protected]>
- Loading branch information
1 parent
c03cf30
commit a287adc
Showing
10 changed files
with
193 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...app/android-views/src/main/kotlin/com/example/redwood/testing/android/views/ViewButton.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.example.redwood.testing.android.views | ||
|
||
import android.view.View | ||
import android.widget.Button as WidgetButton | ||
import app.cash.redwood.Modifier | ||
import com.example.redwood.testing.widget.Button | ||
|
||
internal class ViewButton( | ||
override val value: WidgetButton, | ||
) : Button<View> { | ||
override var modifier: Modifier = Modifier | ||
|
||
override fun text(text: String?) { | ||
value.text = text | ||
} | ||
|
||
override fun onClick(onClick: (() -> Unit)?) { | ||
value.setOnClickListener( | ||
if (onClick != null) { | ||
{ onClick.invoke() } | ||
} else { | ||
null | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
import TestAppKt | ||
import UIKit | ||
|
||
class ButtonBinding: Button { | ||
private let root: UIButton = { | ||
let view = UIButton() | ||
view.backgroundColor = UIColor.gray | ||
return view | ||
}() | ||
|
||
var modifier: Modifier = ExposedKt.modifier() | ||
var value: Any { root } | ||
var onClick: (() -> Void)? = nil | ||
|
||
func text(text: String?) { | ||
root.setTitle(text, for: .normal) | ||
|
||
// This very simple integration wraps the size of whatever text is entered. Calling | ||
// this function will update the bounds and trigger relayout in the parent. | ||
root.sizeToFit() | ||
} | ||
|
||
func onClick(onClick: (() -> Void)? = nil) { | ||
self.onClick = onClick | ||
if (onClick != nil) { | ||
root.addTarget(self, action: #selector(clicked), for: .touchUpInside) | ||
} else { | ||
root.removeTarget(self, action: #selector(clicked), for: .touchUpInside) | ||
} | ||
} | ||
|
||
@objc func clicked() { | ||
if (self.onClick != nil) { | ||
self.onClick() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
test-app/presenter/src/commonMain/kotlin/com/example/redwood/testing/presenter/TestApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.example.redwood.testing.presenter | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import app.cash.redwood.Modifier | ||
import app.cash.redwood.layout.api.Constraint | ||
import app.cash.redwood.layout.api.Constraint.Companion | ||
import app.cash.redwood.layout.api.CrossAxisAlignment | ||
import app.cash.redwood.layout.api.Overflow | ||
import app.cash.redwood.layout.compose.Column | ||
import app.cash.redwood.ui.Margin | ||
import app.cash.redwood.ui.dp | ||
import com.example.redwood.testing.compose.Button | ||
import com.example.redwood.testing.compose.Text | ||
|
||
@Composable | ||
fun TestApp(httpClient: HttpClient) { | ||
val screen = remember { mutableStateOf<Screen?>(null) } | ||
val activeScreen = screen.value | ||
if (activeScreen == null) { | ||
HomeScreen(screen) | ||
} else { | ||
Column( | ||
width = Constraint.Fill, | ||
height = Constraint.Fill, | ||
) { | ||
Button("Back", onClick = { screen.value = null }) | ||
activeScreen.Show(httpClient, modifier = Modifier.grow(1.0)) | ||
} | ||
} | ||
} | ||
|
||
@Suppress("unused") // Used via reflection. | ||
enum class Screen { | ||
RepoSearch { | ||
@Composable | ||
override fun Show(httpClient: HttpClient, modifier: Modifier) { | ||
RepoSearch(httpClient, modifier) | ||
} | ||
}, | ||
; | ||
|
||
@Composable | ||
abstract fun Show(httpClient: HttpClient, modifier: Modifier) | ||
} | ||
|
||
@Composable | ||
private fun HomeScreen(screen: MutableState<Screen?>) { | ||
Column( | ||
width = Constraint.Fill, | ||
height = Companion.Fill, | ||
overflow = Overflow.Scroll, | ||
horizontalAlignment = CrossAxisAlignment.Stretch, | ||
) { | ||
Text("Test App Screens:", modifier = Modifier.margin(Margin(8.dp))) | ||
Screen.entries.forEach { | ||
Button(it.name, onClick = { | ||
screen.value = it | ||
}) | ||
} | ||
} | ||
} |