diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/AlertCallback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/AlertCallback.kt new file mode 100644 index 0000000000..8ed7e55b60 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/AlertCallback.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps + +typealias AlertCallback = () -> Unit diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ConfirmCallback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ConfirmCallback.kt new file mode 100644 index 0000000000..4c9f85296b --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/ConfirmCallback.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps + +typealias ConfirmCallback = (confirmed: Boolean) -> Unit diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt index e0e5dc7f37..f83d2fd7dc 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventHandler.kt @@ -5,3 +5,4 @@ import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo typealias EventHandler = WebApp.() -> Unit typealias ViewportChangedEventHandler = WebApp.(ViewportChangedData) -> Unit typealias InvoiceClosedEventHandler = WebApp.(InvoiceClosedInfo) -> Unit +typealias PopupClosedEventHandler = WebApp.(String?) -> Unit diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt index a75b20218c..ff71329074 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/EventType.kt @@ -7,4 +7,5 @@ sealed class EventType(val typeName: String) { object BackButtonClicked : EventType("backButtonClicked") object SettingsButtonClicked : EventType("settingsButtonClicked") object InvoiceClosed : EventType("invoiceClosed") + object PopupClosed : EventType("popupClosed") } diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt index 365d07af4b..f983b5d4e4 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebApp.kt @@ -3,6 +3,8 @@ package dev.inmo.tgbotapi.webapps import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo +import dev.inmo.tgbotapi.webapps.popup.ClosePopupCallback +import dev.inmo.tgbotapi.webapps.popup.PopupParams external class WebApp { val version: String @@ -24,6 +26,15 @@ external class WebApp { val viewportHeight: Float val viewportStableHeight: Float + + val isClosingConfirmationEnabled: Boolean + fun enableClosingConfirmation() + fun disableClosingConfirmation() + + fun showPopup(params: PopupParams, callback: ClosePopupCallback? = definedExternally) + fun showAlert(message: String, callback: AlertCallback? = definedExternally) + fun showConfirm(message: String, callback: ConfirmCallback? = definedExternally) + @JsName("MainButton") val mainButton: MainButton @@ -38,6 +49,8 @@ external class WebApp { internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit) @JsName("onEvent") internal fun onEventWithInvoiceClosedInfo(type: String, callback: (InvoiceClosedInfo) -> Unit) + @JsName("onEvent") + internal fun onEventWithPopupClosedInfo(type: String, callback: (String?) -> Unit) fun offEvent(type: String, callback: () -> Unit) @JsName("offEvent") @@ -100,6 +113,18 @@ fun WebApp.onEvent(type: EventType.InvoiceClosed, eventHandler: InvoiceClosedEve ) } +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onEvent(type: EventType.PopupClosed, eventHandler: PopupClosedEventHandler) = { it: String? -> + eventHandler(js("this").unsafeCast(), it) +}.also { + onEventWithPopupClosedInfo( + type.typeName, + callback = it + ) +} + /** * @return The callback which should be used in case you want to turn off events handling */ @@ -124,6 +149,10 @@ fun WebApp.onSettingsButtonClicked(eventHandler: EventHandler) = onEvent(EventTy * @return The callback which should be used in case you want to turn off events handling */ fun WebApp.onInvoiceClosed(eventHandler: InvoiceClosedEventHandler) = onEvent(EventType.InvoiceClosed, eventHandler) +/** + * @return The callback which should be used in case you want to turn off events handling + */ +fun WebApp.onPopupClosed(eventHandler: PopupClosedEventHandler) = onEvent(EventType.PopupClosed, eventHandler) fun WebApp.isInitDataSafe(botToken: String) = TelegramAPIUrlsKeeper(botToken).checkWebAppData( initData, diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt index 92f7b87fd2..d2d7a1237b 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/WebAppUser.kt @@ -17,10 +17,14 @@ external interface WebAppUser { val username: String? @JsName(languageCodeField) val languageCode: String? + val is_premium: Boolean? @JsName(photoUrlField) val photoUrl: String? } +val WebAppUser.isPremium + get() = is_premium == true + fun WebAppUser.asUser() = if (isBot == true) { CommonBot( UserId(id), diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/ClosePopupCallback.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/ClosePopupCallback.kt new file mode 100644 index 0000000000..17727bef69 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/ClosePopupCallback.kt @@ -0,0 +1,3 @@ +package dev.inmo.tgbotapi.webapps.popup + +typealias ClosePopupCallback = (id: String) -> Unit diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt new file mode 100644 index 0000000000..65f2513ef1 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupButton.kt @@ -0,0 +1,45 @@ +package dev.inmo.tgbotapi.webapps.popup + +external class PopupButton( + id: String, + type: String, + text: String? = definedExternally +) { + val id: String + val type: String + val text: String? +} + +value class PopupButtonType( + val typeName: String +) { + companion object { + val Default = PopupButtonType("default") + val Ok = PopupButtonType("ok") + val Close = PopupButtonType("close") + val Cancel = PopupButtonType("cancel") + val Destructive = PopupButtonType("destructive") + } +} + +fun DefaultPopupButton( + id: String, + text: String +) = PopupButton(id, PopupButtonType.Default.typeName, text) + +fun OkPopupButton( + id: String +) = PopupButton(id, PopupButtonType.Ok.typeName) + +fun ClosePopupButton( + id: String +) = PopupButton(id, PopupButtonType.Close.typeName) + +fun CancelPopupButton( + id: String +) = PopupButton(id, PopupButtonType.Cancel.typeName) + +fun DestructivePopupButton( + id: String, + text: String +) = PopupButton(id, PopupButtonType.Destructive.typeName, text) diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt new file mode 100644 index 0000000000..42f00aac22 --- /dev/null +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/popup/PopupParams.kt @@ -0,0 +1,36 @@ +package dev.inmo.tgbotapi.webapps.popup + +external class PopupParams( + message: String, + title: String?, + buttons: Array +) { + val title: String? +} + +fun PopupParams( + message: String, + firstButton: PopupButton, + vararg otherButtons: PopupButton +) = PopupParams( + message, + null, + arrayOf( + firstButton, + *otherButtons + ) +) + +fun PopupParams( + title: String, + message: String, + firstButton: PopupButton, + vararg otherButtons: PopupButton +) = PopupParams( + message, + title, + arrayOf( + firstButton, + *otherButtons + ) +)