Skip to content

Update latest code #25

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
wants to merge 2 commits into
base: mobile-sdk-custom
Choose a base branch
from

Conversation

huynhtanduysource
Copy link

@huynhtanduysource huynhtanduysource commented Jun 16, 2025

Summary by CodeRabbit

  • New Features

    • Introduced delegate-based event callbacks for SDK lifecycle, survey events, error reporting, and success notifications.
    • Added support for injecting custom services and SSL certificate pinning for enhanced security.
    • Enabled passing hidden fields to surveys and improved survey tracking capabilities.
    • Made the main SwiftUI survey view publicly accessible.
  • Improvements

    • Enhanced error handling and reporting throughout the SDK.
    • Improved setup validation, configurability, and retry logic for network operations.
    • Updated survey presentation to use the top-most view controller and improved UI transparency.
  • Bug Fixes

    • Improved safety in date calculations and optional data handling to prevent crashes.
  • Tests

    • Added comprehensive unit tests for environment configuration, API client, service endpoints, update queue, and edge cases.
    • Enhanced test reliability with better setup, teardown, and asynchronous handling.
  • Documentation

    • Updated and clarified public interfaces and error descriptions for better developer experience.

@CLAassistant
Copy link

CLAassistant commented Jun 16, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

coderabbitai bot commented Jun 16, 2025

Caution

Review failed

Failed to post review comments.

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d5bb2bf and f4670b0.

