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

feat: Added the updated base structure for handling the animations #1012

Merged
merged 13 commits into from
Sep 29, 2024

Conversation

Jhalakupadhyay
Copy link
Contributor

@Jhalakupadhyay Jhalakupadhyay commented Sep 22, 2024

Summary by Sourcery

Add new utility functions for byte and binary data conversion, introduce a new provider for managing badge animations and drawing, and refactor existing components to integrate with the new provider structure. This update enhances the animation handling capabilities and improves the modularity of the badge rendering system.

New Features:

  • Introduce a new utility function to convert byte arrays to binary arrays and binary strings to 2D lists, enhancing data manipulation capabilities.
  • Add a new badgeAnimation function in the Converters class to handle badge animations based on message input.
  • Implement a new DrawBadgeProvider class to manage badge drawing and animation states, replacing the previous DrawBadgeProvider implementation.

Enhancements:

  • Refactor the BadgeWidget to accept a grid parameter, allowing for more flexible badge rendering.
  • Update the HomeScreen to initialize and manage animations using the new DrawBadgeProvider.

Chores:

  • Replace the drawbadge_provider.dart with badgeview_provider.dart across multiple files to align with the new provider structure.

Jhalakupadhyay and others added 6 commits September 8, 2024 01:42
…badge.

feat: Added the modes support for left right top down fixed and animation support for flash and marquee.

animation right
…badge.

feat: Added the modes support for left right top down fixed and animation support for flash and marquee.

animation right

feat:Added the base structure for the animations with speed controll.
Copy link
Contributor

sourcery-ai bot commented Sep 22, 2024

Reviewer's Guide by Sourcery

This pull request implements a new base structure for handling animations in the BadgeMagic app. It introduces new utility functions for converting data formats, updates the UI to support animations, and adds new providers and abstract classes for managing badge animations and effects.

File-Level Changes

Change Details Files
Added new utility functions for data conversion
  • Implemented byteArrayToBinaryArray function
  • Added hexToBin function
  • Created binaryStringTo2DList function
lib/bademagic_module/utils/byte_array_utils.dart
Updated HomeScreen to support animations
  • Added textNotifier and textfieldLength variables
  • Implemented _controllerListner function
  • Updated initState to initialize animation
lib/view/homescreen.dart
Added badgeAnimation function to Converters class
  • Implemented badgeAnimation function to convert message to binary grid
  • Updated messageTohex function
lib/bademagic_module/utils/converters.dart
Added animation speed constants and calculation function
  • Defined constants for animation speeds
  • Implemented aniSpeedStrategy function
lib/constants.dart
Refactored BadgeWidget to use grid prop
  • Updated BadgeWidget to accept grid as a prop
  • Modified _BMBadgeState to use Provider for grid data
lib/virtualbadge/widgets/badge_widget.dart
lib/virtualbadge/view/draw_badge.dart
Created new BadgeViewProvider
  • Implemented DrawBadgeProvider class
  • Added functions for managing grid state and animations
lib/providers/badgeview_provider.dart
Added abstract classes for animations and effects
  • Created BadgeAnimation abstract class
  • Implemented BadgeEffect abstract class
lib/badge_animation/animation_abstract.dart
lib/badge_effect/badgeeffectabstract.dart

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Jhalakupadhyay - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding more inline comments to explain complex logic, particularly in new utility functions like binaryStringTo2DList.
  • Review the debug logging statements (e.g., logger.d) and consider removing them or implementing a way to disable them in production builds.
Here's what I looked at during the review
  • 🟡 General issues: 5 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

binaryArray[rowIndex].addAll(binaryRepresentation);

rowIndex = (rowIndex + 1) % 11;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Simplify nested loop structure in binaryStringTo2DList

The current nested loop structure is complex and hard to follow. Consider simplifying it by using integer division and modulo operations to determine the row and column for each bit.

List<List<int>> binary2DList = List.generate(maxHeight, (_) => []);

for (int i = 0; i < binaryString.length; i++) {
  int row = i ~/ 8;
  int col = i % 8;
  if (row < maxHeight) {
    binary2DList[row].add(int.parse(binaryString[i]));
  }
}

@@ -26,6 +29,7 @@ class HomeScreen extends StatefulWidget {
}

class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
final ValueNotifier<String> textNotifier = ValueNotifier<String>('');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Remove unused textNotifier

This ValueNotifier is declared but never used. Consider removing it if it's not needed for future functionality.

Suggested change
final ValueNotifier<String> textNotifier = ValueNotifier<String>('');
// Remove the following line:
// final ValueNotifier<String> textNotifier = ValueNotifier<String>('');

