Skip to content

Add lazy connection toggle#56

Open
doromaraujo wants to merge 2 commits intomainfrom
feature/add-lazy-connection-toggle
Open

Add lazy connection toggle#56
doromaraujo wants to merge 2 commits intomainfrom
feature/add-lazy-connection-toggle

Conversation

@doromaraujo
Copy link
Copy Markdown
Contributor

@doromaraujo doromaraujo commented Feb 16, 2026

This PR adds a toggle in advanced settings for enabling lazy connection on the iOS peer.

Summary by CodeRabbit

  • New Features

    • Added "Enable lazy connection" toggle in Advanced settings, enabled by default.
  • Improvements

    • Refined configuration change notification system with a unified alert for all configuration updates.

This depends on NetBirdSDK at least at commit 1024d4569, which exports
lazy connection variables (whether if it's enabled and the inactivity threshold)
both for Android and iOS
Copilot AI review requested due to automatic review settings February 16, 2026 20:33
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 16, 2026

📝 Walkthrough

Walkthrough

The PR adds lazy connection configuration to the iOS NetBird app by introducing a new UI toggle in AdvancedView, implementing persistence through UserDefaults, updating the ViewModel to handle the new setting, and exposing it via environment variables. The force-relay alert is replaced with a generic config-change alert.

Changes

Cohort / File(s) Summary
ViewModel & UI Updates
NetBird/Source/App/ViewModels/MainViewModel.swift, NetBird/Source/App/Views/AdvancedView.swift
Added enableLazyConnection property and showConfigChangeAlert flag to ViewModel; replaced showForceRelayAlert with generic config-change alert; added setEnableLazyConnection() and getLazyConnectionEnabled() methods; added new UI toggle in AdvancedView bound to the lazy connection setting.
Configuration Constants
NetbirdKit/GlobalConstants.swift
Added new public constant keyEnableLazyConnection with value "isLazyConnectionEnabled".
Environment Packaging
NetbirdKit/EnvVarPackager.swift
Registered and exposed lazy connection default value as NBLazyConn environment variable; set NBInactivityThreshold to 5.
Test Coverage
NetBirdTests/GlobalConstantsTests.swift, NetBirdTests/SharedUserDefaultsTests.swift
Added test for the new lazy connection constant key; added test teardown and verification test that lazy connection defaults to true.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ios-client#37: Modifies the same ViewModel, View, EnvVarPackager, and GlobalConstants files to manage connection-toggle settings; this PR replaces/renames the earlier force-relay option with lazy connection.

Suggested reviewers

  • pascal-fischer
  • mlsmaycon

Poem

🐰 With a toggle and a hop, lazy connections now take flight,
Config changes alert with gentle cheer, no force relay in sight,
UserDefaults hold the setting true, environment vars agree,
This bunny hops to simpler code, as it was meant to be!

🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add lazy connection toggle' accurately and concisely describes the main objective of the pull request—introducing a new UI toggle for lazy connection control across the codebase.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/add-lazy-connection-toggle

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.

🤖 Fix all issues with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.

In `@NetbirdKit/EnvVarPackager.swift`:
- Line 33: Replace the magic number 5 used in
envList.put(NetBirdSDKGetEnvKeyNBInactivityThreshold(), value: String(5)) with a
named constant defined in GlobalConstants (e.g., NB_INACTIVITY_THRESHOLD) and
document the unit (seconds/minutes) in that constant's declaration; update the
call to use String(GlobalConstants.NB_INACTIVITY_THRESHOLD) so the value is
centralized and clearer for maintainability.
🧹 Nitpick comments (1)
🤖 Fix all nitpicks with AI agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.

In `@NetbirdKit/EnvVarPackager.swift`:
- Line 33: Replace the magic number 5 used in
envList.put(NetBirdSDKGetEnvKeyNBInactivityThreshold(), value: String(5)) with a
named constant defined in GlobalConstants (e.g., NB_INACTIVITY_THRESHOLD) and
document the unit (seconds/minutes) in that constant's declaration; update the
call to use String(GlobalConstants.NB_INACTIVITY_THRESHOLD) so the value is
centralized and clearer for maintainability.
NetbirdKit/EnvVarPackager.swift (1)

33-33: Magic number for inactivity threshold.

The value 5 is hardcoded without a named constant or documentation of its unit (seconds? minutes?). Consider extracting this to a constant in GlobalConstants for clarity and maintainability, similar to how other settings are managed.

🤖 Prompt for AI Agents
Before applying any fix, first verify the finding against the current code and
decide whether a code change is actually needed. If the finding is not valid or
no change is required, do not modify code for that item and briefly explain why
it was skipped.
In `@NetbirdKit/EnvVarPackager.swift` at line 33, Replace the magic number 5 used
in envList.put(NetBirdSDKGetEnvKeyNBInactivityThreshold(), value: String(5))
with a named constant defined in GlobalConstants (e.g., NB_INACTIVITY_THRESHOLD)
and document the unit (seconds/minutes) in that constant's declaration; update
the call to use String(GlobalConstants.NB_INACTIVITY_THRESHOLD) so the value is
centralized and clearer for maintainability.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a toggle in the iOS app's advanced settings to enable/disable lazy connection functionality, which automatically manages peer connections based on activity. The feature defaults to enabled (true) and applies an inactivity threshold of 5 seconds.

Changes:

  • Added a new user preference key for lazy connection settings
  • Implemented UI toggle in AdvancedView with configuration change alert
  • Refactored ForceRelayAlert to ConfigChangeAlert for reusability across multiple settings
  • Added comprehensive test coverage for the new preference

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
NetbirdKit/GlobalConstants.swift Added new constant keyEnableLazyConnection for storing the lazy connection preference
NetbirdKit/EnvVarPackager.swift Added lazy connection and inactivity threshold environment variables to the SDK configuration
NetBirdTests/SharedUserDefaultsTests.swift Added test to verify lazy connection defaults to true and added teardown cleanup
NetBirdTests/GlobalConstantsTests.swift Added test to verify the lazy connection key constant value
NetBird/Source/App/Views/AdvancedView.swift Added lazy connection toggle UI and renamed ForceRelayAlert to ConfigChangeAlert for reusability
NetBird/Source/App/ViewModels/MainViewModel.swift Added state management for lazy connection setting and updated alert property name

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

envList.put(NetBirdSDKGetEnvKeyNBForceRelay(), value: String(forceRelayConnection))

envList.put(NetBirdSDKGetEnvKeyNBLazyConn(), value: String(isLazyConnectionEnabled))
envList.put(NetBirdSDKGetEnvKeyNBInactivityThreshold(), value: String(5))
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

The hardcoded inactivity threshold value of 5 should be extracted to a named constant in GlobalConstants.swift for better maintainability. This would follow the established pattern seen with other configuration values in the codebase and make it easier to adjust this value in the future without searching through the code.

Copilot uses AI. Check for mistakes.
Comment on lines +399 to +401
let userDefaults = UserDefaults(suiteName: GlobalConstants.userPreferencesSuiteName)
userDefaults?.register(defaults: [GlobalConstants.keyEnableLazyConnection: true])
return userDefaults?.bool(forKey: GlobalConstants.keyEnableLazyConnection) ?? true
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

The getLazyConnectionEnabled function should follow the same pattern as getForcedRelayConnectionEnabled by including platform-specific defaults using #if os(iOS) guards. According to the PR description, lazy connection is for the iOS peer, suggesting it may need different defaults on tvOS similar to how forceRelayConnection has platform-specific handling.

Suggested change
let userDefaults = UserDefaults(suiteName: GlobalConstants.userPreferencesSuiteName)
userDefaults?.register(defaults: [GlobalConstants.keyEnableLazyConnection: true])
return userDefaults?.bool(forKey: GlobalConstants.keyEnableLazyConnection) ?? true
let userDefaults = UserDefaults(suiteName: GlobalConstants.userPreferencesSuiteName)
#if os(iOS)
userDefaults?.register(defaults: [GlobalConstants.keyEnableLazyConnection: true])
return userDefaults?.bool(forKey: GlobalConstants.keyEnableLazyConnection) ?? true
#else
// lazy connection is for the iOS peer; disable by default on Apple TV
userDefaults?.register(defaults: [GlobalConstants.keyEnableLazyConnection: false])
return userDefaults?.bool(forKey: GlobalConstants.keyEnableLazyConnection) ?? false
#endif

Copilot uses AI. Check for mistakes.
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.

2 participants