📒 Files selected for processing (21)
  • Sources/FormbricksSDK/Extension/Calendar+DaysBetween.swift (1 hunks)
  • Sources/FormbricksSDK/Formbricks.swift (11 hunks)
  • Sources/FormbricksSDK/Helpers/ConfigBuilder.swift (3 hunks)
  • Sources/FormbricksSDK/Helpers/FormbricksEnvironment.swift (1 hunks)
  • Sources/FormbricksSDK/Manager/PresentSurveyManager.swift (1 hunks)
  • Sources/FormbricksSDK/Manager/SurveyManager.swift (11 hunks)
  • Sources/FormbricksSDK/Manager/UserManager.swift (4 hunks)
  • Sources/FormbricksSDK/Model/Environment/EnvironmentResponse.swift (1 hunks)
  • Sources/FormbricksSDK/Model/Error/SDKError.swift (2 hunks)
  • Sources/FormbricksSDK/Networking/Base/APIClient.swift (4 hunks)
  • Sources/FormbricksSDK/Networking/Queue/UpdateQueue.swift (2 hunks)
  • Sources/FormbricksSDK/Networking/Service/FormbricksService.swift (2 hunks)
  • Sources/FormbricksSDK/WebView/FormbricksView.swift (1 hunks)
  • Sources/FormbricksSDK/WebView/FormbricksViewModel.swift (5 hunks)
  • Sources/FormbricksSDK/WebView/SurveyWebView.swift (4 hunks)
  • Tests/FormbricksSDKTests/FormbricksEnvironmentTests.swift (1 hunks)
  • Tests/FormbricksSDKTests/FormbricksSDKTests.swift (2 hunks)
  • Tests/FormbricksSDKTests/MockFormbricksService/MockFormbricksService.swift (2 hunks)
  • Tests/FormbricksSDKTests/Networking/APIClientTests.swift (1 hunks)
  • Tests/FormbricksSDKTests/Networking/ClientAPIEndpointsTests.swift (1 hunks)
  • Tests/FormbricksSDKTests/Networking/UpdateQueueTests.swift (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (8)
Sources/FormbricksSDK/Networking/Queue/UpdateQueue.swift (3)
Sources/FormbricksSDK/Manager/UserManager.swift (1)
  • syncUser (88-120)
Tests/FormbricksSDKTests/Networking/UpdateQueueTests.swift (1)
  • syncUser (8-12)
Sources/FormbricksSDK/Logger/Logger.swift (2)
  • error (37-39)
  • debug (25-27)
Tests/FormbricksSDKTests/FormbricksEnvironmentTests.swift (4)
Tests/FormbricksSDKTests/FormbricksSDKTests.swift (2)
  • setUp (12-16)
  • tearDown (18-21)
Sources/FormbricksSDK/Formbricks.swift (2)
  • cleanup (284-298)
  • setup (64-122)
Sources/FormbricksSDK/Networking/Queue/UpdateQueue.swift (1)
  • cleanup (116-124)
Sources/FormbricksSDK/Helpers/ConfigBuilder.swift (2)
  • setLogLevel (56-59)
  • build (67-69)
Sources/FormbricksSDK/Manager/UserManager.swift (3)
Sources/FormbricksSDK/Helpers/ConfigBuilder.swift (1)
  • service (61-64)
Tests/FormbricksSDKTests/Networking/UpdateQueueTests.swift (1)
  • syncUser (8-12)
Sources/FormbricksSDK/Logger/Logger.swift (1)
  • error (37-39)
Sources/FormbricksSDK/WebView/FormbricksViewModel.swift (2)
Sources/FormbricksSDK/Model/Environment/EnvironmentResponse.swift (1)
  • getSurveyJson (15-22)
Sources/FormbricksSDK/Logger/Logger.swift (1)
  • error (37-39)
Sources/FormbricksSDK/Manager/PresentSurveyManager.swift (1)
Sources/FormbricksSDK/Logger/Logger.swift (1)
  • debug (25-27)
Sources/FormbricksSDK/Networking/Service/FormbricksService.swift (1)
Tests/FormbricksSDKTests/MockFormbricksService/MockFormbricksService.swift (2)
  • getEnvironmentState (14-20)
  • postUser (22-28)
Tests/FormbricksSDKTests/FormbricksSDKTests.swift (6)
Tests/FormbricksSDKTests/FormbricksEnvironmentTests.swift (2)
  • setUp (6-10)
  • tearDown (12-15)
Sources/FormbricksSDK/Formbricks.swift (7)
  • cleanup (284-298)
  • setUserId (133-147)
  • setAttributes (178-187)
  • setAttribute (158-167)
  • setup (64-122)
  • track (228-244)
  • logout (255-265)
Sources/FormbricksSDK/Networking/Queue/UpdateQueue.swift (5)
  • cleanup (116-124)
  • set (25-30)
  • set (32-37)
  • set (51-69)
  • add (39-49)
Sources/FormbricksSDK/Helpers/ConfigBuilder.swift (5)
  • set (38-41)
  • set (44-47)
  • add (50-53)
  • service (61-64)
  • build (67-69)
Sources/FormbricksSDK/Manager/UserManager.swift (5)
  • set (38-40)
  • set (48-50)
  • set (53-55)
  • add (43-45)
  • logout (123-156)
Sources/FormbricksSDK/Manager/SurveyManager.swift (9)
  • refreshEnvironmentIfNeeded (111-155)
  • track (67-105)
  • dismissSurveyWebView (172-175)
  • postResponse (158-160)
  • onNewDisplay (163-166)
  • filterSurveys (39-63)
  • create (17-27)
  • refreshEnvironmentAfter (201-210)
  • getLanguageCode (299-338)
Sources/FormbricksSDK/Manager/SurveyManager.swift (5)
Sources/FormbricksSDK/Helpers/ConfigBuilder.swift (1)
  • service (61-64)
Sources/FormbricksSDK/Formbricks.swift (1)
  • track (228-244)
Sources/FormbricksSDK/Logger/Logger.swift (2)
  • debug (25-27)
  • error (37-39)
Sources/FormbricksSDK/Networking/Service/FormbricksService.swift (1)
  • getEnvironmentState (6-9)
Sources/FormbricksSDK/Manager/PresentSurveyManager.swift (1)
  • present (16-31)
🪛 SwiftLint (0.57.0)
Sources/FormbricksSDK/Manager/PresentSurveyManager.swift

[Warning] 23-23: Prefer at least one space after slashes for comments

(comment_spacing)

Tests/FormbricksSDKTests/FormbricksSDKTests.swift

[Warning] 115-115: Prefer at least one space after slashes for comments

(comment_spacing)

Tests/FormbricksSDKTests/Networking/APIClientTests.swift

[Warning] 105-105: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 152-152: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 216-216: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 255-255: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 293-293: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 331-331: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 389-389: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 416-416: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 449-449: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 483-483: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 518-518: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 553-553: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 588-588: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 627-627: Prefer at least one space after slashes for comments

(comment_spacing)


[Warning] 676-676: Prefer at least one space after slashes for comments

(comment_spacing)


[Error] 75-75: Force tries should be avoided

(force_try)


[Error] 116-116: Force tries should be avoided

(force_try)


[Error] 262-262: Force tries should be avoided

(force_try)


[Error] 300-300: Force tries should be avoided

(force_try)


[Error] 595-595: Force tries should be avoided

(force_try)


[Warning] 185-185: Prefer non-optional Data(_:) initializer when converting String to Data

(non_optional_string_data_conversion)


[Warning] 461-461: Prefer non-optional Data(_:) initializer when converting String to Data

(non_optional_string_data_conversion)


[Warning] 496-496: Prefer non-optional Data(_:) initializer when converting String to Data

(non_optional_string_data_conversion)


[Warning] 531-531: Prefer non-optional Data(_:) initializer when converting String to Data

(non_optional_string_data_conversion)


[Warning] 566-566: Prefer non-optional Data(_:) initializer when converting String to Data

(non_optional_string_data_conversion)


[Warning] 603-603: Prefer non-optional Data(_:) initializer when converting String to Data

(non_optional_string_data_conversion)

🔇 Additional comments (6)
Sources/FormbricksSDK/Helpers/FormbricksEnvironment.swift (1)

22-29: Leading “/” removed – verify callers

getEnvironmentRequestEndpoint & postUserRequestEndpoint no longer start
with /. If any existing code concatenates these strings onto a base-URL
without inserting a separator, the resulting URL will be malformed
(…comapi/v2/…).
Please double-check all call-sites (e.g. APIClient.request(...)) to ensure a
slash is injected.

Sources/FormbricksSDK/Model/Environment/EnvironmentResponse.swift (1)

3-5: Optional data requires downstream nil-handling

data is now optional, but many call-sites still assume non-optional access
(e.g. userResponse.data.state?.data). Ensure all usages are updated,
otherwise you’ll get silent nil propagation. Unit tests cover some paths,
yet a quick grep for .data. without ? is advisable.

Tests/FormbricksSDKTests/FormbricksEnvironmentTests.swift (1)

17-28: Potential false-positive when Formbricks.setup is blocked by double-initialisation

Formbricks.setup bails out (with delegate error) if the SDK is already initialised, leaving FormbricksEnvironment.baseApiUrl untouched.
Although you call Formbricks.cleanup() in setUp, a later early-return elsewhere in the code could leave the SDK partially initialised and cause these assertions to pass when they shouldn’t.

Consider asserting that Formbricks.isInitialized is true after each setup, or moving the property access into a defer block that first waits for setup completion.

Sources/FormbricksSDK/Helpers/ConfigBuilder.swift (1)

10-20: Retain cycle / lifetime concern for customService

FormbricksConfig keeps a strong reference to the injected service.
When the host app also owns the same object, you can inadvertently create dual strong references and delay de-allocation.

Document the ownership semantics, or consider unowned capture inside the SDK if the service is expected to be long-lived and owned by the host.

Sources/FormbricksSDK/Manager/UserManager.swift (1)

8-11: Nice! Service injection improves testability
Constructor-based DI removes the hidden dependency and simplifies mocking – good upgrade.

Sources/FormbricksSDK/WebView/FormbricksViewModel.swift (1)

55-58: setResponseFinished is never used

The closure captured via getSetIsResponseSendingFinished is stored but never invoked inside this script. If the React SDK expects the native side to call this callback, the current implementation will do nothing. Verify the contract and either remove the unused mechanism or forward the call back to native.

Walkthrough

This update introduces delegate-based event and error callbacks across the SDK, adds SSL pinning for enhanced security, and improves error handling and configurability. It enables dependency injection for service classes, enhances survey presentation with hidden field support, and expands test coverage with new and updated unit tests. Several public APIs and protocols are extended.

Changes

Files/Paths Change Summary
Sources/FormbricksSDK/Extension/Calendar+DaysBetween.swift Improved safety in numberOfDaysBetween by guarding against nil and returning absolute value.
Sources/FormbricksSDK/Formbricks.swift Added delegate/event callbacks, extended setup validation, improved error reporting, and enhanced tracking and logout APIs.
Sources/FormbricksSDK/Helpers/ConfigBuilder.swift Added support for injecting a custom service via config and builder.
Sources/FormbricksSDK/Helpers/FormbricksEnvironment.swift Made URL properties optional and safer, improved URL construction and validation.
Sources/FormbricksSDK/Manager/PresentSurveyManager.swift Added hiddenFields support, improved view presentation logic, and added helper methods for top view controller retrieval.
Sources/FormbricksSDK/Manager/SurveyManager.swift Enabled service injection, improved error handling and delegate callbacks, added retry logic, and extended survey presentation methods.
Sources/FormbricksSDK/Manager/UserManager.swift Enabled service injection, added retry logic and delegate callbacks for user sync.
Sources/FormbricksSDK/Model/Environment/EnvironmentResponse.swift Made data property optional in EnvironmentResponse.
Sources/FormbricksSDK/Model/Error/SDKError.swift Added new error cases to FormbricksSDKErrorType.
Sources/FormbricksSDK/Networking/Base/APIClient.swift Added SSL pinning, enforced HTTPS, improved error reporting, and session/request timeout configuration.
Sources/FormbricksSDK/Networking/Queue/UpdateQueue.swift Updated sync method signature and improved error callback on missing user ID.
Sources/FormbricksSDK/Networking/Service/FormbricksService.swift Introduced FormbricksServiceProtocol and updated service class to conform.
Sources/FormbricksSDK/WebView/FormbricksView.swift Made FormbricksView and its body public.
Sources/FormbricksSDK/WebView/FormbricksViewModel.swift Added hiddenFields support, improved template and JS, enhanced error reporting.
Sources/FormbricksSDK/WebView/SurveyWebView.swift Disabled scrolling, added more delegate callbacks for survey events and errors.
Tests/FormbricksSDKTests/FormbricksEnvironmentTests.swift Added new tests for FormbricksEnvironment URL properties.
Tests/FormbricksSDKTests/FormbricksSDKTests.swift Improved setup/teardown, async handling, and added edge case tests.
Tests/FormbricksSDKTests/MockFormbricksService/MockFormbricksService.swift Refactored JSON loading into a helper method.
Tests/FormbricksSDKTests/Networking/APIClientTests.swift Added comprehensive tests for APIClient covering success, error, and edge cases.
Tests/FormbricksSDKTests/Networking/ClientAPIEndpointsTests.swift Added tests for API endpoint request initialization.
Tests/FormbricksSDKTests/Networking/UpdateQueueTests.swift Added tests for UpdateQueue covering debouncing, state, and error handling.

Possibly related PRs

  • fix: recontact days calculation #20: Both PRs modify the numberOfDaysBetween(_:and:) method in the Calendar extension for safer, non-crashing behavior.
  • fix: webview scroll issue #21: Both PRs extend FormbricksViewModel to accept hidden fields and add the getSetIsResponseSendingFinished JS function, with overlapping changes in survey web view logic.

Suggested reviewers

  • mattinannt
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@huynhtanduysource huynhtanduysource changed the base branch from main to mobile-sdk-custom June 16, 2025 07:22
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.

3 participants