Comment on lines +57 to +64
void _controllerListner() {
logger
.d('Controller Listener : ${inlineImageProvider.getController().text}');
converters.badgeAnimation(inlineImageProvider.getController().text.isEmpty
? ""
: inlineImageProvider.getController().text);
inlineImageProvider.controllerListener();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Optimize badge animation updates

The badgeAnimation call on every text change could be inefficient for longer inputs. Consider debouncing this operation or updating only when the text input is complete.

void _controllerListener() {
  final text = inlineImageProvider.getController().text;
  logger.d('Controller Listener: $text');

  if (_debounce?.isActive ?? false) _debounce!.cancel();
  _debounce = Timer(const Duration(milliseconds: 300), () {
    converters.badgeAnimation(text.isEmpty ? "" : text);
  });

  inlineImageProvider.controllerListener();
}

Comment on lines +25 to +29
int aniSpeedStrategy(int speedLevel) {
int speedInMicroseconds = aniBaseSpeed.inMicroseconds -
(speedLevel * aniBaseSpeed.inMicroseconds ~/ 8);
return speedInMicroseconds;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Clarify animation speed calculation

The speed calculation seems arbitrary. Consider adding a comment explaining the logic behind this calculation or explore a more nuanced approach that might provide better control over animation speeds.

Suggested change
int aniSpeedStrategy(int speedLevel) {
int speedInMicroseconds = aniBaseSpeed.inMicroseconds -
(speedLevel * aniBaseSpeed.inMicroseconds ~/ 8);
return speedInMicroseconds;
}
int aniSpeedStrategy(int speedLevel) {
final maxSpeedReduction = aniBaseSpeed.inMicroseconds * 7 ~/ 8;
final speedReduction = (speedLevel * maxSpeedReduction) ~/ 8;
return aniBaseSpeed.inMicroseconds - speedReduction;
}

Comment on lines +92 to +101
Map<int, BadgeAnimation?> animationMap = {
// 0: LeftAnimation(),
// 1: RightAnimation(),
// 2: UpAnimation(),
// 3: DownAnimation(),
// 4: FixedAnimation(),
// 5: SnowFlakeAnimation(),
// 6: PictureAnimation(),
// 7: AniAnimation(),
// 8: LaserAnimation(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Implement missing animations

The animation map is currently empty and commented out. This suggests that the animation system is not fully implemented, which could lead to runtime errors or unexpected behavior. Prioritize implementing these animations or provide a default fallback.

@Jhalakupadhyay Jhalakupadhyay removed the request for review from adityastic September 22, 2024 09:40
Copy link
Collaborator

@adityastic adityastic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comments from sourcery to look at

@@ -28,3 +28,56 @@ List<int> hexStringToByteArray(String hexString) {
logger.d(data.length);
return data;
}

List<List<int>> byteArrayToBinaryArray(List<int> byteArray) {
List<List<int>> binaryArray = List.generate(11, (_) => []);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use badge height

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

List.generate(11, (i) => List.generate(44, (j) => 0));
badgeList.setNewGrid(image);
} else {
List<String> hexStrings = await messageTohex(message);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid these 3 operations and decide a scheme to store messages to one format which can be used across the app. List<List> would be a good candidate. It does not make sense to have these translations for every step when we could built our logic just on one kind of data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in order to get the bitmap (List<List>) we would first need to have the hexstring and then convert it to bitmap.
Can you clarify what you mean by avoiding the operations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hex String -> Byte Array, Byte Array -> Binary Array, Binary Array -> Bool Array. Why not Hex String -> Bool Array

Copy link

@JhalakUpadhyay7 JhalakUpadhyay7 Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not seen this conversion so far is it possible?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give me some time I will look into it and if I can make it then it will be very efficient.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're trying to minimize the number of conversions. HexString to something you can work off of everywhere. Maybe 2 conversions is fine but not more

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So HexString -> ByteArray is fine. Then maybe ByteArray -> Bool Array when needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a point in holding up 4 bytes of memory whereas you can get away with just a bit

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction, flutter uses 8 bytes int so more memory usage for no reason

Comment on lines +12 to +18
List<List<bool>> homeViewGrid =
List.generate(11, (i) => List.generate(44, (j) => false));

//List that contains the state of each cell of the badge for draw view
List<List<bool>> drawViewGrid =
List.generate(11, (i) => List.generate(44, (j) => false));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be cleaned. 2 seperate widgets for draw and display seperately

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this I will clear in seperate PR where I will add separate custom paint class for the drawbadge

@Jhalakupadhyay Jhalakupadhyay force-pushed the flutter_app branch 2 times, most recently from ae0b572 to 6f16c2a Compare September 28, 2024 19:20
@adityastic adityastic merged commit 461c414 into fossasia:flutter_app Sep 29, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants