Skip to content
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

feat: implement connectivity observer #102

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from

Conversation

1abhishekpandey
Copy link
Contributor

@1abhishekpandey 1abhishekpandey commented Jan 14, 2025

Description

  • In this PR, we've added functionality to observe for connectivity changes i.e., both network becomes available and unvailable.
  • Android modules rely on the Android OS to handle the connectivity callback, whereas the core module defaults to always enabling network connectivity.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactor/optimization

Implementation Details

ConnectivityState

  • It supports three actions:
    • Default: It signifies the default state of the connection. It is used in the core module to always default to true.
    • Enable: It sets the connection state to true and anyone watching on this variable will be notified immediately after calling this action.
    • Disable: It sets the connection state to false and anyone watching on this variable will be notified immediately after calling this action.

AndroidConnectivityObserverPlugin

  • It has the logic to detect if the connection is available or not in the Android module and modify the connectivityState whenever whenever connection state changes. It handles both legacy version (i.e., from API version 21 to 23) and compatible SDK (i.e., API version N = 24 or above).
    • NOTE: In case there is any exception while checking for connection availability, it sets the connection state to true.
  • Added some utils methods.
  • Added unit test.

Checklist

  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary documentation (if appropriate).
  • I have ensured that my code follows the project's code style.
  • I have checked for potential performance impacts and optimized if necessary.
  • I have checked the code for security issues.
  • I have updated the changelog (if required).

How to test?

  1. Add this code in the Analytics class and call it:
private fun connectivityObserver() {
    this.analyticsScope.launch {
        [email protected]
            // .filter { it }            // Un-comment this to get notified only when network becomes available.
            .collect { connected ->
                println("Rudderstack: Network connection is: $connected")
            }
    }
}
  1. It'll print whenever network changes.
  2. Toggle the network on and off.

Breaking Changes

Maintainers Checklist

  • The code has been reviewed.
  • CI tests have passed.
  • All necessary documentation has been updated.

Screenshots (if applicable)

Additional Context

I'll implement the TODO directly in the SourceConfig PR.

@1abhishekpandey 1abhishekpandey self-assigned this Jan 14, 2025
@1abhishekpandey 1abhishekpandey changed the base branch from main to develop January 14, 2025 13:16
Copy link
Collaborator

@ChryssaAliferi ChryssaAliferi left a comment

Choose a reason for hiding this comment

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

Great effort!! 🚀

*/
@InternalRudderApi
@Suppress("TooGenericExceptionCaught")
inline fun safelyExecute(block: () -> Unit, onException: (Exception) -> Unit, onFinally: () -> Unit = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am having some doubts on the safelyExecute method. I understand that it encapsulates and forces the try / catch / finally block, but at the same time it hides the code and does not promote much readability. I need some time to see how this would work. What were you trying to achieve with this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this, I'm trying to achieve two use cases:

  1. Encapsulate the boilerplate try-catch code.
  2. In the future, when error collection tools are implemented in our SDK, we can utilize the catch block to track and notify any exceptions. This will ensure that code modifications are not required everywhere; only a single change will be needed.

e.g., something like this:

inline fun safelyExecute(block: () -> Unit, onException: (Exception) -> Unit, onFinally: () -> Unit = {}) {
    try {
        block()
    } catch (e: Exception) {
        onException(e)
        ExceptionTracker.notify("Some exception occurred: $e"); // We only need a single change to track and notify exceptions.
    } finally {
        onFinally()
    }
}

Please let me know, if it doesn't help in promoting readability then I think it would be better to directly use the try-catch block.

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

Successfully merging this pull request may close these issues.

2 participants