Repository navigation
fix: Android widget ETA and refresh failure states #101
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
Open
steventeng2022
wants to merge
1
commit into
YetAnotherBusDeveloper:main
Choose a base branch
from
steventeng2022:codex/issue-30-widget-eta
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
15 changes: 15 additions & 0 deletions
15
android/app/src/main/kotlin/tw/avianjay/taiwanbus/flutter/WidgetEtaText.kt
This file contains hidden or 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,15 @@ | ||
| package tw.avianjay.taiwanbus.flutter | ||
|
|
||
| /** Keep unavailable estimates distinct from a bus actually entering the stop. */ | ||
| internal fun widgetEtaText(seconds: Int?, message: String?): String { | ||
| val normalizedMessage = message?.trim().orEmpty() | ||
| if (normalizedMessage.isNotEmpty() && !normalizedMessage.equals("null", ignoreCase = true)) { | ||
| return normalizedMessage | ||
| } | ||
| return when { | ||
| seconds == null || seconds < 0 -> "--" | ||
| seconds == 0 -> "進站中" | ||
| seconds < 60 -> "即將進站" | ||
| else -> "${seconds / 60}分" | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
android/app/src/test/kotlin/tw/avianjay/taiwanbus/flutter/FavoriteWidgetRenderingTest.kt
This file contains hidden or 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,55 @@ | ||
| package tw.avianjay.taiwanbus.flutter | ||
|
|
||
| import android.content.Context | ||
| import android.view.View | ||
| import android.widget.FrameLayout | ||
| import android.widget.LinearLayout | ||
| import android.widget.TextView | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.RuntimeEnvironment | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [28]) | ||
| class FavoriteWidgetRenderingTest { | ||
| private val context: Context = RuntimeEnvironment.getApplication() | ||
|
|
||
| @Test | ||
| fun failedStationIsNotReportedAsNoUpcomingBus() { | ||
| // Blank provider fails locally, with no network request. | ||
| val result = render(listOf(item("station"))) | ||
| val root = result.views.apply(context, FrameLayout(context)) | ||
| assertEquals("更新失敗", root.findViewById<TextView>(R.id.favorite_widget_item_eta).text.toString()) | ||
| assertEquals("無法取得班次,請重試", root.findViewById<TextView>(R.id.favorite_widget_item_stop).text.toString()) | ||
| assertEquals(View.VISIBLE, root.findViewById<View>(R.id.favorite_widget_empty).visibility) | ||
| assertFalse(result.updateTimestamp) | ||
| } | ||
|
|
||
| @Test | ||
| fun hiddenEntriesDoNotTriggerFailures() { | ||
| val result = render(List(6) { item("route") } + item("station")) | ||
| val root = result.views.apply(context, FrameLayout(context)) | ||
| assertEquals(6, root.findViewById<LinearLayout>(R.id.favorite_widget_items_container).childCount) | ||
| assertEquals(View.GONE, root.findViewById<View>(R.id.favorite_widget_empty).visibility) | ||
| } | ||
|
|
||
| private fun render(items: List<FavoriteWidgetItem>): WidgetRenderResult { | ||
| val method = FavoriteGroupWidgetSupport::class.java.getDeclaredMethod( | ||
| "buildContentRemoteViews", Context::class.java, Int::class.javaPrimitiveType, | ||
| String::class.java, List::class.java, | ||
| ) | ||
| method.isAccessible = true | ||
| return method.invoke(FavoriteGroupWidgetSupport, context, 1, "Test", items) as WidgetRenderResult | ||
| } | ||
|
|
||
| private fun item(type: String) = FavoriteWidgetItem( | ||
| type = type, provider = "", routeKey = 1, pathId = 0, stopId = 1, | ||
| routeId = null, routeName = "Test route", routeDescription = null, | ||
| stopName = "Test stop", stationId = "test", stationName = "Test station", | ||
| destinationPathId = null, destinationStopId = null, destinationStopName = null, | ||
| ) | ||
| } |
37 changes: 37 additions & 0 deletions
37
android/app/src/test/kotlin/tw/avianjay/taiwanbus/flutter/WidgetEtaTextTest.kt
This file contains hidden or 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,37 @@ | ||
| package tw.avianjay.taiwanbus.flutter | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class WidgetEtaTextTest { | ||
| @Test | ||
| fun unavailableEstimatesAreNotArriving() { | ||
| listOf<Int?>(null, -1, -60, Int.MIN_VALUE).forEach { seconds -> | ||
| assertEquals("--", widgetEtaText(seconds, null)) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun arrivalBoundaries() { | ||
| assertEquals("進站中", widgetEtaText(0, null)) | ||
| assertEquals("即將進站", widgetEtaText(1, null)) | ||
| assertEquals("即將進站", widgetEtaText(59, null)) | ||
| assertEquals("1分", widgetEtaText(60, null)) | ||
| assertEquals("1分", widgetEtaText(119, null)) | ||
| assertEquals("2分", widgetEtaText(120, null)) | ||
| } | ||
|
|
||
| @Test | ||
| fun meaningfulMessagesTakePrecedence() { | ||
| assertEquals("未發車", widgetEtaText(-1, " 未發車 ")) | ||
| assertEquals("末班已過", widgetEtaText(0, "末班已過")) | ||
| } | ||
|
|
||
| @Test | ||
| fun emptyAndJsonNullMessagesDoNotHideEstimates() { | ||
| listOf<String?>(null, "", " ", "null", " NULL ").forEach { message -> | ||
| assertEquals("2分", widgetEtaText(120, message)) | ||
| assertEquals("--", widgetEtaText(-1, message)) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.