Thanks to my associate Tonu Vanatalu for this. He wrote it in response to some brainstorming we did in my larger team about common code patterns in Windows Store apps. It’s written in C#, but it just uses WinRT APIs so I trust that JavaScript devs can translate the casing and insert fully-qualified identifiers where needed:

public App()
{
    // Set initial behavior based on connectivity
    SetDataConnectivityOptions();

    // Set up handler for network status change
    NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
}

void NetworkInformation_NetworkStatusChanged(object sender)
{
    // Set behavior based on connectivity status
    SetDataConnectivityOptions();
}

void SetDataConnectivityOptions()
{
    // Get connection profile for currently active connection
    ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();

    if (profile != null)
    {
        NetworkConnectivityLevel connectivityLevel = profile.GetNetworkConnectivityLevel();
        if (connectivityLevel == NetworkConnectivityLevel.InternetAccess)
        {
            // Get connection cost associated with the profile
            ConnectionCost cost = profile.GetConnectionCost();

            // Check if there is limit imposed on maximum transfer size
            DataPlanStatus dataplanStatus = profile.GetDataPlanStatus();

            if (dataplanStatus.MaxTransferSizeInMegabytes != null)
            {
                // Maximum transfer size is limited
                // Suspend transfers exceeding the size and warn the user
            }

            switch (cost.NetworkCostType)
            {
                caseNetworkCostType.Fixed:
                    // The use of this connection is unrestricted up to a specific limit.
                    if (cost.Roaming == true)
                    {
                        // Even if the cost is fixed up to a limit the user is roaming
                        // Which means the limit might be relatively low and cost per byte expensive
                    }

                    if (cost.ApproachingDataLimit == true)
                    {
                        // The connection is approaching it’s unrestricted limit
                    }

                    if (cost.OverDataLimit == true)
                    {
                        // The connection is over the data limit, possibly either connectivity
                        // is severely limited or very costly
                    }

                    break;

                caseNetworkCostType.Unknown:
                    // Cost information is not available
                    break;

caseNetworkCostType.Unrestricted:
// The connection is unlimited and has unrestricted usage charges 
                    // and capacity constraints.
break;

                caseNetworkCostType.Variable:
                    // The connection is costed on a per-byte basis.
                    if (cost.Roaming == true)
                    {
                        // User is roaming, possibly on a very expensive network
                    }
                    break;
            }
        }
        else
        {
            // Limited network access
            switch (connectivityLevel)
            {
                caseNetworkConnectivityLevel.ConstrainedInternetAccess:
                    // This value indicates captive portal connectivity, where local
                    // access to a web portal is provided, but access to the Internet
                    // requires that specific credentials are provided via the portal.
                    break;

                caseNetworkConnectivityLevel.LocalAccess:
                    // Local access only
                    break;

caseNetworkConnectivityLevel.None:
                    // No network access
                    break;
            }
        }
    }
    else
    {
        // Currently there are no network connections
        // Suspend pending network operations until network becomes available
    }
}

 


Comments are closed