Skip to content

Fix ChatTheme crashing previews when ChatClient is not initialized#6366

Merged
aleksandar-apostolov merged 1 commit intodevelopfrom
bug/AND-1147_fix_previews_not_working
Apr 15, 2026
Merged

Fix ChatTheme crashing previews when ChatClient is not initialized#6366
aleksandar-apostolov merged 1 commit intodevelopfrom
bug/AND-1147_fix_previews_not_working

Conversation

@VelikovPetar
Copy link
Copy Markdown
Contributor

@VelikovPetar VelikovPetar commented Apr 14, 2026

Goal

Fix ChatTheme crashing with ChatClient is not initialized in Compose previews and VRT tests. This was caused by ChatClient.instance().cdn being called eagerly at composition time, which fails when ChatClient hasn't been built yet.

Implementation

  • Changed CDNImageInterceptor to resolve the CDN lazily via a () -> CDN? provider instead of taking a direct CDN instance. The default provider safely checks ChatClient.isInitialized before accessing the instance.
  • ChatTheme now always installs the CDNImageInterceptor (with the lazy default). When ChatClient is not initialized, the interceptor is a no-op pass-through. When it is initialized later, the next image request picks up the CDN automatically.
  • Same change applied to StreamCoil (XML view system path).
  • Added test for the null-CDN pass-through case.
  • Updated KDoc for imageLoaderFactory parameter to document CDN + interceptor interaction.

UI Changes

No UI changes.

Testing

  • Existing CDNImageInterceptorTest tests updated and passing.
  • New test intercept passes through when cdn returns null covers the preview/VRT scenario.
  • Manual testing: verified custom single-arg StreamCoilImageLoaderFactory still works correctly (interceptors silently dropped by default two-arg fallback, customer's loader returned as-is).

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of image loading with CDN functionality and custom image headers during app initialization.
  • Documentation

    • Updated guidance for implementing custom image loaders with CDN or custom header configurations.

…ent is not initialized

Co-Authored-By: Claude <noreply@anthropic.com>
@VelikovPetar VelikovPetar added the pr:bug Bug fix label Apr 14, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 14, 2026

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled.

🎉 Great job! This PR is ready for review.

@VelikovPetar VelikovPetar changed the title Fix ChatTheme crashing when ChatClient is not initialized Fix ChatTheme crashing previews when ChatClient is not initialized Apr 14, 2026
@VelikovPetar VelikovPetar marked this pull request as ready for review April 14, 2026 15:12
@VelikovPetar VelikovPetar requested a review from a team as a code owner April 14, 2026 15:12
@github-actions
Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.26 MB 5.26 MB 0.00 MB 🟢
stream-chat-android-offline 5.49 MB 5.49 MB 0.00 MB 🟢
stream-chat-android-ui-components 10.64 MB 10.64 MB 0.00 MB 🟢
stream-chat-android-compose 12.87 MB 12.87 MB 0.00 MB 🟢

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
61.5% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 14, 2026

Walkthrough

The changes refactor image loading to use lazy CDN resolution instead of eager initialization. CDNImageInterceptor now accepts a lazy resolver function that provides CDN on each request, enabling graceful pass-through when ChatClient isn't initialized. Factory construction no longer performs upfront CDN lookup.

Changes

Cohort / File(s) Summary
Lazy CDN Resolution
stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptor.kt
Constructor signature changed to accept lazy resolver function cdn: () -> CDN? instead of eager CDN instance. Interceptor now evaluates resolver on each request, passing through unchanged when resolver returns null.
Factory Initialization
stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/images/internal/StreamCoil.kt
Removed upfront ChatClient CDN lookup. Now unconditionally constructs StreamImageLoaderFactory with CDNImageInterceptor(), delegating CDN resolution to interceptor's lazy resolver.
Theme Documentation & Logic
stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/theme/ChatTheme.kt
Updated imageLoaderFactory documentation clarifying lazy resolution requirements. Removed conditional overload selection logic; interceptors are now always passed to imageLoaderFactory.imageLoader(context.applicationContext, interceptors).
Test Updates
stream-chat-android-ui-common/src/test/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptorTest.kt
Updated test setup to use lambda suppliers instead of direct CDN instances. Added test case verifying pass-through behavior when CDN supplier returns null.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A lazy hare once built with care,
No eager fetches in the air—
When CDN's not yet prepared,
We hop right through, unharmed, unpaired!
Efficient streams of image flow!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main bug fix: preventing ChatTheme from crashing when ChatClient is not initialized in previews.
Description check ✅ Passed The description covers Goal, Implementation, Testing, and confirms no UI changes, matching the template structure. However, UI Changes section is minimal and Contributor/Reviewer checklists are not completed.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bug/AND-1147_fix_previews_not_working

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
stream-chat-android-ui-common/src/test/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptorTest.kt (1)

146-157: Exercise the real preview path in this regression test.

CDNImageInterceptor { null } only validates the injected-null branch. The crash was in the default CDNImageInterceptor() supplier touching ChatClient, so an eager-access regression there would still pass here. Prefer leaving ChatClient uninitialized and constructing the interceptor with its default args.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@stream-chat-android-ui-common/src/test/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptorTest.kt`
around lines 146 - 157, The test currently constructs the interceptor with
CDNImageInterceptor { null } which only exercises the injected-null branch;
change the test to construct the interceptor with its default constructor (use
CDNImageInterceptor() instead of the lambda) so the real preview path that
touches ChatClient is exercised, keep the rest of the setup
(ImageRequest.Builder(...), FakeCoilChain(request)) and the final assertion that
chain.directProceed is true; ensure no ChatClient is initialized in the test
setup so the default supplier runs against an uninitialized ChatClient.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@stream-chat-android-ui-common/src/test/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptorTest.kt`:
- Around line 146-157: The test currently constructs the interceptor with
CDNImageInterceptor { null } which only exercises the injected-null branch;
change the test to construct the interceptor with its default constructor (use
CDNImageInterceptor() instead of the lambda) so the real preview path that
touches ChatClient is exercised, keep the rest of the setup
(ImageRequest.Builder(...), FakeCoilChain(request)) and the final assertion that
chain.directProceed is true; ensure no ChatClient is initialized in the test
setup so the default supplier runs against an uninitialized ChatClient.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2106cd1b-2827-4f01-877a-1d4092b86469

📥 Commits

Reviewing files that changed from the base of the PR and between 1ef382a and 8fc7ddc.

📒 Files selected for processing (4)
  • stream-chat-android-compose/src/main/java/io/getstream/chat/android/compose/ui/theme/ChatTheme.kt
  • stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptor.kt
  • stream-chat-android-ui-common/src/main/kotlin/io/getstream/chat/android/ui/common/images/internal/StreamCoil.kt
  • stream-chat-android-ui-common/src/test/kotlin/io/getstream/chat/android/ui/common/images/internal/CDNImageInterceptorTest.kt

@aleksandar-apostolov aleksandar-apostolov merged commit 8394fa8 into develop Apr 15, 2026
20 of 23 checks passed
@aleksandar-apostolov aleksandar-apostolov deleted the bug/AND-1147_fix_previews_not_working branch April 15, 2026 09:52
@stream-public-bot stream-public-bot added the released Included in a release label Apr 15, 2026
@stream-public-bot
Copy link
Copy Markdown
Contributor

🚀 Available in v6.37.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:bug Bug fix released Included in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants