Skip to content

Commit e724e84

Browse files
committed
Rename Initialize Method
1 parent 0375058 commit e724e84

File tree

2 files changed

+68
-70
lines changed

2 files changed

+68
-70
lines changed

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const String layoutID = '0QulTBDc3r8gHgv0KeVK';
88
void main() async {
99
WidgetsFlutterBinding.ensureInitialized();
1010

11-
await Codelessly.initializeSDK(
11+
await Codelessly.initialize(
1212
config: const CodelesslyConfig(
1313
authToken: authToken,
1414
isPreview: false,

lib/src/codelessly.dart

Lines changed: 67 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import '../codelessly_sdk.dart';
77
import '../firedart.dart';
88
import 'auth/codelessly_auth_manager.dart';
99
import 'cache/codelessly_cache_manager.dart';
10-
import 'codelessly_config.dart';
1110
import 'data/firebase_data_repository.dart';
1211
import 'data/web_data_repository.dart';
1312
import 'logging/error_handler.dart';
@@ -89,6 +88,17 @@ class Codelessly {
8988
/// when they are triggered.
9089
final Map<String, CodelesslyFunction> functions = {};
9190

91+
CodelesslyStatus _status = CodelesslyStatus.empty;
92+
93+
/// Returns the current status of this SDK instance.
94+
CodelesslyStatus get status => _status;
95+
96+
final StreamController<CodelesslyStatus> _statusStreamController =
97+
StreamController.broadcast()..add(CodelesslyStatus.empty);
98+
99+
/// Returns a stream of status updates for this SDK instance.
100+
Stream<CodelesslyStatus> get statusStream => _statusStreamController.stream;
101+
92102
/// Creates a new instance of [Codelessly].
93103
Codelessly({
94104
CodelesslyConfig? config,
@@ -124,19 +134,7 @@ class Codelessly {
124134
}
125135
}
126136

127-
CodelesslyStatus _status = CodelesslyStatus.empty;
128-
129-
/// Returns the current status of this SDK instance.
130-
CodelesslyStatus get status => _status;
131-
132-
final StreamController<CodelesslyStatus> _statusStreamController =
133-
StreamController.broadcast()..add(CodelesslyStatus.empty);
134-
135-
/// Returns a stream of status updates for this SDK instance.
136-
Stream<CodelesslyStatus> get statusStream => _statusStreamController.stream;
137-
138137
/// Disposes this instance of the SDK permanently.
139-
@mustCallSuper
140138
void dispose() {
141139
assert(
142140
!isGlobalInstance(this),
@@ -157,6 +155,62 @@ class Codelessly {
157155
_previewDataManager = null;
158156
}
159157

158+
/// Initializes this instance of the SDK.
159+
///
160+
/// To know when the SDK is ready, simply listen to the status events from the
161+
/// [Codelessly.instance.statusStream]. No need to await the future.
162+
///
163+
/// If the [CodelesslyWidget] recognizes that this instance of the
164+
/// [Codelessly] SDK is the global instance rather than a local one, it will
165+
/// initialize the SDK automatically, if specified.
166+
static Future<CodelesslyStatus> initialize({
167+
CodelesslyConfig? config,
168+
169+
// Raw managers.
170+
AuthManager? authManager,
171+
DataManager? publishDataManager,
172+
DataManager? previewDataManager,
173+
CacheManager? cacheManager,
174+
175+
// Raw data.
176+
Map<String, dynamic>? data,
177+
Map<String, CodelesslyFunction>? functions,
178+
}) async {
179+
assert(
180+
(config == null) != (_instance._config == null),
181+
_instance._config == null
182+
? 'The SDK cannot be initialized if it is not configured. '
183+
'\nConsider specifying a [CodelesslyConfig] when initializing.'
184+
: 'A [CodelesslyConfig] was already provided.'
185+
'\nConsider removing the duplicate config or calling '
186+
'[Codelessly.dispose] before reinitializing.',
187+
);
188+
189+
try {
190+
return _instance.init(
191+
config: config,
192+
cacheManager: cacheManager,
193+
authManager: authManager,
194+
publishDataManager: publishDataManager,
195+
previewDataManager: previewDataManager,
196+
data: data,
197+
functions: functions,
198+
);
199+
} catch (error, stacktrace) {
200+
_instance._config ??= config;
201+
_instance.initErrorHandler(
202+
automaticallySendCrashReports:
203+
(_instance._config?.automaticallyCollectCrashReports) ?? false,
204+
);
205+
206+
CodelesslyErrorHandler.instance.captureException(
207+
error,
208+
stacktrace: stacktrace,
209+
);
210+
return CodelesslyStatus.error;
211+
}
212+
}
213+
160214
/// Resets the state of the SDK. This is useful for resetting the data without
161215
/// disposing the instance permanently.
162216
///
@@ -455,60 +509,4 @@ class Codelessly {
455509
functions: functions,
456510
);
457511
}
458-
459-
/// Initializes this instance of the SDK.
460-
///
461-
/// To know when the SDK is ready, simply listen to the status events from the
462-
/// [Codelessly.instance.statusStream]. No need to await the future.
463-
///
464-
/// If the [CodelesslyWidget] recognizes that this instance of the
465-
/// [Codelessly] SDK is the global instance rather than a local one, it will
466-
/// initialize the SDK automatically, if specified.
467-
static Future<CodelesslyStatus> initializeSDK({
468-
CodelesslyConfig? config,
469-
470-
// Raw managers.
471-
AuthManager? authManager,
472-
DataManager? publishDataManager,
473-
DataManager? previewDataManager,
474-
CacheManager? cacheManager,
475-
476-
// Raw data.
477-
Map<String, dynamic>? data,
478-
Map<String, CodelesslyFunction>? functions,
479-
}) async {
480-
assert(
481-
(config == null) != (_instance._config == null),
482-
_instance._config == null
483-
? 'The SDK cannot be initialized if it is not configured. '
484-
'\nConsider specifying a [CodelesslyConfig] when initializing.'
485-
: 'A [CodelesslyConfig] was already provided.'
486-
'\nConsider removing the duplicate config or calling '
487-
'[Codelessly.dispose] before reinitializing.',
488-
);
489-
490-
try {
491-
return _instance.init(
492-
config: config,
493-
cacheManager: cacheManager,
494-
authManager: authManager,
495-
publishDataManager: publishDataManager,
496-
previewDataManager: previewDataManager,
497-
data: data,
498-
functions: functions,
499-
);
500-
} catch (error, stacktrace) {
501-
_instance._config ??= config;
502-
_instance.initErrorHandler(
503-
automaticallySendCrashReports:
504-
(_instance._config?.automaticallyCollectCrashReports) ?? false,
505-
);
506-
507-
CodelesslyErrorHandler.instance.captureException(
508-
error,
509-
stacktrace: stacktrace,
510-
);
511-
return CodelesslyStatus.error;
512-
}
513-
}
514512
}

0 commit comments

Comments
 (0)