reducing code duplication

This commit is contained in:
Leandro Borges Ferreira
2023-02-15 19:40:51 +01:00
parent c0936f87ab
commit df98fd5d8d
3 changed files with 91 additions and 4 deletions
@@ -100,6 +100,7 @@ class _MessageActionsModalState extends State<MessageActionsModal> {
final mediaQueryData = MediaQuery.of(context); final mediaQueryData = MediaQuery.of(context);
final size = mediaQueryData.size; final size = mediaQueryData.size;
final user = StreamChat.of(context).currentUser; final user = StreamChat.of(context).currentUser;
final orientation = mediaQueryData.orientation;
final roughMaxSize = size.width * 2 / 3; final roughMaxSize = size.width * 2 / 3;
@@ -140,6 +141,7 @@ class _MessageActionsModalState extends State<MessageActionsModal> {
roughMaxSize, roughMaxSize,
fontSize, fontSize,
numberOfReactions, numberOfReactions,
orientation,
), ),
0, 0,
), ),
@@ -43,6 +43,7 @@ class StreamMessageReactionsModal extends StatelessWidget {
final size = MediaQuery.of(context).size; final size = MediaQuery.of(context).size;
final user = StreamChat.of(context).currentUser; final user = StreamChat.of(context).currentUser;
final _userPermissions = StreamChannel.of(context).channel.ownCapabilities; final _userPermissions = StreamChannel.of(context).channel.ownCapabilities;
final orientation = MediaQuery.of(context).orientation;
final hasReactionPermission = final hasReactionPermission =
_userPermissions.contains(PermissionType.sendReaction); _userPermissions.contains(PermissionType.sendReaction);
@@ -72,6 +73,7 @@ class StreamMessageReactionsModal extends StatelessWidget {
roughMaxSize, roughMaxSize,
fontSize, fontSize,
numberOfReactions, numberOfReactions,
orientation,
), ),
0, 0,
), ),
@@ -9,9 +9,8 @@ double calculateReactionsHorizontalAlignmentValue(
double maxSize, double maxSize,
double? fontSize, double? fontSize,
int reactionsCount, int reactionsCount,
Orientation orientation,
) { ) {
var result = 0.0;
final shiftFactor = reactionsCount < 5 ? (5 - reactionsCount) * 0.1 : 0.0; final shiftFactor = reactionsCount < 5 ? (5 - reactionsCount) * 0.1 : 0.0;
final maxWidth = constraints.maxWidth; final maxWidth = constraints.maxWidth;
final maxHeight = constraints.maxHeight; final maxHeight = constraints.maxHeight;
@@ -22,11 +21,42 @@ double calculateReactionsHorizontalAlignmentValue(
? 1 ? 1
: (roughSentenceSize == 0 ? 1 : (roughSentenceSize / maxSize)); : (roughSentenceSize == 0 ? 1 : (roughSentenceSize / maxSize));
if (orientation == Orientation.portrait) {
return _portraitAlign(
user,
message,
maxWidth,
maxHeight,
shiftFactor,
divFactor,
);
} else {
return _landScapeAlign(
user,
message,
maxWidth,
maxHeight,
shiftFactor,
divFactor,
);
}
}
double _portraitAlign(
User? user,
Message message,
double maxWidth,
double maxHeight,
double shiftFactor,
num divFactor,
) {
print('INFO - max width: $maxWidth. ' print('INFO - max width: $maxWidth. '
'maxHeight: $maxHeight ' 'maxHeight: $maxHeight '
'shiftFactor: $shiftFactor ' 'shiftFactor: $shiftFactor '
'divFactor: $divFactor'); 'divFactor: $divFactor');
var result = 0.0;
if (user?.id == message.user?.id) { if (user?.id == message.user?.id) {
if (divFactor >= 1.0) { if (divFactor >= 1.0) {
/* /*
@@ -49,11 +79,64 @@ double calculateReactionsHorizontalAlignmentValue(
} }
} }
print('INFO - result: $result'); print('INFO - result portrait: $result');
// Ensure reactions don't get pushed past the edge of the screen. // Ensure reactions don't get pushed past the edge of the screen.
// //
// Hacky!!! Needs improvement!!! // This happens if divFactor is really big. When this happens, we can simply
// move the model all the way to the end of screen.
if (result > 1.0) {
return 1;
} else if (result < -1.0) {
return -1;
} else {
return result;
}
}
double _landScapeAlign(
User? user,
Message message,
double maxWidth,
double maxHeight,
double shiftFactor,
num divFactor,
) {
var result = 0.0;
print('INFO - max width: $maxWidth. '
'maxHeight: $maxHeight '
'shiftFactor: $shiftFactor '
'divFactor: $divFactor');
if (user?.id == message.user?.id) {
if (divFactor >= 1.7) {
/*
This is an empiric value. This number tries to approximate all the
offset necessary for the position of reaction look the best way
possible.
*/
const constant = 3000;
result = shiftFactor - maxWidth / constant;
} else {
// Small messages, it is simpler to align then.
result = 1 - divFactor * 0.6;
}
} else {
if (divFactor >= 1.7) {
result = shiftFactor + 0.2;
} else {
result = -(1.2 - divFactor);
}
}
print('INFO - result landscape: $result');
// Ensure reactions don't get pushed past the edge of the screen.
//
// This happens if divFactor is really big. When this happens, we can simply
// move the model all the way to the end of screen.
if (result > 1.0) { if (result > 1.0) {
return 1; return 1;
} else if (result < -1.0) { } else if (result < -1.0) {