-
Notifications
You must be signed in to change notification settings - Fork 5
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
Implement Messages feature #61
Conversation
pritam-bs
commented
Jan 4, 2024
- Provided NStackMessageWidget for the user to get and show messages from NStack
- The user can use either the default dialog provided by the SDK or override the dialog and implement the UI as per the requirements.
- Provided NStackMessageWidget for the user to get and show messages from NStack - The user can use either the default dialog provided by the SDK or override the dialog and implement the UI as per the requirements.
@pritam-bs let's wait for @nivisi to review before merging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: two changes requested on the nstack.dart
must actually be on the .txt
file, jfyi 😄
example/lib/nstack.dart
Outdated
abstract class NStackMessageOptions {} | ||
|
||
class DefaultNstackMessageOptions implements NStackMessageOptions { | ||
/// Title of the OK button. | ||
final String? okButtonTitle; | ||
|
||
/// Title of the Open URL button. | ||
final String? openUrlButtonTitle; | ||
|
||
/// Title of the dialog. | ||
final String? dialogTitle; | ||
|
||
DefaultNstackMessageOptions({ | ||
this.okButtonTitle, | ||
this.openUrlButtonTitle, | ||
this.dialogTitle, | ||
}); | ||
} | ||
|
||
class CustomNstackMessageOptions implements NStackMessageOptions { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since dart 3 we have new class modifiers that allow us to put restrictions on who and how can inherit from our classes (Official docs). My suggestion would be to follow the new rules:
- Make the parent
NStackMessageOptions
asealed
class, meaning it can only be inherited within our library. It will allow us to use aswitch
statement in the_onMessage
callback. Refer to my message below. - Make two subclasses
final
to prevent subtyping outside of this NStack library.
abstract class NStackMessageOptions {} | |
class DefaultNstackMessageOptions implements NStackMessageOptions { | |
/// Title of the OK button. | |
final String? okButtonTitle; | |
/// Title of the Open URL button. | |
final String? openUrlButtonTitle; | |
/// Title of the dialog. | |
final String? dialogTitle; | |
DefaultNstackMessageOptions({ | |
this.okButtonTitle, | |
this.openUrlButtonTitle, | |
this.dialogTitle, | |
}); | |
} | |
class CustomNstackMessageOptions implements NStackMessageOptions { | |
sealed class NStackMessageOptions {} | |
final class DefaultNstackMessageOptions implements NStackMessageOptions { | |
/// Title of the OK button. | |
final String? okButtonTitle; | |
/// Title of the Open URL button. | |
final String? openUrlButtonTitle; | |
/// Title of the dialog. | |
final String? dialogTitle; | |
DefaultNstackMessageOptions({ | |
this.okButtonTitle, | |
this.openUrlButtonTitle, | |
this.dialogTitle, | |
}); | |
} | |
final class CustomNstackMessageOptions implements NStackMessageOptions { |
example/lib/nstack.dart
Outdated
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, | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we'll use a sealed
base class, we'll be able to do:
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, | |
); | |
} | |
} | |
void _onMessage(Message message) { | |
final messageOptions = widget.messageOptions; | |
switch (messageOptions) { | |
case CustomNstackMessageOptions(): | |
messageOptions.onMessage(message); | |
break; | |
case DefaultNstackMessageOptions(): | |
NStackMessageDialog.show( | |
context, | |
message: message, | |
okButtonTitle: messageOptions.okButtonTitle, | |
openUrlButtonTitle: messageOptions.openUrlButtonTitle, | |
dialogTitle: messageOptions.dialogTitle, | |
); | |
break; | |
} | |
} |
Future<void> setMessageViewed(int messageId) async { | ||
try { | ||
if (_appOpenData != null) { | ||
await _repository.postMessageSeen( | ||
appOpenData: _appOpenData!, | ||
messageId: messageId, | ||
); | ||
} else { | ||
LogUtil.log('NStack --> Could not post message seen.'); | ||
} | ||
} catch (e) { | ||
LogUtil.log('NStack --> Could not post message seen.'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future<void> setMessageViewed(int messageId) async { | |
try { | |
if (_appOpenData != null) { | |
await _repository.postMessageSeen( | |
appOpenData: _appOpenData!, | |
messageId: messageId, | |
); | |
} else { | |
LogUtil.log('NStack --> Could not post message seen.'); | |
} | |
} catch (e) { | |
LogUtil.log('NStack --> Could not post message seen.'); | |
} | |
} | |
Future<void> setMessageViewed(int messageId) async { | |
final appOpenData = _appOpenData; | |
if (appOpenData == null) { | |
LogUtil.log('NStack --> Could not post message seen.'); | |
return; | |
} | |
try { | |
await _repository.postMessageSeen( | |
appOpenData: appOpenData, | |
messageId: messageId, | |
); | |
} catch (e) { | |
LogUtil.log('NStack --> Could not post message seen.'); | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
}); | ||
|
||
/// Configuration of how the message will be handled. | ||
/// It can be either `DefaultNstackHandlerConfiguration` or `CustomNstackHandlerConfiguration`. |
There was a problem hiding this comment.
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.
/// It can be either `DefaultNstackHandlerConfiguration` or `CustomNstackHandlerConfiguration`. | |
/// It can be either [DefaultNstackHandlerConfiguration] or [CustomNstackHandlerConfiguration]. |