Skip to content

Commit

Permalink
feat: update sdk instructions (#230)
Browse files Browse the repository at this point in the history
Update SDK instructions
  • Loading branch information
dbolson authored May 2, 2024
1 parent 8afec4f commit 2909424
Show file tree
Hide file tree
Showing 13 changed files with 871 additions and 460 deletions.
146 changes: 89 additions & 57 deletions internal/sdks/sdk_instructions/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,66 @@ dependencies {

3. Open the file `MainActivity.kt` and add the following code:
```java
package com.launchdarkly.hello_android

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.launchdarkly.hello_android.MainApplication.Companion.LAUNCHDARKLY_MOBILE_KEY
import com.launchdarkly.sdk.android.LDClient

class MainActivity : AppCompatActivity() {

// Set BOOLEAN_FLAG_KEY to the boolean feature flag you want to evaluate.
val BOOLEAN_FLAG_KEY = "my-flag-key"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView : TextView = findViewById(R.id.textview)

val client = LDClient.get()

// to get the variation the SDK has cached
textView.text = getString(
R.string.flag_evaluated,
BOOLEAN_FLAG_KEY,
client.boolVariation(BOOLEAN_FLAG_KEY, false).toString()
)

// to register a listener to get updates in real time
client.registerFeatureFlagListener(BOOLEAN_FLAG_KEY) {
textView.text = getString(
R.string.flag_evaluated,
BOOLEAN_FLAG_KEY,
client.boolVariation(BOOLEAN_FLAG_KEY, false).toString()
)
}

// This call ensures all evaluation events show up immediately for this demo. Otherwise, the
// SDK sends them at some point in the future. You don't need to call this in production,
// because the SDK handles them automatically at an interval. The interval is customizable.
client.flush()
}
// Set BOOLEAN_FLAG_KEY to the feature flag key you want to evaluate.
val BOOLEAN_FLAG_KEY = "my-flag-key"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView : TextView = findViewById(R.id.textview)
val fullView : View = window.decorView

if (LAUNCHDARKLY_MOBILE_KEY == "example-mobile-key") {
val builder = AlertDialog.Builder(this)
builder.setMessage("LAUNCHDARKLY_MOBILE_KEY was not customized for this application.")
builder.create().show()
}

val client = LDClient.get()
val flagValue = client.boolVariation(BOOLEAN_FLAG_KEY, false)

// to get the variation the SDK has cached
textView.text = getString(
R.string.flag_evaluated,
BOOLEAN_FLAG_KEY,
flagValue.toString()
)

// Style the display
textView.setTextColor(resources.getColor(R.color.colorText))
if(flagValue) {
fullView.setBackgroundColor(resources.getColor(R.color.colorBackgroundTrue))
} else {
fullView.setBackgroundColor(resources.getColor(R.color.colorBackgroundFalse))
}

// to register a listener to get updates in real time
client.registerFeatureFlagListener(BOOLEAN_FLAG_KEY) {
val changedFlagValue = client.boolVariation(BOOLEAN_FLAG_KEY, false)
textView.text = getString(
R.string.flag_evaluated,
BOOLEAN_FLAG_KEY,
changedFlagValue.toString()
)
if(changedFlagValue) {
fullView.setBackgroundColor(resources.getColor(R.color.colorBackgroundTrue))
} else {
fullView.setBackgroundColor(resources.getColor(R.color.colorBackgroundFalse))
}
}
}
}
```

Expand All @@ -60,47 +85,54 @@ class MainActivity : AppCompatActivity() {

5. Create `MainApplication.kt` and add the following code:
```java
package com.launchdarkly.hello_android

import android.app.Application
import com.launchdarkly.sdk.ContextKind
import com.launchdarkly.sdk.LDContext
import com.launchdarkly.sdk.android.LDClient
import com.launchdarkly.sdk.android.LDConfig
import com.launchdarkly.sdk.android.LDConfig.Builder.AutoEnvAttributes

class MainApplication : Application() {

companion object {

// Set LAUNCHDARKLY_MOBILE_KEY to your LaunchDarkly SDK mobile key.
const val LAUNCHDARKLY_MOBILE_KEY = "myMobileKey"
}
companion object {

override fun onCreate() {
super.onCreate()
// Set LAUNCHDARKLY_MOBILE_KEY to your LaunchDarkly SDK mobile key.
const val LAUNCHDARKLY_MOBILE_KEY = "myMobileKey"
}

val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey(LAUNCHDARKLY_MOBILE_KEY)
.build()
override fun onCreate() {
super.onCreate()

// Set up the context properties. This context should appear on your LaunchDarkly contexts
// list soon after you run the demo.
val context = if (isUserLoggedIn()) {
LDContext.builder(ContextKind.DEFAULT, getUserKey())
.name(getUserName())
.build()
} else {
LDContext.builder(ContextKind.DEFAULT, "example-user-key")
.anonymous(true)
.build()
}
// Set LAUNCHDARKLY_MOBILE_KEY to your LaunchDarkly mobile key found on the LaunchDarkly
// dashboard in the start guide.
// If you want to disable the Auto EnvironmentAttributes functionality.
// Use AutoEnvAttributes.Disabled as the argument to the Builder
val ldConfig = LDConfig.Builder(AutoEnvAttributes.Enabled)
.mobileKey(LAUNCHDARKLY_MOBILE_KEY)
.build()

LDClient.init(this@MainApplication, ldConfig, context)
}
// Set up the context properties. This context should appear on your LaunchDarkly context
// dashboard soon after you run the demo.
val context = if (isUserLoggedIn()) {
LDContext.builder(ContextKind.DEFAULT, getUserKey())
.name(getUserName())
.build()
} else {
LDContext.builder(ContextKind.DEFAULT, "example-user-key")
.anonymous(true)
.build()
}

private fun isUserLoggedIn(): Boolean = false
LDClient.init(this@MainApplication, ldConfig, context)
}

private fun getUserKey(): String = "user-key-123abc"
private fun isUserLoggedIn(): Boolean = false

private fun getUserName(): String = "Sandy"
private fun getUserKey(): String = "example-user-key"

private fun getUserName(): String = "Sandy"
}
```

Expand Down
112 changes: 75 additions & 37 deletions internal/sdks/sdk_instructions/dotnet-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,98 @@ Install-Package LaunchDarkly.ServerSdk
3. Open the file `Program.cs` and add the following code:
```cs
using System;
using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Server;
using System.Threading.Tasks;
using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Server;

namespace HelloDotNet
namespace HelloDotNet
{
class Hello
{
class Program
public static void ShowBanner(){
Console.WriteLine(
@" ██
██
████████
███████
██ LAUNCHDARKLY █
███████
████████
██
██
");
}

static void Main(string[] args)
{
// Set SdkKey to your LaunchDarkly SDK key.
public const string SdkKey = "1234567890abcdef";
bool CI = Environment.GetEnvironmentVariable("CI") != null;

string SdkKey = Environment.GetEnvironmentVariable("LAUNCHDARKLY_SDK_KEY");

// Set FeatureFlagKey to the feature flag key you want to evaluate.
public const string FeatureFlagKey = "my-flag-key";
string FeatureFlagKey = "my-flag-key";

private static void ShowMessage(string s) {
Console.WriteLine("*** " + s);
Console.WriteLine();
if (string.IsNullOrEmpty(SdkKey))
{
Console.WriteLine("*** Please set LAUNCHDARKLY_SDK_KEY environment variable to your LaunchDarkly SDK key first\n");
Environment.Exit(1);
}

static void Main(string[] args)
{
var ldConfig = Configuration.Default(SdkKey);
var ldConfig = Configuration.Default(SdkKey);

var client = new LdClient(ldConfig);
var client = new LdClient(ldConfig);

if (client.Initialized)
{
ShowMessage("SDK successfully initialized!");
}
else
{
ShowMessage("SDK failed to initialize");
Environment.Exit(1);
}
if (client.Initialized)
{
Console.WriteLine("*** SDK successfully initialized!\n");
}
else
{
Console.WriteLine("*** SDK failed to initialize\n");
Environment.Exit(1);
}

// Set up the evaluation context. This context should appear on your LaunchDarkly contexts
// dashboard soon after you run the demo.
var context = Context.Builder("example-user-key")
.Name("Sandy")
.Build();
// Set up the evaluation context. This context should appear on your LaunchDarkly contexts
// dashboard soon after you run the demo.
var context = Context.Builder("example-user-key")
.Name("Sandy")
.Build();

var flagValue = client.BoolVariation(FeatureFlagKey, context, false);
if (Environment.GetEnvironmentVariable("LAUNCHDARKLY_FLAG_KEY") != null)
{
FeatureFlagKey = Environment.GetEnvironmentVariable("LAUNCHDARKLY_FLAG_KEY");
}

var flagValue = client.BoolVariation(FeatureFlagKey, context, false);

ShowMessage(string.Format("Feature flag '{0}' is {1}",
FeatureFlagKey, flagValue));
Console.WriteLine(string.Format("*** The {0} feature flag evaluates to {1}.\n",
FeatureFlagKey, flagValue));

// Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
// events to LaunchDarkly before the program exits. If analytics events are not delivered,
// the context attributes and flag usage statistics will not appear on your dashboard. In
// a normal long-running application, the SDK would continue running and events would be
// delivered automatically in the background.
client.Dispose();
if (flagValue)
{
ShowBanner();
}

client.FlagTracker.FlagChanged += client.FlagTracker.FlagValueChangeHandler(
FeatureFlagKey,
context,
(sender, changeArgs) => {
Console.WriteLine(string.Format("*** The {0} feature flag evaluates to {1}.\n",
FeatureFlagKey, changeArgs.NewValue));

if (changeArgs.NewValue.AsBool) ShowBanner();
}
);

if(CI) Environment.Exit(0);

Console.WriteLine("*** Waiting for changes \n");

Task waitForever = new Task(() => {});
waitForever.Wait();
}
}
}
```

Now that your application is ready, run the application to see what value we get.
Expand Down
Loading

0 comments on commit 2909424

Please sign in to comment.