Skip to content

Commit

Permalink
fix: Update to new standard format (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 authored Apr 17, 2024
1 parent 528cc9b commit aaade33
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 69 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
name: Build
on:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 9 * * *'
push:
branches: [ main ]
branches: [ main, 'feat/**' ]
paths-ignore:
- '**.md' # Do not need to run CI for markdown changes.
pull_request:
branches: [ main ]
branches: [ main, 'feat/**' ]
paths-ignore:
- '**.md'

Expand Down
2 changes: 2 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Repository Maintainers
* @launchdarkly/team-sdk-swift
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
### LaunchDarkly Sample macOS Application ###
# LaunchDarkly sample macOS application

[![Build](https://github.com/launchdarkly/hello-macos/actions/workflows/ci.yml/badge.svg)](https://github.com/launchdarkly/hello-macos/actions/workflows/ci.yml)
We've built a simple mobile application that demonstrates how LaunchDarkly's SDK works.

We've built a simple macOS application that demonstrates how LaunchDarkly's SDK works. Below, you'll find the basic build procedure, but for more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/).
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [iOS reference guide](https://docs.launchdarkly.com/sdk/client-side/ios).

##### Build instructions #####
## Build instructions

1. Make sure you have [XCode](https://itunes.apple.com/us/app/xcode/id497799835?ls=1&mt=12) installed
2. Open `hello-macos.xcworkspace` in XCode
3. Copy the mobile key from your account settings page and the feature flag key from your LaunchDarkly dashboard into `AppDelegate.swift`
4. Run your application through XCode
1. Make sure you have [Xcode](https://itunes.apple.com/us/app/xcode/id497799835?ls=1&mt=12) installed
1. Open `hello-macos.xcworkspace` in XCode
1. Set the value of `sdkKey` in `AppDelegate.swift` to your LaunchDarkly mobile key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `featureFlagKey` in `ViewController.swift` to the flag key.

```swift
// AppDelegate.swift
let sdkKey = ""

// ViewController.swift
let featureFlagKey = "sample-feature"
```

You should see the message "The <flagKey> feature flag evaluates to <flagValue>.", the application will run continuously and react to the flag changes in LaunchDarkly.
2 changes: 1 addition & 1 deletion hello-macos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
repositoryURL = "https://github.com/launchdarkly/ios-client-sdk.git";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 9.3.0;
minimumVersion = 9.6.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
7 changes: 4 additions & 3 deletions hello-macos.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"originHash" : "5dfcdeb61541ed6a3b745c9c3541a4c3b2417a1d63a7ab81046871a35c646347",
"pins" : [
{
"identity" : "ios-client-sdk",
"kind" : "remoteSourceControl",
"location" : "https://github.com/launchdarkly/ios-client-sdk.git",
"state" : {
"revision" : "4e535facdbd2682da9453c78c5a5ff75bb2ef7cc",
"version" : "9.0.2"
"revision" : "a71c356ffb8fc45ff352bddcdf13390128fd40c9",
"version" : "9.6.2"
}
},
{
Expand All @@ -19,5 +20,5 @@
}
}
],
"version" : 2
"version" : 3
}
31 changes: 8 additions & 23 deletions hello-macos/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
//
// AppDelegate.swift
// hello-macos
//
// Copyright © 2017 LaunchDarkly. All rights reserved.
//

import Cocoa
import LaunchDarkly

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

let launchDarklyMobileKey = ""
// Set sdkKey to your LaunchDarkly mobile key.
let sdkKey = ""

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application

setupLDClient()
if let viewController = NSApp.windows.first?.contentViewController as? ViewController {
viewController.onApplicationStarted()
}
}

func setupLDClient() {
var contextBuilder = LDContextBuilder(key: "test@email.com")
contextBuilder.trySetValue("firstName", .string("Bob"))
contextBuilder.trySetValue("lastName", .string("Loblaw"))
contextBuilder.trySetValue("groups", .array([.string("beta_testers")]))
// Set up the evaluation context. This context should appear on your
// LaunchDarkly contexts dashboard soon after you run the demo.
var contextBuilder = LDContextBuilder(key: "example-user-key")
contextBuilder.kind("user")
contextBuilder.name("Sandy")

guard case .success(let context) = contextBuilder.build()
else { return }

var config = LDConfig(mobileKey: launchDarklyMobileKey, autoEnvAttributes: .enabled)
config.flagPollingInterval = 30.0
config.enableBackgroundUpdates = true
config.eventFlushInterval = 30.0
var config = LDConfig(mobileKey: sdkKey, autoEnvAttributes: .enabled)

LDClient.start(config: config, context: context)
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}

51 changes: 19 additions & 32 deletions hello-macos/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
//
// ViewController.swift
// hello-macos
//
// Copyright © 2017 LaunchDarkly. All rights reserved.
//

import Cocoa
import LaunchDarkly

class ViewController: NSViewController {

let flagKey = "test-flag"
// Set featureFlagKey to the feature flag key you want to evaluate.
let featureFlagKey = "sample-feature"

@IBOutlet weak var valueLabel: NSTextField!

func onApplicationStarted() {
registerLDClientObservers()
checkFeatureValue()
}
if let ld = LDClient.get() {
ld.observe(key: featureFlagKey, owner: self) { [weak self] changedFlag in
guard let me = self else { return }
guard case .bool(let booleanValue) = changedFlag.newValue else { return }

func registerLDClientObservers() {
LDClient.get()!.observe(key: flagKey, owner: self) { [weak self] changedFlag in
self?.featureFlagDidUpdate(changedFlag.key)
me.updateUi(flagKey: changedFlag.key, result: booleanValue)
}
let result = ld.boolVariation(forKey: featureFlagKey, defaultValue: false)
updateUi(flagKey: featureFlagKey, result: result)
}
}

func checkFeatureValue() {
let showFeature = LDClient.get()!.boolVariation(forKey: flagKey, defaultValue: false)
updateLabel(value: "\(showFeature)")
}

func updateLabel(value: String){
valueLabel.stringValue = "Flag value: \(value)"
}

//MARK: - ClientDelegate Methods

func featureFlagDidUpdate(_ key: String!) {
if key == flagKey {
checkFeatureValue()
}

func updateUi(flagKey: String, result: Bool) {
self.valueLabel.stringValue = "The \(flagKey) feature flag evaluates to \(result)"

let toggleOn = NSColor(red: 0, green: 0.52, blue: 0.29, alpha: 1).cgColor
let toggleOff = NSColor(red: 0.22, green: 0.22, blue: 0.25, alpha: 1).cgColor

self.view.wantsLayer = true
self.view.layer?.backgroundColor = result ? toggleOn : toggleOff
}

}

0 comments on commit aaade33

Please sign in to comment.