Skip to content

fix: force widget re-render on app launch to recover Glance state#25

Merged
wassupluke merged 1 commit intofeature/alarm-widget-font-sizefrom
fix/widget-glance-state-recovery
Apr 3, 2026
Merged

fix: force widget re-render on app launch to recover Glance state#25
wassupluke merged 1 commit intofeature/alarm-widget-font-sizefrom
fix/widget-glance-state-recovery

Conversation

@wassupluke
Copy link
Copy Markdown
Owner

@wassupluke wassupluke commented Apr 3, 2026

Summary

  • Glance's internal appWidgetId→class mapping can become stale after force-stop or app update, causing one widget to render the other's content and tap targets
  • Calls updateAll for both WeatherWidget and AlarmWidget on every app launch, forcing Glance to re-render with the correct provideGlance and re-establish its state mapping
  • Uninstall/reinstall fixes the issue permanently, but this mitigation self-heals on the next app open

Mitigates #23

Test plan

  • Place both weather and alarm widgets on home screen
  • Force-stop the app, then reopen it
  • Verify both widgets show their correct content (temp vs alarm) and tap targets

Summary by Sourcery

Bug Fixes:

  • Ensure weather and alarm widgets re-render with the correct content and tap targets by calling Glance updateAll for both widgets whenever the app is opened.

Glance's internal appWidgetId-to-class mapping can become stale after
a force-stop or app update, causing content bleed between widgets.
Calling updateAll for both widget types on every app launch
re-establishes the correct mapping.

Mitigates #23
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Apr 3, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

On app launch, MainActivity now forces both Glance-based widgets (WeatherWidget and AlarmWidget) to re-render via updateAll before scheduling background work, ensuring Glance’s internal widget-to-class state mapping is corrected after force-stop or app updates.

Sequence diagram for widget re-render on app launch

sequenceDiagram
    actor User
    participant MainActivity
    participant LifecycleScope
    participant WeatherWidget
    participant AlarmWidget
    participant GlanceRuntime
    participant DataStore
    participant WorkScheduler

    User ->> MainActivity: launch app
    MainActivity ->> LifecycleScope: launch(Dispatchers.IO)

    rect rgb(230, 245, 255)
        LifecycleScope ->> WeatherWidget: updateAll(applicationContext)
        WeatherWidget ->> GlanceRuntime: request re-render for all WeatherWidget instances
        GlanceRuntime -->> WeatherWidget: rebuild state mapping for WeatherWidget

        LifecycleScope ->> AlarmWidget: updateAll(applicationContext)
        AlarmWidget ->> GlanceRuntime: request re-render for all AlarmWidget instances
        GlanceRuntime -->> AlarmWidget: rebuild state mapping for AlarmWidget
    end

    LifecycleScope ->> DataStore: read preferences
    DataStore -->> LifecycleScope: prefs with LOCATION_LAT, LOCATION_LON

    alt location set in prefs
        LifecycleScope ->> WorkScheduler: schedule background work
    else location not set
        LifecycleScope -->> MainActivity: skip scheduling
    end

    MainActivity -->> User: app UI ready, widgets corrected on home screen
Loading

Class diagram for MainActivity widget update integration

classDiagram
    class MainActivity {
        +onCreate(savedInstanceState: Bundle): void
        +lifecycleScope: LifecycleCoroutineScope
    }

    class WeatherWidget {
        +updateAll(context: Context): void
    }

    class AlarmWidget {
        +updateAll(context: Context): void
    }

    class WorkScheduler {
        +scheduleWork(lat: Double, lon: Double): void
    }

    class WeatherDataStore {
        <<object>>
        +LOCATION_LAT: PreferencesKey
        +LOCATION_LON: PreferencesKey
    }

    class DataStore {
        +data: Flow
    }

    MainActivity --> WeatherWidget : uses
    MainActivity --> AlarmWidget : uses
    MainActivity --> WorkScheduler : uses
    MainActivity --> DataStore : reads
    DataStore --> WeatherDataStore : uses keys for location
Loading

File-Level Changes

Change Details Files
Force Glance widgets to re-render on every app launch to repair potentially stale internal appWidgetId-to-widget-class mappings.
  • Import AlarmWidget and WeatherWidget into MainActivity for direct use.
  • Import Glance’s updateAll extension for triggering widget updates.
  • Launch a lifecycleScope IO coroutine that first calls updateAll on WeatherWidget and AlarmWidget using applicationContext.
  • Retain and slightly reword the existing WorkManager scheduling logic to run after widget updates, still conditioned on stored location preferences.
app/src/main/java/com/wassupluke/widgets/ui/MainActivity.kt

Possibly linked issues

  • #(unlisted): PR forces both widgets to re-render on app launch, correcting the post–force-stop configuration mismatch described in the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Consider limiting the unconditional WeatherWidget().updateAll and AlarmWidget().updateAll calls (e.g., by checking whether any instances exist or whether this is the first launch after an upgrade/force-stop) to avoid doing potentially expensive widget re-renders on every app launch.
  • You might want to extract the self-healing Glance re-render logic into a dedicated helper or manager (e.g., WidgetStateRecovery.repairIfNeeded(context)) so this behavior is centralized and easier to reuse or evolve if more widgets are added.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider limiting the unconditional `WeatherWidget().updateAll` and `AlarmWidget().updateAll` calls (e.g., by checking whether any instances exist or whether this is the first launch after an upgrade/force-stop) to avoid doing potentially expensive widget re-renders on every app launch.
- You might want to extract the self-healing Glance re-render logic into a dedicated helper or manager (e.g., `WidgetStateRecovery.repairIfNeeded(context)`) so this behavior is centralized and easier to reuse or evolve if more widgets are added.

## Individual Comments

### Comment 1
<location path="app/src/main/java/com/wassupluke/widgets/ui/MainActivity.kt" line_range="62-63" />
<code_context>
         lifecycleScope.launch(Dispatchers.IO) {
+            // Force both widgets to re-render, re-establishing correct Glance
+            // state mapping in case it drifted after a force-stop or update
+            WeatherWidget().updateAll(applicationContext)
+            AlarmWidget().updateAll(applicationContext)
+
+            // Schedule WorkManager (if location is already set)
</code_context>
<issue_to_address>
**issue (bug_risk):** Consider isolating widget updates from the WorkManager scheduling so a failure in `updateAll` doesn't block scheduling.

Because both widget updates run in the same coroutine as scheduling, any exception from `updateAll` will cancel the coroutine and prevent scheduling from running. To avoid missing periodic updates, consider wrapping each `updateAll` in its own try/catch or moving the scheduling into a separate coroutine.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +62 to +63
WeatherWidget().updateAll(applicationContext)
AlarmWidget().updateAll(applicationContext)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Consider isolating widget updates from the WorkManager scheduling so a failure in updateAll doesn't block scheduling.

Because both widget updates run in the same coroutine as scheduling, any exception from updateAll will cancel the coroutine and prevent scheduling from running. To avoid missing periodic updates, consider wrapping each updateAll in its own try/catch or moving the scheduling into a separate coroutine.

@wassupluke wassupluke merged commit 9f168bc into feature/alarm-widget-font-size Apr 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant