Skip to content

Commit c21df7d

Browse files
authored
Merge pull request #3 from context-sdk/reinhard/pre-release
chore: Clarify Example code
2 parents adb3ba6 + c86f6d6 commit c21df7d

File tree

3 files changed

+83
-52
lines changed

3 files changed

+83
-52
lines changed

example/lib/main.dart

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ class _MyAppState extends State<MyApp> {
2929
"ddf1949adb3751a25c71cb106b4201eaba9960cbc57e814ffd97ce7d381b0e24c07955a4cdd612f90854198ea600bc15939c182f72308159974b88d5107fe310")
3030
.then((value) => {print("Setup result: $value")});
3131

32-
_contextSdkPlugin.setGlobalCustomSignals({
33-
'string': 'string',
34-
'int': 12,
35-
'bool': true,
36-
'float': 1.124
37-
});
32+
_contextSdkPlugin.setGlobalCustomSignals(
33+
{'string': 'string', 'int': 12, 'bool': true, 'float': 1.124});
3834
}
3935

4036
// Platform messages are asynchronous, so we initialize in an async method.
@@ -80,7 +76,7 @@ class _MyAppState extends State<MyApp> {
8076
})
8177
},
8278
child: const Text('Track Event')),
83-
ElevatedButton(
79+
ElevatedButton(
8480
onPressed: () => {
8581
_contextSdkPlugin.trackPageView("my-page", {
8682
'string': 'string',
@@ -90,7 +86,7 @@ class _MyAppState extends State<MyApp> {
9086
})
9187
},
9288
child: const Text('Track Page View')),
93-
ElevatedButton(
89+
ElevatedButton(
9490
onPressed: () => {
9591
_contextSdkPlugin.trackUserAction("my-action", {
9692
'string': 'string',
@@ -100,9 +96,9 @@ class _MyAppState extends State<MyApp> {
10096
})
10197
},
10298
child: const Text('Track User Action')),
103-
ElevatedButton(
99+
ElevatedButton(
104100
onPressed: () async => {
105-
_contextSdkPlugin.calibrate("my_flow", null, {
101+
_contextSdkPlugin.calibrate("my_flow", customSignals: {
106102
'string': 'string',
107103
'int': 14,
108104
'bool': true,
@@ -119,69 +115,102 @@ class _MyAppState extends State<MyApp> {
119115
})
120116
},
121117
child: const Text('Calibrate')),
122-
ElevatedButton(
118+
ElevatedButton(
123119
onPressed: () async => {
124-
_contextSdkPlugin.optimize("my_flow", null, {
125-
'string': 'string',
126-
'int': 14,
127-
'bool': true,
128-
'float': 1.124
129-
}, (value) async {
130-
print("Optimized context: ${await value.validate()}");
131-
value.appendOutcomeMetadata({
120+
_contextSdkPlugin.optimize("my_flow", (context) async {
121+
print("Optimized context: ${await context.validate()}");
122+
context.appendOutcomeMetadata({
132123
'string': 'string',
133124
'int': 14,
134125
'bool': true,
135126
'float': 1.124
136127
});
137-
value.log(Outcome.positive);
128+
await context.log(Outcome.positive);
138129
})
139130
},
140131
child: const Text('Optimize')),
141-
ElevatedButton(
132+
ElevatedButton(
142133
onPressed: () async => {
143-
_contextSdkPlugin.fetchContext("my_flow", 3, {
134+
_contextSdkPlugin.optimize("my_flow", customSignals: {
144135
'string': 'string',
145136
'int': 14,
146137
'bool': true,
147138
'float': 1.124
148-
}).then((value) async {
149-
print("Fetch context: ${await value.validate()}");
150-
value.appendOutcomeMetadata({
139+
}, (context) async {
140+
print("Optimized context: ${await context.validate()}");
141+
context.appendOutcomeMetadata({
151142
'string': 'string',
152143
'int': 14,
153144
'bool': true,
154145
'float': 1.124
155146
});
156-
value.log(Outcome.positive);
147+
await context.log(Outcome.positive);
157148
})
158149
},
159-
child: const Text('Fetch Context')),
160-
ElevatedButton(
150+
child: const Text('Optimize (Custom Signals)')),
151+
ElevatedButton(
161152
onPressed: () async => {
162-
_contextSdkPlugin.fetchContext("my_flow", 3, {
163-
'string': 'string',
164-
'int': 14,
165-
'bool': true,
166-
'float': 1.124
167-
}).then((value) async {
168-
print("Instant context: ${await value.validate()}");
169-
value.appendOutcomeMetadata({
153+
_contextSdkPlugin.optimize("my_flow", maxDelay: 0,
154+
(context) async {
155+
print("Optimized context: ${await context.validate()}");
156+
context.appendOutcomeMetadata({
170157
'string': 'string',
171158
'int': 14,
172159
'bool': true,
173160
'float': 1.124
174161
});
175-
value.log(Outcome.positive);
162+
await context.log(Outcome.positive);
176163
})
177164
},
178-
child: const Text('Instant Context')),
179-
ElevatedButton(
165+
child: const Text('Optimize (Instant)')),
166+
ElevatedButton(
180167
onPressed: () async => {
181-
_contextSdkPlugin.recentContext("my_flow").then((value) async {
182-
print("Recent context: ${await value?.validate() ?? "No Context Found"}");
168+
_contextSdkPlugin.fetchContext("my_flow", 3,
169+
customSignals: {
170+
'string': 'string',
171+
'int': 14,
172+
'bool': true,
173+
'float': 1.124
174+
}).then((value) async {
175+
print("Fetch context: ${await value.validate()}");
176+
value.appendOutcomeMetadata({
177+
'string': 'string',
178+
'int': 14,
179+
'bool': true,
180+
'float': 1.124
181+
});
182+
value.log(Outcome.positive);
183183
})
184184
},
185+
child: const Text('Fetch Context')),
186+
ElevatedButton(
187+
onPressed: () async {
188+
final context =
189+
await _contextSdkPlugin.instantContext("my_flow", 3);
190+
print("Instant context: ${await context.validate()}");
191+
if (await context.shouldUpsell()) {
192+
await context.appendOutcomeMetadata({
193+
'string': 'string',
194+
'int': 14,
195+
'bool': true,
196+
'float': 1.124
197+
});
198+
await context.log(Outcome.positive);
199+
} else {
200+
await context.log(Outcome.skipped);
201+
}
202+
},
203+
child: const Text('Instant Context')),
204+
ElevatedButton(
205+
onPressed: () async {
206+
final context =
207+
await _contextSdkPlugin.recentContext("my_flow");
208+
if (context != null) {
209+
print("Recent context: ${await context.validate()}");
210+
} else {
211+
print("No recent context found");
212+
}
213+
},
185214
child: const Text('Recent Context')),
186215
],
187216
)),

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ packages:
4747
path: ".."
4848
relative: true
4949
source: path
50-
version: "0.0.1"
50+
version: "1.0.0"
5151
cupertino_icons:
5252
dependency: "direct main"
5353
description:

lib/context_sdk.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ class ContextSdk {
5050
}
5151

5252
void calibrate(
53-
String flowName,
54-
int? maxDelay,
55-
Map<String, dynamic>? customSignals,
56-
ValueSetter<RealWorldContext> onContextReady) async {
53+
String flowName,
54+
ValueSetter<RealWorldContext> onContextReady, {
55+
int? maxDelay,
56+
Map<String, dynamic>? customSignals,
57+
}) async {
5758
ContextSdkPlatform.instance.calibrate(flowName, maxDelay, customSignals,
5859
(contextId) {
5960
onContextReady(RealWorldContext(ContextIdHolder(contextId)));
@@ -66,10 +67,11 @@ class ContextSdk {
6667
///
6768
/// [onGoodMoment] Will only be called if ContextSDK deems it a good moment, meaning it might never be called.
6869
void optimize(
69-
String flowName,
70-
int? maxDelay,
71-
Map<String, dynamic>? customSignals,
72-
ValueSetter<RealWorldContext> onGoodMoment) {
70+
String flowName,
71+
ValueSetter<RealWorldContext> onGoodMoment, {
72+
int? maxDelay,
73+
Map<String, dynamic>? customSignals,
74+
}) {
7375
ContextSdkPlatform.instance.optimize(flowName, maxDelay, customSignals,
7476
(contextId) {
7577
onGoodMoment(RealWorldContext(ContextIdHolder(contextId)));
@@ -82,7 +84,7 @@ class ContextSdk {
8284
/// - [customSignals] A map of custom signals you want to add to the context
8385
/// Returns: A Future that resolves to a [RealWorldContext] object. If ContextSDK wasn't running this might take upto [duration] seconds to resolve, otherwise this will resolve instantly.
8486
Future<RealWorldContext> fetchContext(String flowName, int duration,
85-
Map<String, dynamic>? customSignals) async {
87+
{Map<String, dynamic>? customSignals}) async {
8688
final contextId = await ContextSdkPlatform.instance
8789
.fetchContext(flowName, duration, customSignals);
8890
return RealWorldContext(ContextIdHolder(contextId));
@@ -95,7 +97,7 @@ class ContextSdk {
9597
/// - [customSignals] A map of custom signals you want to add to the context
9698
/// Returns: A [RealWorldContext] object. This will always resolve instantly, but might lead to an incomplete context object if the duration wasn't reached.
9799
Future<RealWorldContext> instantContext(String flowName, int duration,
98-
Map<String, dynamic>? customSignals) async {
100+
{Map<String, dynamic>? customSignals}) async {
99101
final contextId = await ContextSdkPlatform.instance
100102
.instantContext(flowName, duration, customSignals);
101103
return RealWorldContext(ContextIdHolder(contextId));

0 commit comments

Comments
 (0)