Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a TreehouseApp argument to CodeListener #1694

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 app.cash.redwood.treehouse

internal interface CodeEventPublisher {
fun onInitialCodeLoading(
view: TreehouseView<*>,
)

fun onCodeLoaded(
view: TreehouseView<*>,
initial: Boolean,
)

fun onUncaughtException(
view: TreehouseView<*>,
exception: Throwable,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ public open class CodeListener {
* Invoked when the initial code is still loading. This can be used to signal a loading state
* in the UI before there is anything to display.
*/
public open fun onInitialCodeLoading(view: TreehouseView<*>) {}
public open fun onInitialCodeLoading(
app: TreehouseApp<*>,
view: TreehouseView<*>,
) {
}

/**
* Invoked each time new code is loaded. This is called after the view's old children have
* been cleared but before the children of the new code have been added.
*
* @param initial true if this is the first code loaded for this view's current content.
*/
public open fun onCodeLoaded(view: TreehouseView<*>, initial: Boolean) {}
public open fun onCodeLoaded(
app: TreehouseApp<*>,
view: TreehouseView<*>,
initial: Boolean,
) {
}

/**
* Invoked when the application powering [view] fails with an uncaught exception. This function
Expand All @@ -47,6 +56,7 @@ public open class CodeListener {
* called.
*/
public open fun onUncaughtException(
app: TreehouseApp<*>,
view: TreehouseView<*>,
exception: Throwable,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 app.cash.redwood.treehouse

internal class RealCodeEventPublisher(
private val codeListener: CodeListener,
private val app: TreehouseApp<*>,
) : CodeEventPublisher {
override fun onInitialCodeLoading(view: TreehouseView<*>) {
return codeListener.onInitialCodeLoading(app, view)
}

override fun onCodeLoaded(view: TreehouseView<*>, initial: Boolean) {
return codeListener.onCodeLoaded(app, view, initial)
}

override fun onUncaughtException(view: TreehouseView<*>, exception: Throwable) {
return codeListener.onUncaughtException(app, view, exception)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class TreehouseApp<A : AppService> private constructor(
return TreehouseAppContent(
codeHost = codeHost,
dispatchers = dispatchers,
codeListener = codeListener,
codeEventPublisher = RealCodeEventPublisher(codeListener, this),
source = source,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private sealed interface CodeState<A : AppService> {
internal class TreehouseAppContent<A : AppService>(
private val codeHost: CodeHost<A>,
private val dispatchers: TreehouseDispatchers,
private val codeListener: CodeListener,
private val codeEventPublisher: CodeEventPublisher,
private val source: TreehouseContentSource<A>,
) : Content, CodeHost.Listener<A>, CodeSession.Listener<A> {
private val stateFlow = MutableStateFlow<State<A>>(
Expand Down Expand Up @@ -147,7 +147,7 @@ internal class TreehouseAppContent<A : AppService>(
// Make sure we're showing something in the view; either loaded code or a spinner to show that
// code is coming.
when (nextCodeState) {
is CodeState.Idle -> codeListener.onInitialCodeLoading(view)
is CodeState.Idle -> codeEventPublisher.onInitialCodeLoading(view)
is CodeState.Running -> nextCodeState.viewContentCodeBinding.initView(view)
}

Expand Down Expand Up @@ -259,7 +259,7 @@ internal class TreehouseAppContent<A : AppService>(
// If there's an error and a UI, show it.
val view = (viewState as? ViewState.Bound)?.view
if (exception != null && view != null) {
codeListener.onUncaughtException(view, exception)
codeEventPublisher.onUncaughtException(view, exception)
}

val nextCodeState = CodeState.Idle<A>(isInitialLaunch = false)
Expand All @@ -281,7 +281,7 @@ internal class TreehouseAppContent<A : AppService>(
dispatchers = dispatchers,
eventPublisher = codeSession.eventPublisher,
contentSource = source,
codeListener = codeListener,
codeEventPublisher = codeEventPublisher,
stateFlow = stateFlow,
codeSession = codeSession,
isInitialLaunch = isInitialLaunch,
Expand Down Expand Up @@ -310,7 +310,7 @@ private class ViewContentCodeBinding<A : AppService>(
val dispatchers: TreehouseDispatchers,
val eventPublisher: EventPublisher,
val contentSource: TreehouseContentSource<A>,
val codeListener: CodeListener,
val codeEventPublisher: CodeEventPublisher,
val stateFlow: MutableStateFlow<State<A>>,
val codeSession: CodeSession<A>,
private val isInitialLaunch: Boolean,
Expand Down Expand Up @@ -427,7 +427,7 @@ private class ViewContentCodeBinding<A : AppService>(

if (changesCount++ == 0) {
view.reset()
codeListener.onCodeLoaded(view, isInitialLaunch)
codeEventPublisher.onCodeLoaded(view, isInitialLaunch)
}

bridge.sendChanges(changes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CodeHostTest {
appScope = appScope,
frameClockFactory = FakeFrameClock.Factory,
)
private val codeListener = FakeCodeListener(eventLog)
private val codeEventPublisher = FakeCodeEventPublisher(eventLog)
private val onBackPressedDispatcher = FakeOnBackPressedDispatcher(eventLog)

@AfterTest
Expand Down Expand Up @@ -288,7 +288,7 @@ class CodeHostTest {
return TreehouseAppContent(
codeHost = codeHost,
dispatchers = dispatchers,
codeListener = codeListener,
codeEventPublisher = codeEventPublisher,
source = { app -> app.newUi() },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@
*/
package app.cash.redwood.treehouse

class FakeCodeListener(
class FakeCodeEventPublisher(
private val eventLog: EventLog,
) : CodeListener() {
override fun onInitialCodeLoading(
view: TreehouseView<*>,
) {
) : CodeEventPublisher {
override fun onInitialCodeLoading(view: TreehouseView<*>) {
eventLog += "codeListener.onInitialCodeLoading($view)"
}

override fun onCodeLoaded(
view: TreehouseView<*>,
initial: Boolean,
) {
override fun onCodeLoaded(view: TreehouseView<*>, initial: Boolean) {
eventLog += "codeListener.onCodeLoaded($view, initial = $initial)"
}

override fun onUncaughtException(
view: TreehouseView<*>,
exception: Throwable,
) {
override fun onUncaughtException(view: TreehouseView<*>, exception: Throwable) {
// Canonicalize "java.lang.Exception(boom!)" to "kotlin.Exception(boom!)".
val exceptionString = exception.toString().replace("java.lang.", "kotlin.")
eventLog += "codeListener.onUncaughtException($view, $exceptionString)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TreehouseAppContentTest {
appScope = appScope,
frameClockFactory = FakeFrameClock.Factory,
)
private val codeListener = FakeCodeListener(eventLog)
private val codeEventPublisher = FakeCodeEventPublisher(eventLog)
private val uiConfiguration = UiConfiguration()

@BeforeTest
Expand Down Expand Up @@ -450,7 +450,7 @@ class TreehouseAppContentTest {
return TreehouseAppContent(
codeHost = codeHost,
dispatchers = dispatchers,
codeListener = codeListener,
codeEventPublisher = codeEventPublisher,
source = { app -> app.newUi() },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ class EmojiSearchActivity : ComponentActivity() {
}

private val codeListener: CodeListener = object : CodeListener() {
override fun onUncaughtException(view: TreehouseView<*>, exception: Throwable) {
override fun onUncaughtException(
app: TreehouseApp<*>,
view: TreehouseView<*>,
exception: Throwable,
) {
treehouseLayout.reset()
treehouseLayout.addView(
ExceptionView(treehouseLayout, exception),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class EmojiSearchCodeListener : CodeListener {
self.treehouseView = treehouseView
}

override func onUncaughtException(view: TreehouseView, exception: KotlinThrowable) {
override func onUncaughtException(app: TreehouseApp<AnyObject>, view: TreehouseView, exception: KotlinThrowable) {
treehouseView.reset()

let exceptionView = ExceptionView(exception)
Expand Down