Skip to content

Commit

Permalink
🎹 Updated InlineKeyboardand InlineMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal committed Nov 1, 2024
1 parent 740f0e1 commit b57c924
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/src/telegram/models/copy_text_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CopyTextButton {
}

/// Creates the [CopyTextButton] from JSON map
factory CopyTextButton.fromJson(Map<String, dynamic> map) {
return CopyTextButton(text: map['text']);
factory CopyTextButton.fromJson(Map<String, dynamic> json) {
return CopyTextButton(text: json['text']);
}
}
4 changes: 4 additions & 0 deletions lib/src/telegram/models/inline_keyboard_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class InlineKeyboardButton {
json['switch_inline_query_chosen_chat'],
)
: null,
copyText: json['copy_text'] != null
? CopyTextButton.fromJson(json['copy_text'])
: null,
);
}

Expand All @@ -103,6 +106,7 @@ class InlineKeyboardButton {
'pay': pay,
'web_app': webApp?.toJson(),
'switch_inline_query_chosen_chat': switchInlineQueryChosenChat?.toJson(),
'copy_text': copyText?.toJson(),
}..removeWhere(_nullFilter);
}
}
16 changes: 16 additions & 0 deletions lib/src/televerse/markups/inline_keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,20 @@ class InlineKeyboard extends InlineKeyboardMarkup {
);
return this;
}

/// Adds a button which when pressed copies an arbitrary text specified by [copyText]
InlineKeyboard copyText(
String text, {
required String copyText,
}) {
inlineKeyboard.last.add(
InlineKeyboardButton(
text: text,
copyText: CopyTextButton(
text: copyText,
),
),
);
return this;
}
}
24 changes: 24 additions & 0 deletions lib/src/televerse/markups/inline_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ class _InlineMenuPayButton<CTX extends Context> extends _TMenuButton<CTX> {
}
}

class _InlineMenuCopyTextButton<CTX extends Context> extends _TMenuButton<CTX> {
final String copyText;
const _InlineMenuCopyTextButton(super.text, this.copyText);

@override
Map<String, dynamic> toJson() {
return {
'text': text,
'copy_text': CopyTextButton(text: copyText).toJson(),
};
}
}

/// This object represents a Inline Keyboard with the action to be done.
class InlineMenu<CTX extends Context>
implements InlineKeyboardMarkup, TeleverseMenu<CTX> {
Expand Down Expand Up @@ -336,6 +349,17 @@ class InlineMenu<CTX extends Context>
return this;
}

/// Creates a button when tappd copies the arbitary text
InlineMenu<CTX> copyText(
String text, {
required String copyText,
}) {
if (_buttons.isEmpty) _buttons.add([]);
_buttons.last.add(_InlineMenuCopyTextButton<CTX>(text, copyText));
inlineKeyboard = TeleverseMenu._makeInlineKeyboard<CTX>(_buttons);
return this;
}

/// List of rows of the keyboard
@override
List<List<InlineKeyboardButton>> inlineKeyboard;
Expand Down

0 comments on commit b57c924

Please sign in to comment.