Skip to content

Implement Messages feature #61

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

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Absorb review changes
  • Loading branch information
pritam-bs committed Jan 25, 2024
commit 8080fca6375a0dac5bd999a016bc783daa1dd89f
10 changes: 5 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ class MainScreen extends StatelessWidget {
// print(message.message);
// }
// }
//
// final customNstackMessageOptions =
// CustomNstackMessageOptions(onMessage: onMessage);

// final customNstackHandlerConfiguration =
// CustomNstackHandlerConfiguration(onMessage: onMessage);

// Message option for showing default dialog.
final defaultNstackMessageOptions = DefaultNstackMessageOptions(
final defaultHandlerConfiguration = DefaultNstackHandlerConfiguration(
okButtonTitle: localizationAsset.test.okButtonTitle,
openUrlButtonTitle: localizationAsset.test.openUrlButtonTitle,
dialogTitle: localizationAsset.test.dialogTitle,
);

return NStackMessageWidget(
messageOptions: defaultNstackMessageOptions,
handlerConfiguration: defaultHandlerConfiguration,
child: Scaffold(
appBar: AppBar(
title: Text(localizationAsset.test.testDollarSign),
Expand Down
63 changes: 38 additions & 25 deletions example/lib/nstack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
* `context.localization.assets.yourSection.yourKey`.
*
* 💬 MESSAGES
*
* To use messages, you can use `NStackMessageWidget`,
* and if you want the default dialog, use `DefaultNstackMessageOptions`.
* Or you can use `CustomNstackMessageOptions` to get the `Message` object from the NStack.
*
* Use `NStackMessageWidget` to access the Messages feature.
*
* [NStackMessageWidget.handlerConfiguration] allows you to configure how incoming messages are handled.
* - Use `DefaultNstackHandlerConfiguration` if you want to display a default adaptive dialog
* - Or use `CustomNstackHandlerConfiguration` if you want to handle the message yourself.
* it has the `onMessage` callback that provides you with the received `Message` object.
*
* 🛠️ IMPORTANT NOTES FOR SDK USERS
*
Expand Down Expand Up @@ -256,9 +259,16 @@ class NStackState extends State<NStackWidget> {
}
}

abstract class NStackMessageOptions {}
/*
*
* NStack Message
*
*/

sealed class NStackHandlerConfiguration {}

class DefaultNstackMessageOptions implements NStackMessageOptions {
final class DefaultNstackHandlerConfiguration
implements NStackHandlerConfiguration {
/// Title of the OK button.
final String? okButtonTitle;

Expand All @@ -268,30 +278,33 @@ class DefaultNstackMessageOptions implements NStackMessageOptions {
/// Title of the dialog.
final String? dialogTitle;

DefaultNstackMessageOptions({
DefaultNstackHandlerConfiguration({
this.okButtonTitle,
this.openUrlButtonTitle,
this.dialogTitle,
});
}

class CustomNstackMessageOptions implements NStackMessageOptions {
final class CustomNstackHandlerConfiguration
implements NStackHandlerConfiguration {
/// Callback to customize the message UI.
final void Function(Message message) onMessage;

CustomNstackMessageOptions({
CustomNstackHandlerConfiguration({
required this.onMessage,
});
}

class NStackMessageWidget extends StatefulWidget {
const NStackMessageWidget({
super.key,
required this.messageOptions,
required this.handlerConfiguration,
this.child,
});

final NStackMessageOptions messageOptions;
/// Configuration of how the message will be handled.
/// It can be either `DefaultNstackHandlerConfiguration` or `CustomNstackHandlerConfiguration`.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we use square brackets, you'll be able to navigate to the references classes (basically anything referenced: methods, fields etc) easier by holding the CMND (or whatever) button.

Suggested change
/// It can be either `DefaultNstackHandlerConfiguration` or `CustomNstackHandlerConfiguration`.
/// It can be either [DefaultNstackHandlerConfiguration] or [CustomNstackHandlerConfiguration].

final NStackHandlerConfiguration handlerConfiguration;

final Widget? child;

Expand All @@ -318,20 +331,20 @@ class _NStackMessageWidgetSate extends State<NStackMessageWidget> {
}

void _onMessage(Message message) {
if (widget.messageOptions is CustomNstackMessageOptions) {
(widget.messageOptions as CustomNstackMessageOptions)
.onMessage
.call(message);
} else {
final messageOptions =
widget.messageOptions as DefaultNstackMessageOptions;
NStackMessageDialog.show(
context,
message: message,
okButtonTitle: messageOptions.okButtonTitle,
openUrlButtonTitle: messageOptions.openUrlButtonTitle,
dialogTitle: messageOptions.dialogTitle,
);
final messageOptions = widget.handlerConfiguration;
switch (messageOptions) {
case CustomNstackHandlerConfiguration():
messageOptions.onMessage(message);
break;
case DefaultNstackHandlerConfiguration():
NStackMessageDialog.show(
context,
message: message,
okButtonTitle: messageOptions.okButtonTitle,
openUrlButtonTitle: messageOptions.openUrlButtonTitle,
dialogTitle: messageOptions.dialogTitle,
);
break;
}
}

Expand Down
19 changes: 11 additions & 8 deletions lib/sdk/messages/nstack_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ class NStackMessages {
}) : _repository = repository;

Future<void> setMessageViewed(int messageId) async {
final appOpenData = _appOpenData;

if (appOpenData == null) {
LogUtil.log('NStack --> Could not post message seen.');
return;
}

try {
if (_appOpenData != null) {
await _repository.postMessageSeen(
appOpenData: _appOpenData!,
messageId: messageId,
);
} else {
LogUtil.log('NStack --> Could not post message seen.');
}
await _repository.postMessageSeen(
appOpenData: appOpenData,
messageId: messageId,
);
} catch (e) {
LogUtil.log('NStack --> Could not post message seen.');
}
Expand Down
63 changes: 38 additions & 25 deletions lib/templates/nstack_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
* `context.localization.assets.yourSection.yourKey`.
*
* 💬 MESSAGES
*
* To use messages, you can use `NStackMessageWidget`,
* and if you want the default dialog, use `DefaultNstackMessageOptions`.
* Or you can use `CustomNstackMessageOptions` to get the `Message` object from the NStack.
*
* Use `NStackMessageWidget` to access the Messages feature.
*
* [NStackMessageWidget.handlerConfiguration] allows you to configure how incoming messages are handled.
* - Use `DefaultNstackHandlerConfiguration` if you want to display a default adaptive dialog
* - Or use `CustomNstackHandlerConfiguration` if you want to handle the message yourself.
* it has the `onMessage` callback that provides you with the received `Message` object.
*
* 🛠️ IMPORTANT NOTES FOR SDK USERS
*
Expand Down Expand Up @@ -206,9 +209,16 @@ class NStackState extends State<NStackWidget> {
}
}

abstract class NStackMessageOptions {}
/*
*
* NStack Message
*
*/

sealed class NStackHandlerConfiguration {}

class DefaultNstackMessageOptions implements NStackMessageOptions {
final class DefaultNstackHandlerConfiguration
implements NStackHandlerConfiguration {
/// Title of the OK button.
final String? okButtonTitle;

Expand All @@ -218,30 +228,33 @@ class DefaultNstackMessageOptions implements NStackMessageOptions {
/// Title of the dialog.
final String? dialogTitle;

DefaultNstackMessageOptions({
DefaultNstackHandlerConfiguration({
this.okButtonTitle,
this.openUrlButtonTitle,
this.dialogTitle,
});
}

class CustomNstackMessageOptions implements NStackMessageOptions {
final class CustomNstackHandlerConfiguration
implements NStackHandlerConfiguration {
/// Callback to customize the message UI.
final void Function(Message message) onMessage;

CustomNstackMessageOptions({
CustomNstackHandlerConfiguration({
required this.onMessage,
});
}

class NStackMessageWidget extends StatefulWidget {
const NStackMessageWidget({
super.key,
required this.messageOptions,
required this.handlerConfiguration,
this.child,
});

final NStackMessageOptions messageOptions;
/// Configuration of how the message will be handled.
/// It can be either `DefaultNstackHandlerConfiguration` or `CustomNstackHandlerConfiguration`.
final NStackHandlerConfiguration handlerConfiguration;

final Widget? child;

Expand All @@ -268,20 +281,20 @@ class _NStackMessageWidgetSate extends State<NStackMessageWidget> {
}

void _onMessage(Message message) {
if (widget.messageOptions is CustomNstackMessageOptions) {
(widget.messageOptions as CustomNstackMessageOptions)
.onMessage
.call(message);
} else {
final messageOptions =
widget.messageOptions as DefaultNstackMessageOptions;
NStackMessageDialog.show(
context,
message: message,
okButtonTitle: messageOptions.okButtonTitle,
openUrlButtonTitle: messageOptions.openUrlButtonTitle,
dialogTitle: messageOptions.dialogTitle,
);
final messageOptions = widget.handlerConfiguration;
switch (messageOptions) {
case CustomNstackHandlerConfiguration():
messageOptions.onMessage(message);
break;
case DefaultNstackHandlerConfiguration():
NStackMessageDialog.show(
context,
message: message,
okButtonTitle: messageOptions.okButtonTitle,
openUrlButtonTitle: messageOptions.openUrlButtonTitle,
dialogTitle: messageOptions.dialogTitle,
);
break;
}
}

Expand Down