Skip to content

Commit 112f446

Browse files
TF-3603: Integrate Cozy features
TF-3603 [TEMP] expose Cozy integration result to console.log
1 parent 64c4369 commit 112f446

File tree

6 files changed

+175
-3
lines changed

6 files changed

+175
-3
lines changed

lib/main.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:tmail_ui_user/main/pages/app_pages.dart';
1616
import 'package:tmail_ui_user/main/routes/app_routes.dart';
1717
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
1818
import 'package:tmail_ui_user/main/utils/app_utils.dart';
19+
import 'package:tmail_ui_user/main/utils/cozy_config.dart';
1920
import 'package:url_strategy/url_strategy.dart';
2021
import 'package:worker_manager/worker_manager.dart';
2122

@@ -59,6 +60,26 @@ class _TMailAppState extends State<TMailApp> {
5960
if (PlatformInfo.isMobile) {
6061
_deepLinksManager = getBinding<DeepLinksManager>();
6162
_deepLinksManager?.registerDeepLinkStreamListener();
63+
} else {
64+
final cozyConfig = CozyConfig();
65+
66+
cozyConfig.manager.injectCozyScript().then((_) async {
67+
final isInsideCozy = await cozyConfig.manager.isInsideCozy;
68+
if (!isInsideCozy) return;
69+
70+
await cozyConfig.manager.initialize();
71+
72+
// Get contacts example
73+
cozyConfig.getCozyContacts().then((contacts) {
74+
print('Contacts: $contacts');
75+
});
76+
77+
// Get flag example
78+
cozyConfig.getCozyFeatureFlag('cozy.search.enabled').then((flag) {
79+
final isEnabled = flag == true || flag == 'true';
80+
print('Search enabled: $isEnabled');
81+
});
82+
});
6283
}
6384
}
6485

lib/main/utils/cozy_config.dart

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
import 'dart:js_interop';
4+
5+
import 'package:tmail_ui_user/main/utils/cozy_contact.dart';
6+
import 'package:tmail_ui_user/main/utils/cozy_js_interop.dart';
7+
import 'package:linagora_design_flutter/cozy_config_manager/cozy_config_manager.dart';
8+
import 'package:universal_html/js_util.dart';
9+
10+
class CozyConfig {
11+
static final CozyConfig _instance = CozyConfig._internal();
12+
13+
factory CozyConfig() {
14+
return _instance;
15+
}
16+
17+
CozyConfig._internal();
18+
19+
final manager = CozyConfigManager();
20+
21+
Future<List<CozyContact>> getCozyContacts() async {
22+
try {
23+
final contacts = await promiseToFuture(getContactsJs());
24+
return (contacts as JSArray<JSObject>)
25+
.toDart
26+
.map((contact) => CozyContact.fromJson(jsonDecode(stringify(contact))))
27+
.toList();
28+
} catch (e) {
29+
print('Error getting cozy contacts: $e');
30+
return [];
31+
}
32+
}
33+
34+
Future<dynamic> getCozyFeatureFlag(String flagName) async {
35+
try {
36+
final flag = await promiseToFuture(getFlagJs(flagName));
37+
return switch (flag) {
38+
JSBoolean() => flag.toDart,
39+
JSString() => flag.toDart,
40+
JSNumber() => flag.toDartDouble,
41+
JSObject() => stringify(flag),
42+
_ => null,
43+
};
44+
} catch (e) {
45+
print('Error getting cozy feature flag: $e');
46+
return null;
47+
}
48+
}
49+
}

