Skip to content

Commit

Permalink
feat:Message holder with notifications (#1)
Browse files Browse the repository at this point in the history
* Adds CopperframeMessagesHolder

* Adds unit tests for messages holder

* Update example and readme

* Adds image hero

* Update sdk for github

* Uses flutter github action
  • Loading branch information
olih authored Oct 15, 2024
1 parent 37d4a7d commit c4092a0
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 76 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ jobs:
os:
- ubuntu-latest
- macos-latest
- windows-latest
sdk:
- 3.1
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@v1
- name: Clone repository
uses: actions/checkout@v4
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
sdk: ${{ matrix.sdk }}
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart test
channel: stable
flutter-version-file: pubspec.yaml
- run: flutter pub get
- run: flutter test
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@

> Delivering impactful messages to your dashboard
Delivering impactful messages to your dashboard
Manages a list of message objects and notifies listeners when the message
list changes. This is useful for maintaining reactive state in Flutter
applications, ensuring UI components can respond to message updates
efficiently.

![Hero image for message\_copperframe](doc/message_copperframe.jpeg)

Highlights:

- The slot can be described by a title, description, prominence or status
- Every time the slot is modified a notification is sent
- A registry allow to modify multiple slots at once
- Reactive Updates which notifies listeners when the message list
changes, enabling efficient UI updates.

- Immutable State Checks that compares old and new messages to avoid
unnecessary notifications.

- Easy Integration which works seamlessly with Flutter's ChangeNotifier
for state management.

A few examples:

Create a new slot class:
Create a message holder:

```dart
final CopperframeMessagesHolder holder = CopperframeMessagesHolder();
```

Create a message:

```dart
final infoMessage = CopperframeMessage( label: 'This is an info
message', level: CopperframeMessageLevel.info, category: 'usage', );
```

Update the message holder and notify:

```dart
class InfoMessageSlot extends CopperframeSlotBase { InfoMessageSlot({
required super.tags,});}
holder.messages = [infoMessage]
```

## Documentation and links
Expand Down
19 changes: 12 additions & 7 deletions baldrick-broth.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
model:
project:
title: Delivering impactful messages to your dashboard
description: Delivering impactful messages to your dashboard
description: Manages a list of message objects and notifies listeners when the message list changes. This is useful for maintaining reactive state in Flutter applications, ensuring UI components can respond to message updates efficiently.
version: 0.3.0
keywords:
- message
- panel
- state management
- ChangeNotifier
readme:
highlights:
- The slot can be described by a title, description, prominence or status
- Every time the slot is modified a notification is sent
- A registry allow to modify multiple slots at once
- Reactive Updates which notifies listeners when the message list changes, enabling efficient UI updates.
- Immutable State Checks that compares old and new messages to avoid unnecessary notifications.
- Easy Integration which works seamlessly with Flutter's ChangeNotifier for state management.
links:
- "[Dart dependencies](DEPENDENCIES.md)"
- "[Usage](USAGE.md)"
Expand All @@ -21,8 +22,12 @@ model:
- image-hero
cheatsheetFormat: dart
cheatsheet:
- title: Create a new slot class
example: class InfoMessageSlot extends CopperframeSlotBase { InfoMessageSlot({ required super.tags,});}
- title: Create a message holder
example: "final CopperframeMessagesHolder holder = CopperframeMessagesHolder();"
- title: Create a message
example: "final infoMessage = CopperframeMessage( label: 'This is an info message', level: CopperframeMessageLevel.info, category: 'usage', );"
- title: Update the message holder and notify
example: "holder.messages = [infoMessage]"

github:
account: flarebyte
Expand Down
Binary file added doc/message_copperframe.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:grand_copperframe/grand_copperframe.dart';
import 'package:message_copperframe/message_copperframe.dart';

void main() {
final CopperframeMessagesHolder holder = CopperframeMessagesHolder();
holder.messages = [
CopperframeMessage(
label: 'Blue message',
level: CopperframeMessageLevel.info,
category: 'usage',
),
CopperframeMessage(
label: 'Green message',
level: CopperframeMessageLevel.info,
category: 'usage',
),
];
}
6 changes: 1 addition & 5 deletions lib/message_copperframe.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
library message_copperframe;

/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(int value) => value + 1;
}
export 'src/messages_holder.dart';
70 changes: 70 additions & 0 deletions lib/src/messages_holder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'package:flutter/foundation.dart';
import 'package:grand_copperframe/grand_copperframe.dart';

/// A class that holds and manages a list of [CopperframeMessage] objects.
///
/// The class extends [ChangeNotifier] to notify listeners when the list of
/// messages changes. The messages can be retrieved or updated, and listeners
/// will be notified of any changes to the list.
class CopperframeMessagesHolder with ChangeNotifier {
/// Private backing field for the list of [CopperframeMessage] instances.
List<CopperframeMessage> _messages = [];

/// Default constructor for creating an instance of [CopperframeMessagesHolder].
CopperframeMessagesHolder();

/// Updates the list of messages.
///
/// If the new list of messages is different from the current list,
/// the internal list is updated, and listeners are notified.
///
/// ```dart
/// holder.messages = newMessages;
/// ```
///
/// Use this setter to replace the entire message list and trigger
/// notifications for any change.
set messages(List<CopperframeMessage> newMessages) {
if (_haveMessagesChanged(newMessages)) {
_messages = newMessages;
notifyListeners();
}
}

/// Returns the current list of [CopperframeMessage] objects.
///
/// This getter allows accessing the messages stored within this holder.
///
/// ```dart
/// List<CopperframeMessage> currentMessages = holder.messages;
/// ```
List<CopperframeMessage> get messages {
return _messages;
}

/// Checks if the provided list of [CopperframeMessage] objects differs from
/// the current list.
///
/// Returns `true` if the length of the lists differ or if any individual
/// message has changed. Returns `false` otherwise.
///
/// This is a helper method used internally to avoid unnecessary updates
/// when the messages have not changed.
bool _haveMessagesChanged(List<CopperframeMessage> newMessages) {
if (newMessages.length != _messages.length) {
return true;
}
if (_messages.isEmpty) {
return false;
}

for (var i = 0; i < _messages.length; i++) {
final hasChanged = _messages[i] != newMessages[i];
if (hasChanged) {
return true;
}
}

return false;
}
}
50 changes: 9 additions & 41 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
name: message_copperframe
description: "A new Flutter package project."
version: 0.0.1
homepage:
description: "Delivering impactful messages to your dashboard"
version: 0.3.0
homepage: https://github.com/flarebyte/message_copperframe

environment:
sdk: ^3.5.0
flutter: ">=1.17.0"
sdk: ^3.4.4
flutter: "3.24.0"

dependencies:
flutter:
sdk: flutter
grand_copperframe:
git:
url: https://github.com/flarebyte/grand_copperframe.git
ref: v0.3.0

dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

# To add assets to your package, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
#
# For details regarding assets in packages, see
# https://flutter.dev/to/asset-from-package
#
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/to/resolution-aware-images

# To add custom fonts to your package, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts in packages, see
# https://flutter.dev/to/font-from-package
86 changes: 80 additions & 6 deletions test/message_copperframe_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,86 @@
import 'package:flutter_test/flutter_test.dart';

import 'package:grand_copperframe/grand_copperframe.dart';
import 'package:message_copperframe/message_copperframe.dart';

void main() {
test('adds one to input values', () {
final calculator = Calculator();
expect(calculator.addOne(2), 3);
expect(calculator.addOne(-7), -6);
expect(calculator.addOne(0), 1);
late CopperframeMessagesHolder holder;
late List<CopperframeMessage> initialMessages;

setUp(() {
holder = CopperframeMessagesHolder();
initialMessages = [
CopperframeMessage(
label: 'Blue message',
level: CopperframeMessageLevel.info,
category: 'usage',
),
CopperframeMessage(
label: 'Green message',
level: CopperframeMessageLevel.info,
category: 'usage',
),
];
});

test('Initial state should have an empty message list', () {
expect(holder.messages, isEmpty);
});

test('Setting new messages should update the message list', () {
holder.messages = initialMessages;
expect(holder.messages, initialMessages);
});

test('Setting the same messages should not notify listeners', () {
var notificationCalled = false;
holder.addListener(() {
notificationCalled = true;
});

holder.messages = initialMessages;
notificationCalled = false;

holder.messages = initialMessages;

expect(notificationCalled, isFalse);
});

test('Setting a different number of messages should notify listeners', () {
var notificationCalled = false;
holder.addListener(() {
notificationCalled = true;
});

// Act
holder.messages = initialMessages;
holder.messages = [
CopperframeMessage(
label: 'Red message',
level: CopperframeMessageLevel.info,
category: 'usage',
),
];

expect(notificationCalled, isTrue);
});

test('Setting different messages should notify listeners', () {
var notificationCalled = false;
holder.addListener(() {
notificationCalled = true;
});

// Act
holder.messages = initialMessages;
holder.messages = [
initialMessages[0],
CopperframeMessage(
label: 'Red message',
level: CopperframeMessageLevel.info,
category: 'usage',
),
];

expect(notificationCalled, isTrue);
});
}

0 comments on commit c4092a0

Please sign in to comment.