Skip to content

Commit

Permalink
fix(nextcloud)!: Move RichObjectParameter from spreed to core and fix…
Browse files Browse the repository at this point in the history
… rich parameter typing for broken apps

Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Sep 15, 2024
1 parent 7f81cfe commit 37b5bb5
Show file tree
Hide file tree
Showing 24 changed files with 1,776 additions and 3,080 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:neon_framework/models.dart';
import 'package:neon_framework/theme.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:talk_app/l10n/localizations.dart';
import 'package:talk_app/src/blocs/message_bloc.dart';
Expand Down Expand Up @@ -55,7 +56,7 @@ TextSpan buildChatMessage({
message = message.replaceAll('\n', ' ');
}

final unusedParameters = <String, spreed.RichObjectParameter>{};
final unusedParameters = <String, core.RichObjectParameter>{};

var parts = [message];
for (final entry in chatMessage.messageParameters.entries) {
Expand All @@ -71,7 +72,9 @@ TextSpan buildChatMessage({
}

if (!found) {
unusedParameters[entry.key] = entry.value;
unusedParameters[entry.key] = core.RichObjectParameter.fromJson(
entry.value.map((key, value) => MapEntry(key, value.toString())).toMap(),
);
}

parts = newParts;
Expand Down Expand Up @@ -111,7 +114,9 @@ TextSpan buildChatMessage({
if ('{${entry.key}}' == part) {
children.add(
buildRichObjectParameter(
parameter: entry.value,
parameter: core.RichObjectParameter.fromJson(
entry.value.map((key, value) => MapEntry(key, value.toString())).toMap(),
),
textStyle: style,
isPreview: isPreview,
),
Expand Down Expand Up @@ -157,7 +162,7 @@ TextSpan buildChatMessage({

/// Renders a rich object [parameter] to be interactive.
InlineSpan buildRichObjectParameter({
required spreed.RichObjectParameter parameter,
required core.RichObjectParameter parameter,
required TextStyle? textStyle,
required bool isPreview,
}) {
Expand All @@ -171,19 +176,19 @@ InlineSpan buildRichObjectParameter({
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: switch (parameter.type) {
spreed.RichObjectParameter_Type.user ||
spreed.RichObjectParameter_Type.call ||
spreed.RichObjectParameter_Type.guest ||
spreed.RichObjectParameter_Type.userGroup =>
core.RichObjectParameter_Type.user ||
core.RichObjectParameter_Type.call ||
core.RichObjectParameter_Type.guest ||
core.RichObjectParameter_Type.userGroup =>
TalkRichObjectMention(
parameter: parameter,
textStyle: textStyle,
),
spreed.RichObjectParameter_Type.file => TalkRichObjectFile(
core.RichObjectParameter_Type.file => TalkRichObjectFile(
parameter: parameter,
textStyle: textStyle,
),
spreed.RichObjectParameter_Type.deckCard => TalkRichObjectDeckCard(
core.RichObjectParameter_Type.deckCard => TalkRichObjectDeckCard(
parameter: parameter,
),
_ => TalkRichObjectFallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/core.dart' as core;

/// Widget to display a Deck card from a rich object.
class TalkRichObjectDeckCard extends StatelessWidget {
Expand All @@ -13,7 +13,7 @@ class TalkRichObjectDeckCard extends StatelessWidget {
});

/// The parameter to display.
final spreed.RichObjectParameter parameter;
final core.RichObjectParameter parameter;

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/core.dart' as core;

/// Widget used to render rich object parameters with unknown types.
class TalkRichObjectFallback extends StatelessWidget {
Expand All @@ -14,7 +14,7 @@ class TalkRichObjectFallback extends StatelessWidget {
});

/// The parameter to display.
final spreed.RichObjectParameter parameter;
final core.RichObjectParameter parameter;

/// The TextStyle to applied to all text elements in this rich object.
final TextStyle? textStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:neon_framework/models.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/spreed.dart' as spreed;

/// Displays a file from a rich object.
class TalkRichObjectFile extends StatelessWidget {
Expand All @@ -15,13 +14,11 @@ class TalkRichObjectFile extends StatelessWidget {
});

/// The parameter to display.
final spreed.RichObjectParameter parameter;
final core.RichObjectParameter parameter;

/// The TextStyle to applied to all text elements in this rich object.
final TextStyle? textStyle;

int _parseDimension(({int? $int, String? string}) dimension) => dimension.$int ?? int.parse(dimension.string!);

@override
Widget build(BuildContext context) {
final child = LayoutBuilder(
Expand All @@ -35,8 +32,8 @@ class TalkRichObjectFile extends StatelessWidget {

if (parameter.width != null && parameter.height != null) {
deviceSize = Size(
_parseDimension(parameter.width!).toDouble(),
_parseDimension(parameter.height!).toDouble(),
int.parse(parameter.width!).toDouble(),
int.parse(parameter.height!).toDouble(),
);

// Convert to logical pixels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:neon_framework/models.dart';
import 'package:neon_framework/theme.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/core.dart' as core;

/// Displays a mention chip from a rich object.
class TalkRichObjectMention extends StatelessWidget {
Expand All @@ -17,7 +17,7 @@ class TalkRichObjectMention extends StatelessWidget {
});

/// The parameter to display.
final spreed.RichObjectParameter parameter;
final core.RichObjectParameter parameter;

/// The TextStyle to applied to all text elements in this rich object.
final TextStyle? textStyle;
Expand All @@ -28,7 +28,7 @@ class TalkRichObjectMention extends StatelessWidget {
final bool highlight;

switch (parameter.type) {
case spreed.RichObjectParameter_Type.user:
case core.RichObjectParameter_Type.user:
final account = NeonProvider.of<Account>(context);

highlight = account.username == parameter.id;
Expand All @@ -37,7 +37,7 @@ class TalkRichObjectMention extends StatelessWidget {
account: NeonProvider.of<Account>(context),
userStatusBloc: null,
);
case spreed.RichObjectParameter_Type.call:
case core.RichObjectParameter_Type.call:
highlight = true;
child = CircleAvatar(
child: ClipOval(
Expand All @@ -47,13 +47,13 @@ class TalkRichObjectMention extends StatelessWidget {
),
),
);
case spreed.RichObjectParameter_Type.guest:
case core.RichObjectParameter_Type.guest:
// TODO: Add highlighting when the mention is about the current guest user.
highlight = false;
child = CircleAvatar(
child: Icon(AdaptiveIcons.person),
);
case spreed.RichObjectParameter_Type.userGroup:
case core.RichObjectParameter_Type.userGroup:
final userDetailsBloc = NeonProvider.of<UserDetailsBloc>(context);
final groups = userDetailsBloc.userDetails.valueOrNull?.data?.groups ?? BuiltList();

Expand Down
1 change: 1 addition & 0 deletions packages/neon_framework/packages/talk_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:

dev_dependencies:
build_runner: ^2.4.12
built_value: ^8.9.2
custom_lint: ^0.6.5
flutter_keyboard_visibility: ^6.0.0
flutter_test:
Expand Down
67 changes: 32 additions & 35 deletions packages/neon_framework/packages/talk_app/test/message_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:built_collection/built_collection.dart';
import 'package:built_value/json_object.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -1520,10 +1521,10 @@ void main() {
group(isPreview ? 'As preview' : 'Complete', () {
group('Mention', () {
for (final type in [
spreed.RichObjectParameter_Type.user,
spreed.RichObjectParameter_Type.call,
spreed.RichObjectParameter_Type.guest,
spreed.RichObjectParameter_Type.userGroup,
core.RichObjectParameter_Type.user,
core.RichObjectParameter_Type.call,
core.RichObjectParameter_Type.guest,
core.RichObjectParameter_Type.userGroup,
]) {
testWidgets(type.value, (tester) async {
final userDetails = MockUserDetails();
Expand All @@ -1543,7 +1544,7 @@ void main() {
],
child: RichText(
text: buildRichObjectParameter(
parameter: spreed.RichObjectParameter(
parameter: core.RichObjectParameter(
(b) => b
..type = type
..id = ''
Expand Down Expand Up @@ -1573,9 +1574,9 @@ void main() {
],
child: RichText(
text: buildRichObjectParameter(
parameter: spreed.RichObjectParameter(
parameter: core.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.file
..type = core.RichObjectParameter_Type.file
..id = '0'
..name = 'name',
),
Expand All @@ -1595,9 +1596,9 @@ void main() {
TestApp(
child: RichText(
text: buildRichObjectParameter(
parameter: spreed.RichObjectParameter(
parameter: core.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.deckCard
..type = core.RichObjectParameter_Type.deckCard
..id = ''
..name = 'name'
..boardname = 'boardname'
Expand All @@ -1619,9 +1620,9 @@ void main() {
TestApp(
child: RichText(
text: buildRichObjectParameter(
parameter: spreed.RichObjectParameter(
parameter: core.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.addressbook
..type = core.RichObjectParameter_Type.addressbook
..id = ''
..name = 'name',
),
Expand Down Expand Up @@ -1671,12 +1672,11 @@ void main() {
when(() => chatMessage.message).thenReturn('test');
when(() => chatMessage.messageParameters).thenReturn(
BuiltMap({
type: spreed.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.user
..id = ''
..name = '',
),
type: BuiltMap<String, JsonObject>({
'type': JsonObject('user'),
'id': JsonObject(''),
'name': JsonObject(''),
}),
}),
);

Expand All @@ -1696,12 +1696,11 @@ void main() {
when(() => chatMessage.message).thenReturn('test');
when(() => chatMessage.messageParameters).thenReturn(
BuiltMap({
'file': spreed.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.file
..id = ''
..name = '',
),
'file': BuiltMap<String, JsonObject>({
'type': JsonObject('file'),
'id': JsonObject(''),
'name': JsonObject(''),
}),
}),
);

Expand All @@ -1723,18 +1722,16 @@ void main() {
when(() => chatMessage.message).thenReturn('123 {actor1} 456 {actor2} 789');
when(() => chatMessage.messageParameters).thenReturn(
BuiltMap({
'actor1': spreed.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.user
..id = ''
..name = '',
),
'actor2': spreed.RichObjectParameter(
(b) => b
..type = spreed.RichObjectParameter_Type.user
..id = ''
..name = '',
),
'actor1': BuiltMap<String, JsonObject>({
'type': JsonObject('user'),
'id': JsonObject(''),
'name': JsonObject(''),
}),
'actor2': BuiltMap<String, JsonObject>({
'type': JsonObject('user'),
'id': JsonObject(''),
'name': JsonObject(''),
}),
}),
);

Expand Down
Loading

0 comments on commit 37b5bb5

Please sign in to comment.