lib/main/utils/cozy_contact.dart

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import 'package:equatable/equatable.dart';
2+
import 'package:json_annotation/json_annotation.dart';
3+
4+
part 'cozy_contact.g.dart';
5+
6+
@JsonSerializable(createToJson: false)
7+
class CozyContact with EquatableMixin {
8+
final String? id;
9+
10+
@JsonKey(name: '_id')
11+
final String? privateId;
12+
13+
@JsonKey(name: '_type')
14+
final String? type;
15+
16+
@JsonKey(name: '_rev')
17+
final String? rev;
18+
19+
final List<CozyAccountUrl>? cozy;
20+
final String? displayName;
21+
final List<CozyEmail>? email;
22+
final String? fullname;
23+
final CozyIndexes? indexes;
24+
final bool? me;
25+
26+
CozyContact({
27+
this.id,
28+
this.privateId,
29+
this.type,
30+
this.rev,
31+
this.cozy,
32+
this.displayName,
33+
this.email,
34+
this.fullname,
35+
this.indexes,
36+
this.me,
37+
});
38+
39+
factory CozyContact.fromJson(Map<String, dynamic> json) => _$CozyContactFromJson(json);
40+
41+
@override
42+
List<Object?> get props => [id, privateId, type, rev, cozy, displayName, email, fullname, indexes, me];
43+
44+
@override
45+
bool? get stringify => true;
46+
}
47+
48+
@JsonSerializable(createToJson: false)
49+
class CozyAccountUrl with EquatableMixin {
50+
final bool? primary;
51+
final String? url;
52+
53+
CozyAccountUrl({
54+
this.primary,
55+
this.url,
56+
});
57+
58+
factory CozyAccountUrl.fromJson(Map<String, dynamic> json) => _$CozyAccountUrlFromJson(json);
59+
60+
@override
61+
List<Object?> get props => [primary, url];
62+
}
63+
64+
@JsonSerializable(createToJson: false)
65+
class CozyEmail with EquatableMixin {
66+
final String? address;
67+
final bool? primary;
68+
69+
CozyEmail({
70+
this.address,
71+
this.primary,
72+
});
73+
74+
factory CozyEmail.fromJson(Map<String, dynamic> json) => _$CozyEmailFromJson(json);
75+
76+
@override
77+
List<Object?> get props => [address, primary];
78+
}
79+
80+
@JsonSerializable(createToJson: false)
81+
class CozyIndexes with EquatableMixin {
82+
final String? byFamilyNameGivenNameEmailCozyUrl;
83+
84+
CozyIndexes({
85+
this.byFamilyNameGivenNameEmailCozyUrl,
86+
});
87+
88+
factory CozyIndexes.fromJson(Map<String, dynamic> json) => _$CozyIndexesFromJson(json);
89+
90+
@override
91+
List<Object?> get props => [byFamilyNameGivenNameEmailCozyUrl];
92+
}

lib/main/utils/cozy_js_interop.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'dart:js_interop';
2+
3+
@JS('window._cozyBridge.getContacts')
4+
external JSPromise<JSAny?> getContactsJs();
5+
6+
@JS('window._cozyBridge.getFlag')
7+
external JSPromise<JSAny?> getFlagJs(String flagName);
8+
9+
@JS('JSON.stringify')
10+
external String stringify(JSAny? value);

pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,8 @@ packages:
13811381
dependency: "direct main"
13821382
description:
13831383
path: "."
1384-
ref: master
1385-
resolved-ref: "0f666ca14fd4f0710775ca3de03cde73b21de7d6"
1384+
ref: cozy-integration
1385+
resolved-ref: "3815684d6de2e8c5c9c069c5985214a0cab002d4"
13861386
url: "https://github.com/linagora/linagora-design-flutter.git"
13871387
source: git
13881388
version: "0.0.1"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ dependencies:
9797
linagora_design_flutter:
9898
git:
9999
url: https://github.com/linagora/linagora-design-flutter.git
100-
ref: master
100+
ref: cozy-integration
101101

102102
# TODO: Fix bug where apps cannot be launched on iOS 18. We will change it when the PR in upstream repository will be merged
103103
# https://github.com/GeekyAnts/external_app_launcher/pull/42

0 commit comments

Comments
 (0)