Skip to content

Commit

Permalink
Merge #129: Data into InlineMenu from devsdocs/main
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal committed Jul 13, 2023
2 parents f0b421d + 5ab0fea commit fcee1d8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
15 changes: 10 additions & 5 deletions lib/src/televerse/markups/inline_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ part of televerse;
class InlineMenu
implements
InlineKeyboardMarkup,
TeleverseMenu<CallbackQueryContext, CallbackQueryHandler> {
TeleverseMenu<CallbackQueryContext, CallbackQueryHandler,
InlineMenuData> {
/// Name of the menu
@override
String name;

/// Map that represents the text and action to be done
@override
List<Map<String, CallbackQueryHandler>> actions;
List<Map<InlineMenuData, CallbackQueryHandler>> actions;

/// Constructs a InlineMenu
InlineMenu({
List<Map<String, CallbackQueryHandler>>? actions,
List<Map<InlineMenuData, CallbackQueryHandler>>? actions,
String? name,
}) : actions = actions ?? [{}],
inlineKeyboard = TeleverseMenu._makeInlineKeyboard(actions),
Expand All @@ -30,9 +31,13 @@ class InlineMenu
}

/// Add new item to the last row
InlineMenu text(String text, CallbackQueryHandler handler) {
InlineMenu text(
String text,
CallbackQueryHandler handler, {
String? data,
}) {
if (actions.isEmpty) actions.add({});
actions.last.addAll({text: handler});
actions.last.addAll({InlineMenuData(text, data): handler});
inlineKeyboard = TeleverseMenu._makeInlineKeyboard(actions);
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/televerse/markups/keyboard_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ part of televerse;
class KeyboardMenu
implements
ReplyKeyboardMarkup,
TeleverseMenu<MessageContext, MessageHandler> {
TeleverseMenu<MessageContext, MessageHandler, String> {
/// Name of the menu
@override
String name;
Expand Down
11 changes: 6 additions & 5 deletions lib/src/televerse/markups/menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ part of televerse;

/// Abstract class to represent a menu
abstract class TeleverseMenu<CTX extends Context,
CtxHandler extends FutureOr<void> Function(CTX)> {
CtxHandler extends FutureOr<void> Function(CTX), T> {
/// Map that represents the text and action to be done
List<Map<String, CtxHandler>> actions;
List<Map<T, CtxHandler>> actions;

/// Name of the menu
String name;
Expand All @@ -14,12 +14,13 @@ abstract class TeleverseMenu<CTX extends Context,

/// Converts a list of rows to a list of InlineKeyboardButton
static List<List<InlineKeyboardButton>> _makeInlineKeyboard(
List<Map<String, CallbackQueryHandler>>? rows,
List<Map<InlineMenuData, CallbackQueryHandler>>? rows,
) {
if (rows == null) return [];
return rows.map((row) {
return row.keys.map((element) {
return InlineKeyboardButton(text: element, callbackData: element);
return InlineKeyboardButton(
text: element.text, callbackData: element.data ?? element.text);
}).toList();
}).toList();
}
Expand Down Expand Up @@ -66,7 +67,7 @@ abstract class TeleverseMenu<CTX extends Context,

/// Constructs a TeleverseMenu
TeleverseMenu({
List<Map<String, CtxHandler>>? actions,
List<Map<T, CtxHandler>>? actions,
String? name,
}) : actions = actions ?? [{}],
name = name ?? _getRandomID();
Expand Down
13 changes: 13 additions & 0 deletions lib/src/televerse/models/inline_menu_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
part of televerse.models;

/// Class for handling inline menu displayed text and callback data used in [InlineMenu]
class InlineMenuData {
/// Create a [InlineMenuData], if [data] is null, [text] will be used as callback data in [InlineMenu]
InlineMenuData(this.text, [this.data]);

/// Text displayed
final String text;

/// Callback data
final String? data;
}
1 change: 1 addition & 0 deletions lib/src/televerse/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ part 'televerse_exception.dart';
part 'mention.dart';
part 'custom_emoji.dart';
part 'handler_scope.dart';
part 'inline_menu_data.dart';
5 changes: 3 additions & 2 deletions lib/src/televerse/televerse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1820,11 +1820,12 @@ class Televerse<TeleverseSession extends Session> {
int cols = menu.actions[i].length;
for (int j = 0; j < cols; j++) {
final key = menu.actions[i].keys.elementAt(j);
final data = key.data ?? key.text;
final action = menu.actions[i][key]!;
_internalCallbackQueryRegister(
key,
data,
action,
name: "${menu.name}-$key",
name: "${menu.name}-$data",
);
}
}
Expand Down

0 comments on commit fcee1d8

Please sign in to comment.