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

[Fix] context problem with device changes #1906

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 4 additions & 8 deletions modules/ensemble/lib/framework/data_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,15 @@ class DataContext implements Context {
_contextMap['auth'] = GetIt.instance<AuthContextManager>();
}

_addLegacyDataContext(buildContext);
_addLegacyDataContext();
}


// device is a common name. If user already uses that, don't override it
// This is now in ensemble.device.*
void _addLegacyDataContext(BuildContext? newBuildContext) {
void _addLegacyDataContext() {
if (_contextMap['device'] == null) {
_contextMap['device'] = Device(newBuildContext);
} else {
// If device exists, update its context
final device = _contextMap['device'];
device.updateContext(newBuildContext);
_contextMap['device'] = Device();
}
}

Expand Down Expand Up @@ -472,7 +468,7 @@ class NativeInvokable extends ActionInvokable {
'user': () => UserInfo(),
'formatter': () => Formatter(),
'utils': () => _EnsembleUtils(),
'device': () => Device(buildContext),
'device': () => Device(),
'version': () => _cache['version'],
};
}
Expand Down
43 changes: 2 additions & 41 deletions modules/ensemble/lib/framework/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,13 @@ class Device
DeviceInfoCapability,
WidgetsBindingObserver {
static final Device _instance = Device._internal();
static late BuildContext context;

Device._internal() {
WidgetsBinding.instance.addObserver(this);
}
Device._internal();

factory Device([BuildContext? buildContext]) {
if (buildContext != null) {
context = buildContext;
}
factory Device() {
return _instance;
}

// method to update context
void updateContext(BuildContext? newContext) {
if (newContext != null) {
context = newContext;
}
}

@override
void didChangeMetrics() {
WidgetsBinding.instance
.addPostFrameCallback((_) => _handleMediaQueryChange());
}

void _handleMediaQueryChange() {
final newData = MediaQuery.of(context);

// Compare with existing static data
if (MediaQueryCapability.data?.orientation != newData.orientation ||
MediaQueryCapability.data?.size != newData.size) {
MediaQueryCapability.data = newData;

// Dispatch individual property changes
ScreenController().dispatchDeviceChanges(context, 'width', screenWidth);
ScreenController().dispatchDeviceChanges(context, 'height', screenHeight);
ScreenController()
.dispatchDeviceChanges(context, 'orientation', screenOrientation);
ScreenController()
.dispatchDeviceChanges(context, 'safeAreaTop', safeAreaTop);
ScreenController()
.dispatchDeviceChanges(context, 'safeAreaBottom', safeAreaBottom);
}
}

@override
Map<String, Function> getters() {
return {
Expand Down