Skip to content

Commit

Permalink
Add a code load status snackbar for Android view frontend (#1526)
Browse files Browse the repository at this point in the history
Other frontends to follow.
  • Loading branch information
JakeWharton authored Sep 29, 2023
1 parent cda5a95 commit 659328e
Showing 1 changed file with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import com.example.redwood.emojisearch.launcher.EmojiSearchAppSpec
import com.example.redwood.emojisearch.treehouse.EmojiSearchPresenter
import com.example.redwood.emojisearch.widget.EmojiSearchProtocolNodeFactory
import com.example.redwood.emojisearch.widget.EmojiSearchWidgetFactories
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.snackbar.Snackbar.LENGTH_INDEFINITE
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.flowOf
Expand All @@ -49,6 +51,7 @@ import okio.Path.Companion.toOkioPath

class EmojiSearchActivity : ComponentActivity() {
private val scope: CoroutineScope = CoroutineScope(Main)
private lateinit var treehouseLayout: TreehouseLayout

@SuppressLint("ResourceType")
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -71,13 +74,42 @@ class EmojiSearchActivity : ComponentActivity() {
)
}

setContentView(
TreehouseLayout(this, widgetSystem, onBackPressedDispatcher).apply {
// The view needs to have an id for Android to populate saved data back
this.id = 9000
treehouseContentSource.bindWhenReady(this, treehouseApp)
},
)
treehouseLayout = TreehouseLayout(this, widgetSystem, onBackPressedDispatcher).apply {
// The view needs to have an id for Android to populate saved data back
this.id = 9000
treehouseContentSource.bindWhenReady(this, treehouseApp)
}
setContentView(treehouseLayout)
}

private val appEventListener: EventListener = object : EventListener() {
private var success = true
private var snackbar: Snackbar? = null

override fun codeLoadFailed(app: TreehouseApp<*>, manifestUrl: String?, exception: Exception, startValue: Any?) {
Log.w("Treehouse", "codeLoadFailed", exception)
if (success) {
// Only show the Snackbar on the first transition from success.
success = false
snackbar =
Snackbar.make(treehouseLayout, "Unable to load guest code from server", LENGTH_INDEFINITE)
.setAction("Dismiss") { maybeDismissSnackbar() }
.also(Snackbar::show)
}
}

override fun codeLoadSuccess(app: TreehouseApp<*>, manifestUrl: String?, manifest: ZiplineManifest, zipline: Zipline, startValue: Any?) {
Log.i("Treehouse", "codeLoadSuccess")
success = true
maybeDismissSnackbar()
}

private fun maybeDismissSnackbar() {
snackbar?.let {
it.dismiss()
snackbar = null
}
}
}

private fun createTreehouseApp(): TreehouseApp<EmojiSearchPresenter> {
Expand Down Expand Up @@ -117,13 +149,3 @@ class EmojiSearchActivity : ComponentActivity() {
super.onDestroy()
}
}

val appEventListener: EventListener = object : EventListener() {
override fun codeLoadFailed(app: TreehouseApp<*>, manifestUrl: String?, exception: Exception, startValue: Any?) {
Log.w("Treehouse", "codeLoadFailed", exception)
}

override fun codeLoadSuccess(app: TreehouseApp<*>, manifestUrl: String?, manifest: ZiplineManifest, zipline: Zipline, startValue: Any?) {
Log.i("Treehouse", "codeLoadSuccess")
}
}

0 comments on commit 659328e

Please sign in to comment.