Skip to content
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
136 changes: 90 additions & 46 deletions app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextView>(R.id.tv_changelog_version)
tvVersion.text = getString(R.string.changelog_version, versionName)

findViewById<View>(R.id.tv_website_link)?.setOnClickListener {
CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://leanbitlab.github.io/LeanBitLab/"))
}
bindRemainingTabsWhenIdle()

findViewById<View>(R.id.tv_github_link).setOnClickListener {
CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://github.com/LeanBitLab/Lwidget"))
}

findViewById<View>(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<ExtendedFloatingActionButton>(R.id.fab_update)
fab.setOnClickListener {
updateWidget()
Expand Down Expand Up @@ -270,22 +243,18 @@ class MainActivity : AppCompatActivity() {

private fun setupTabLayout() {
val tabLayout = findViewById<TabLayout>(R.id.tab_layout) ?: return
val containerStyle = findViewById<View>(R.id.container_tab_style)
val containerContent = findViewById<View>(R.id.container_tab_content)
val containerLayout = findViewById<View>(R.id.container_tab_layout)
val containerSystem = findViewById<View>(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<View>(it) }

tabLayout.removeAllTabs()
tabLayout.addTab(tabLayout.newTab().setText(R.string.category_appearance).setIcon(R.drawable.ic_palette))
tabLayout.addTab(tabLayout.newTab().setText(R.string.category_content).setIcon(R.drawable.ic_tasks))
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
}

Expand All @@ -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) {
Expand Down Expand Up @@ -338,6 +308,7 @@ class MainActivity : AppCompatActivity() {

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
ensureAllTabsReady()
checkAllPermissions()
if (requestCode == 101) {
// Task permission result - trigger update
Expand Down Expand Up @@ -384,13 +355,59 @@ class MainActivity : AppCompatActivity() {
internal val nestedViews = mutableMapOf<String, View>()
internal val nestedHeaders = mutableMapOf<String, View>()

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<android.view.ViewStub>(tabStubIds[index])?.inflate()
findViewById<View>(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()
Expand All @@ -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<TextView>(R.id.tv_changelog_version)
tvVersion.text = getString(R.string.changelog_version, versionName)

findViewById<View>(R.id.tv_website_link)?.setOnClickListener {
CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://leanbitlab.github.io/LeanBitLab/"))
}

findViewById<View>(R.id.tv_github_link).setOnClickListener {
CustomTabsIntent.Builder().build().launchUrl(this@MainActivity, android.net.Uri.parse("https://github.com/LeanBitLab/Lwidget"))
}

findViewById<View>(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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ internal fun MainActivity.setupThemeSection(colorOptions: List<String>) {
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()
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ 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<View>(R.id.row_events_toggle).findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch).isChecked = false
findViewById<View>(R.id.row_events_size).visibility = View.GONE
findViewById<View>(R.id.row_events_toggle)?.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch)?.isChecked = false
findViewById<View>(R.id.row_events_size)?.visibility = View.GONE
widgetNeedsUpdate = true
}

// Check Tasks
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<View>(R.id.row_tasks_toggle).findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch).isChecked = false
findViewById<View>(R.id.row_tasks_size).visibility = View.GONE
findViewById<View>(R.id.row_tasks_toggle)?.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch)?.isChecked = false
findViewById<View>(R.id.row_tasks_size)?.visibility = View.GONE
widgetNeedsUpdate = true
}

Expand All @@ -58,34 +58,34 @@ internal fun MainActivity.checkAllPermissions() {
}
if (prefs.getBoolean("show_steps", false) && stepMissing) {
prefs.edit().putBoolean("show_steps", false).apply()
findViewById<View>(R.id.row_steps_toggle).findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch).isChecked = false
findViewById<View>(R.id.row_steps_size).visibility = View.GONE
findViewById<View>(R.id.row_steps_toggle)?.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch)?.isChecked = false
findViewById<View>(R.id.row_steps_size)?.visibility = View.GONE
widgetNeedsUpdate = true
}

// Check Screen Time
val usageMissing = !hasUsageStatsPermission()
if (prefs.getBoolean("show_screen_time", false) && usageMissing) {
prefs.edit().putBoolean("show_screen_time", false).apply()
findViewById<View>(R.id.row_screen_time_toggle).findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch).isChecked = false
findViewById<View>(R.id.row_screen_time_size).visibility = View.GONE
findViewById<View>(R.id.row_screen_time_toggle)?.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch)?.isChecked = false
findViewById<View>(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<View>(R.id.row_data_toggle).findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch).isChecked = false
findViewById<View>(R.id.row_data_size).visibility = View.GONE
findViewById<View>(R.id.row_data_toggle)?.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch)?.isChecked = false
findViewById<View>(R.id.row_data_size)?.visibility = View.GONE
widgetNeedsUpdate = true
}

// Check Breezy Weather
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<View>(R.id.row_weather_toggle).findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch).isChecked = false
findViewById<View>(R.id.row_weather_size).visibility = View.GONE
findViewById<View>(R.id.row_weather_toggle)?.findViewById<com.google.android.material.switchmaterial.SwitchMaterial>(R.id.row_switch)?.isChecked = false
findViewById<View>(R.id.row_weather_size)?.visibility = View.GONE
widgetNeedsUpdate = true
}

Expand Down
21 changes: 10 additions & 11 deletions app/src/main/java/com/leanbitlab/lwidget/SettingsSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ internal fun MainActivity.setupGlobalSettingsSearch() {
val btnSearchClear = findViewById<View>(R.id.btn_search_clear)
val appBar = findViewById<AppBarLayout>(R.id.app_bar)

val tabContainers = listOf(
findViewById<View>(R.id.container_tab_style),
findViewById<View>(R.id.container_tab_content),
findViewById<View>(R.id.container_tab_layout),
findViewById<View>(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<View>(it) }
val tabLayout = findViewById<com.google.android.material.tabs.TabLayout>(R.id.tab_layout)

fun closeSearch() {
Expand All @@ -50,6 +46,7 @@ internal fun MainActivity.setupGlobalSettingsSearch() {
}

fun openSearch() {
ensureAllTabsReady()
headerNormal?.visibility = View.GONE
headerSearch?.visibility = View.VISIBLE
appBar?.setExpanded(true, true)
Expand Down Expand Up @@ -119,7 +116,8 @@ internal fun MainActivity.setupGlobalSettingsSearch() {
)
// i18n-ignore-end

val headerLabels: List<View> = listOfNotNull(
// These live in the lazily inflated Content tab, so look them up when needed.
fun headerLabels(): List<View> = listOfNotNull(
findViewById(R.id.label_category_content),
findViewById(R.id.label_category_stats)
)
Expand Down Expand Up @@ -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 }
Expand All @@ -163,21 +162,21 @@ 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 {
cardTextIndex = null
// 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<View>(cardId)?.visibility = View.VISIBLE
}
headerLabels.forEach { label ->
headerLabels().forEach { label ->
label.visibility = View.VISIBLE
}
}
Expand Down
Loading
Loading