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: SSE / real time flags support #32 #67

Merged
merged 18 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Example/FlagsmithClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
7F76D20A2C9852510028470B /* SwiftUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7F76D2092C9852510028470B /* SwiftUIView.swift */; };
C87CA15C7294632245730C64 /* Pods_FlagsmithClient_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9F7BEB767BE008D76723DD6 /* Pods_FlagsmithClient_Example.framework */; };
DAED1E94268DBF9100F91DBC /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAED1E8C268DBF9100F91DBC /* ViewController.swift */; };
DAED1E95268DBF9100F91DBC /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = DAED1E8D268DBF9100F91DBC /* LaunchScreen.xib */; };
Expand All @@ -18,6 +19,7 @@
/* Begin PBXFileReference section */
5357A0EF6DA00FB7B1E84F8C /* Pods-FlagsmithClient_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlagsmithClient_Example.release.xcconfig"; path = "Target Support Files/Pods-FlagsmithClient_Example/Pods-FlagsmithClient_Example.release.xcconfig"; sourceTree = "<group>"; };
607FACD01AFB9204008FA782 /* FlagsmithClient_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FlagsmithClient_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
7F76D2092C9852510028470B /* SwiftUIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIView.swift; sourceTree = "<group>"; };
9DFD4B33E10902546A38D90F /* Pods-FlagsmithClient_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlagsmithClient_Example.debug.xcconfig"; path = "Target Support Files/Pods-FlagsmithClient_Example/Pods-FlagsmithClient_Example.debug.xcconfig"; sourceTree = "<group>"; };
C4197E729B096AA93F72A537 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
C9F7BEB767BE008D76723DD6 /* Pods_FlagsmithClient_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FlagsmithClient_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -96,6 +98,7 @@
DAED1E8F268DBF9100F91DBC /* Main.storyboard */,
DAED1E91268DBF9100F91DBC /* Images.xcassets */,
DAED1E92268DBF9100F91DBC /* AppDelegate.swift */,
7F76D2092C9852510028470B /* SwiftUIView.swift */,
);
path = FlagsmithClient;
sourceTree = "<group>";
Expand Down Expand Up @@ -220,6 +223,7 @@
files = (
DAED1E98268DBF9100F91DBC /* AppDelegate.swift in Sources */,
DAED1E94268DBF9100F91DBC /* ViewController.swift in Sources */,
7F76D20A2C9852510028470B /* SwiftUIView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1320"
LastUpgradeVersion = "1540"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
15 changes: 15 additions & 0 deletions Example/FlagsmithClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import UIKit
import FlagsmithClient
#if canImport(SwiftUI)
import SwiftUI
#endif

func isSuccess<T, F>(_ result: Result<T, F>) -> Bool {
if case .success = result { return true } else { return false }
Expand Down Expand Up @@ -43,6 +46,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// set analytics on or off
Flagsmith.shared.enableAnalytics = true

// Enable real time updates
Flagsmith.shared.enableRealtimeUpdates = true

// set the analytics flush period in seconds
Flagsmith.shared.analyticsFlushPeriod = 10
Expand All @@ -65,6 +71,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

// Flagsmith.shared.setTrait(Trait(key: "<my_key>", value: "<my_value>"), forIdentity: "<my_identity>") { (result) in print(result) }
// Flagsmith.shared.getIdentity("<my_key>") { (result) in print(result) }

// Launch SwiftUIView
if #available(iOS 13.0, *) {
let swiftUIView = SwiftUIView()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIHostingController(rootView: swiftUIView)
window?.makeKeyAndVisible()
}

return true
}

Expand Down
17 changes: 5 additions & 12 deletions Example/FlagsmithClient/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -11,14 +13,8 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2019 SolidStateGroup. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="440" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" systemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="FlagsmithClient" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="140" width="440" height="43"/>
<rect key="frame" x="20" y="139.66666666666666" width="440" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<color key="textColor" systemColor="darkTextColor"/>
<nil key="highlightedColor"/>
Expand All @@ -28,9 +24,6 @@
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
Expand Down
68 changes: 68 additions & 0 deletions Example/FlagsmithClient/SwiftUIView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// SwiftUIView.swift
// FlagsmithClient_Example
//
// Created by Gareth Reese on 16/09/2024.
// Copyright © 2024 CocoaPods. All rights reserved.
//

#if canImport(SwiftUI)
import SwiftUI
#endif
import FlagsmithClient

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *)
struct SwiftUIView: View {
@State private var flags: [Flag] = []
@State private var isLoading = true

let flagsmith = Flagsmith.shared

var body: some View {
VStack {
if isLoading {
ProgressView()
} else {
Text("Feature Flags")
.font(.title)
.padding()
List(flags, id: \.feature.name) { flag in
HStack {
Text("\(flag.feature.name): \(flag.value)")
Spacer()
Text("\(flag.enabled ? "" : "")")
}
}
}
}
.onAppear {
initializeFlagsmith()
subscribeToFlagUpdates()
}
}

func initializeFlagsmith() {
// Fetch initial flags
flagsmith.getFeatureFlags { result in
DispatchQueue.main.async {
switch result {
case .success(let fetchedFlags):
flags = fetchedFlags
case .failure(let error):
print("Error fetching flags: \(error)")
}
isLoading = false
}
}
}

func subscribeToFlagUpdates() {
Task {
for await updatedFlags in flagsmith.flagStream {
DispatchQueue.main.async {
flags = updatedFlags
}
}
}
}
}
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- FlagsmithClient (3.6.0)
- FlagsmithClient (3.6.2)
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved

DEPENDENCIES:
- FlagsmithClient (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
FlagsmithClient: 101151384696085c085d06b1c202946827e058d6
FlagsmithClient: 31e78456bd614b87be83b6c83efb10b917fc62fc

PODFILE CHECKSUM: 9fc876dee0cf031cae843156b0740a94b4994d8c

Expand Down
8 changes: 4 additions & 4 deletions Example/Pods/Local Podspecs/FlagsmithClient.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading