-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Message holder with notifications (#1)
* 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
Showing
9 changed files
with
225 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |