diff --git a/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt b/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt index 8fe2ccd..40f77e6 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt @@ -136,41 +136,14 @@ class MainActivity : AppCompatActivity() { checkAllPermissions() StepCounterService.startServiceIfEnabled(this) - setupSections() + // Only the visible tab is inflated and bound before the first frame; the rest follow when idle. + bindTab(savedTabIndex()) setupPreviewWallpaper() setupTabLayout() setupGlobalSettingsSearch() updateLivePreview() - - // Advanced Section - bindSlider(R.id.row_update_interval, getString(R.string.row_update_interval), "update_interval", 15f, 1f, 60f, "m") { - // The repeating alarm only reads the interval when scheduled, so reschedule it now - val ids = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, AwidgetProvider::class.java)) - if (ids.isNotEmpty()) AwidgetProvider.scheduleWork(this) - } - - // Setup Changelog - val versionName = try { - packageManager.getPackageInfo(packageName, 0).versionName - } catch (e: Exception) { - getString(R.string.version_unknown) - } - val tvVersion = findViewById(R.id.tv_changelog_version) - tvVersion.text = getString(R.string.changelog_version, versionName) - - findViewById(R.id.tv_website_link)?.setOnClickListener { - CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://leanbitlab.github.io/LeanBitLab/")) - } + bindRemainingTabsWhenIdle() - findViewById(R.id.tv_github_link).setOnClickListener { - CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://github.com/LeanBitLab/Lwidget")) - } - - findViewById(R.id.tv_privacy_policy).setOnClickListener { - CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://github.com/LeanBitLab/Lwidget/wiki/Privacy-Policy")) - } - - val fab = findViewById(R.id.fab_update) fab.setOnClickListener { updateWidget() @@ -270,12 +243,8 @@ class MainActivity : AppCompatActivity() { private fun setupTabLayout() { val tabLayout = findViewById(R.id.tab_layout) ?: return - val containerStyle = findViewById(R.id.container_tab_style) - val containerContent = findViewById(R.id.container_tab_content) - val containerLayout = findViewById(R.id.container_tab_layout) - val containerSystem = findViewById(R.id.container_tab_system) - - val tabContainers = listOf(containerStyle, containerContent, containerLayout, containerSystem) + // Tabs are inflated lazily, so look the containers up each time instead of caching them. + fun tabContainers() = tabContainerIds.map { findViewById(it) } tabLayout.removeAllTabs() tabLayout.addTab(tabLayout.newTab().setText(R.string.category_appearance).setIcon(R.drawable.ic_palette)) @@ -283,9 +252,9 @@ class MainActivity : AppCompatActivity() { tabLayout.addTab(tabLayout.newTab().setText(R.string.header_reorder).setIcon(R.drawable.ic_reorder)) tabLayout.addTab(tabLayout.newTab().setText(R.string.category_system).setIcon(R.drawable.ic_tune)) - val savedTab = prefs.getInt("selected_settings_tab", 0).coerceIn(0, 3) + val savedTab = savedTabIndex() tabLayout.getTabAt(savedTab)?.select() - tabContainers.forEachIndexed { index, container -> + tabContainers().forEachIndexed { index, container -> container?.visibility = if (index == savedTab) View.VISIBLE else View.GONE } @@ -296,7 +265,8 @@ class MainActivity : AppCompatActivity() { searchInput?.setText("") } prefs.edit().putInt("selected_settings_tab", tab.position).apply() - tabContainers.forEachIndexed { index, container -> + bindTab(tab.position) + tabContainers().forEachIndexed { index, container -> container?.visibility = if (index == tab.position) View.VISIBLE else View.GONE } if (tab.position == 2) { @@ -338,6 +308,7 @@ class MainActivity : AppCompatActivity() { override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) + ensureAllTabsReady() checkAllPermissions() if (requestCode == 101) { // Task permission result - trigger update @@ -384,13 +355,59 @@ class MainActivity : AppCompatActivity() { internal val nestedViews = mutableMapOf() internal val nestedHeaders = mutableMapOf() - private fun setupSections() { + private val tabStubIds = intArrayOf(R.id.stub_tab_style, R.id.stub_tab_content, R.id.stub_tab_layout, R.id.stub_tab_system) + internal val tabContainerIds = intArrayOf(R.id.container_tab_style, R.id.container_tab_content, R.id.container_tab_layout, R.id.container_tab_system) + private val tabBound = BooleanArray(4) + + internal fun savedTabIndex(): Int = prefs.getInt("selected_settings_tab", 0).coerceIn(0, 3) + + /** Inflates (once) and binds the settings tab at [index]. Safe to call repeatedly. */ + internal fun bindTab(index: Int) { + if (tabBound[index]) return + tabBound[index] = true + findViewById(tabStubIds[index])?.inflate() + findViewById(tabContainerIds[index])?.visibility = + if (index == savedTabIndex()) View.VISIBLE else View.GONE + when (index) { + 0 -> bindStyleTab() + 1 -> bindContentTab() + 2 -> bindReorderSection() + 3 -> { + bindSystemTab() + // Fills the permission rows that live in the System tab. + checkAllPermissions() + } + } + } + + /** Binds every tab that is not ready yet. Call before anything that spans tabs (search, permission results). */ + internal fun ensureAllTabsReady() { + for (i in 0..3) bindTab(i) + } + + /** Inflates the tabs that are not visible one at a time while the main thread is idle, so the first frame is not delayed. */ + private fun bindRemainingTabsWhenIdle() { + val next = (0..3).firstOrNull { !tabBound[it] } ?: return + android.os.Looper.myQueue().addIdleHandler { + if (!isFinishing && !isDestroyed) { + bindTab(next) + bindRemainingTabsWhenIdle() + } + false + } + } + + private fun bindStyleTab() { + val colorOptions = listOf(getString(R.string.color_default), getString(R.string.color_system_accent), getString(R.string.color_custom)) + setupThemeSection(colorOptions) + } + + private fun bindContentTab() { contentSwitches.clear() val zoneIds = java.time.ZoneId.getAvailableZoneIds().sorted() val dateFormatOptions = listOf(getString(R.string.date_format_full), getString(R.string.date_format_short), getString(R.string.date_format_numeric)) val timeFormatOptions = listOf(getString(R.string.format_12h), getString(R.string.format_24h)) - val colorOptions = listOf(getString(R.string.color_default), getString(R.string.color_system_accent), getString(R.string.color_custom)) setupTimeSection(timeFormatOptions) setupNextAlarmSection() @@ -404,21 +421,48 @@ class MainActivity : AppCompatActivity() { setupRamSection() setupStepsSection() setupScreenTimeSection() + setupEventsAndTasksSections() + } + + private fun bindSystemTab() { setupKeepAliveSection() setupBatteryOptimizationSection() - setupEventsAndTasksSections() - setupThemeSection(colorOptions) setupLanguageSection() - - // System sections + bindCategoryFoldable(R.id.header_language, R.id.content_language, getString(R.string.section_app_language), R.drawable.ic_language, "section_language_expanded") bindCategoryFoldable(R.id.header_advanced, R.id.content_advanced, getString(R.string.header_advanced), R.drawable.ic_tune, "section_advanced_expanded") bindCategoryFoldable(R.id.header_permissions, R.id.content_permissions, getString(R.string.header_permissions), R.drawable.ic_shield, "section_permissions_expanded") bindCategoryFoldable(R.id.header_about, R.id.content_about, getString(R.string.header_about), R.drawable.ic_info, "section_about_expanded") bindNestedCard(R.id.header_changelog, "", R.id.content_changelog, "section_changelog_expanded", R.id.chevron_changelog) - } + // Advanced Section + bindSlider(R.id.row_update_interval, getString(R.string.row_update_interval), "update_interval", 15f, 1f, 60f, "m") { + // The repeating alarm only reads the interval when scheduled, so reschedule it now + val ids = AppWidgetManager.getInstance(this).getAppWidgetIds(ComponentName(this, AwidgetProvider::class.java)) + if (ids.isNotEmpty()) AwidgetProvider.scheduleWork(this) + } + + // Setup Changelog + val versionName = try { + packageManager.getPackageInfo(packageName, 0).versionName + } catch (e: Exception) { + getString(R.string.version_unknown) + } + val tvVersion = findViewById(R.id.tv_changelog_version) + tvVersion.text = getString(R.string.changelog_version, versionName) + + findViewById(R.id.tv_website_link)?.setOnClickListener { + CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://leanbitlab.github.io/LeanBitLab/")) + } + + findViewById(R.id.tv_github_link).setOnClickListener { + CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://github.com/LeanBitLab/Lwidget")) + } + findViewById(R.id.tv_privacy_policy).setOnClickListener { + CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://github.com/LeanBitLab/Lwidget/wiki/Privacy-Policy")) + } + } private val updateHandler = android.os.Handler(android.os.Looper.getMainLooper()) private val deferredUpdate = Runnable { updateWidget() } diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsAppearance.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsAppearance.kt index e6bfce0..39e958e 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsAppearance.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsAppearance.kt @@ -37,9 +37,6 @@ internal fun MainActivity.setupThemeSection(colorOptions: List) { bindNestedCard(R.id.header_appearance_transparency, getString(R.string.header_transparency), R.id.content_appearance_transparency, "section_appearance_transparency_expanded", R.id.header_chevron_appearance_transparency) bindNestedCard(R.id.header_appearance_padding, getString(R.string.header_padding), R.id.content_appearance_padding, "section_appearance_padding_expanded", R.id.header_chevron_appearance_padding) - // Reorder section - bindReorderSection() - // Outline toggle bindToggle(R.id.row_outline_toggle, getString(R.string.row_show_outline), "show_outline", false) { _ -> updateWidget() diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt index 8e2c630..d7f2e7b 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt @@ -37,8 +37,8 @@ internal fun MainActivity.checkAllPermissions() { val calMissing = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED if (prefs.getBoolean("show_events", false) && calMissing) { prefs.edit().putBoolean("show_events", false).apply() - findViewById(R.id.row_events_toggle).findViewById(R.id.row_switch).isChecked = false - findViewById(R.id.row_events_size).visibility = View.GONE + findViewById(R.id.row_events_toggle)?.findViewById(R.id.row_switch)?.isChecked = false + findViewById(R.id.row_events_size)?.visibility = View.GONE widgetNeedsUpdate = true } @@ -46,8 +46,8 @@ internal fun MainActivity.checkAllPermissions() { val tasksMissing = ContextCompat.checkSelfPermission(this, AwidgetProvider.PERMISSION_READ_TASKS_ORG) != PackageManager.PERMISSION_GRANTED if (prefs.getBoolean("show_tasks", false) && tasksMissing) { prefs.edit().putBoolean("show_tasks", false).apply() - findViewById(R.id.row_tasks_toggle).findViewById(R.id.row_switch).isChecked = false - findViewById(R.id.row_tasks_size).visibility = View.GONE + findViewById(R.id.row_tasks_toggle)?.findViewById(R.id.row_switch)?.isChecked = false + findViewById(R.id.row_tasks_size)?.visibility = View.GONE widgetNeedsUpdate = true } @@ -58,8 +58,8 @@ internal fun MainActivity.checkAllPermissions() { } if (prefs.getBoolean("show_steps", false) && stepMissing) { prefs.edit().putBoolean("show_steps", false).apply() - findViewById(R.id.row_steps_toggle).findViewById(R.id.row_switch).isChecked = false - findViewById(R.id.row_steps_size).visibility = View.GONE + findViewById(R.id.row_steps_toggle)?.findViewById(R.id.row_switch)?.isChecked = false + findViewById(R.id.row_steps_size)?.visibility = View.GONE widgetNeedsUpdate = true } @@ -67,16 +67,16 @@ internal fun MainActivity.checkAllPermissions() { val usageMissing = !hasUsageStatsPermission() if (prefs.getBoolean("show_screen_time", false) && usageMissing) { prefs.edit().putBoolean("show_screen_time", false).apply() - findViewById(R.id.row_screen_time_toggle).findViewById(R.id.row_switch).isChecked = false - findViewById(R.id.row_screen_time_size).visibility = View.GONE + findViewById(R.id.row_screen_time_toggle)?.findViewById(R.id.row_switch)?.isChecked = false + findViewById(R.id.row_screen_time_size)?.visibility = View.GONE widgetNeedsUpdate = true } // Check Data Usage if (prefs.getBoolean("show_data_usage", false) && usageMissing) { prefs.edit().putBoolean("show_data_usage", false).apply() - findViewById(R.id.row_data_toggle).findViewById(R.id.row_switch).isChecked = false - findViewById(R.id.row_data_size).visibility = View.GONE + findViewById(R.id.row_data_toggle)?.findViewById(R.id.row_switch)?.isChecked = false + findViewById(R.id.row_data_size)?.visibility = View.GONE widgetNeedsUpdate = true } @@ -84,8 +84,8 @@ internal fun MainActivity.checkAllPermissions() { val weatherMissing = !isAppInstalled("org.breezyweather") || ContextCompat.checkSelfPermission(this, "org.breezyweather.READ_PROVIDER") != PackageManager.PERMISSION_GRANTED if (prefs.getBoolean("show_weather_condition", false) && weatherMissing) { prefs.edit().putBoolean("show_weather_condition", false).apply() - findViewById(R.id.row_weather_toggle).findViewById(R.id.row_switch).isChecked = false - findViewById(R.id.row_weather_size).visibility = View.GONE + findViewById(R.id.row_weather_toggle)?.findViewById(R.id.row_switch)?.isChecked = false + findViewById(R.id.row_weather_size)?.visibility = View.GONE widgetNeedsUpdate = true } diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt index a9fdcdc..6a12437 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt @@ -32,12 +32,8 @@ internal fun MainActivity.setupGlobalSettingsSearch() { val btnSearchClear = findViewById(R.id.btn_search_clear) val appBar = findViewById(R.id.app_bar) - val tabContainers = listOf( - findViewById(R.id.container_tab_style), - findViewById(R.id.container_tab_content), - findViewById(R.id.container_tab_layout), - findViewById(R.id.container_tab_system) - ) + // Tabs are inflated lazily, so look the containers up each time instead of caching them. + fun tabContainers() = tabContainerIds.map { findViewById(it) } val tabLayout = findViewById(R.id.tab_layout) fun closeSearch() { @@ -50,6 +46,7 @@ internal fun MainActivity.setupGlobalSettingsSearch() { } fun openSearch() { + ensureAllTabsReady() headerNormal?.visibility = View.GONE headerSearch?.visibility = View.VISIBLE appBar?.setExpanded(true, true) @@ -119,7 +116,8 @@ internal fun MainActivity.setupGlobalSettingsSearch() { ) // i18n-ignore-end - val headerLabels: List = listOfNotNull( + // These live in the lazily inflated Content tab, so look them up when needed. + fun headerLabels(): List = listOfNotNull( findViewById(R.id.label_category_content), findViewById(R.id.label_category_stats) ) @@ -153,8 +151,9 @@ internal fun MainActivity.setupGlobalSettingsSearch() { btnSearchClear?.visibility = if (isSearching) View.VISIBLE else View.GONE if (isSearching) { + ensureAllTabsReady() // Make all tab containers visible so search shows matches across all categories - tabContainers.forEach { container -> container?.visibility = View.VISIBLE } + tabContainers().forEach { container -> container?.visibility = View.VISIBLE } tabLayout?.visibility = View.GONE val index = cardTextIndex ?: buildCardTextIndex().also { cardTextIndex = it } @@ -163,7 +162,7 @@ internal fun MainActivity.setupGlobalSettingsSearch() { val matches = keywords.any { it.contains(query) } || index[cardId]?.contains(query) == true card.visibility = if (matches) View.VISIBLE else View.GONE } - headerLabels.forEach { label -> + headerLabels().forEach { label -> label.visibility = View.GONE } } else { @@ -171,13 +170,13 @@ internal fun MainActivity.setupGlobalSettingsSearch() { // Restore tabs and active container tabLayout?.visibility = View.VISIBLE val selectedTab = tabLayout?.selectedTabPosition?.coerceIn(0, 3) ?: 0 - tabContainers.forEachIndexed { index, container -> + tabContainers().forEachIndexed { index, container -> container?.visibility = if (index == selectedTab) View.VISIBLE else View.GONE } for ((cardId, _) in cardMap) { findViewById(cardId)?.visibility = View.VISIBLE } - headerLabels.forEach { label -> + headerLabels().forEach { label -> label.visibility = View.VISIBLE } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3c5c902..7bb6430 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -228,1737 +228,36 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:layout_height="wrap_content" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:layout_height="wrap_content" /> - - - - - - - - - - - - - + android:layout_height="wrap_content" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + android:layout_height="wrap_content" /> diff --git a/app/src/main/res/layout/tab_content.xml b/app/src/main/res/layout/tab_content.xml new file mode 100644 index 0000000..c63cfb0 --- /dev/null +++ b/app/src/main/res/layout/tab_content.xml @@ -0,0 +1,712 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/tab_layout.xml b/app/src/main/res/layout/tab_layout.xml new file mode 100644 index 0000000..aa3c13c --- /dev/null +++ b/app/src/main/res/layout/tab_layout.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/tab_style.xml b/app/src/main/res/layout/tab_style.xml new file mode 100644 index 0000000..c1ce2e0 --- /dev/null +++ b/app/src/main/res/layout/tab_style.xml @@ -0,0 +1,595 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/tab_system.xml b/app/src/main/res/layout/tab_system.xml new file mode 100644 index 0000000..d973065 --- /dev/null +++ b/app/src/main/res/layout/tab_system.xml @@ -0,0 +1,379 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/test/java/com/leanbitlab/lwidget/MainActivitySmokeTest.kt b/app/src/test/java/com/leanbitlab/lwidget/MainActivitySmokeTest.kt index c4dc5ad..e8a7adf 100644 --- a/app/src/test/java/com/leanbitlab/lwidget/MainActivitySmokeTest.kt +++ b/app/src/test/java/com/leanbitlab/lwidget/MainActivitySmokeTest.kt @@ -1,6 +1,7 @@ package com.leanbitlab.lwidget import android.content.Context +import android.os.Looper import android.view.View import androidx.test.core.app.ApplicationProvider import org.junit.Assert.assertNotNull @@ -10,6 +11,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config /** @@ -34,6 +36,7 @@ class MainActivitySmokeTest { val activity = Robolectric.buildActivity(MainActivity::class.java).setup().get() assertTrue("activity should not have finished", !activity.isFinishing) + activity.ensureAllTabsReady() listOf( R.id.row_update_interval, R.id.fab_update, @@ -42,6 +45,59 @@ class MainActivitySmokeTest { ).forEach { id -> assertNotNull("missing view $id", activity.findViewById(id)) } } + @Test + fun onlyTheSelectedTabIsInflatedBeforeTheMainThreadIsIdle() { + val activity = Robolectric.buildActivity(MainActivity::class.java).create().get() + + // Style is the default tab; the others are still ViewStubs until idle or needed. + assertNotNull(activity.findViewById(R.id.container_tab_style)) + assertTrue(activity.findViewById(R.id.container_tab_system) == null) + assertNotNull(activity.findViewById(R.id.stub_tab_system)) + + // Once the main thread goes idle the remaining tabs are inflated and bound. + shadowOf(Looper.getMainLooper()).idle() + listOf( + R.id.container_tab_content, + R.id.container_tab_layout, + R.id.container_tab_system, + ).forEach { id -> assertNotNull("tab $id should be ready once idle", activity.findViewById(id)) } + } + + @Test + fun ensureAllTabsReadyInflatesAndBindsEveryTab() { + val activity = Robolectric.buildActivity(MainActivity::class.java).setup().get() + + activity.ensureAllTabsReady() + activity.ensureAllTabsReady() // idempotent + + listOf( + R.id.container_tab_style, + R.id.container_tab_content, + R.id.container_tab_layout, + R.id.container_tab_system, + ).forEach { id -> assertNotNull("missing tab $id", activity.findViewById(id)) } + // Only the selected tab is visible. + assertTrue(activity.findViewById(R.id.container_tab_style).visibility == View.VISIBLE) + assertTrue(activity.findViewById(R.id.container_tab_content).visibility == View.GONE) + assertTrue(activity.findViewById(R.id.container_tab_system).visibility == View.GONE) + } + + @Test + fun launchesWithEachSavedTab() { + for (tab in 0..3) { + context.getSharedPreferences("com.leanbitlab.lwidget.PREFS", Context.MODE_PRIVATE) + .edit().putInt("selected_settings_tab", tab).commit() + val activity = Robolectric.buildActivity(MainActivity::class.java).setup().get() + assertTrue("tab $tab should launch", !activity.isFinishing) + activity.ensureAllTabsReady() + val ids = intArrayOf(R.id.container_tab_style, R.id.container_tab_content, R.id.container_tab_layout, R.id.container_tab_system) + ids.forEachIndexed { i, id -> + val expected = if (i == tab) View.VISIBLE else View.GONE + assertTrue("tab $tab: container $i visibility", activity.findViewById(id).visibility == expected) + } + } + } + @Test fun firstLaunchRedirectsToSetup() { context.getSharedPreferences("com.leanbitlab.lwidget.PREFS", Context.MODE_PRIVATE) diff --git a/scripts/check_strings.py b/scripts/check_strings.py index 205616d..ae6589b 100644 --- a/scripts/check_strings.py +++ b/scripts/check_strings.py @@ -130,13 +130,13 @@ def check_hardcoded(problems): def check_layout_text(problems): - """Hardcoded UI text in app layouts: any text attribute in activity_*.xml screens, and contentDescription + """Hardcoded UI text in app layouts: any text attribute in activity_*.xml and tab_*.xml screens, and contentDescription everywhere. Widget layouts (RemoteViews) and the inflated settings_* row templates are not scanned for text: their placeholder text is overwritten at runtime.""" for f in sorted((RES).glob("layout*/*.xml")): if f.name.startswith("widget_layout"): continue - screen = f.name.startswith("activity_") + screen = f.name.startswith(("activity_", "tab_")) for i, line in enumerate(f.read_text(encoding="utf-8").splitlines(), 1): if "i18n-ignore" in line: continue